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


This section explains how to create ROS message server and client nodes in C++, as well as how to build and test these two types of nodes.

1. Writing the Server Node Code

Use KDevelop to create a C++ file named add_two_ints_server in the ros_tutorials/src directory:

$ roscd ros_tutorials
$ cd src
$ kdevelop add_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;
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;
}

Explanation of parts of the code:

The line #include "ros_tutorials/AddTwoInts.h" refers to the header file generated by the build system based on the previously created .srv file.

The function bool add(...) is the service handler that performs the addition of two integers. It takes the request values from the Request object and returns the result in the Response object. The data types are defined in the .srv file, and the function returns a boolean indicating success or failure.

The line ros::ServiceServer service = n.advertiseService("add_two_ints", add); creates and advertises the service on the ROS network.

2. Writing the Client Node Code

Create a C++ file called add_two_ints_client in the same directory using KDevelop:

$ roscd ros_tutorials
$ cd src
$ kdevelop add_two_ints_client.cpp

Enter the following code:

#include "ros/ros.h"
#include "ros_tutorials/AddTwoInts.h"

int main(int argc, char **argv)
{
ros::init(argc, argv, "add_two_ints_client");
if (argc != 3)
{
ROS_INFO("usage: add_two_ints_client X Y");
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", (long int)srv.response.sum);
}
else
{
ROS_ERROR("Failed to call service add_two_ints");
return 1;
}
return 0;
}

Explanation of parts of the code:

The line ros::ServiceClient client = n.serviceClient<...>("add_two_ints"); creates a client for the add_two_ints service.

The code ros_tutorials::AddTwoInts srv; instantiates the service class, which contains the request and response objects. Values are assigned to the request members, and then the service is called using client.call(srv).

3. Building the Nodes

Edit the CMakeLists.txt file in the ros_tutorials package and add the following lines:

add_executable(add_two_ints_server src/add_two_ints_server.cpp)
target_link_libraries(add_two_ints_server ${catkin_LIBRARIES})
add_dependencies(add_two_ints_server ros_tutorials_gencpp)

add_executable(add_two_ints_client src/add_two_ints_client.cpp)
target_link_libraries(add_two_ints_client ${catkin_LIBRARIES})
add_dependencies(add_two_ints_client ros_tutorials_gencpp)

This will generate two executables, add_two_ints_server and add_two_ints_client, located in the devel/lib/ros_tutorials directory. You can run them with rosrun.

Run catkin_make in your workspace:

$ cd ~/catkin_ws
$ catkin_make

4. Testing the Server and Client Nodes

Start roscore first:

$ roscore

In another terminal, start the server node:

$ rosrun ros_tutorials add_two_ints_server

You should see the message: Ready to add two ints.

Open a new terminal and run the client node:

$ rosrun ros_tutorials add_two_ints_client 2 3

You should see output similar to: Sum: 5

USB To RJ45 Adapters

USB To RJ45 Adapters


A USB to Ethernet adapter is a device that is capable of connecting a USB port to an Ethernet cable. USB to Ethernet adapters allow users to connect multiple devices together via an Ethernet cable rather than using a USB Cable, which is generally shorter and less reliable.


7

Usb Hub for Macbook Pro,Usb C Charger,Usb Charger Plug,Usb to Ethernet Adapter

Pogo Technology International Ltd , https://www.wisesir.net

Posted on