A node publishing a topic

For the next steps, since we will need to deal with several simultaneous terminals, we will make use of a very handy tool, Terminator, that allows you to simultaneously handle several terminals. Launch these commands to install it on your system:

$ sudo apt-get update
$ sudo apt-get install terminator

Launch Terminator and divide the screen into four terminals (you can right-click on the mouse to divide the windows successively). We will reference them as T1, T2, T3, and T4, as shown in the following screenshot:

Launch the roscore node in terminal 1 (the top-left window):

T1 $ roscore

You should be able to see this output in T1:

This is the root process of ROS. roscore launches a node and starts the master service, as ROS is a centralized system. The master node is always needed so that other nodes may execute and it has to be launched prior to any other node.

In the next terminal, T2, run this command to launch the publisher node:

T2 $ rosrun ros_basics topic_publisher.py

This launches the topic_publisher node, but nothing happens! That's right. A publisher is just that, a publisher. We need a listener to know what data is being sent.

Go to terminal 3 and list the topics currently declared:

T3 $ rostopic list
/counter
/rosout
/rosout_agg

In the listing, it appears that /counter is the topic that updates an incremental counter every 0.5 seconds. The other two topics, /rosout and /rosout_agg, are the console log-reporting (http://wiki.ros.org/rosout) mechanisms in ROS.

This line of the topic_publisher.py file is the one that sets up the /counter topic publisher:

pub = rospy.Publisher('counter', Int32, queue_size=10)

To watch the published messages, launch this command in the terminal:

T3 $ rostopic echo counter -n 5

This will output the next five messages that will be published in the /counter topic:

data: 530
---
data: 531
---
data: 532
---
data: 533
---
data: 534
---

Finally, we will show a live view that will print the real-time frequency at which the messages are sent:

T3 $ rostopic hz counter

average rate: 2.000

min: 0.500s max: 0.501s std dev: 0.00020s window: 146
average rate: 2.000
min: 0.500s max: 0.501s std dev: 0.00020s window: 148
average rate: 2.000
min: 0.500s max: 0.501s std dev: 0.00020s window: 150
average rate: 2.000
min: 0.500s max: 0.501s std dev: 0.00020s window: 152
average rate: 2.000
min: 0.500s max: 0.501s std dev: 0.00020s window: 154
average rate: 2.000
min: 0.500s max: 0.501s std dev: 0.00020s window: 156
average rate: 2.000

Press Ctrl + C to stop the log in T3. Bear in mind that, if you do this in any of the two previous terminals, the process that each one controls will die with the following consequences:

  • Terminal T2: The publisher process will end and no more messages will be sent through the /counter topic. 
  • Terminal T1: Pressing Ctrl + C in this terminal will kill roscore, making it evident that this process is a single point of failure in ROS, that is, all related processes (including nodes and messages) will die.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset