How can I use the subscribed message?
Hi,
I've written a very simple subscriber and now my task is to use the subscribed message as a "position value" for my servo. But how to do this ? Let's image that I would have a function called motor_position(uint16_t position) which I have to call and give it the new position value, when and where do I have to call the function?
So my question is, what is the simplest way to use the received message.
Thank you in advance
#include "ros/ros.h"
#include "message/message.h" //is a uint16_t var
uint16_t a;
void chatterCallback(const message::message::ConstPtr &msg)
{
a = msg->position;
ROS_INFO("I heard: [%d]", a);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "subscriber");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("topicname", 1000, chatterCallback);
ros::spinOnce();
// HOW CAN I CALL MY FUNCTION ?
// motor_position(a); THIS is wrong for sure.
return 0;
}
////////////////////////////EDIT//////////////////////////////// Does not compile ?!
#include "ros/ros.h"
#include "message/message.h"
void chatterCallback(const message::message::ConstPtr &msg, int arg)
{
//do something with arg
uint16_t a = msg->position;
ROS_INFO("I heard: [%d]", a);
}
int main(int argc, char **argv)
{
int my_arg= 0;
ros::init(argc, argv, "subscriber");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("topicname", 1000, boost::bind(chatterCallback, _1, my_arg ));
ros::spin();
return 0;
}
There is a standard ROS messages: std_msgs/UInt16 that you might wanna use.