Scheduler class template for scheduling tasks on multiple MCP2515 instances Takes in function pointers to member functions of MCP2515 class, and calls them at specified intervals, using a unified central ticker. More...
#include <Scheduler.hpp>
Public Member Functions | |
| Scheduler ()=delete | |
| Scheduler (uint32_t period_us_, uint32_t spin_threshold_us_, unsigned long(*const current_time_us)()) | |
| Construct a new Scheduler object. | |
| void | update () |
| Update the scheduler, checking if tasks need to be run based on the current time. | |
| void | synchronize (unsigned long(*const current_time_us)()) |
| Synchonize the scheduler to the current time, resetting all task counters, used when starting multiple Schedulers across different boards together. | |
| bool | addTask (const McpIndex mcp_index, const TaskFn task, const uint8_t tick_interval) |
| Add a task to the scheduler for a specific MCP2515 index. | |
| bool | removeTask (const McpIndex mcp_index, const TaskFn task) |
| Remove a task from the scheduler for a specific MCP2515 instance. | |
| constexpr uint32_t | getPeriodUs () const |
| Returns the period of the scheduler in microseconds. | |
| constexpr uint32_t | cyclesNeeded (const uint32_t interval_us) const |
| Returns the number of cycles needed for a given interval in microseconds. | |
Private Types | |
| using | TaskFn = void(*)() |
Private Member Functions | |
| void | runTasks () |
| Helper function to run scheduled tasks. | |
Private Attributes | |
| TaskFn | tasks [NUM_MCP2515][NUM_TASKS] |
| uint8_t | task_ticks [NUM_MCP2515][NUM_TASKS] |
| uint8_t | task_counters [NUM_MCP2515][NUM_TASKS] |
| uint8_t | task_cnt [NUM_MCP2515] |
| const uint32_t | PERIOD_US |
| const uint32_t | SPIN_US |
| uint32_t | last_fire_us |
| unsigned long(*const | CURRENT_TIME_US )() |
Scheduler class template for scheduling tasks on multiple MCP2515 instances Takes in function pointers to member functions of MCP2515 class, and calls them at specified intervals, using a unified central ticker.
The Scheduler class holds function pointers to tasks to be run on multiple MCP2515 instances. Because how most MCP2515 are dedicated to a specific function (e.g., motor control, BMS, datalogger), instead of the scheduler holding a queue of messages to send and a queue of read messages, it holds function pointers to tasks that handle sending and receiving messages for each MCP2515. The object wishing to send/receive messages holds their own MCP2515 object (reference).
The Scheduler runs on a fixed period. When Scheduler.update() is called, it checks if a period has already passed. If not, it checks if it is almost passed. If the time to the next cycle is less than SPIN_US, it spin-waits until the period is reached. Otherwise, it returns immediately, allowing other non-scheduler tasks to run. Once the time to fire is reached, it runs the tasks via the pointers in a round-robin fashion, going from one MCP2515 to another.
With a modified mcp2515.h that doesn't directly use SPI.h, we can skip waiting for the SPI transaction to complete, and instead work on compiling the next message while the previous SPI transaction is still ongoing. Although it would be easier to only have a single queue (instead NUM_MCP2515 queues), we give room for the CAN-bus to be busy on one MCP2515, while another MCP2515 can still send/receive messages, i.e. we distribute the load across multiple CAN buses more evenly, instead of having one bus burst at one time
In the rare case where the system is busy and misses more than one period, the scheduler will skip to the next period, preventing bursts.
| NUM_TASKS | Number of tasks per MCP2515, choose highest of all, but keep as low as possible |
| NUM_MCP2515 | Number of MCP2515 instances |
Definition at line 44 of file Scheduler.hpp.
|
private |
Definition at line 46 of file Scheduler.hpp.
|
delete |
all arguments must be provided
| Scheduler< NUM_TASKS, NUM_MCP2515 >::Scheduler | ( | uint32_t | period_us_, |
| uint32_t | spin_threshold_us_, | ||
| unsigned long(*)() | current_time_us | ||
| ) |
Construct a new Scheduler object.
| NUM_TASKS | Number of tasks per MCP2515 |
| NUM_MCP2515 | Number of MCP2515 instances |
| [in] | period_us_ | Period of the scheduler in microseconds |
| [in] | spin_threshold_us_ | Spin-wait threshold in microseconds |
Definition at line 22 of file Scheduler.tpp.
| bool Scheduler< NUM_TASKS, NUM_MCP2515 >::addTask | ( | const McpIndex | mcp_index, |
| const TaskFn | task, | ||
| const uint8_t | tick_interval | ||
| ) |
Add a task to the scheduler for a specific MCP2515 index.
| NUM_TASKS | Number of tasks per MCP2515 |
| NUM_MCP2515 | Number of MCP2515 instances |
| [in] | mcp_index | Index of the MCP2515 instance |
| [in] | task | Function pointer to the task to be added |
| [in] | tick_interval | Number of ticks between task executions, so 1 for every tick, 10 for every 10 ticks; 0 makes the given task disabled from repeating. |
Definition at line 105 of file Scheduler.tpp.
|
inlineconstexpr |
Returns the number of cycles needed for a given interval in microseconds.
| [in] | interval_us | The interval in microseconds. |
Definition at line 69 of file Scheduler.hpp.
References Scheduler< NUM_TASKS, NUM_MCP2515 >::PERIOD_US.
|
inlineconstexpr |
Returns the period of the scheduler in microseconds.
Definition at line 62 of file Scheduler.hpp.
References Scheduler< NUM_TASKS, NUM_MCP2515 >::PERIOD_US.
| bool Scheduler< NUM_TASKS, NUM_MCP2515 >::removeTask | ( | const McpIndex | mcp_index, |
| const TaskFn | task | ||
| ) |
Remove a task from the scheduler for a specific MCP2515 instance.
| NUM_TASKS | Number of tasks per MCP2515 |
| NUM_MCP2515 | Number of MCP2515 instances |
| [in] | mcp_index | Index of the MCP2515 instance |
| [in] | task | Function pointer to the task to be removed |
Definition at line 131 of file Scheduler.tpp.
|
inlineprivate |
Helper function to run scheduled tasks.
| NUM_TASKS | Number of tasks per MCP2515 |
| NUM_MCP2515 | Number of MCP2515 instances |
Definition at line 168 of file Scheduler.tpp.
| void Scheduler< NUM_TASKS, NUM_MCP2515 >::synchronize | ( | unsigned long(*)() | current_time_us | ) |
Synchonize the scheduler to the current time, resetting all task counters, used when starting multiple Schedulers across different boards together.
| NUM_TASKS | Number of tasks per MCP2515 |
| NUM_MCP2515 | Number of MCP2515 instances |
| [in] | current_time_us | Function pointer to a function returning the current time in microseconds |
Definition at line 79 of file Scheduler.tpp.
| void Scheduler< NUM_TASKS, NUM_MCP2515 >::update |
Update the scheduler, checking if tasks need to be run based on the current time.
| NUM_TASKS | Number of tasks per MCP2515 |
| NUM_MCP2515 | Number of MCP2515 instances |
| [in] | current_time_us | Function pointer to a function returning the current time in microseconds |
Definition at line 44 of file Scheduler.tpp.
|
private |
Function pointer to a function returning the current time in microseconds.
Definition at line 79 of file Scheduler.hpp.
|
private |
Last time scheduler fired, overridden if missed more than one period.
Definition at line 78 of file Scheduler.hpp.
|
private |
Period (tick length).
Definition at line 76 of file Scheduler.hpp.
Referenced by Scheduler< NUM_TASKS, NUM_MCP2515 >::cyclesNeeded(), and Scheduler< NUM_TASKS, NUM_MCP2515 >::getPeriodUs().
|
private |
Threshold to switch from letting non-scheduler task in loop() run, to spin-locking (to ensure on time firing).
Definition at line 77 of file Scheduler.hpp.
|
private |
Array of number of tasks per MCP2515.
Definition at line 75 of file Scheduler.hpp.
|
private |
Counter to hold firing for n ticks, "how many ticks left before firing?".
Definition at line 74 of file Scheduler.hpp.
|
private |
Period (in ticks) of each function, 1 is fire every tick, 0 is disabled.
Definition at line 73 of file Scheduler.hpp.
|
private |
Array of tasks, sorted by each MCP2515.
Definition at line 72 of file Scheduler.hpp.