ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How can I use the subscribed message?

asked 2013-09-10 03:05:55 -0500

Ros451 gravatar image

updated 2013-09-10 06:16:12 -0500

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;
}
edit retag flag offensive close merge delete

Comments

1

There is a standard ROS messages: std_msgs/UInt16 that you might wanna use.

dornhege gravatar image dornhege  ( 2013-09-10 03:31:53 -0500 )edit

2 Answers

Sort by » oldest newest most voted
0

answered 2013-09-10 03:30:43 -0500

dornhege gravatar image

Put it in the callback and that's it.

edit flag offensive delete link more

Comments

Thank you! This is what I was looking for, but can I also pass some parameters to the callback function from the main() like my filedescriptor for my port etc. ?

Ros451 gravatar image Ros451  ( 2013-09-10 03:54:17 -0500 )edit

Yes, check out boost::bind for this. You might also find some examples around here. - Although for your setup that might not be what you want. If you plan is to connect to a device, you should do that once in init and not on every message.

dornhege gravatar image dornhege  ( 2013-09-10 04:22:17 -0500 )edit

Thank you for your reply, I've tried myself and failed, the code doesn't compile (I edited my post see the new code). I want just to pass my_arg to the callback function.

Ros451 gravatar image Ros451  ( 2013-09-10 05:07:06 -0500 )edit
0

answered 2013-09-10 03:37:58 -0500

Johannes gravatar image

if you want to call the function motor_position in a regular basis (let's say every x Hz) you call it during a loop in the main function. In this case use a variable in the callback to save the value for later use in the main function. If you want to call it just once ever time you get a message from the subscriber then call it in the Callback function. As your code lets me guess you want to do the first solution. Your problem is that you do not have a while loop in the main.

The main should look somehow like this:

int main(int argc, char **argv) {
  ros::init(argc, argv, "subscriber");
  ros::NodeHandle n;

  ros::Subscriber sub = n.subscribe("topicname", 1000, chatterCallback);

  ros::Rate loop_rate(10);
  while (ros::ok()) {
    motor_position(a);
    ros::spinOnce();
    loop_rate.sleep();
  }
  return 0;
}
edit flag offensive delete link more

Comments

Thank you very much, this helps me a lot! For the second case I have a further question: can I also pass some parameters to the callback function from the main() like my filedescriptor for my port etc. ?

Ros451 gravatar image Ros451  ( 2013-09-10 03:51:26 -0500 )edit

To answere this my programming skills are not good enought, but I would suggest the simplest way would be to use a global variable für the filedescriptor that can be accessed from all functions in this class (therefore also the callback function).

Johannes gravatar image Johannes  ( 2013-10-15 21:57:24 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2013-09-10 03:05:55 -0500

Seen: 464 times

Last updated: Sep 10 '13