ROS Concept and Practice Part 2
ROS Concept and Practice Part 2
Refresh
the rqt
graph
Unchecked Debug
- Debug will
always show
“other
stuff”(explain
later)
• Topic is like a
DNS server
Create a python publisher
Follow analogy, create radio transmitter as publisher
• Create a new python file inside your python scripts folder
• Make it executable
• And edit it
• Let’s keep the node name similar to file name for now
• Call the Publisher class in ros
• Specify the topic name for the publisher, say /robot_news_radio
• We also need to specify the data/message type for the topic
• For the radio transmitter example, we wanted to send String message
• Check out std_msgs string message for standard command available
• Message String just contain a simple string data
• So need to import String from std_msgs
• And specify data/message type String
• With the string message, must include queue_size
• The queue_size acts like a buffer
• A topic can send gazillion messages, so subscribers may not have the
time to listen and process them all, the buffer allows the subsribers
some time to process the messages
• While the node is running, we will publish data of the topic, following
a rate, say 2 Hz 2 messages at every second
• Specify the type and message content
• Publish the message using the publish method
• The shutdown() kills the node but not the program/script
• This is a problem because when we kill the node we usually wants the
program to stop (kill the program too)
• Good practice to add to make sure node is stopped before exiting the
program
• If you run, nothing is output, because we only publish, we didn’t ask
program to print something to the terminal.
• rosnode list will show /robot_news_radio_transmitter node is running
• rostopic list
• To listen to a publisher’s message, use the rostopic echo
• Here u can see the message is published at every 0.5 seconds (rate =
2hz = publish 2 message every 1 second)
• If you kill the node (ctrl+Z), “Node was stopped” appears
Create a python subscriber
Follow analogy, create smartphone as subscriber
• Create a python program called smartphone
• Make it executable
• Edit it
• Initialize a new node smartphone
• Using class Subscriber to create a subscriber to the topic
robot_news_radio (make sure the name of topic is correct)
• Specify the same message type (in this case String)
• For subscribers, we do not specify queue_size, instead we create a
callback function to receive the data
The callback should indicate the message is received
• A callback receive the message
• So need to create a function that will have 1 msg as a parameter
• A spin will keep the script running with all the callback, until the
rosnode smartphone is stopped
• Running the subscriber will not
show anything too, because
currently there is no messages
received
• The subscriber will only get
messages when a publisher is
run too! So need to run
robot_news_radio_transmitter
• both publisher and subscriber are running
Create a C++ Publisher