Follow+Star Public Account Number, don’t miss out on exciting content
Author | strongerHuang
WeChat Public Account | Embedded ColumnThe application of robots is becoming increasingly widespread. The well-known ZhiHuiJun has directly started a business in robotics, which indicates that in the next decade, robotics will definitely be a hot industry.Currently, many robots on the market are developed based on a system called ROS.Today, I will share a lightweight (micro) ROS running on an MCU based on FreeRTOS.As market demand continues to expand, this MCU-based ROS will become more and more popular, and it is necessary for readers engaged in robotics-related work to understand it.
About ROS
ROS: Robot Operating System.
Unlike ordinary OS (RTOS, TSOS), ROS is primarily aimed at robots and is built on top of an operating system, providing a series of libraries and tools to help software developers create robotic application software. It offers hardware abstraction, device drivers, library functions, visualization, messaging, and package management, among many other features. ROS adheres to the BSD open-source license.ROS designers describe ROS as “ROS = Plumbing + Tools + Capabilities + Ecosystem,” meaning that ROS is a collection of communication mechanisms, tool packages, high-level skills for robots, and the robotic ecosystem.
micro-ROS
The micro-ROS mentioned in this article is a lightweight ROS system optimized based on ROS2. It provides most of the attractive tools and features of the fully deployed ROS 2 ecosystem and has excellent capabilities for embedded and low-resource devices, capable of running on MCU hardware platforms.
Traditionally, even if robots contain many ROS, ROS still remains at the boundary of microcontrollers. They are usually integrated with tools like ROS-serial in the old version of ROS through serial protocols.Wouldn’t it be great to have all the functionalities of ROS2 and the same API within microcontrollers? That is exactly what micro-ROS provides – an embedded part of the ROS development ecosystem within robotic systems.micro-ROS allows developers to run ROS 2 nodes close to the hardware level. This makes all hardware peripherals available for the application, enabling direct interaction with low-level buses like SPI or I²C to interface with sensors and actuators.micro-ROS is a set of layered libraries that can directly reuse ROS 2 libraries and adapt them to the functionalities and needs of resource-constrained devices.Specifically, if we turn to the ROS 2 architecture, the layers maintained by micro-ROS are the ROS Client Library (RCL) and the ROS Middleware Interface (RMW). Similarly, RCLCPP is the C++ abstraction layer on top of RCL, which, although most directly interfaces with RCL, can also be used by micro-ROS application components. This layer provides additional functionalities relative to ROS 2 in RCLC, which is a library written in C99, specifically designed and developed to provide functionalities similar to those offered by RCLCPP, such as convenience functions or executors, to suit microcontrollers.
Typically, ROS is a system that runs on top of Linux systems, while this micro-ROS system runs on FreeRTOS.This makes micro-ROS compatible with most embedded platforms at both hardware and software levels.However, the final component of the micro-ROS architecture is the RMW implementation, which is based on a middleware library called Micro XRCE-DDS. Micro XRCE-DDS is a C/C++ implementation of the DDS-XRCE (Data Distribution Service for Extremely Resource-Constrained Environments) protocol defined and maintained by the Object Management Group (OMG).
As the name suggests, DDS-XRCE is a wired protocol that allows the introduction of a data-centric publisher-subscriber DDS model into the embedded world. DDS-XRCE relies on a client-server architecture, where the client is a lightweight entity written in C99 that can run on low-resource devices, while the proxy (a C++11 application) acts as a bridge between the client and the DDS world. The DDS-XRCE protocol is responsible for passing requests and messages between these two entities. Accordingly, the proxy is able to communicate with the global DDS data space through standard DDS wired protocols. In the DDS world, the proxy represents the client by communicating with other DDS participants. This communication is coordinated by the client proxy, which is able to interact with DDS through a simulated DDS application that can communicate with all standard DDS entities. The proxy keeps the client’s state in its memory, so even if the proxy disconnects, it can survive. Communication between the proxy and the client follows a request-response pattern, which is bidirectional and based on operations and responses.
Why Choose FreeRTOS?
Due to their lightweight nature, the XRCE-DDS client library and micro-ROS are easy to run on top of real-time operating systems, allowing them to meet the strict timing requirements posed by their typical target applications, which involve tasks that require deadlines or deterministic responses.
Specifically, FreeRTOS has become one of the first RTOS supported by the micro-ROS project, and it has been integrated into its software stack. This allows for the reuse of all tools and implementations provided by the FreeRTOS community and partners. Since the micro-ROS software stack is modular, it is expected and anticipated to exchange software entities.FreeRTOS is an ideal choice for developing micro-ROS and Micro XRCE-DDS applications. First, it provides an independent solution for many different architectures and development tools, is written in a very clear and transparent manner, and has a very large user base, ensuring that a significant number of FreeRTOS users will be able to integrate their applications with micro-ROS applications. Moreover, it is well-known for being a highly reliable RTOS. Crucially, FreeRTOS has minimal ROM, RAM, and processing overhead. Typically, the size of the RTOS kernel binary image ranges from 6K to 12K bytes. Since there is competition for resources with the RTOS, this memory is ideal when minimizing the memory footprint of micro-ROS applications on MCUs.In the following sections, we will discuss several features provided by FreeRTOS and how micro-ROS leverages them to optimize the required functionalities of the different libraries that make up its stack.
Tasks and Scheduler
FreeRTOS provides a minimal set of task entities that, along with the use of the scheduler, provide the necessary tools for achieving determinism in applications.The micro-ROS client libraries (RCL, RCLC, and RCLCPP) access the resources of the RTOS to control scheduling and power management mechanisms, thus providing developers with the possibility to optimize their applications.
FreeRTOS provides two types of tasks: standard tasks and idle tasks. The former are created by the user and can be seen as applications on the RTOS. Crucially, integrating micro-ROS applications into the RTOS as one of these tasks with a given priority is essential. Idle tasks, on the other hand, are lower-priority tasks that only enter the running mode when no other tasks are running. Since micro-ROS is primarily aimed at low-power and IoT devices, these idle tasks and related idle hooks are very suitable for enabling deep sleep states in MCUs. Since the stateless XRCE-DDS client is implemented as micro-ROS middleware, these deep sleep states can be volatile, meaning that due to the connection-oriented middleware wired protocol, deep sleep modes can be used without RAM persistence.Using the FreeRTOS scheduler, micro-ROS is able to manage the priorities of its main tasks as well as the tasks responsible for the transport layer. Typically, tasks responsible for the network stack or serial interfaces must take precedence over micro-ROS applications.
Memory Management
One of the most anticipated features provided by FreeRTOS (which is very interesting for micro-ROS developers and users) is stack management and the ability to create static stacks.When handling the creation of tasks in micro-ROS, stack allocation is typically a critical design decision.FreeRTOS allows for fine-grained stack size management, which in turn enables programmers to know how much stack memory is being used during program execution, or for example, to determine whether stack memory allocation is in static or dynamic memory, thus helping to correctly utilize the memory of the MCU, which is a valuable resource in embedded systems.Crucially, heavy stack usage tasks can be provided for micro-ROS and assigned statically allocated stacks, thus preventing future heap and other task initialization issues.
In this regard, it is worth mentioning that these memory management tools provide an ideal framework for benchmarking the memory footprint of micro-ROS and XRCE-DDS. Specifically, a thorough stack consumption analysis has been conducted to assess the memory consumption of the XRCE-DDS client. The stack is a block of memory unknown to the programmer before running the application. To measure it, the FreeRTOS uxTaskGetStackHighWaterMark() function can be used, which returns the amount of unused stack when the XRCE-DDS task stack reaches its maximum during execution. By subtracting this value from the total stack, the peak stack used by the XRCE-DDS application can be obtained. The results obtained using this method are summarized in the report published here.We also note that since a pluggable dynamic memory management approach is used in FreeRTOS, micro-ROS is able to complete the necessary interfaces for managing memory. In this way, functions such as calloc() or realloc() have been implemented using heap_4 as a reference. These functions have been wrapped before feeding into the micro-ROS memory management API to analyze dynamic memory consumption.Similar to static memory situations, FreeRTOS’s interchangeable dynamic memory management approach makes it particularly easy to perform dynamic memory profiling analysis in embedded systems. Indeed, while in other RTOSs, dynamic (de)allocation functionalities are hidden deep within the RTOS or standard libraries, in FreeRTOS, they are exposed to users and easy to customize, thus simplifying the process of handling and controlling dynamic memory usage.
Transport
Similar to how the client support library accesses specific primitives and functionalities of FreeRTOS (such as scheduling mechanisms), the middleware implementation Micro XRCE-DDS requires access to the transport and timing resources of the RTOS to function properly.Regarding IP transport, in the specific case of FreeRTOS, Micro XRCE-DDS uses the lwIP implementation on this RTOS.lwIP (Lightweight IP) is a widely used open-source TCP/IP stack designed for embedded systems, aimed at reducing resource usage while still providing a complete TCP stack.This makes the use of lwIP particularly suitable for embedded systems and resource-constrained environments targeted by micro-ROS.
In addition to the TCP/IP stack, lwIP has several other important components, such as network interfaces, operating system emulation layers, buffers, and memory management parts. The operating system emulation layer and network interface allow the network stack to be ported to the operating system, as it provides a common interface between lwIP code and the operating system kernel.The integration of FreeRTOS with lwIP is designed from the ground up, with standard and familiar interfaces (Berkeley sockets) and thread safety, aimed at making it as easy to use as possible. Moreover, it can keep buffer management within the portable layer.Note that the XRCE-DDS client also supports the FreeRTOS + TCP network stack. FreeRTOS + TCP is the official FreeRTOS extension library for TCP/IP stack protocol support.Efforts have also been made to make FreeRTOS + TCP compatible with micro-ROS. This includes support for TCP and UDP connections, which rely on the FreeRTOS + TCP API to provide the abstraction layer required by the micro XRCE-DDS Client API to communicate with the proxy using these protocols.Additionally, there is the possibility of using FreeRTOS’s timing measurement capabilities, allowing the XRCE-DDS library to perform time-based tasks without the user noticing the implementation.
POSIX Extensions
Another significant reason for the seamless and profitable integration of FreeRTOS into micro-ROS is the availability of POSIX extensions.The Portable Operating System Interface (POSIX) is a set of standards specified by the IEEE Computer Society to maintain compatibility between operating systems.The FreeRTOS + POSIX layer provided by FreeRTOS Labs implements a subset of the POSIX API.
Indeed, although the micro-ROS middleware has lower POSIX dependencies (just the clock_gettime() function), the entire micro-ROS stack has higher dependencies related to functionalities and type definitions. Moreover, since one of the fundamental principles of the micro-ROS project is to port or reuse the natively coded ROS 2 from Linux (primarily POSIX-compatible operating systems), a certain degree of RTOS that is compatible with Linux is used. POSIX is clearly beneficial as it requires minimal effort to port the code.To this end, functions like sleep() and usleep() are used. The POSIX type definition dependencies of micro-ROS rely on certain structures not defined in the FreeRTOS kernel, such as struct timeval or struct timespec. Files like type.h, signal.h, or unistd.h are also needed to define some standard type definitions and structures.For errno.h, although it is not implemented in the FreeRTOS + POSIX layer, micro-ROS must include some unavailable definitions for compilation purposes.By using the FreeRTOS + FAT library, these definitions should be reconstructed in the micro-ROS stack to ensure full compatibility with FreeRTOS + POSIX. This way, advanced micro-ROS functionalities that depend on file system support, such as logging mechanisms, can be fully supported.
More Related Tutorials
How to Create and Run Your First micro-ROS Application on the Olimex STM32-E407 Evaluation Board Using FreeRTOS.
The tutorial address:https://micro-ros.github.io/docs/tutorials/core/first_application_rtos/freertos/(The public account does not support external links, please copy the link to open in the browser)For more content, please refer to:https://micro-ros.github.io/
———— END ————

● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorials”
● Selected Tutorials from the Embedded Column
Follow the public account Reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Click “Read the Original” to see more shares.