ros::nh.serviceClinet
时间: 2023-09-23 16:13:29 浏览: 96
The ros::nh.serviceClient is a client object used for calling ROS services in a ROS node. It is a part of the ROS C++ API and is used to establish a connection to a service server and send requests to it.
Here's an example of how to use ros::nh.serviceClient in a ROS node:
```cpp
#include <ros/ros.h>
#include <my_service/MyService.h> // replace with your service message header
int main(int argc, char** argv)
{
ros::init(argc, argv, "my_service_client_node");
ros::NodeHandle nh;
ros::ServiceClient client = nh.serviceClient<my_service::MyService>("my_service");
my_service::MyService srv;
srv.request.input = 42; // set the request message
if (client.call(srv)) // call the service
{
ROS_INFO("Service call successful. Output: %d", srv.response.output);
}
else
{
ROS_ERROR("Service call failed.");
}
return 0;
}
```
In this example, we create a ros::ServiceClient object to connect to a service server named "my_service". We then create a request message of the appropriate type (my_service::MyService in this case), set its input field to 42, and call the service using the call() method of the client object. If the call is successful, we print the output field of the response message to the console. If the call fails, we print an error message.
阅读全文