Skip to content
Thomas Chou edited this page Jul 24, 2024 · 28 revisions

micro_ros_arduino_examples_platformio

This is a port of examples from micro_ros_arduino to PlatfromIO.

Install the software tools

The latest ROS2 LTS release is Jazzy and the latest Ubuntu LTS release is 24.04. This combination is recommended for beginners and new designs. The micro_ros_platformio is based on Arduino framework. Most Arduino drivers should work. The following guide assumes you are building esp32 micro-ROS firmware for ROS2 Jazzy on Ubuntu 24.04.

Install Ubuntu 24.04 and low latency kernel

Install Ubuntu 24.04 Desktop. Or, if you use Rpi4/5 as robot computer, you may use vscode/ssh for remote development on your Windows or Linux desktop.

On Ubuntu graphical login, click setup wheel in the lower right corner, select "Ubuntu on Xorg". The RVIZ needs Xorg to work properly.

Install low latency kernel and reboot to use new kernel. Default linux kernel is optimized for computation. Switch to low latency kernel will improve realtime response for robot control.

sudo apt update
sudo apt upgrade -y
sudo apt-get install -y --install-recommends linux-lowlatency
sudo reboot

Prepare

Install essential build tools. Remove brltty package which interferes with CH340 USB serial bridge on some esp32 boards.

sudo apt remove -y brltty
sudo apt install -y python3-venv build-essential cmake git curl

Install PlatfromIO and udev rules

curl -fsSL -o get-platformio.py https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py
python3 get-platformio.py
rm get-platformio.py
echo "PATH=\"\$PATH:\$HOME/.platformio/penv/bin\"" >> $HOME/.bashrc
source ~/.bashrc
curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/system/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules
sudo service udev restart
sudo usermod -a -G dialout $USER
sudo usermod -a -G plugdev $USER

Blink - Optional

Blink is the "hello world" for micro-controller. It should be run when we get a new board as the very first test. Clone the example source into your working directory. Change the env "esp32" to the board you use, eg "pico" or "teensy41" etc. Build and upload. The on-board LED should blink every two seconds.

git clone https://github.com/hippo5329/Blink-platformio.git
cd Blink-platformio
pio run -e esp32 -t upload

Install ROS2 Jazzy

(PS. If you still want to use Humble and Ubuntu 22.04, you will need to replace all commands here from "jazzy" to "humble".)

sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
sudo apt update
sudo apt install -y ros-jazzy-desktop ros-dev-tools python3-colcon-common-extensions python3-pip
sudo rosdep init
rosdep update
echo "source /opt/ros/jazzy/setup.bash" >> ~/.bashrc
source ~/.bashrc

Build micro-ROS agent

mkdir ~/uros_ws/src -p
cd ~/uros_ws/src
git clone -b $ROS_DISTRO https://github.com/micro-ROS/micro-ROS-Agent.git
git clone -b $ROS_DISTRO https://github.com/micro-ROS/micro_ros_msgs.git
cd ..
rosdep install --from-paths src --ignore-src -r -y
colcon build
cd ~
echo "source \$HOME/uros_ws/install/setup.bash" >> ~/.bashrc
source ~/.bashrc

Clone examples repository

Clone the examples source into your working directory.

git clone https://github.com/hippo5329/micro_ros_arduino_examples_platformio.git

Serial transport

Take micro-ros_publisher as an example, build and upload to esp32.

cd micro_ros_arduino_examples_platformio/micro-ros_publisher
pio run -e esp32 -t upload

Then connect to micro-ROS serial agent.

ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/ttyUSB0 --baudrate 921600

Screen output after connect.

[1718994292.702493] info     | TermiosAgentLinux.cpp | init                     | running...             | fd: 14
[1718994292.702712] info     | Root.cpp           | set_verbose_level        | logger setup           | verbose_level: 4
[1718994293.013253] info     | Root.cpp           | create_client            | create                 | client_key: 0x64E60CB5, session_id: 0x81
[1718994293.013293] info     | SessionManager.hpp | establish_session        | session established    | client_key: 0x64E60CB5, address: 0
[1718994293.033488] info     | ProxyClient.cpp    | create_participant       | participant created    | client_key: 0x64E60CB5, participant_id: 0x000(1)
[1718994293.049832] info     | ProxyClient.cpp    | create_topic             | topic created          | client_key: 0x64E60CB5, topic_id: 0x000(2), participant_id: 0x000(1)
[1718994293.060696] info     | ProxyClient.cpp    | create_publisher         | publisher created      | client_key: 0x64E60CB5, publisher_id: 0x000(3), participant_id: 0x000(1)
[1718994293.072949] info     | ProxyClient.cpp    | create_datawriter        | datawriter created     | client_key: 0x64E60CB5, datawriter_id: 0x000(5), publisher_id: 0x000(3)

