ROS has software for organized packages. The package may contain ROS nodes, data sets, configuration files, third-party software, or any other program needed to run the program. The purpose of libraries is to provide useful functions that can be easily used for different purposes.
The libraries can be installed using the Linux package manager apt-get or placed in the src directory of your workspace. The package called ros-tutorials contains packages that demonstrate the capabilities of ROS and are necessary to follow this tutorial.
Install the package named ros-tutorials:
$ sudo apt-get install ros-melodic-ros-tutorials
Once the package is successfully installed, the working environment must be recompiled:
$ catkin_make #compile the computing environment
ROS Master manages the communication between nodes. Every node registers at startup with the master. The ros master node must work. This node can be started with roscore.
Open a new terminal window (Ctrl + Alt + T) and launch roscore:
$ roscore
If everything is set up correctly, something should appear on the terminal:
... logging to /home/mohsen/.ros/log/83e74152-7a80-11eb/roslaunch-laptop-22969.log Checking log directory for disk usage. This may take a while. Press Ctrl-C to interrupt Done checking log file disk usage. Usage is <1GB. started roslaunch server http://laptop:38393/ ros_comm version 1.14.6 SUMMARY ======== PARAMETERS * /rosdistro: melodic * /rosversion: 1.14.6 NODES auto-starting new master process[master]: started with pid [22980] ROS_MASTER_URI=http://laptop:11311/ setting /run_id to 83e74152-7a80-11eb-b6ad-ccaf78b12fc1 process[rosout-1]: started with pid [22991] started core service [/rosout]
Only one roscore can be run at a time. Now ROS nodes can communicate with each other.
ROS nodes are actually programs that run on your PC. It's a single-purpose and executable program that can be compiled, run, and managed individually. Nodes are like building blocks of your program. Nodes are organized in packages. Each package may include several nodes. Each node should be registered with the ROS master:
To run a node:
$ rosrun package_name node_name
To see the list of current active node:
$ rosnode list
If you need to get some information about a specific node:
$ rosnode info node_name
Nodes communicate with each other through ROS topics. One node can publish and subscribes to topics. Actually, topics are a stream of messages. The figure below shows how a topic transfer message between two nodes while one is publishing and the other is subscribing to the topic.
To see the list of active topics:
$ rostopic list
Subscribe and print the message inside a topic:
$ rostopic echo /topic_name
To get the information about a topic such as a publisher and subscribers and the type of the message:
$ rostopic info /topic_name
Nodes publish messages over topics. Messages define the data structure of the information that flows from one node to the other. It includes simple data structures such as integers, floats, booleans, string, etc. They are defined in the *.msg file. You can see the message type inside a topic by running rostopic info /topic_name. To see the message structure of a specific type you need to run:
$ rosmsg show message_type
for example :
$ rosmsg show geometry_msgs/Twist geometry_msgs/Vector3 linear float64 x float64 y float64 z geometry_msgs/Vector3 angular float64 x float64 y float64 z
Above you installed a package called ros-tutorials. This package contains two nodes. The first node is called talker, which publishes the text “hello world” into the /chatter topic.
We use rosrun to execute the node.
Open a new terminal window (Ctrl + Alt + T) and run a node called talker there:
$ rosrun rospy_tutorials talker
Make sure roscore is running, you can't start a node without it. If the node is successfully started, the terminal should appear:
[INFO] [WallTime: 1314931831.774057] hello world 1314931831.77 [INFO] [WallTime: 1314931832.775497] hello world 1314931832.77 [INFO] [WallTime: 1314931833.778937] hello world 1314931833.78 [INFO] [WallTime: 1314931834.782059] hello world 1314931834.78 [INFO] [WallTime: 1314931835.784853] hello world 1314931835.78 [INFO] [WallTime: 1314931836.788106] hello world 1314931836.79
The publisher goes and publishes messages to /chatter, now you need a subscriber who listens to those messages.
The second node is called listener and subscribes to /chatter and starts displaying messages sent to that topic.
Open a new terminal window (Ctrl + Alt + T) and execute a subscriber named listener:
$ rosrun rospy_tutorials listener
When messages sent by talker appear on the terminal, the node started successfully, ordered the /chatter, and the nodes now communicate successfully through ROS:
[INFO] [WallTime: 1314931969.258941]/listener_17657_1314931968795I heard hello world 1314931969.26 [INFO] [WallTime: 1314931970.262246]/listener_17657_1314931968795I heard hello world 1314931970.26 [INFO] [WallTime: 1314931971.266348]/listener_17657_1314931968795I heard hello world 1314931971.26 [INFO] [WallTime: 1314931972.270429]/listener_17657_1314931968795I heard hello world 1314931972.27 [INFO] [WallTime: 1314931973.274382]/listener_17657_1314931968795I heard hello world 1314931973.27 [INFO] [WallTime: 1314931974.277694]/listener_17657_1314931968795I heard hello world 1314931974.28 [INFO] [WallTime: 1314931975.283708]/listener_17657_1314931968795I heard hello world 1314931975.28
ROS-based robots usually have a large number of nodes, each with its own specific function. Nodes can be located on different computers and communicate through different protocols. But now we're getting to know the tools that come with ROS, which allow us to track and manage nodes and topics.
Entering the rostopic command shows all the ways you can use it:
$rostopic rostopic BW display bandwidth used by topic rostopic echo print messages to screen rostopic Hz display publishing rate of topic rostopic list print information about active topics rostopic pub publish data to a topic rostopic type print topic type
Make sure roscore, talker, and listener work in the background.
We will display all the topics currently in use:
$rostopic list /chatter /rosout /rosout_agg
/chatter is the topic through which talker and listener interact.
We use the rostopic info command to display the necessary information about the topic:
$ rostopic info/chatter Type: std_msgs/String Publishers: * /talker_12621_1542817185271 (http://rage-HP-EliteBook:45495/) Subscribers: * /listener_12836_1542817212996 (http://rage-HP-EliteBook:34711/)
You will see which nodes have subscribed to the topic and which ones are publishing to the topic, as well as the message type, which is std_msgs/String.
We use rostopic hz to see the frequency of messages:
$ rostopic hz/chatter subscribed to [/chatter] average rate: 10.005 min: 0.100s max: 0.100s std dev: 0.00017s window: 10
We see that the average frequency of messages is about 10 Hz (Ctrl + C aborts the process).
We use echo to display messages sent to the topic:
$ rostopic echo /chatter date: "hello world 1542817743.91" --- date: "hello world 1542817744.01" --- date: "hello world 1542817744.11" --- date: "hello world 1542817744.21" --- date: "hello world 1542817744.31" ---
We use pub to post a message to:
$ rostopic pub /chatter std_msgs /String "data: 'hello world'" publishing and latching messages. Press "Ctrl + C" to terminate
While listening to the subject at the same time, we see that the message we sent was published:
date: "hello world 1542817787.91" --- date: "hello world 1542817788.01" --- date: "hello world 1542817788.11" --- data: "hello world" --- date: "hello world 1542817788.21" --- date: "hello world 1542817788.31" --- date: "hello world 1542817788.41"
We use rosnode list to display all currently running nodes.
$ rosnode list /listener_12836_1542817212996 /rosout /talker_12621_1542817185271
We use rosnode info to display the necessary information about the node:
$ rosnode info /talker_12621_1542817185271 -------------------------------------------------- ------------------------------ Node [/talker_12621_1542817185271] Publications: * /chatter [std_msgs /String] * /rosout [rosgraph_msgs /Log] Subscriptions: None Services: * /talker_12621_1542817185271/get_loggers * /talker_12621_1542817185271/set_logger_level contacting node http://rage-HP-EliteBook:45495/... Hold: 12,621 Connections: * topic: /chatter * to: /listener_12836_1542817212996 * direction: outbound * transport: TCPROS * topic: /rosout * to: /rosout * direction: outbound * transport: TCPROS
We can see which computer the node is running on and through which protocol it is connected to the ROS.