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

Having trouble applying the C++ Subscriber application

asked 2014-04-22 05:55:00 -0600

paturdc gravatar image

I am trying to retrieve some values from a published topic, and using that data in my main function. I can almost make it work with global variables, but I believe that is poor programming etiquette? Before I continue debugging my global variable solution, I would like to know if there is a simpler, more efficient way. Here is the callback function:

void chatterCallback(const sensor_msgs::JointState msg)
{
   for (int i=0;i<NUMBER_OF_JOINTS;i++)
    {
    std::cout << "The " << msg.name.at(i) << " is now at " << msg.position.at(i);
    std::cout << " moving with velocity " << msg.velocity.at(i) << "\n";
    }
}

I want to pass the data contained in the sensor_msgs::JointState to my main function, in order to start the program by ascertaining the robot's joint values. Also, I'm not sure the subscriber callback function is allowed to use sensor_msgs::JointState, rather than ConstPtr. The code works like this, though.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-04-22 06:15:32 -0600

dornhege gravatar image

The subscriber signature works, but it is better to pass by reference, i.e. add &.

Depending on the program size, global variables might be fine. A usual larger approach for something like this would be to capture the JointState message in a class where the code lies that uses this. Within the class you can subscribe the callback in a member function and store the message in there.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-04-22 05:55:00 -0600

Seen: 175 times

Last updated: Apr 22 '14