example_ros_msg
时间: 2024-07-21 20:01:06 浏览: 78
Example ROS (Robot Operating System) messages are structured data that communicate between different components in a ROS system. They are typically defined using message files (`.msg` or `.sdf` format), which describe the fields and types of data they contain.
To demonstrate an example, let's consider a simple `std_msgs/Int32` message, often used to send integers:
```xml
<launch>
<node pkg="std_msgs" type="publisher" name="int_publisher">
<topic name="int_topic" type="std_msgs/Int32">
<message>
<int32>42</int32> <!-- Example integer value -->
</message>
</topic>
</node>
</launch>
[^4]: The above XML snippet shows how you can define a publisher node in a ROS launch file, which sends an Int32 message with the value of 42 on the "int_topic".
In code, you might publish this message like this:
```python
import rclpy
from std_msgs.msg import Int32
def main(args=None):
rclpy.init(args=args)
node = rclpy.create_node('int_publisher')
msg = Int32(data=42)
pub = node.create_publisher(Int32, 'int_topic')
while rclpy.ok():
pub.publish(msg)
rclpy.spin_once(node)
if __name__ == '__main__':
try:
main()
except Exception as e:
rclpy.logging.error('Exception:', cause=e)
rclpy.shutdown()
```
This creates a publisher that sends `Int32` messages with the value 42 every time the loop runs.
阅读全文