The fourth parameter usage of Subscribe, and the subscribe parameter usage

Source: Internet
Author: User

The fourth parameter usage of Subscribe, and the subscribe parameter usage

Reading other people's code is really a good learning process.

Previously, Subscribe was used in a simple way, for example:

ros::Subscriber sub = node.subscribe<uhf_rfid_api::UhfRfid>("messageepc", 0, rfid_callback) ;

In this way, only three parameters are used.

In practice, if you want to pass parameters to the callback function, you can use the boost: bind () function in the C ++ boost library, for example:

image_transport::Subscriber sub = it.subscribe(topic_root + "/camera/image", 0, boost::bind(image_callback, _1, &pub_vt));

Here, the first parameter in boost: bind is the name of the callback function, and the second _ 1 is a placeholder, because the first parameter of the callback function image_callback is msg, and the position should be left for it. The third parameter is a passed parameter. If you want to transmit two parameters, such as boost: bind (image_callback, _ 1, & pub_vt, & pub_odo), do not use _ 1 as the number of parameters.

In the callback function, use the following code:

void image_callback(sensor_msgs::ImageConstPtr image, ros::Publisher * pub_vt)

You can write the parameter at the second real parameter O (argument _ argument) O

Next, let's focus on the usage of the fourth parameter I learned. Let's look at the example above:

ros::Subscriber mysub = nh.subscribe("/move_cmd", 0, &Robot::moveCallback, &tars);

The prototype in the API is as follows:

template<class M , class T >Subscriber ros::NodeHandle::subscribe(const std::string &        topic,                                      uint32_t                   queue_size,                                      void(T::*)(M)              fp,                                      T *                        obj,                                      const TransportHints &     transport_hints = TransportHints()                                       )                          [inline]

The above prototype should pay attention to the callback function, which should be in the same class as the fourth parameter. The above examples are all in class T, both fp and obj are pointer types.

The fourth parameter, T * obj, is to pass the pointer of the object instance of class T to the same type of callback function, void (T: *) (M) fp. Therefore, if you change the value of a variable in the instance class obj, it can also be reflected in the callback function. In this way, it is equivalent to using global variables in the class.

Okay, I have explained it. Let me explain it:

void Robot::moveCallback(const fsm_robot::rcmd_move msg){  cmd_int = n;  n++;}

In this example, cmd_int is the global variable in the class instance.

 

Related Article

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.