use pico

pio run -e pico -t upload
ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/ttyACM0

use teensy41

pio run -e teensy41 -t upload
ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/ttyACM0

Check topic

Open another terminal to check the publisher node and topic.

ubuntu@better-snipe:~$ ros2 node list
/micro_ros_arduino_node

ubuntu@better-snipe:~$ ros2 node info /micro_ros_arduino_node
/micro_ros_arduino_node
Subscribers:
Publishers:
    /micro_ros_arduino_node_publisher: std_msgs/msg/Int32
Service Servers:
Service Clients:
Action Servers:
Action Clients:

ubuntu@better-snipe:~$ ros2 topic list -t
/micro_ros_arduino_node_publisher [std_msgs/msg/Int32]
/parameter_events [rcl_interfaces/msg/ParameterEvent]
/rosout [rcl_interfaces/msg/Log]

ubuntu@better-snipe:~$ ros2 topic echo /micro_ros_arduino_node_publisher
data: 162
---
data: 163
---
data: 164
---

Wifi transport

Update micro_ros_arduino_examples_platformio/platformio/platformio.ini with agent ip (ie. your computer's ip) and wifi keys.

[env]
...
build_flags =
    ...
    '-D AGENT_IP={ 192, 168, 1, 100 }'
    '-D AGENT_PORT=8888'
    '-D WIFI_SSID="wifi_ssid"'
    '-D WIFI_PASSWORD="wifi_password"'

Take micro-ros_publisher as an example, build and upload to esp32.

cd micro_ros_arduino_examples_platformio/micro-ros_publisher
pio run -e esp32_wifi -t upload

Run micro-ROS wifi agent.

ros2 run micro_ros_agent micro_ros_agent udp4 --port 8888

Screen output of agent after connected.

[1719004509.846308] info     | UDPv4AgentLinux.cpp | init                     | running...             | port: 8888
[1719004509.846534] info     | Root.cpp           | set_verbose_level        | logger setup           | verbose_level: 4
[1719004511.136599] info     | Root.cpp           | create_client            | create                 | client_key: 0x6CFC0C00, session_id: 0x81
[1719004511.136763] info     | SessionManager.hpp | establish_session        | session established    | client_key: 0x6CFC0C00, address: 192.168.1.101:47138
[1719004511.348916] info     | ProxyClient.cpp    | create_participant       | participant created    | client_key: 0x6CFC0C00, participant_id: 0x000(1)
[1719004511.399397] info     | ProxyClient.cpp    | create_topic             | topic created          | client_key: 0x6CFC0C00, topic_id: 0x000(2), participant_id: 0x000(1)
[1719004511.416526] info     | ProxyClient.cpp    | create_publisher         | publisher created      | client_key: 0x6CFC0C00, publisher_id: 0x000(3), participant_id: 0x000(1)
[1719004511.461559] info     | ProxyClient.cpp    | create_datawriter        | datawriter created     | client_key: 0x6CFC0C00, datawriter_id: 0x000(5), publisher_id: 0x000(3)

Open another terminal to check the publisher node and topic as it was in serial transport.

Supported ROS and Linux distributions

  • rolling -- ubuntu 24.04
  • jazzy -- ubuntu 24.04 -- recommended
  • iron -- ubuntu 22.04
  • humble -- ubuntu 22.04
  • (foxy EOL) -- (ubuntu 20.04 EOL)

Supported boards

portenta_h7_m7, teensy41, teensy40, teensy36, teensy35, teensy31, due, zero, olimex_e407, esp32, nanorp2040connect, pico.

micro_ros_arduino examples

Supported examples:

  • micro-ros_subscriber
  • micro-ros_subscriber_twist
  • micro-ros_addtwoints_service
  • micro-ros_time_sync
  • micro-ros_publisher
  • micro-ros_types_handling
  • micro-ros_reconnection_example

Unsupported examples due to specific boards/sensors requirement:

  • micro-ros_IMU_ML
  • micro-ros_tf_publisher
  • micro-ros_decibels

Removed examples:

  • micro-ros_publisher_ethernet = micro-ros_publisher
  • micro-ros_publisher_wifi = micro-ros_publisher

Development on Windows or Linux desktop and use VScode remote development SSH to Rpi4/5

If you run Windows or Linux desktop and want to develop on Rpi4/5, you can use remote development SSH feature of VSCode. Though the setup may take some time, it will save a lot of time in the future.

Overview

  1. Install VSCode on Windows or Linux desktop
  2. Install Remote development extension on VSCode
  3. Generate SSH key pair on Windows or Linux desktop
  4. Upload SSH public key to your github account
  5. Import SSH key from github to Rpi
  6. Connect to Rpi from VSCode on Windows or Linux desktop
  7. Edit and build your application on Rpi through VSCode on Windows or Linux desktop

In details

Install VSCode on Windows or Linux desktop

Open browser on Windows. Download and install VSCode. https://code.visualstudio.com/download

Install Remote development extension on VSCode

Run VSCode and add the "Remote Development" extension from microsoft.com.

Generate SSH key pair on Windows or Linux desktop

Run PowerShell on Windows or open a terminal on Linux desktop. Run ssh-keygen and press Enter to the questions.

ssh-keygen -t ed25519
cat .ssh/id_ed25519.pub
XXXXXX

The long string XXXXXX is your SSH public key. Copy it to clipboard.

Upload SSH public key to your github account

Open browser. Sign-in to your github. In Profile - Settings - SSH and GPG keys - New SSH Key, paste the key from your clipboard and save.

Import SSH key from github to Rpi

You may create your Rpi SD card using RPI Imager, select "Ubuntu 24.04 LTS Server" to run ROS2 Jazzy. Then setup your WIFI keys, username, password and ssh key XXXXXX. Or, after you login the Rpi on screen or ssh, import the ssk key from github.

sudo apt install -y ssh-import-id
ssh-import-id-gh <your github account name>

It should install and list your public keys on github.

Connect to Rpi from VSCode on Windows or Linux desktop

From VSCode, press F1, search SSH, Remote SSH: Add New SSH Host, enter the command below, change ubuntu to your username on Rpi, change 192.168.1.100 to your Rpi IP address.

ssh ubuntu@192.168.1.100

Save it to .ssh/config on your Windows account. Press F1 again, search SSH, Remote SSH: Connect to Host.. or Connect current Windows to Host..

Edit and build your application on Rpi through VSCode on Windows or Linux desktop

Now the VSCode is connected to the remote Rpi. Open a terminal in vscode to run command on Rpi. Run "ssh-keygen -t ed25519" for Rpi and add the public key to your github.

You will install VSCode extensions, such as PlatfromIO, GitLens etc, to your Rpi. And clone repository from github, edit, build and develop to Rpi from your Windows or Linux desktop. The code will be built on the Rpi. This way, you develop remotely on your VSCode on Windows or Linux desktop. You do not need a monitor connected to your Rpi.

You may start with the Blink example. It is the "hello world" for micro-controller. It should be run when we get a new board as the first test.

https://github.com/hippo5329/Blink-platformio

You may fork the Blink example, use vscode on Windows to clone build and upload on Rpi4/5 to esp32. You will use use "SSH" url to clone your forks instead of "HTTPS" when you clone the other people's repos.

VSCode on Windows --> Rpi4/5 --> esp32

VSCode on Windows or Linux desktop is your command center. You can open terminals in VSCode on Windows to run and launch ros2 on Rpi4. A browser (mostly to github.com) and VSCode will server your development work. Once you get familiar with this github/vscode/platformio workflow, you will work better then ever. enjoy.

Tip: enable trailing whitespace trimming.

Additional tutorials

Learn VSCode

Learn VSCode SSH

Git guide on github

Getting started on VSCode

VSCode SSH Pi

PlatfromIO IDE for VSCode - a bit outdated

ROS2 and VSCode - 2024 very new

micro-ROS demos on PlatfromIO

linorobot2_hardware firmware for mobile robots

VNC server Install VNC server on Rpi, and view RVIZ etc on Windows with VNC viewer.

Add two int service example

cd micro_ros_arduino_examples_platformio/micro-ros_addtwoints_service

Build, upload and connect to micro-ROS agent as before. Open another terminal to call the service.

ros2 service call <service_name> <service_type> <arguments>
ros2 interface show <type_name>

ros2 service call /addtwoints example_interfaces/srv/AddTwoInts "{ a: 12, b: 13 }"

ubuntu@better-snipe:~$ ros2 node list
/add_twoints_client_rclc

ubuntu@better-snipe:~$ ros2 node info /add_twoints_client_rclc
/add_twoints_client_rclc
Subscribers:
Publishers:
Service Servers:
    /addtwoints: example_interfaces/srv/AddTwoInts
Service Clients:
Action Servers:
Action Clients:

ubuntu@better-snipe:~$ ros2 service list -t
/addtwoints [example_interfaces/srv/AddTwoInts]

ubuntu@better-snipe:~$ ros2 service call /addtwoints example_interfaces/srv/AddTwoInts "{ a: 12, b: 23 }"
requester: making request: example_interfaces.srv.AddTwoInts_Request(a=12, b=23)

response:
example_interfaces.srv.AddTwoInts_Response(sum=35)