How to Nest multi service clients to a service server callback?
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;
}
}
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.There is something wrong about the edit function, so I attach a picture instead.
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.
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?
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.
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.
Or pass it as an additional arg to the callback, using
std::bind
orboost::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.
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.