# ROS2 Real-time Programming
###### tags: `software` `electrical_system` `NTURT`
## Introduction to real-time in linux
Using a realtime Linux kernel such as `PREEMPT_RT patch` does not mean that your program will automatically run in realtime, necessary setups and consideration should be taken care of for the application to perform well.
Since the `PREEMPT_RT patch` is the easiest way to make a program run realtime, we will only discuss this method onwards.
- Excellent overview of real-time Linux programming: [Real-time programming with Linux](https://shuhaowu.com/blog/2022/01-linux-rt-appdev-part1.html)
- Official website: [Linux Foundation](https://wiki.linuxfoundation.org/realtime/start)
- Tutorial from official website: [Documentaion](https://wiki.linuxfoundation.org/realtime/documentation/start)
### Using docker
- Using docker with `PREEMPT_RT patch`: [2b-t/docker-realtime](https://github.com/2b-t/docker-realtime)
### Real-time memory allocator
- Two Level Segregate Fit: [TLSF](http://www.gii.upv.es/tlsf/)
- Implementation: [mattconte/tlsf](https://github.com/mattconte/tlsf)
## Real-time ROS2
### Resources
- ROS2 real-time working group: [Documentation](https://ros-realtime.github.io/)
- Ubuntu 22.04 realtime image for raspbetty pi 4: [ros-realtime/ros-realtime-rpi4-image](https://github.com/ros-realtime/ros-realtime-rpi4-image)
### Examples
- ROS2 real-time control example: [ros2-realtime-demo/pendulum](https://github.com/ros2-realtime-demo/pendulum)
- Example about programming ros2 in real-time fasion: [ros-realtime/ros2-realtime-examples](https://github.com/ros-realtime/ros2-realtime-examples)
- Other examples still work in progress: [rolling-experimental](https://github.com/ros-realtime/ros2-realtime-examples/tree/rolling-experimental)
### IMPORTANT NOTE
Despite that real-time programming puts STRONG enphasis on preemption that let different programs to have different priorities, the rclcpp executor does NOT have such support. Instead, there's a unique underlying mechanism that determines in what order callbacks are called in ros2. Please checkout [Executors](https://docs.ros.org/en/humble/Concepts/About-Executors.html) for more information.
### ==VERY IMPORTANT NOTE==
Despite that you're using custom allocators to avoid using `malloc`, the underlying dds of ros2 uses it EVERYWHERE, which leads to unbounded program execution time and HIGH jitter.
This is documented in the ros2 real-time example above: [minimal_dds_tuning](https://github.com/ros-realtime/ros2-realtime-examples/tree/rolling-experimental/minimal_dds_tuning). Despite that in [2019 ros con](https://vimeo.com/379127767) very minial allocation is obtained, by replicating his fast dds config file to current ros2 version `humble`, it just don't work until I change the [<historyMemoryPolicy>](https://github.com/ros-realtime/ros2-realtime-examples/blob/rolling-experimental/minimal_dds_tuning/dds_profiles/rmw_fastrtps/REALTIME_FASTRTPS_PROFILES.xml#L44) from `PREALLOCATED` to `PREALLOCATED_WITH_REALLOC`, which still allocate memory A LOT, such a huge bummer.