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

Revision history [back]

click to hide/show revision 1
initial version

There are few things wrong in your code, you should really read the wiki about subscribers, the example match your situation.

First thing to say is : why do you create a class for this ? Eventhough you'll probably go further seeing all the possible inputs, you could separate everything into different functions that would be call in the switch..case instead of a class.

Secondly, the line kuka listen_0(nh); is within the loop which means you create a new object each time you are looping which is bad because you would loose every attributes data (if you had one). There is a similar issue with your function status(), you redefine a subscriber each time you call the function. Knowing that a subscriber takes time to register to a topic if you needed to get messages that are published at high rate you would lose a lot of data.

Finally your loop as it is wouldn't allow to add ros::spinOnce() to be called at some rate because of the cin. Because of cin your program is blocked (meaning the callback too) until you input something. As soon as you do you start again your loop where you create a new obect and also a new subscriber, thus a new callback so the previous message (if there was any) would be lost.

Your code should have this template :

//Global variable to trigger the command input
bool trigger_user_input = false;

void callback()
{
    //Do your stuff
    trigger_user_input = true
}
int main(int argc, char** argv)
{
    //Init the node, subscribers/publishers, main variables here
    while (ros::ok())
    {
        //Call spinOnce to continue the loop
        ros::spinOnce();

        //Do your stuff only when you had the callback
        if (trigger_user_input)
        {
            //Set the flag to false to continue spinning next time
            trigger_user_input = false;
            std::cin << loop;
            //Do your stuff with cin here
        }
    }
    return 0;
 }

And again, you should read the ros tutorials, there are plenty of examples in there.