Ros 101 - arieldo/MorBot GitHub Wiki

Welcome To "ROS 101" wiki.

Understand Nodes, Topics, and Messages for Robotics Communication.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Nodes
  4. Topics
  5. Messages

Introduction:

In this tutorial, you will learn about nodes, topics, and messages in the ROS. These concepts are essential for building and understanding the structure of ROS-based systems, as they facilitate communication between different components of a robot.

Prerequisites:

Nodes:

A node in ROS is an individual unit of computation, typically a single process that performs a specific task. Nodes communicate with each other by publishing or subscribing to topics, or by using ROS services. Nodes are the fundamental building blocks of a ROS-based system, as they modularize the system into smaller, more manageable pieces.

  • Purpose: Nodes perform specific tasks in a robot's software architecture, making the system maintainable, scalable, and easier to debug.

  • Communication: Nodes communicate using topics (asynchronous, many-to-many), services (synchronous, one-to-one), and actions (asynchronous, one-to-one with feedback).

  • Naming and Namespaces: Each node has a unique name; namespaces group related nodes, e.g., "/sensors", "/actuators", "/planning".

  • Launch Files: ROS launch files, with a .launch extension, start multiple nodes and configure their parameters simultaneously, simplifying management of complex systems.

  • ROS Master: The ROS Master manages node registration and lookup, enabling nodes to discover each other, while actual data transfer occurs peer-to-peer.

  • There are several useful command-line tools for working with nodes:

To list active nodes in your ROS system, open a terminal and run:

rosnode list

To get information about a specific node, run:

rosnode info <node_name>

To Tests the connectivity between the local node and the specified node, run:

rosnode ping <node_name>

Topics:

Topics are essential components of the Robot Operating System that facilitate communication between nodes using a publish-subscribe pattern, allowing for modular and decoupled system design. These communication channels enable message passing between nodes, with a node publishing messages to a topic while other nodes subscribe to receive those messages. By supporting a many-to-many communication pattern, topics allow multiple publishers and subscribers to exchange messages asynchronously, ultimately streamlining data exchange and improving overall system functionality in robotic applications.

  • Topic Naming: Topics in ROS have unique, hierarchical names (e.g., /camera/rgb/image_raw), similar to file system structures, for easy organization and understanding.

  • Message Types: Each topic is associated with a specific message type, and only messages of that type can be published to the topic (e.g., sensor_msgs/Image or nav_msgs/Odometry).

  • Many-to-Many Communication: Topics enable many-to-many communication, allowing multiple nodes to publish and subscribe to the same topic, which simplifies scaling and reconfiguration.

  • Asynchronous Communication: Topic-based communication is asynchronous, with publishers and subscribers operating independently, sending and processing messages at their own pace.

  • Bandwidth Considerations: When designing a ROS system, consider bandwidth requirements for topics. To reduce bandwidth usage, employ techniques like data compression, message filtering, or decreasing publishing frequency.

  • There are several useful command-line tools for working with nodes:

To list active topics in your ROS system, open a terminal and run:

rostopic list

Subscribes to a topic and displays the received messages in the terminal, run:

rostopic echo <topic>

To get information about a specific topic, run:

rostopic info <topic_name>

To Publishes a single message to a topic.

rostopic pub <topic> <msg_type> <args>

Messages:

Messages are the data structures used for communication between nodes in ROS. Each topic has a specific message type associated with it. ROS provides many standard message types in the std_msgs package, and other packages may define their own custom messages.

  1. Message Types: ROS provides many standard message types in the std_msgs package, which includes common data structures like integers, floats, strings, and arrays. For example:
  • std_msgs/String: A string message type, used for sending text messages.
  • std_msgs/Int32: A 32-bit integer message type, used for sending integer values.
  • std_msgs/Float32: A 32-bit floating-point message type, used for sending floating-point values.
  • std_msgs/Bool: A boolean message type, used for sending boolean values.

Other packages can define custom message types for specific applications, such as the geometry_msgs package, which includes message types for representing geometric shapes, poses, and transformations:

  • geometry_msgs/Point: A message type for representing a 3D point.
  • geometry_msgs/Pose: A message type for representing a 3D pose.
  • geometry_msgs/Transform: A message type for representing a 3D transformation.
  • geometry_msgs/Quaternion: A quaternion for representing orientations.
  • geometry_msgs/Twist: A combination of linear and angular velocities.
  1. Message Definition: A message type is defined in a .msg file, which contains the data structure of the message. The file consists of a sequence of field declarations, one per line, where each field has a type and a name.

For example, the geometry_msgs/Point message type is defined in a Point.msg file like this:

float64 x
float64 y
float64 z
  1. Message Generation: You can create custom message types for your ROS packages when the standard message types don't fit your needs. To create a custom message:

a. Create a directory named msg in your package. b. In the msg directory, create a .msg file with the desired message structure. c. Modify the package's CMakeLists.txt and package.xml files to include the necessary dependencies and build instructions.

For example, to create a custom message type that represents the temperature and humidity readings from a sensor, you would create a TemperatureHumidity.msg file in the msg directory with the following content:

float64 temperature
float64 humidity
  1. Nested Messages: Messages can also include other messages as fields. This allows you to create complex data structures by combining existing message types. For example, the geometry_msgs/Pose message type includes a geometry_msgs/Point and a geometry_msgs/Quaternion as fields:
geometry_msgs/Point position
geometry_msgs/Quaternion orientation
  1. Working with Messages in Code: In Python and C++, you can import and use message types like any other module or class. For example, in Python, you can import the String message type from the std_msgs package like this:
from std_msgs.msg import String

You can then create a String message object and set its value like this:

msg = String()
msg.data = "Hello World"

Understanding ROS messages is crucial for developing ROS-based systems since they provide the means for communication between different components of a robot. You can create and use messages to exchange information between sensors, actuators, and algorithms, allowing your robot to function effectively and efficiently.

  • There are several useful command-line tools for working with nodes:

To get information about a message type, use the following command, replacing MessageType with the desired message type (e.g., std_msgs/String):

rosmsg show MessageType

For example:

rosmsg show std_msgs/String

To get a list of all available message types, run:

rosmsg list

To recap, nodes are the primary components of a ROS-based system and represent individual processes that perform specific tasks. Topics enable communication between nodes through a publish-subscribe mechanism, and messages are the data structures that define the information exchanged between nodes.

As you continue to learn more about ROS, you'll discover a wide variety of nodes, topics, and messages that cater to different use cases and robotic applications. Experiment with different combinations to create more complex systems and expand your understanding of how these elements work together.

Remember that practice and hands-on experience are crucial in developing your skills in ROS. Keep exploring, building, and testing different scenarios to solidify your knowledge and become proficient in working with nodes, topics, and messages in the context of real-world robotic systems.

next

⚠️ **GitHub.com Fallback** ⚠️