Difference between revisions of "Writing ROS code"
(cell padding) |
(add a couple more rows) |
||
Line 18: | Line 18: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|Imports the main ROS client library and the Float32 message type. | |Imports the main ROS client library and the Float32 message type. | ||
+ | |- | ||
+ | |<syntaxhighlight lang="python"> | ||
+ | rospy.init_node("asdf") | ||
+ | </syntaxhighlight> | ||
+ | The node name is "asdf". Also takes an optional argument <code>anonymous=True</code>, which lets you simultaneously run multiple copies of the same node | ||
+ | |<syntaxhighlight lang="c++"> | ||
+ | typedef ros::NodeHandle_<ArduinoHardware, 1, 3, 500, 500> RosNodeHandle; | ||
+ | RosNodeHandle nh; | ||
+ | |||
+ | // in a function: | ||
+ | nh.initNode(); | ||
+ | </syntaxhighlight> | ||
+ | Node: <code>ros::init</code> may also be required, but we don't call it on the Arduino | ||
+ | |Initializes a node | ||
+ | |- | ||
+ | |<syntaxhighlight lang="python"> | ||
+ | pub = rospy.Publisher("hjkl", Float32, queue_size=10) | ||
+ | </syntaxhighlight> | ||
+ | |<syntaxhighlight lang="c++"> | ||
+ | std_msgs::Float32 hjkl_data; | ||
+ | ros::Publisher hjkl_pub("hjkl", &hjkl_data); | ||
+ | </syntaxhighlight> | ||
+ | |Sets up a publisher. | ||
|} | |} | ||
== Launch files == | == Launch files == |
Revision as of 22:27, 19 February 2020
This page covers writing ROS nodes, which are programs that connect (often with TCP) to a central ROS server, called the ROS core. After connecting with the core, ROS nodes can communicate with each other. Three ways ROS nodes communicate are topics, services, and parameters. We mostly use topics. This page has Python and C++ (Arduino) examples for communicating between two nodes using topics.
Topics are like Slack channels. Each topic has a name and a data type. Names usually start with a forward slash (which means they're in the "global namespace", which is like Java's default package), such as /bike_state
. Data types are ROS-defined. Here's a list of the standard ones. We mostly use Float32 and Float32MultiArray. Nodes publish to and subscribe from topics. Multiple nodes can publish to and subscribe from the same topic. There are also command-line and graphical tools for publishing to a topic and showing messages published to a topic.
Writing a node
Python | Arduino | Description |
---|---|---|
import rospy
from std_msgs.msg import Float32
|
#include <ros.h>
#include <std_msgs/Float32.h>
|
Imports the main ROS client library and the Float32 message type. |
rospy.init_node("asdf")
The node name is "asdf". Also takes an optional argument |
typedef ros::NodeHandle_<ArduinoHardware, 1, 3, 500, 500> RosNodeHandle;
RosNodeHandle nh;
// in a function:
nh.initNode();
Node: |
Initializes a node |
pub = rospy.Publisher("hjkl", Float32, queue_size=10)
|
std_msgs::Float32 hjkl_data;
ros::Publisher hjkl_pub("hjkl", &hjkl_data);
|
Sets up a publisher. |