Micro-ROS Ping Pong

post hero image

Introduction

Before reading this article, please take a look at Install micro-ROS build system slice from ROS2 Humble Basics.

Architecture

The architecture of the micro-ROS stack follows the ROS 2 architecture. Dark blue components are developed specifically for micro-ROS. Light blue components are taken from the standard ROS 2 stack.

micro-ROS architecture

Communication Flow

This blog shows a node with two publisher-subscriber pairs associated with ping and pong topics. The node sends a ping package using a ping publisher. If the ping subscriber receives a ping from an external node, the pong publisher reply with a pong. To test if the logic works, it has been implemented a communication with a ROS 2 node that:

  • listens to the topics published by the ping subscriber
  • publishes a fake_ping package, received by the micro-ROS ping subscriber.
  • The pong publisher on the micro-ROS application will then publish a pong: it received the fake_ping correctly

The diagram below shows the communication flow:

communication-flow

Build the firmware

Since you’ll be using different Terminal windows, let’s name them: the first one will be T1.

First of all, let’s locate ping_pong directory from microros_workspace, that were created in ROS2 Humble Basics article and setup the workspace. It was in the home directory, so let’s open a terminal and type:

cd ~/microros_workspace/src/micro_ros_setup/scripts

# build the firmware and source the local installation
ros2 run micro_ros_setup build_firmware.sh
source install/local_setup.bash

Create the micro-ROS agent

The micro-ROS application is now ready to be connected to a micro-ROS agent to start talking with the rest of the ROS 2 world. Let’s first create a micro-ROS agent: source /opt/ros/$ROS_DISTRO/setup.sh

# download micro-ROS-Agent packages
ros2 run micro_ros_setup create_agent_ws.sh

# build the agent packages and source the installation
ros2 run micro_ros_setup build_agent.sh
source install/local_setup.bash

Run the micro-ROS app

Both the client and the agent are correctly installed in the host machine. To give micro-ROS access to the ROS 2 dataspace, run the agent:

# run a micro-ROS agent
ros2 run micro_ros_agent micro_ros_agent udp4 --port 8888

Then open a new terminal window (T2) to run the micro-ROS node:

source /opt/ros/$ROS_DISTRO/setup.sh
source install/local_setup.sh

# use RMW Micro XRCE-DDS implementation
export RMW_IMPLEMENTATION=rmw_microxrcedds

# run a micro-ROS node
ros2 run micro_ros_demos_rclc ping_pong

The output will be:

micro ros demos rclc output

Testing the micro-ROS app

To check that everything is working, open a Terminal window (T3) and listen to the ping topic with ROS 2 to check whether the micro-ROS Ping Pong node is correctly publishing the expected pings:

source /opt/ros/$ROS_DISTRO/setup.sh

# subscribe to micro-ROS ping topic
ros2 topic echo /microROS/ping

T1 next to T2 (on the right) looks like:

subscribe to micro-ROS ping output

Let’s open T3* to subscribe to the pong topic:

source /opt/ros/$ROS_DISTRO/setup.sh

# subscribe to micro-ROS pong topic
ros2 topic echo /microROS/pong

Now take T2 and interrupt the running command with Ctrl + C (will be indicated with ^C), then publish a fake_ping with ROS 2:

# send a fake ping
ros2 topic pub --once /microROS/ping std_msgs/msg/Header '{frame_id: "fake_ping"}'

You should see the fake_ping in the pong subscriber console.

Conclusion

Documentation

Here is the full list of links I used to write this article: