Difference between revisions of "Getting Started with ROS on Embedded Systems/User Guide/C++/Names"

From RidgeRun Developer Connection
Jump to: navigation, search
m
m
Line 2: Line 2:
  
 
== Introduction ==
 
== Introduction ==
This wiki is based on the following page: http://wiki.ros.org/roscpp/Overview/Names%20and%20Node%20Information
+
This wiki is based on the following page: http://wiki.ros.org/roscpp/Overview/Publishers%20and%20Subscribers
  
Name manipulation is an important section for ROS, in here you can find useful functions that work well when doing this manipulation
+
Publishers and subscribers are one of the main ways of communication when using ROS.
  
== Functions ==
+
== Publishers ==
 +
In order to publish messages, you will need a node handle, and create a ros::Publisher using the advertise function. Like the following example:
  
An useful function is the resolve, which lets you create a name with the namespace of the node and a given name, resulting in node_namespace/name
+
<syntaxhighlight lang="cpp">
 +
ros::Publisher pub = nh.advertise<std_msgs::String>("topic_name", 5);
 +
std_msgs::String str;
 +
str.data = "hello world";
 +
pub.publish(str);
 +
</syntaxhighlight>
 +
 
 +
This uses the function
  
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
std::string ros::names::resolve(const std::string& name, bool remap = true);
+
template<class M>
 +
ros::Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
This is useful when you don't want to have the node name preappended to your topic names.  
+
In which:
 +
 
 +
* M is the message type to publish on the topic
 +
* topic is the name of the topic to publish to
 +
* queue_size is the size of the message queue, if the publishing is faster than the message sending, it will start dropping older messages.
 +
* latch: Save messages for future subscriber connections automatically.
 +
 
 +
== Subscribers ==
  
Another function is the append, which simply appends two strings resulting in left/right
+
Similar to publishers, a ROS subscriber needs a node handle and with the subscribe function it will create a new ros::Subscriber, by using the following:
  
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
std::string ros::names::append(const std::string& left, const std::string& right);
+
template<class M>
 +
ros::Subscriber subscribe(const std::string& topic, uint32_t queue_size, <callback, which may involve multiple arguments>, const ros::TransportHints& transport_hints = ros::TransportHints());
 +
</syntaxhighlight>
 +
 
 +
In which:
 +
 
 +
* M is the message type to publish on the topic, normally deduced by the compiler
 +
* topic is the name of the topic to subscribe to
 +
* queue_size is the size of the message queue, if messages are too fast arriving and the application can't keep up, they will start dropping.
 +
* callback: Normally a pointer to a function with the function to execute on each message arrival
 +
* transport_hints: Hints to roscpp's transport layer. Such ad preferring UDP, tcp nodelay.
 +
 
 +
===Callbacks===
 +
 
 +
The first callback you can use is with a function, for example:
 +
 
 +
<syntaxhighlight lang="cpp">
 +
void callback(const std_msgs::StringConstPtr& str)
 +
{
 +
...
 +
}
 +
 
 +
...
 +
ros::Subscriber sub = nh.subscribe("my_topic", 1, callback);
 +
</syntaxhighlight>
 +
 
 +
 
 +
The other callback is used with class methods, for example:
 +
 
 +
<syntaxhighlight lang="cpp">
 +
void Foo::callback(const std_msgs::StringConstPtr& message)
 +
{
 +
}
 +
 
 +
...
 +
Foo foo_object;
 +
ros::Subscriber sub = nh.subscribe("my_topic", 1, &Foo::callback, &foo_object);
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 12:40, 10 November 2021




Previous: User Guide/C++/Topics Index Next: User Guide/C++/Publishers_and_subscribers




Introduction

This wiki is based on the following page: http://wiki.ros.org/roscpp/Overview/Publishers%20and%20Subscribers

Publishers and subscribers are one of the main ways of communication when using ROS.

Publishers

In order to publish messages, you will need a node handle, and create a ros::Publisher using the advertise function. Like the following example:

ros::Publisher pub = nh.advertise<std_msgs::String>("topic_name", 5);
std_msgs::String str;
str.data = "hello world";
pub.publish(str);

This uses the function

template<class M>
ros::Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false);

In which:

  • M is the message type to publish on the topic
  • topic is the name of the topic to publish to
  • queue_size is the size of the message queue, if the publishing is faster than the message sending, it will start dropping older messages.
  • latch: Save messages for future subscriber connections automatically.

Subscribers

Similar to publishers, a ROS subscriber needs a node handle and with the subscribe function it will create a new ros::Subscriber, by using the following:

template<class M>
ros::Subscriber subscribe(const std::string& topic, uint32_t queue_size, <callback, which may involve multiple arguments>, const ros::TransportHints& transport_hints = ros::TransportHints());

In which:

  • M is the message type to publish on the topic, normally deduced by the compiler
  • topic is the name of the topic to subscribe to
  • queue_size is the size of the message queue, if messages are too fast arriving and the application can't keep up, they will start dropping.
  • callback: Normally a pointer to a function with the function to execute on each message arrival
  • transport_hints: Hints to roscpp's transport layer. Such ad preferring UDP, tcp nodelay.

Callbacks

The first callback you can use is with a function, for example:

void callback(const std_msgs::StringConstPtr& str)
{
...
}

...
ros::Subscriber sub = nh.subscribe("my_topic", 1, callback);


The other callback is used with class methods, for example:

void Foo::callback(const std_msgs::StringConstPtr& message)
{
}

...
Foo foo_object;
ros::Subscriber sub = nh.subscribe("my_topic", 1, &Foo::callback, &foo_object);