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

ros c++ pause unpause gazebo

asked 2019-03-28 01:10:29 -0500

TouchDeeper gravatar image

Hi, I want to control the gazebo simulation by c++. My core code is below:

        ros::ServiceClient pauseGazebo = n_.serviceClient<std_srvs::Empty>("/gazebo/pause_physics");
        std_srvs::Empty pauseSrv;
        pauseGazebo.call(pauseSrv);
        std::cout<<"enter q to continue"<<std::endl;
        char key;
        std::cin>>key;
        if('q' == key)
        {
            pauseGazebo.shutdown();
            ros::ServiceClient unpauseGazebo = n_.serviceClient<std_srvs::Empty>("/gazebo/unpause_physics");
            std_srvs::Empty unpauseSrv;
            pauseGazebo.call(unpauseSrv);
            std::cout<<"q is pushed"<<std::endl;
        }

The puase work well, after I enter q, the "q is pushed" output, but the gazebo is still in the state of pasue. Can you provide some tip?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2019-03-28 01:57:23 -0500

janindu gravatar image

You have a typo in your code. The second service call should be

unpauseGazebo.call(unpauseSrv);

whereas you have

pauseGazebo.call(unpauseSrv);

which you have already shutdown.

A couple of more suggestions / information (as I have OCD)

You don't need to explicitly call shutdown as per documentation. When the service client goes out of scope, it will be automatically shutdown.

Also, declaring everything on the top makes the code more readable, like this

    ros::ServiceClient pauseGazebo = n_.serviceClient<std_srvs::Empty>("/gazebo/pause_physics");
    ros::ServiceClient unpauseGazebo = n_.serviceClient<std_srvs::Empty>("/gazebo/unpause_physics");
    std_srvs::Empty pauseSrv;
    std_srvs::Empty unpauseSrv;
    char key;

    pauseGazebo.call(pauseSrv);

    std::cout<<"enter q to continue"<<std::endl;

    std::cin>>key;
    if('q' == key)
    {           
        unpauseGazebo.call(unpauseSrv);
        std::cout<<"q is pushed"<<std::endl;
    }
edit flag offensive delete link more

Comments

Hi,@janindu Thanks for your point. You are right. I‘m too careless.

TouchDeeper gravatar image TouchDeeper  ( 2019-03-28 07:50:49 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-03-28 01:10:29 -0500

Seen: 848 times

Last updated: Mar 28 '19