1.2.3. ROS 2 Service Server

This example tackles the task of bridging a ROS 2 server with one or more client applications, implemented using a wide variety of protocols.

Specifically, we discuss how to forward petitions coming from Fast DDS, ROS 1 and a WebSocket service client applications to a ROS 2 add_two_ints_server server application, from the built-in ROS 2 package demo_nodes_cpp; so that it can process them and fulfill each request with a proper answer message.

../../../_images/ros2-server.png

Note

If you are looking for an example on how to perform a service request from a ROS 2 client to another protocol, please refer to any of the remaining examples in the server/client examples section.

1.2.3.1. Requirements

To prepare the deployment and setup the environment, you need to have Integration Service correctly installed in your system. To do so, please follow the steps delineated in the Installation section.

Also, to get this example working, the following requirements must be met:

  • Having Fast DDS (v.2.0.0 or superior) installed and the Integration Service DDSAddTwoInts example working. This example can be found in the main Integration Service repository, under the examples/utils/dds/DDSAddTwoInts folder; to compile it, you can either compile the whole Integration Service project using colcon with the CMake flag BUILD_EXAMPLES enabled; or execute the following steps:

    cd ~/is-workspace/src/Integration-Service/examples/utils/dds/DDSAddTwoInts
    mkdir build && cd build
    cmake .. -DBUILD_EXAMPLES=ON && make
    
  • Having the Fast DDS System Handle installed. You can download it from the FastDDS-SH dedicated repository into the is-workspace where you have Integration Service installed:

    cd ~/is-workspace
    git clone https://github.com/eProsima/FastDDS-SH.git src/FastDDS-SH
    
  • Having ROS 1 (Melodic or superior) installed and the Integration Service example_interfaces ROS 1 package compiled. This package can be found in the main Integration Service repository, under the examples/utils/ros1/src/example_interfaces folder. To compile and install it:

    source /opt/ros/$<ROS1_DISTRO>/setup.bash
    cd ~/is-workspace/src/Integration-Service/example/utils/ros1/catkin_ws
    catkin_make -DBUILD_EXAMPLES=ON -DCMAKE_INSTALL_PREFIX=/opt/ros/$<ROS1_DISTRO> install
    
  • Having the ROS 1 System Handle installed. You can download it from the ROS1-SH dedicated repository into the is-workspace where you have Integration Service installed:

    cd ~/is-workspace
    git clone https://github.com/eProsima/ROS1-SH.git src/ROS1-SH
    
  • Having ROS 2 (Foxy or superior) installed, along with the demo_nodes_cpp package. To install it:

    apt install ros-$<ROS2_DISTRO>-demo-nodes-cpp
    
  • Having the ROS 2 System Handle installed. You can download it from the ROS2-SH dedicated repository into the is-workspace where you have Integration Service installed:

    cd ~/is-workspace
    git clone https://github.com/eProsima/ROS2-SH.git src/ROS2-SH src/ros2-sh
    
  • Having OpenSSL and WebSocket++ installed:

    apt install libssl-dev libwebsocketpp-dev
    
  • Having the WebSocket System Handle installed. You can download it from the WebSocket-SH dedicated repository into the is-workspace where you have Integration Service installed:

    cd ~/is-workspace
    git clone https://github.com/eProsima/WebSocket-SH.git src/WebSocket-SH
    

After you have everything correctly installed in your is-workspace, build the packages by running:

source /opt/ros/$<ROS2_DISTRO>/setup.bash
colcon build --packages-skip-regex is-ros1 -DMIX_ROS_PACKAGES="example_interfaces"
source /opt/ros/$<ROS1_DISTRO>/setup.bash
colcon build --cmake-args -DBUILD_EXAMPLES=ON -DMIX_ROS_PACKAGES="example_interfaces"

1.2.3.2. Deployment

Below we explain how to deploy a full example of this communication, calling the ROS 2 service from each of the available clients.

Launch the ROS 2 demo_nodes_cpp add_two_ints_server

To do so, open a terminal and execute the following command:

source /opt/ros/$<ROS2_DISTRO>/setup.bash
ros2 run demo_nodes_cpp add_two_ints_server

The server will start running as an independent ROS 2 node, listening for incoming petitions.

Execute Integration Service

Open two terminals:

  • In the first terminal, source the ROS 1 installation and run the roscore:

    source /opt/ros/$<ROS1_DISTRO>/setup.bash
    roscore
    
  • In the second terminal, go to the is-workspace folder, source the ROS 1, ROS 2 and local installations, and execute Integration Service with the integration-service command followed by the ros2_server__addtwoints.yaml configuration file located in the src/Integration-Service/examples/basic folder.

    source /opt/ros/$<ROS1_DISTRO>/setup.bash
    source /opt/ros/$<ROS2_DISTRO>/setup.bash
    source install/setup.bash
    integration-service src/Integration-Service/examples/basic/ros2_server__addtwoints.yaml
    

Call the service from Fast DDS

In a new terminal, go to the is-workspace folder and execute the following command:

./build/is-examples/dds/DDSAddTwoInts/DDSAddTwoInts -m client -c <number_of_requests>

The DDSAddTwoInts example application will request to add two numbers an specific amount of times, specified with the -c flag; if not present, ten requests will be performed by default.

For instance, if -c 4, should see something like this in your screen, indicating that the ROS 2 server is processing the requests:

AddTwoIntsService client running under DDS Domain ID: 0
AddTwoIntsService client performing 4 requests.
AddTwoIntsService client:
        - Request 1 + 3
        - Received response: 4
AddTwoIntsService client:
        - Request 2 + 4
        - Received response: 6
AddTwoIntsService client:
        - Request 3 + 5
        - Received response: 8
AddTwoIntsService client:
        - Request 4 + 6
        - Received response: 10

Call the service from ROS 1

In a new terminal, source your ROS 1 installation and invoke the service by executing the following instructions:

source /opt/ros/$<ROS1_DISTRO>/setup.bash
rosservice call /add_two_ints 3 4

You should receive the following output from the ROS 2 server processing the petition:

sum: 7

Call the service from WebSocket

In order to call the server from a WebSocket client click here. The hyperlink leads to a webpage that creates a WebSocket connection to ws://localhost:80 where the Integration-Service* has created the WebSocket Server according with the yaml file.

The WebSocket System Handle uses this handshake protocol. Basically once the connection is established the client will send a message to advertise the service it wants to use. The message syntax is like:

{"op": "advertise_service", "service": "add_two_ints", "request_type": "AddTwoInts_Request", "reply_type": "AddTwoInts_Response"}

Once the service has been advertised, the request is send using a message format like:

{"op": "call_service", "service": "add_two_ints", "args": {"a": 14, "b": 25}}

Once the server has met the request it sends back the answer formatted as:

{"op":"service_response","result":true,"service":"add_two_ints","values":{"sum":39}}