# Latency Test
###### tags: `electrical_system` `NTURT`
## Introduction
One of the most important thing in control is to minize the latency, hance the inherent latency of a structure has to be measured. In order to validfy our design choice of using a specific control structure, the measured latency is documented here.
All test sources and results are avalible here: [NTURacingTeam/nturt_latency_test](https://github.com/NTURacingTeam/nturt_latency_test).
## Overall Latency
The overall latency of our control system is roughtly **60 ms** for ep4, which to say is **horrible**. The source code and the testing instruction can be found in [github](https://github.com/NTURacingTeam/nturt_latency_test/tree/master/nturt_control_latency_test).
## ROS2 Latency
### Resources
- An avaliable package specifically for benchmarking ros2 can be found here: [irobot-ros/ros2-performance](https://github.com/irobot-ros/ros2-performance).
- A comprehensive paper about the same topic can be found here: [Latency Analysis of ROS2 Multi-Node System](https://arxiv.org/pdf/2101.02074.pdf), yet it **does NOT** use a real-time kernel.
### Tests
The RTT(round trip test) of ros2 message is around 0.25ms in real-time kernel.
Please checkout: [github](https://github.com/NTURacingTeam/nturt_latency_test/tree/master/nturt_rpi_can_latency_test#ros2-message-round-trip-test).
## Micro ROS Latency
### Resources
There is a benchmarking tutorial avaliable in the [official website](https://micro.ros.org/docs/concepts/benchmarking/benchmarking/), but I barely understand what they're doing, and only result avaliable is from 2020: [Micro-ROS benchmarks](http://www.ofera.eu/storage/deliverables/M32/OFERA_55_D54_Micro-ROS_benchmarks_-_Final.pdf). It;s shown that the latency is in the range of **10 microseconds**, but I was told that it might be **200 milliseconds** instead from a believable source. Hence extensive tests are need.
### Tests
The rtt of micro ros is aroud 2ms, but consumes 99% cpu in freertos architecture.
Please checkout: [github](https://github.com/NTURacingTeam/nturt_latency_test/tree/master/nturt_micro_ros_latency_test).
## RPI CAN Latency
### Resources
- A comprehensive paper about the latency of `socketCAN` using real-time kernel: [Timing Analysis of Linux CAN Drivers](https://lwn.net/lwn/images/conf/rtlws11/papers/proc/p30.pdf), the test reslut shows that the latency is at **1ms** range at 2010.
### Tests
The rtt of can in ros2 architecture is around 3ms.
Please checkout: [github](https://github.com/NTURacingTeam/nturt_latency_test/tree/master/nturt_rpi_can_latency_test#can-latency-round-trip-test).
## STM32 CAN Latency
### Tests
#### Bare Metal - H743 to G431
0.298ms round back
operation brief:
1. H743 start timer: ```HAL_GetTick()``` & ```SysTick->VAL```
2. H743 call to HAL Tx FIFO API: ``HAL_FDCAN_AddMessageToTxFifoQ()``
5. (message transmitted over HAL->CAN controller->physical CAN bus)
6. G431 ``HAL_FDCAN_RxFifo0Callback()`` gets called
7. G431 calls ``HAL_FDCAN_GetRxMessage`` in ISR
8. G431 notify main to send message in ISR
9. G431 call ``HAL_FDCAN_AddMessageToTxFifoQ()`` in main
10. (message transmitted over HAL->CAN controller->physical CAN bus)
11. H743 ``HAL_FDCAN_RxFifo0Callback()`` gets called
12. H743 calls ``HAL_FDCAN_GetRxMessage()`` in ISR
13. H743 stops timer: ``HAL_GetTick()`` & ``SysTick->VAL`` in ISR