How to connect Teensy 3.2 with Micro-ROS and ROS 2 Foxy

Manzur Murshid
7 min readJul 6, 2021

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.

2. Installation of Arduino IDE, Teensyduino and setting up the patch for using the Teensy with micro-ROS and ROS 2 foxy:

Please follow the link for downloading the latest version of Arduino 1.8.15 and install by following this link for the Linux version here.

After installing Arduino IDE download Teensyduino from this link here and follow the instruction as shown on this page. To summarize the instructions which are as follow:

1. Download the Linux udev rules and copy the file to /etc/udev/rules.d. 
https://www.pjrc.com/teensy/00-teensy.rules
2. type the following command in a terminal
$ sudo cp 00-teensy.rules /etc/udev/rules.d/
3. Download and extract one of Arduino's Linux packages.
Note: Arduino from Linux distro packages is not supported.
4. Download the corresponding Teensyduino installer.5. Run the installer in a termincal by adding execute permission and then execute it.
$ chmod 755 TeensyduinoInstall.linux64
$ ./TeensyduinoInstall.linux64

Now let's set up the patch for the teensy Arduino to use the pre-compiled micro-ros-client libraries, Open a terminal window and follow the commands below: For more information follow the GitHub link from micro-ros-arduino

# for me it was $ export ARDUINO_PATH=/home/manzur/arduino-1.8.13/
$ export ARDUINO_PATH=[Your Arduino + Teensiduino path]
$ cd $ARDUINO_PATH/hardware/teensy/avr/$ curl https://raw.githubusercontent.com/micro-ROS/micro_ros_arduino/foxy/extras/patching_boards/platform_teensy.txt > platform.txt

Once the above instruction is complete, we will now be able to use the Teensy 3.2 and program it with the pre-compiled micro-ros-client libraries using Arduino IDE.

3. Program the Teensy

Now that we have patched the teensy Arduino IDE, we will be able to use the pre-compiled library by following these instructions,

1. Go to link to release section and download the last release of micro-ROS library for Arduino. Place the file inside /home/$USERNAME/Arduino/libraries/ as shown below.

Once this process is complete, now let us look at the example folder below:

For this tutorial and test, we will be using mico-ros-publisher example as shown above since this program will only publish integer data which will increase in every cycle. Once we selected the example program, we will then upload the code in the teensy 3.2 connected to our host computer which should show the result as follow.

4. Running micro-ros-agent in ROS 2 Foxy

Now, let's disconnect the teensy for now from the host computer. We will then open a terminal or in the docker run the agent program once again as shown at the end of step 2. Make sure to source the ROS path as below:

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

and then run the agent program:

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

Once the program is running it will show this message:

We will then reconnect the Teensy with the host computer and then we will see that the connection is complete and it shows like this:

This means the connection is complete with teensy containing micro-ros-client and micro-ros-agent in the host computer.

Now for the big moment and test the ROS topic published from the teensy. This time we will open another terminal or docker window and type as follow:

$ ros2 topic list

Which should list as shown below:

See, we have now /micro_ros_arduino_node_publisher topic publishing in the host computer. If we listen to the topic we will see something like this:

The integer msg data increasing in each cycle.

Now, this brings us to the end of this tutorial on how to install and program teensy to use micro-ROS with ROS 2. For more information, I have attached the reference link to the micro-ROS and ROS 2 pages. I am also planning to start tutorials on developing a ROS 2 based mobile robot and its application, so follow for more installation.

References

  1. https://micro.ros.org//docs/tutorials/core/first_application_linux/
  2. https://github.com/micro-ROS/micro_ros_arduino/tree/foxy
  3. https://hub.docker.com/_/ros
  4. https://www.pjrc.com/teensy/td_download.html
  5. https://github.com/micro-ROS/micro_ros_setup/issues
  6. https://github.com/micro-ROS/micro_ros_arduino/issues

--

--

Manzur Murshid

Robot enthusiast, maker, DIY and Open-source advocacy