# C++ Timer System for NTUR ###### tags: `firmware_hardware` `electrical_system` `NTURT` ## Introduction The **C++ Timing System for NTUR** is designed to be a pure C++ based firmware infratructure to imlement a versatile solution on the racecar's firmware. The purpose of this design is to replace FreeRTOS in order to avoid the flaws of RTOS, such as: - Multiple tasks may write the same variable during runtime, leads to confliction - Difficult to debug and find problems of your code, since RTOS is not based on a line-by-line operation logic - Complex hardware & firmware settings may confuse new-learners, such as **mutexs**, **kernel settings**, **memory**, ... Instead of running as multiple threads in RTOS, tasks (ex. CAN communication, dashboard, APPS, ...) in the **C++ Timing System for NTUR** runs in a ==**function-based priority queue**== In simple words, each task is represented by one function, and **C++ function object** can maniluplate the functions as objects, and further manage their precedence in a **priority queue** storing the function objects and run them in a order ## Prerequisite There are few intermediate C++ concepts required to understand how the **C++ Timer System for NTUR** works, the topics of background knowledge you should have in advance are listed below: ### STL Libraries ### Function Object ### Timing We use the `<chrono>` header in C++, it provides a powerful and versatile way to measure and manipulate time intervals, essential concepts of `chrono` are introduced below #### Basic elements - Clocks Represent the source of time measurements. The `<chrono>` library offers three primary clock types: - `std::chrono::system_clock` Retrieves the system's wall-clock time - `std::chrono::steady_clock` Provides a monotonic (always increasing) time source, unaffected by system time changes. It's suitable for measuring elapsed time during code execution. - `std::chrono::high_resolution_clock` Offers the highest possible resolution for time measurements on the current system. It's ideal for very precise timing needs. - Time points Represent specific moments in time obtained from a particular clock. You can use `std::chrono::time_point` to capture the current time from a chosen clock - Durations Represent the elapsed time between two time points. You can calculate the duration between two time_point objects using `std::chrono::duration_cast`