rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes
PeriodicController< TStackSize > Class Template Referenceabstract

Base class for a controller that needs to run periodically to perform work. More...

#include <periodic_thread_controller.h>

Inheritance diagram for PeriodicController< TStackSize >:
Inheritance graph
[legend]
Collaboration diagram for PeriodicController< TStackSize >:
Collaboration graph
[legend]

Public Member Functions

 PeriodicController (const char *name, tprio_t priority, float frequencyHz)
 
 PeriodicController (const char *name)
 
void setPeriod (int periodMs)
 
- Public Member Functions inherited from ThreadController< TStackSize >
 ThreadController (const char *name, tprio_t priority)
 
void start ()
 Start the thread.
 
void stop ()
 Request thread termination and waits for termination.
 

Protected Member Functions

virtual void OnStarted ()
 Called before running the periodic task. Optionally override this method to set up.
 
virtual void PeriodicTask (efitick_t nowNt)=0
 Called periodically. Override this method to do work for your controller.
 
- Protected Member Functions inherited from ThreadController< TStackSize >
void main () override
 

Private Member Functions

void ThreadTask () override final
 

Private Attributes

systime_t m_period
 

Additional Inherited Members

- Protected Attributes inherited from ThreadController< TStackSize >
const char *const m_name
 

Detailed Description

template<int TStackSize>
class PeriodicController< TStackSize >

Base class for a controller that needs to run periodically to perform work.

For example, if we have some PID loop that needs to run at a specified frequency, inherit this class, and perform your period update in PeriodicTask. Any one-time setup work can be performed in OnStarted().

Each instance has one underlying thread meaning that task could be blocking/synchronous. This class effectively implements this functionality:

void thread() { OnStarted();

while(true) { PeriodicTask(getTimeNowNt()); sleep(); } }

Definition at line 36 of file periodic_thread_controller.h.

Constructor & Destructor Documentation

◆ PeriodicController() [1/2]

template<int TStackSize>
PeriodicController< TStackSize >::PeriodicController ( const char name,
tprio_t  priority,
float  frequencyHz 
)
inline

Definition at line 82 of file periodic_thread_controller.h.

83 : ThreadController<TStackSize>(name, priority)
84 // First compute the period in systime_t
85 , m_period(CH_CFG_ST_FREQUENCY / frequencyHz)
86 {
87 }
A base class for a controller that requires its own thread.

◆ PeriodicController() [2/2]

template<int TStackSize>
PeriodicController< TStackSize >::PeriodicController ( const char name)
inline

Definition at line 89 of file periodic_thread_controller.h.

89 : PeriodicController (name, NORMALPRIO, 1) {
90 }
Base class for a controller that needs to run periodically to perform work.

Member Function Documentation

◆ OnStarted()

template<int TStackSize>
virtual void PeriodicController< TStackSize >::OnStarted ( )
inlineprotectedvirtual

Called before running the periodic task. Optionally override this method to set up.

Definition at line 47 of file periodic_thread_controller.h.

47{};

Referenced by PeriodicController< TStackSize >::ThreadTask().

Here is the caller graph for this function:

◆ PeriodicTask()

template<int TStackSize>
virtual void PeriodicController< TStackSize >::PeriodicTask ( efitick_t  nowNt)
protectedpure virtual

Called periodically. Override this method to do work for your controller.

Implemented in CanWrite, and MainLoop.

Referenced by PeriodicController< TStackSize >::ThreadTask().

Here is the caller graph for this function:

◆ setPeriod()

template<int TStackSize>
void PeriodicController< TStackSize >::setPeriod ( int  periodMs)
inline

sets milliseconds period

Definition at line 95 of file periodic_thread_controller.h.

95 {
96 float frequencyHz = 1000.0 / periodMs;
97 this->m_period = CH_CFG_ST_FREQUENCY / frequencyHz;
98 }

◆ ThreadTask()

template<int TStackSize>
void PeriodicController< TStackSize >::ThreadTask ( )
inlinefinaloverrideprivatevirtual

Implements ThreadController< TStackSize >.

Definition at line 55 of file periodic_thread_controller.h.

56 {
57 OnStarted();
58
59 systime_t prev = chVTGetSystemTime();
60 while(!chThdShouldTerminateX()) {
61 efitick_t nowNt = getTimeNowNt();
62
63 {
65
66 // Run the controller's periodic work
67 PeriodicTask(nowNt);
68 }
69
70 // This ensures the loop _actually_ runs at the desired frequency.
71 // Suppose we want a loop speed of 500hz:
72 // If the work takes 1ms, and we wait 2ms (1 / 500hz), we actually
73 // get a loop at 333 hz. We need to wait until 2ms after we START
74 // doing work, so the loop runs at a predictable 500hz.
75 prev = chThdSleepUntilWindowed(prev, chTimeAddX(prev, m_period));
76 }
77
78 criticalError("Thread died: %s", this->m_name);
79 }
virtual void PeriodicTask(efitick_t nowNt)=0
Called periodically. Override this method to do work for your controller.
virtual void OnStarted()
Called before running the periodic task. Optionally override this method to set up.
const char *const m_name
efitick_t getTimeNowNt()
Definition efitime.cpp:19
@ PeriodicControllerPeriodicTask
Here is the call graph for this function:

Field Documentation

◆ m_period

template<int TStackSize>
systime_t PeriodicController< TStackSize >::m_period
private

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