The 3rd chapter of the preparation of ROS program-3

Source: Internet
Author: User

1. Subscribers ' programs

We continue to use Turtlesim as a test platform to subscribe to the/turtle1/pose topics published by Turtlesim_node. The message of this topic describes the posture (position and orientation) of the turtle. Although you are already familiar with some parts of the process, there are three new points of knowledge.

One important difference in writing callback functions for publishing and subscribing to messages is that the subscriber node cannot know when the message arrives. in response to this fact, we have to put the code that responds to the message event in the callback function, which is called once every new message that is received by ROS. The subscriber's callback function is similar to the following:

void function_name (const package_name::type_name &msg)
{
. . .
}

Where the parameters Package_name and type_name are the same as when the message was published , they indicate the message class of the topic we want to subscribe to. The principal of the callback function has access to all domains that receive the message and stores, uses, or discards the received data in a manner that it deems appropriate. as always, we must include the header file that defines the class .

In this routine, the callback function receives a message of type Turtlesim::P ose, so the header file we need is turtlesim/pose.h. (We can use Ros-topic
Info command to determine if this is the correct message type). This callback function simply prints the message data at the terminal via Ros_info_stream, including X, Y, and theta data members. (We can use ROSMSG to see which data fields this message type has). Unlike here, a real program will certainly use the message to do something meaningful.

Note The return value type of the subscriber's callback function is void. In fact, this arrangement is reasonable, because calling this function is the work of Ros , the return value is also given to Ros, so our program can not get the return value , of course, non-void return value type is meaningless.

Create a Subscriber object to subscribe to a topic, we need to create a Ros::subscriber object:

Ros::subscriber sub = Node_handle.subscribe (topic_name,queue_size, pointer_to_callback_function);

This constructor has three parameters, most of which are similar to Ros::P ublisher declarations:

---node_handle is the same as the node handle object we saw many times before.

---topic_name is the name of the topic we want to subscribe to, as a string representation. This routine is "turtle1/pose". Again, we ignore the forward slash to make it a relative name .

---queue_size is the queue size for which this Subscriber receives messages, and is an integer. Typically, you can use a larger integer, such as 1000, without much concern for the queue process.

---The last parameter is a pointer to the callback function, which is the pointer to the callback function when a message arrives. In C + +, you can get a pointer to a function by using the symbolic operator (&, "Fetch") for the function name. In our example, the method is as follows: &posemessagereceived

You may have noticed that when you create a Ros::subscriber object, we don't explicitly mention the message type anywhere. The C + + compiler will determine the correct message type based on the type of data in the function pointers we provide .

The final complication of control over Ros is that it executes our callback function only when we explicitly give Ros permission. There are actually two slightly different ways to do this, one of which is as follows:

Ros::spinonce ();
This code requires ROS to execute all the pending callback functions, and then return the control permissions to us.

Another method is as follows:
Ros::spin ();

This method requires ROS to wait and execute the callback function until the node shuts down.

In other words, Ros::spin () is roughly equal to such a loop:
while (Ros::ok ())
{
Ros::spinonce ();
}

the recommendations for using ros::spinonce () or using Ros::spin () are as follows : Do your programs have any other repetitive work to do in addition to responding to callback functions? If the answer is "no" then use Ros::spin (); otherwise, a reasonable choice is to write a loop, do the other things you need to do, and periodically call ros::spinonce () to handle callbacks. This program uses Ros::spin () because the only thing the program does is to receive and print the received posture messages.

2. Compile and run Subpose

The steps to compile and run this routine are the same as the two routines we saw earlier.

Don't forget to confirm that you added the Turtlesim dependency library for your package because we used the Turtlesim/pose message type.

When Turtlesim_node and pubvel two programs run concurrently, the output of the program is shown in Table 3.6.

Program code:

The 3rd chapter of the preparation of ROS program-3

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.