How to Nest multi service clients to a service server callback?

asked 2019-03-28 07:43:27 -0600

七小丘人 gravatar image

updated 2019-04-03 08:18:40 -0600

The robot is controlled by serial sevices and I need to control the arm on and off. Then the nodehandle need to be global. I think my usage may be wrong, or should I just try to make the nodehandle global? My problem is how to pass the nodehandle into sever callback.The code certainly cannot be executed because the nodehandle in callback is not declared.

int main()
    {
       ros::init(argc, argv, "DobotTask");
       ros::NodeHandle n;
       ros::ServiceClient client;
        // SetCmdTimeout
        client = n.serviceClient<dobot::SetCmdTimeout>("/DobotServer/SetCmdTimeout");
         srv1.request.timeout = 3000;
         if (client.call(srv1) == false) {
               ROS_ERROR("Wait fot dobotserver to be on.");
             }
         ros::ServiceServer arm_server =
         n.advertiseService("arm_car_interact_srv",interact);
         return 0;
     }

bool interact(dobot_msgs::arm_car_interact::Request &req,dobot_msgs::arm_car_interact::Response &res)
    {

       if (req.arm_on == 1){
        do {
              client = n.serviceClient<dobot::SetPTPCmd>("/DobotServer/SetPTPCmd");
              dobot::SetPTPCmd srv;
               srv.request.ptpMode = 1;
               srv.request.x = 200;
               srv.request.y = 100;
                srv.request.z = 100;
                srv.request.r = 0;
                client.call(srv);
                if (srv.response.result == 0) {
                 break;
                }     
                ros::spinOnce();
                if (ros::ok() == false) {
                break;
                }
           } while (1);
       res.car_on = 1;
       }

    }
edit retag flag offensive close merge delete

Comments

Can you please edit your question using the preformatted text (101010) button? It's not currently readable as is. The quote (") is for quoting text/people, not for code, terminal output, etc.

jayess gravatar image jayess  ( 2019-03-28 14:21:14 -0600 )edit

There is something wrong about the edit function, so I attach a picture instead.

七小丘人 gravatar image 七小丘人  ( 2019-03-28 21:54:37 -0600 )edit
1

one could argue that it is a bug/flaw that you have to start with some text and cannot directly start pasting code. This is why you experienced some problem with formatting the main function.

However, in general, some explanation of what your actual problem is, i.e. what do you want to do, what did you try and what failed, does help in getting answers.

mgruhler gravatar image mgruhler  ( 2019-03-29 01:49:08 -0600 )edit

so, what is your problem exactly? I'm guessing this does not compile, but do you get an error? Or do you have another issue?

mgruhler gravatar image mgruhler  ( 2019-03-29 05:37:28 -0600 )edit

Sorry for not explaining exactly. My problem is how to pass the nodehandle into sever callback.The code certainly cannot be executed because the nodehandle in callback is not declared.

七小丘人 gravatar image 七小丘人  ( 2019-03-29 06:56:22 -0600 )edit

Alright, out of the blue (you still haven't shown any error): your problem is that this doesn't compile but you want to call another service in your callback.

You can simply create a new nodehandle where you need it. Otherwise, as you said, you should make it global.

mgruhler gravatar image mgruhler  ( 2019-04-03 09:16:40 -0600 )edit
1

You can simply create a new nodehandle where you need it. Otherwise, as you said, you should make it global.

Or pass it as an additional arg to the callback, using std::bind or boost::bind.


Edit: but creating ServiceClient instances in a callback might not be the best way to approach this.

@七小丘人: try creating the service client in the initialisation phase of your program. Then make that client instance available to your callback and call the service from your callback that way.

gvdhoorn gravatar image gvdhoorn  ( 2019-04-03 10:23:05 -0600 )edit

I did't realize that a ros node can have multiple nodehandles and they refer to the same node. I will try these 3 methods.

七小丘人 gravatar image 七小丘人  ( 2019-04-09 02:11:21 -0600 )edit