Repository Summary

Description Example of how to setup micro-ROS on any STM32 microcontroller
Checkout URI https://github.com/lfatality/stm32_micro_ros_setup.git
VCS Type git
VCS Version main
Last Updated 2023-04-09
Dev Status UNKNOWN
Released UNRELEASED
Tags No category tags.
Contributing Help Wanted (-)
Good First Issues (-)
Pull Requests to Review (-)

Packages

Name Version
micro_ros_setup 0.0.0
drive_base_msgs 0.0.0
micro_ros_agent 1.5.0
micro_ros_msgs 1.0.0

README

Setting up micro-ROS on any STM32 microcontroller

This repository gives an example of how to set up micro-ROS on any STM32 microcontroller. For this repository an STM32F429ZI was chosen but you are free to choose another one.

You can also find the instructions in video form here: https://youtu.be/xbWaHARjSmk

Goal

If you follow all steps in the tutorial you should have an STM32 microcontroller with a micro-ROS publisher that transmits messages via UART to your PC running ROS 2. Here you can receive the messages via ros2 topic echo.

Steps

In the following the required steps to achieve the goal are presented in detail. It will show how to do this for a microcontroller that is not listed as a device directly supported by micro-ROS. If you find that you have problems check out the Troubleshooting section below.

micro-ROS

1.) Create a new CubeMx project for your micro controller
2.) In System Core -> RCC -> High Speed Clock (HSE) select Crystal/Ceramic Resonator
3.) In System Core -> SYS -> Timebase Source select TIM1
4.) In Middleware -> FREERTOS -> Interface select CMSIS_V2
4.1.) In Middleware -> FREERTOS -> Configuration -> Task and Queues double click the defaultTask and set a stack size of 3000. It has to be greater than 10.000 byte (3000 words * 4 byte = 12.000 byte).
5.) In Connectivity choose the UART / USART that you want to use.
5.1.) In the Uart configuration, go to DMA Settings. Click on the Add button. Click on the Select dropdown and choose both Rx and Tx.
5.2.) Click on the Rx DMA you’ve just created and for Mode choose Circular.
5.3.) For the priority of the DMA choose Very high for both Rx and Tx.
5.4.) Go to NVIC Settings of the UART and activate the UARTx global interrupt.
6.) Set up the Clock Configuration for your micro controller
7.) In Project Manager select a folder where to generate your code
7.1.) In Toolchain / IDE select Makefile
7.2.) Optional: In Project Manager -> Code Generator select Generate peripheral intitialization as a pair of '.c/.h' files per peripheral
8.) Click on Generate Code
9.) In the root folder of the code you’ve just generated, clone the following repository (into a subfolder, don’t change its name).:

git clone https://github.com/micro-ROS/micro_ros_stm32cubemx_utils.git

10.) Make sure you have the right branch for your ROS version checked out 11.) In the Makefile that was generated by CubeMx put the following code snippet after the part where it says build the application:

#######################################
# micro-ROS addons
#######################################
LDFLAGS += micro_ros_stm32cubemx_utils/microros_static_library/libmicroros/libmicroros.a
C_INCLUDES += -Imicro_ros_stm32cubemx_utils/microros_static_library/libmicroros/microros_include

# Add micro-ROS utils
C_SOURCES += micro_ros_stm32cubemx_utils/extra_sources/custom_memory_manager.c
C_SOURCES += micro_ros_stm32cubemx_utils/extra_sources/microros_allocators.c
C_SOURCES += micro_ros_stm32cubemx_utils/extra_sources/microros_time.c

# Set here the custom transport implementation
C_SOURCES += micro_ros_stm32cubemx_utils/extra_sources/microros_transports/dma_transport.c

print_cflags:
  @echo $(CFLAGS)

11.) Pull and run the following docker to generate the micro-ros lib. Make sure you use the right ROS version when you pull / run the docker. This should be executed in the root folder of your project.

docker pull microros/micro_ros_static_library_builder:galactic
docker run -it --rm -v $(pwd):/project --env MICROROS_LIBRARY_FOLDER=micro_ros_stm32cubemx_utils/microros_static_library microros/micro_ros_static_library_builder:galactic

12.) If it asks for the CFLAGS, if you can see some, continue. They might look like this:

Found CFLAGS:
-------------
-mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F429xx -ICore/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IMiddlewares/Third_Party/FreeRTOS/Source/include -IMiddlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS -IMiddlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Imicro_ros_stm32cubemx_utils/microros_static_library/libmicroros/microros_include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MFprint_cflags
-------------

If instead the CFLAGS are empty there was likely a mistake. If you get an error about a missing separator in the Makefile, check the line in the Makefile. For me there were 2 similar includes. I deleted one of them.

13.) If you get an error like this during building:

'rcutils' exports library 'dl' which couldn't be found

That’s ok and can be ignored.

14.) Go into Core/main.cpp and adjust it so that it’s similar to the sample_main.cpp you can find in the micro_ros_stm32cubemx_utils repository we found earlier. The most interesting parts are the following:

```cpp #include <rcl/rcl.h> #include <rcl/error_handling.h> #include <rclc/rclc.h> #include <rclc/executor.h> #include <uxr/client/transport.h> #include <rmw_microxrcedds_c/config.h> #include <rmw_microros/rmw_microros.h>

#include <std_msgs/msg/int32.h>

File truncated at 100 lines see the full file