ROS
We use ROS to send messages between the Pi and the Due. As of April 2019, we use the Indigo Igloo version, released in 2014, on the Pi.
Contents
Starting ROS and roscore
What is roscore? roscore is a collection of nodes and programs that are prerequisites of a ROS-based system. You must have a roscore running in order for ROS nodes to communicate. For more information on ROS nodes, see below.
Nodes
A node is a process that performs computation. Each node is an independent part of your application, and they can be combined together into a graph. A robot control system will usually comprise many nodes.
Nodes can communicate with other nodes through ROS communication tools (topics, services, and actions).
Nodes provide several benefits. For one, there is fault tolerance - crashes are isolated to individual nodes. Code complexity is also reduced compared to monolithic systems. Finally, implementation details are well hidden as the nodes expose a minimal API to the rest of the graph.
How do I start a new node?
There are two levels of initialization for a roscpp Node.
First, you can initialize a node through a call to one of the ros::init()
functions.
Second, the node is started through the creation of a ros::NodeHandle
.
How do I interact with nodes? Nodes can communicate with each other through messages, a data structure comprising of typed fields such as integers, floating points, and booleans. Nodes can publish messages to a topic or subscribe to a topic to receive messages. For more information about topics, see the section below.
How do I shut down a node?
At any time, you can call the ros::shutdown()
function to shut down your node. This function will shut down all open subscriptions, publications, service calls, and service servers. You can also check the various states of shut down using ros::ok
which returns false once the node has finished shutting down, or ros::isShuttingDown()
which returns true as soon as ros::shutdown()
is called.
How do I shut down all nodes?
Typing rosnode kill -a
in the terminal will terminate all your nodes. However, this will only work if your roscore
is still running. Otherwise, it will not be able to find the running nodes.
rosserial_arduino
rosserial is a protocol for wrapping ROS services over a device like a serial port or network socket. In particular, we use rosserial_arduino because it allows us to easily get ROS nodes running on Arduino.
Topics
A topic is a name that is used to identify the contents of a message. Messages are routed using a publish/subscribe model. A node sends out messages by publishing it to a given topic, and a node that is interested in the specific kind of data will subscribe to the appropriate topic.
How can I interact with topics from the command line?
You can interact with topics from the command line using rostopic
. This tool can be used to display active topics, the publishers and subscribers of a specific topic, the publishing rate of a topic, the bandwidth of a topic, and messages published to a topic. Here is a list of supported commands:
rostopic bw display bandwidth used by topic rostopic delay display delay for topic which has header rostopic echo print messages to screen rostopic find find topics by type rostopic hz display publishing rate of topic rostopic info print information about active topic rostopic list print information about active topics rostopic pub publish data to topic rostopic type print topic type
How can I interact with topics using nodes?
Nodes communicate by publishing or receiving messages. Here is an example of a publisher: ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000)
. This says that we will be publishing a message of type std_msgs/String on the topic chatter. The second argument is the size of the publishing queue. If we publish too many messages, it will buffer up 1000 messages before throwing the old ones away. Now the line chatter_pub.publish(msg)
will broadcast the message to anyone who is connected.
Here is an example of a subscriber: ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback)
. This subscribes to the chatter topic and will call the chatterCallback()
function whenever a new message arrives. Similar to the publisher, the second argument is the queue size. If the queue reaches 1000 messages, it will start throwing old messages away as new ones arrive.
Messages
(what's a message (aka a data type for a topic, service, etc)?)
(what are the most helpful available message types?)
(how do I work with Float32MultiArrays in Python and C++, since that's the most frequently used message?)
(how do I create my own message?)
Further reading
- Tutorial on writing a Python node that publishes and subscribes to a topic: http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29