3: Topics
What are topics, how are they used. How do messages fit in

PRR Chapter 3: Topics

  • Nodes talk to each other with Topics
  • A node can “publish” a topic
  • Another node can “subscribe” to a topic
    • The two nodes don’t know about each other
    • A node can subscribe to a topic that no one is publishing
    • Two nodes can publish the same topic with no one subscribing
    • etc. etc. etc.
  • What is being published and subscribed?
    • messages
    • A message is a data structure with named and typed fields
    • A collection of messages already exists, e.g. Int32, which is message with one element, a 32 bit integer.

Some details

  • Queues
    • When you define your node as a Publisher you need to supply a queue_size
    • Messages that are published are kept in the queue until all subscribers have received them
    • Therefore if you publish at a rate faster than all the subscribers are processing, you will loose topics
    • A good starting queue_size is 10
  • Latched Topics
    • New subscribers to a latched topic automatically get the last published message for that topic
    • Useful for topics which change rarely
  • Shebang line.
    • Ignore why its called shebang.
    • pronounced shuh-bang
    • It’s an old joke.
    • Here’s what it looks like:
#!/usr/bin/env python
  • To make a text file executable on linux you do these things:
    • Put shebang line as line 1. It tells what the language of that text file
    • Change the file mode to have “executable priviliges”
    • chmod +x filename
    • Execute it from shell $ ./filename

Useful references to look at

Publishing a topic

  • You will find all the book’s examples in the prrexamples github package
  • Use the ones in our github because they have been tested with TB3
  • If it’s not there yet, clone it in your ~/catkin_ws/src directory
  • See ~/catkin_ws/src/prrexamples/scripts/topic_publisher.py
  • Remember:
    • to do the necessary code gen and compilation you must run catkin_make (with the alias cm)
    • to run roscore in another terminal
  • Run it and examine that it is publishing as expected, as follows:
$ roscd prrexamples
$ chmod +x scripts/topic_publisher.py
$ rosrun prrexamples topic_publisher.py
$ rostopic list
$ rostopic echo counter -n 5

Subscribing to a topic

  • See $ ~/catkin_ws/src/prrexamples/scripts/topic_subscriber.py
  • It defines and runs a node whch subscribes to the counter, and prints it
# shell commands
$ chmod +x topic_subscriber.py
$ roscd prrexamples
$ rosrun prrexamples topic_subscriber

Examine what’s going on

  • Make sure you can see that topics are being publsihed (see rostopic)
  • Make sure you can see that topics are being subscribed (see console output)
  • Try running rqt_graph to see the node charts

Custom Message Types

  • First look if any of the standard message types can do the job for you
  • Here’s a List of standard message type
  • The procedure for defining a new message is easy but convoluted
  • This is because you define your message in a .msg file with a particular format
  • And then catkin_make uses that to generate appropriate C++ headers and Python libraries
Steps to create a new message type (see Creating a msg)
  1. Define the message file, foo.msg, and place it in the msg/ subdirectory of the package
  2. Update (if needed) the package.xml file to include two new commands to indicate dependencies ()
  3. Update your python program to import the messages
  4. Update CMakeLists.txt