"11" Writing a simple Publisher and subscriber (C + +)

Source: Internet
Author: User

Publisher

#include "Ros/ros.h"

#include "Std_msgs/string.h"

#include <sstream>

int main (int argc, char **argv)

{

Ros::init (argc, argv, "talker"); 1. Start the Node 2. and set its name (name to be unique)

Ros::nodehandle N; To set a node handle

Ros::P ublisher chatter_pub = n.advertise<std_msgs::string> ("chatter", 1000);

Set the node as the Publisher and inform the node manager of the type and name of the published topic.

The first parameter is the name of the message, which is set to messages;

The second parameter is the size of the buffer. Set the buffer to 1000 messages if the topic publishing data is faster
Ros::rate Loop_rate (10); Frequency of sending data 10Hz

while (Ros::ok ())//Receive CRTL+C message or Ros stops running, the Ros::ok () library executes the Stop node Run command

{

Create a message variable (the type of the message must meet the requirements for sending data)

Std_msgs::string msg;

Std::stringstream SS;

SS << "Hello World" << count;

Msg.data = Ss.str ();

Ros_info ("%s", Msg.data.c_str ()); Ros_info and Friends is our replacement for printf/cout.

Chatter_pub.publish (msg); In this way, the message was released.

Ros::spinonce (); If a subscriber appears, Ros will update and read all topics

Loop_rate.sleep (); Suspend the program according to the frequency of 10Hz. (Why???) )

++count;

}

return 0;

ROS Node

1) in a robot software, there are all kinds of node. Don't answer, all kinds of not refer to Localization,slam, action these functions of various, but refers to have the news producer, the consumer, as well as the consumer and the production deep-processing person. In the first place, this discussion involves the question of whether it is appropriate to build node only from a functional point of view when building a robotic software with Ros. Do you still need to consider making the message flow smoother and making node more concise and efficient? Let's start by saying how node is running. When you create a node, either it runs in a fixed cycle, or it blocks the wait event to occur and is run by the event driver. whatever it is, node has to work, what does node do? Callback the work in the queue. Where did the callback in the callback queue come from? Common is subscriber's callback, and of course other, including publisher's, Service's. When did these callback be called? That's spin () or spinonce (). Spin calls all the availiable callback in the queue, and if there is no availible, it blocks. Spinonce, apparently only called once, to see if there is no ready callback, there is a call, no return. What is availible and ready? For subscribe, the subscriber callback with new news are ready. Now if you understand the operation mechanism of node above. When you feel comfortable, be prepared to accept the problem of annoyance. 1) The first problem is that if Subscriber does not receive a new message, its callback will not be executed. What if you want to run callback every time? Sorry, no way, not virgin I don't marry.

2) The second problem, callback execution has a timeout value, if the setting is unreasonable, or time-consuming, or callback is aborted. Be stopped, brothers, how awful, what if it is an important logical link? Talk about it today, and finally get some benefits, if you want to implement the event-triggered node with spin, and if you want to fix the periodic node, then use Spinonce+sleep, but keep in mind that not all callback are executed in each cycle.

Subscriber

#include "Ros/ros.h"

#include "Std_msgs/string.h"

This function is called every time the node receives a message, and we can use or manipulate the data.

The Ros_info () function is used to print data on the command line

(Now here we print the received data at the command line.) )

void Chattercallback (const std_msgs::string::constptr& msg)

{

Ros_info ("I heard: [%s]", MSG->DATA.C_STR ());

}

int main (int argc, char **argv)

{

Ros::init (argc, argv, "listener");

Ros::nodehandle N;

Create a subscriber and get the message data in the name of chatter from topic.

Set Buffer 1000

The callback function that handles the message is Chattercallback ()

Ros::subscriber sub = N.subscribe ("chatter", chattercallback);

Ros::spin (); The library is the message response loop where node reads the data, and when the message arrives, the callback function is called.

When CTRL + C, node exits the message loop

Ros::spin ();

return 0;

Compile node

The generated CMakeLists.txt should look like this:

Add code:

"11" Writing a simple Publisher and subscriber (C + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.