How to connect Teensy 3.2 with Micro-ROS and ROS 2 Foxy
By: Manzur Murshid
Tags: Robotics, Ros2, Teensy, Arduino, Micro Ros

A few months ago while I was trying to develop a mobile robotic system using a differential drive robot, one important thing that came to my mind is “What robot architecture should I use?” and then immediately I decided to go with the ROS system since I work with ROS-based robot every day in my work. And then I asked myself, what ROS version should I use? ROS1 or ROS 2, and I think most robot developers will tell me here to use ROS 2 since the ROS1 is almost in its EOL (end of life). Since I do not have much experience of using ROS 2, I thought this is the perfect opportunity for me to jump in and learn the ROS 2 system on the fly. This sounds very simple right? not just yet. Let me briefly introduce the hardware system I was planning to use where the problem starts. I have a differential drive robot that will be controlled by a low-level controller and in my case, it is a teensy 3.2 MCU.

Now the problem is that for ROS 2 there is no rosserial package available, unlike in ROS1 rosserial is the official package for communicating with Arduino-based microcontrollers and many more. But after some digging around, I found the micro-ROS package built on the Micro XRCE-DDS client, which is still in the beta phase but closest to rosserial. This package is also compatible with most of the latest Arduino-based Microcontrollers that have more than 75 KB flash memory and 2.5 KB of RAM. Unfortunately, it will not support Arduino Uno or ATmega328P based 8-bit MCUs at the moment based on this news link and micro-ROS developers.

Installation:

So without further due let us start with the basics. For this tutorial, we will need a host computer with either having a native Ubuntu 20.04 installed ROS 2 foxy or using a docker version of the freshly build ROS 2 foxy from this link. Now let us also look at the connection diagram which will help us understand the full picture better.

Connection diagram of Teensy 3.2 with Host PC running ros2 and micro-ros-agent.

1. Installing ROS2 and micro-ROS in the host computer: These first few steps are the same as in the micro-ROS installation page as in this link

To start with, install ROS 2 Foxy FitzRoy on your Ubuntu 20.04 LTS computer. You can do this from binaries, via Ubuntu packages, which are detailed here.

Note: Otherwise it is possible to use fresh docker build of ROS2 foxy installation by running these commands:

$ sudo apt install docker.io

$ sudo docker run -it --net=host -v /dev:/dev --privileged ros:foxy

After running the docker, follow the command to verify if the ROS2 is running and shows the topic list:

Docker builds ROS 2 foxy version can also be used where it is not possible to install native ROS 2 foxy from binaries, e,g. Jetson Nano running jetpack 4.5 with Ubuntu 18.04.

Now Once you have a ROS 2 installation in the computer or docker, follow these steps to install the micro-ROS build system:

#Source the ROS 2 installation

#source /opt/ros/foxy/setup.bash

$ source /opt/ros/$ROS_DISTRO/setup.bash # Create a workspace and download the micro-ROS tools

$ mkdir microros_ws

$ cd microros_ws

$ git clone -b $ROS_DISTRO https://github.com/micro-ROS/micro_ros_setup.git src/micro_ros_setup # Update dependencies using rosdep

$ sudo apt update && rosdep update

$ rosdep install --from-path src --ignore-src -y # Install pip

$ sudo apt-get install python3-pip



# Build micro-ROS tools and source them

$ colcon build

$ source install/local_setup.bash

Once the micro-ROS installation is complete, we can then proceed to install the micro-ros-agent in the host computer or the docker version. Since we are going to use Teensy 3.2 and precompiled micro-ROS client library for our demonstration we will not be going to build the firmware and thus we will skip the firmware build steps from the same page.

To install the micro-ros-agent follow the steps below:

# Download micro-ROS-Agent packages

$ ros2 run micro_ros_setup create_agent_ws.sh

We will now build the agent packages and, when this is done, source the installation:

# Build step

$ ros2 run micro_ros_setup build_agent.sh

$ source install/local_setup.bash

Now, let's give a dry run by running the micro-ros-agent by following the command:

$ ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/ttyACM0

The result should show something like this

This means the installation of the micro-ros-agent is successful. Now we can proceed to the next step which is the installation of Arduino IDE and Teensyduino and patching the Arduino-based Teensy board for using the pre-compiled libraries as described here.