ROS deliver argument to callback function in subscribers C++/Python
Just a memo.
Reference
How to deliver arguments to a callback function? - ROS Answers: Open Source Q&A Forum
Passing an argument to a subscriber callback [closed] - ROS Answers: Open Source Q&A Forum
Code Example cpp
// in callback void chatterCallback(const sensor_msgs::Imu::ConstPtr& msg, boost::numeric::ublas::vector<double>& pose) { ROS_INFO("Imu Seq: [%d]", msg->header.seq); ROS_INFO("Imu Orientation x: [%f], y: [%f], z: [%f], w: [%f]", msg->orientation.x,msg->orientation.y,msg->orientation.z,msg->orientation.w); pose(0) += 0.1; ROS_INFO("Pose test: [%f]", pose(0)); }
// in main() // init pose with 0 using namespace boost::numeric::ublas; vector<double> pose(3); ros::Subscriber sub = n.subscribe<sensor_msgs::Imu>("imu/data", 1000, boost::bind(chatterCallback, _1, pose));
Python Example
In python, the code become more simpler:
def callback(data, args): dict_1 = args[0] dict_2 = args[1] ... sub = rospy.Subscriber("text", String, callback, (dict_1, dict_2))
Passing arguments to callback in Python - ROS Answers: Open Source Q&A Forum
Remember python is always give argument to function with a pointer of container.
By using adequate function, it is able to change arguments from callback function.