Shanghai Silicon Step ROS Serial Series 40 creates server nodes and client nodes


This section describes how to write ROS message server nodes and client nodes in C++, and how to build and test these two nodes.

1. Write server node code

Use kdevelop to create a C++ file called add_two_ints_server in the ros_tutorials/src directory:

$ roscd ros_tutorials

$ cd src

$kdevelopadd_two_ints_server.cpp

Enter the following code:

#include"ros/ros.h"

#include "ros_tutorials/AddTwoInts.h"

Bool add(ros_tutorials::AddTwoInts::Request &req,

Ros_tutorials::AddTwoInts::Response&res)

{

Res.sum = req.a + req.b;1

ROS_INFO("request: x=%ld, y=%ld",(long int)req.a, (long int)req.b);

ROS_INFO("sending back response:[%ld]", (long int)res.sum);

Return true;

}

Int main(int argc, char**argv)

{

Ros::init(argc, argv,"add_two_ints_server");

Ros::NodeHandle n;

Ros::ServiceServer service =n.advertiseService("add_two_ints", add);

ROS_INFO("Ready to add two ints.");

Ros::spin();

Return 0;

}

Part of the code is explained as follows:

#include "ros_tutorials/AddTwoInts.h"

Ros_tutorials/AddTwoInts.h is the header file corresponding to the srv file generated automatically by the build system based on the previously created srv file.

Bool add(ros_tutorials::AddTwoInts::Request &req,

Ros_tutorials::AddTwoInts::Response&res)

This function provides two services that sum the int values. The int value is obtained from the Request, and the return data is loaded into the Response. These data types are defined inside the srv file, and the function returns a bool value.

{

Res.sum = req.a + req.b;

ROS_INFO("request: x=%ld, y=%ld",(long int)req.a, (long int)req.b);

ROS_INFO("sending back response:[%ld]", (long int)res.sum);

Return true;

}

Two integer additions are performed and the result is stored in the Response. Record some information about Request and Response, and finally return True when the operation is complete.

Ros::ServiceServerservice = n.advertiseService("add_two_ints", add);

Create a service service and broadcast it on the ROS network.

2. Write client node code

Use kdevelop to create a C++ file called add_two_ints_client in the ros_tutorials/src directory:

$ roscd ros_tutorials

$ cd src

$ kdevelopadd_two_ints_client.cpp

Enter the following code:

#include"ros/ros.h"

#include "ros_tutorials/AddTwoInts.h"

#include

Int main(int argc, char**argv)

{

Ros::init(argc, argv,"add_two_ints_client");

If (argc != 3)

{

ROS_INFO("usage: add_two_ints_client XY");

Return 1;

}

Ros::NodeHandle n;

Ros::ServiceClient client =n.serviceClient ("add_two_ints");

Ros_tutorials::AddTwoInts srv;

Srv.request.a = atoll(argv[1]);

Srv.request.b = atoll(argv[2]);

If (client.call(srv))

{

ROS_INFO("Sum: %ld", (longint)srv.response.sum);

}

Else

{

ROS_ERROR("Failed to call serviceadd_two_ints");

Return 1;

}

Return 0;

}

Part of the code is explained as follows:

Ros::ServiceClientclient = n.serviceClient ("add_two_ints");

Create a client for the add_two_ints service. The ros::ServiceClient object is used to subsequently call the service.

Ros_tutorials::AddTwoIntssrv;

Srv.request.a= atoll(argv[1]);

Srv.request.b= atoll(argv[2]);

Instantiate an automatically generated service class and assign a value to its request member. A service class consists of two members, request and response, and also defines the two classes: Request and Response.

If(client.call(srv))

Call the service and return immediately after the call completes. If the service call succeeds, call() will return true, at which point the value in srv.response is valid. If the service call fails, call() will return false and the value in srv.response will be invalid.

3. Build node

Open ~/catkin_ws/src/ros_tutorials/CMakeLists.txt and add the following code to the end:

Add_executable(add_two_ints_serversrc/add_two_ints_server.cpp)

Target_l ink_libraries(add_two_ints_server${catkin_LIBRARIES})

Add_dependencies(add_two_ints_serverros_tutorials_gencpp)

Add_executable(add_two_ints_clientsrc/add_two_ints_client.cpp)

Target_l ink_libraries(add_two_ints_client${catkin_LIBRARIES})

Add_dependencies(add_two_ints_clientros_tutorials_gencpp)

This will create two executables, add_two_ints_server and add_two_ints_client. By default, they will be located in the ros_tutorials package directory in your development space (~/catkin_ws/devel/lib/ros_tutorials), you can call them directly or run them using rosrun.

Run catkin_make:

# In your catkinworkspace

$ cd ~/catkin_ws

$ catkin_make

4. Test server and client nodes

Start roscore first:

$ roscore

Start the server node by running the following command in another terminal window:

$ rosrun ros_tutorialsadd_two_ints_server

You will see the following output:

Ready to add two ints.

Create a new terminal and run the client node:

$ rosrun ros_tutorials add_two_ints_client 2 3

You will see output similar to the following:

Requesting 2+3

2 + 3 = 5


KW4-Micro Switch Quick Connect Terminal

quick connect terminal micro switch,Basic Micro Switch,Quick Connect Terminal 2 Pin

Ningbo Jialin Electronics Co.,Ltd , https://www.donghai-switch.com