Red Bird Racing VCU v2
 
Loading...
Searching...
No Matches
Scheduler< NUM_TASKS, NUM_MCP2515 > Class Template Reference

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>

Collaboration diagram for Scheduler< NUM_TASKS, NUM_MCP2515 >:

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 )()
 

Detailed Description

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
class Scheduler< NUM_TASKS, NUM_MCP2515 >

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.

Template Parameters
NUM_TASKSNumber of tasks per MCP2515, choose highest of all, but keep as low as possible
NUM_MCP2515Number of MCP2515 instances

Definition at line 44 of file Scheduler.hpp.

Member Typedef Documentation

◆ TaskFn

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
using Scheduler< NUM_TASKS, NUM_MCP2515 >::TaskFn = void (*)()
private

Definition at line 46 of file Scheduler.hpp.

Constructor & Destructor Documentation

◆ Scheduler() [1/2]

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
Scheduler< NUM_TASKS, NUM_MCP2515 >::Scheduler ( )
delete

all arguments must be provided

◆ Scheduler() [2/2]

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
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.

Template Parameters
NUM_TASKSNumber of tasks per MCP2515
NUM_MCP2515Number of MCP2515 instances
Parameters
[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.

Member Function Documentation

◆ addTask()

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
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.

Template Parameters
NUM_TASKSNumber of tasks per MCP2515
NUM_MCP2515Number of MCP2515 instances
Parameters
[in]mcp_indexIndex of the MCP2515 instance
[in]taskFunction pointer to the task to be added
[in]tick_intervalNumber of ticks between task executions, so 1 for every tick, 10 for every 10 ticks; 0 makes the given task disabled from repeating.
Returns
true if the task was added successfully, false otherwise

Definition at line 105 of file Scheduler.tpp.

◆ cyclesNeeded()

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
constexpr uint32_t Scheduler< NUM_TASKS, NUM_MCP2515 >::cyclesNeeded ( const uint32_t  interval_us) const
inlineconstexpr

Returns the number of cycles needed for a given interval in microseconds.

Parameters
[in]interval_usThe interval in microseconds.
Returns
The number of cycles needed.

Definition at line 69 of file Scheduler.hpp.

References Scheduler< NUM_TASKS, NUM_MCP2515 >::PERIOD_US.

◆ getPeriodUs()

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
constexpr uint32_t Scheduler< NUM_TASKS, NUM_MCP2515 >::getPeriodUs ( ) const
inlineconstexpr

Returns the period of the scheduler in microseconds.

Returns
The period in microseconds.

Definition at line 62 of file Scheduler.hpp.

References Scheduler< NUM_TASKS, NUM_MCP2515 >::PERIOD_US.

◆ removeTask()

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
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.

Template Parameters
NUM_TASKSNumber of tasks per MCP2515
NUM_MCP2515Number of MCP2515 instances
Parameters
[in]mcp_indexIndex of the MCP2515 instance
[in]taskFunction pointer to the task to be removed
Returns
true if the task was removed successfully, false otherwise

Definition at line 131 of file Scheduler.tpp.

◆ runTasks()

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
void Scheduler< NUM_TASKS, NUM_MCP2515 >::runTasks
inlineprivate

Helper function to run scheduled tasks.

Template Parameters
NUM_TASKSNumber of tasks per MCP2515
NUM_MCP2515Number of MCP2515 instances

Definition at line 168 of file Scheduler.tpp.

◆ synchronize()

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
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.

Template Parameters
NUM_TASKSNumber of tasks per MCP2515
NUM_MCP2515Number of MCP2515 instances
Parameters
[in]current_time_usFunction pointer to a function returning the current time in microseconds

Definition at line 79 of file Scheduler.tpp.

◆ update()

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
void Scheduler< NUM_TASKS, NUM_MCP2515 >::update

Update the scheduler, checking if tasks need to be run based on the current time.

Template Parameters
NUM_TASKSNumber of tasks per MCP2515
NUM_MCP2515Number of MCP2515 instances
Parameters
[in]current_time_usFunction pointer to a function returning the current time in microseconds

Definition at line 44 of file Scheduler.tpp.

Member Data Documentation

◆ CURRENT_TIME_US

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
unsigned long(*const Scheduler< NUM_TASKS, NUM_MCP2515 >::CURRENT_TIME_US) ()
private

Function pointer to a function returning the current time in microseconds.

Definition at line 79 of file Scheduler.hpp.

◆ last_fire_us

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
uint32_t Scheduler< NUM_TASKS, NUM_MCP2515 >::last_fire_us
private

Last time scheduler fired, overridden if missed more than one period.

Definition at line 78 of file Scheduler.hpp.

◆ PERIOD_US

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
const uint32_t Scheduler< NUM_TASKS, NUM_MCP2515 >::PERIOD_US
private

◆ SPIN_US

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
const uint32_t Scheduler< NUM_TASKS, NUM_MCP2515 >::SPIN_US
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.

◆ task_cnt

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
uint8_t Scheduler< NUM_TASKS, NUM_MCP2515 >::task_cnt[NUM_MCP2515]
private

Array of number of tasks per MCP2515.

Definition at line 75 of file Scheduler.hpp.

◆ task_counters

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
uint8_t Scheduler< NUM_TASKS, NUM_MCP2515 >::task_counters[NUM_MCP2515][NUM_TASKS]
private

Counter to hold firing for n ticks, "how many ticks left before firing?".

Definition at line 74 of file Scheduler.hpp.

◆ task_ticks

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
uint8_t Scheduler< NUM_TASKS, NUM_MCP2515 >::task_ticks[NUM_MCP2515][NUM_TASKS]
private

Period (in ticks) of each function, 1 is fire every tick, 0 is disabled.

Definition at line 73 of file Scheduler.hpp.

◆ tasks

template<uint8_t NUM_TASKS, uint8_t NUM_MCP2515>
TaskFn Scheduler< NUM_TASKS, NUM_MCP2515 >::tasks[NUM_MCP2515][NUM_TASKS]
private

Array of tasks, sorted by each MCP2515.

Definition at line 72 of file Scheduler.hpp.


The documentation for this class was generated from the following files: