Difference between revisions of "ROS"
(→Nodes) |
(new section) |
||
(7 intermediate revisions by 2 users not shown) | |||
Line 16: | Line 16: | ||
First, you can initialize a node through a call to one of the <code>ros::init()</code> functions. | First, you can initialize a node through a call to one of the <code>ros::init()</code> functions. | ||
Second, the node is started through the creation of a <code>ros::NodeHandle</code>. | Second, the node is started through the creation of a <code>ros::NodeHandle</code>. | ||
+ | |||
+ | '''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?''' | '''How do I shut down a node?''' | ||
Line 21: | Line 24: | ||
'''How do I shut down all nodes?''' | '''How do I shut down all nodes?''' | ||
− | Typing <code>rosnode kill -a</code> in the terminal will terminate all your nodes. However, this will only work if your <code>roscore</code> is still running. Otherwise, it will not be able to find the running nodes. | + | Typing <code>rosnode kill -a</code> in the terminal will terminate all your nodes. However, this will only work if your <code>roscore</code> is still running. Otherwise, it will not be able to find the running nodes. |
− | |||
=== rosserial_arduino === | === 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. | 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. | ||
Line 28: | Line 30: | ||
== Topics == | == 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. | 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 <code>rostopic</code>. 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: <br> | |
+ | <pre>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</pre> | ||
+ | |||
+ | '''How can I interact with topics using nodes?''' | ||
+ | Nodes communicate by publishing or receiving messages. Here is an example of a publisher: <code>ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000)</code>. 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 <code>chatter_pub.publish(msg)</code> will broadcast the message to anyone who is connected. | ||
+ | |||
+ | Here is an example of a subscriber: <code>ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback)</code>. This subscribes to the chatter topic and will call the <code>chatterCallback()</code> 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 === | === Messages === | ||
(what's a message (aka a data type for a topic, service, etc)?)<br /> | (what's a message (aka a data type for a topic, service, etc)?)<br /> | ||
Line 36: | Line 53: | ||
(how do I work with Float32MultiArrays in Python and C++, since that's the most frequently used message?)<br /> | (how do I work with Float32MultiArrays in Python and C++, since that's the most frequently used message?)<br /> | ||
(how do I create my own message?) | (how do I create my own message?) | ||
+ | |||
+ | == Working with ROS packages == | ||
+ | How to install a ROS package from its GitHub repository: | ||
+ | |||
+ | # Somehow get the cloned folder into the ~/ros_ws/src folder on the Pi (either by [[Pi#Giving the Pi the Internet|connecting the Pi to the Internet]] or by using scp) | ||
+ | # Go back to ros_ws and run catkin_make (or maybe catkin_make_isolated) | ||
+ | |||
+ | == Further reading == | ||
+ | * Tutorial on writing a Python node that publishes and subscribes to a topic: http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29 |
Latest revision as of 20:47, 8 March 2020
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?)
Working with ROS packages
How to install a ROS package from its GitHub repository:
- Somehow get the cloned folder into the ~/ros_ws/src folder on the Pi (either by connecting the Pi to the Internet or by using scp)
- Go back to ros_ws and run catkin_make (or maybe catkin_make_isolated)
Further reading
- Tutorial on writing a Python node that publishes and subscribes to a topic: http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29