Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed Nov 27, 2024
1 parent 0954a8c commit fa7e4a6
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions firmware/controllers/system/periodic_thread_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,32 @@ class PeriodicController : public ThreadController<TStackSize>
{
private:
// time in ChibiOS time units, see CH_CFG_ST_FREQUENCY
systime_t m_period;
systime_t m_period;
protected:
/**
* The target period between calls to PeriodicTask.
*/
/**
* The target period between calls to PeriodicTask.
*/
// const float m_periodSeconds;

/**
* @brief Called before running the periodic task. Optionally override this method to set up.
*/
virtual void OnStarted() {};
/**
* @brief Called before running the periodic task. Optionally override this method to set up.
*/
virtual void OnStarted() {};

/**
* @brief Called periodically. Override this method to do work for your controller.
*/
virtual void PeriodicTask(efitick_t nowNt) = 0;
/**
* @brief Called periodically. Override this method to do work for your controller.
*/
virtual void PeriodicTask(efitick_t nowNt) = 0;

private:
void ThreadTask() override final
{
OnStarted();
void ThreadTask() override final
{
OnStarted();

while(!chThdShouldTerminateX()) {
systime_t before = chVTGetSystemTime();
efitick_t nowNt = getTimeNowNt();
while(!chThdShouldTerminateX()) {
systime_t before = chVTGetSystemTime();
efitick_t nowNt = getTimeNowNt();

{
ScopePerf perf(PE::PeriodicControllerPeriodicTask);
Expand All @@ -71,38 +71,38 @@ class PeriodicController : public ThreadController<TStackSize>
PeriodicTask(nowNt);
}

// This ensures the loop _actually_ runs at the desired frequency.
// Suppose we want a loop speed of 500hz:
// If the work takes 1ms, and we wait 2ms (1 / 500hz), we actually
// get a loop at 333 hz. We need to wait until 2ms after we START
// doing work, so the loop runs at a predictable 500hz.
chThdSleepUntilWindowed(before, before + m_period);
}
// This ensures the loop _actually_ runs at the desired frequency.
// Suppose we want a loop speed of 500hz:
// If the work takes 1ms, and we wait 2ms (1 / 500hz), we actually
// get a loop at 333 hz. We need to wait until 2ms after we START
// doing work, so the loop runs at a predictable 500hz.
chThdSleepUntilWindowed(before, before + m_period);
}

firmwareError(ObdCode::OBD_PCM_Processor_Fault, "Thread died: %s", this->m_name);
}

public:
PeriodicController(const char* name, tprio_t priority, float frequencyHz)
: ThreadController<TStackSize>(name, priority)
// First compute the period in systime_t
, m_period(CH_CFG_ST_FREQUENCY / frequencyHz)
// Then compute the float period off of the integer one to
// get the ACTUAL period, which may be slightly different than requested.
PeriodicController(const char* name, tprio_t priority, float frequencyHz)
: ThreadController<TStackSize>(name, priority)
// First compute the period in systime_t
, m_period(CH_CFG_ST_FREQUENCY / frequencyHz)
// Then compute the float period off of the integer one to
// get the ACTUAL period, which may be slightly different than requested.
// , m_periodSeconds(m_period / (float)CH_CFG_ST_FREQUENCY)
{
}
{
}

PeriodicController(const char* name) : PeriodicController (name, NORMALPRIO, 1) {
}
PeriodicController(const char* name) : PeriodicController (name, NORMALPRIO, 1) {
}

/**
* sets milliseconds period
*/
void setPeriod(int periodMs) {
float frequencyHz = 1000.0 / periodMs;
this->m_period = CH_CFG_ST_FREQUENCY / frequencyHz;
}
/**
* sets milliseconds period
*/
void setPeriod(int periodMs) {
float frequencyHz = 1000.0 / periodMs;
this->m_period = CH_CFG_ST_FREQUENCY / frequencyHz;
}
};

// let's make sure period is not below specified threshold
Expand Down

0 comments on commit fa7e4a6

Please sign in to comment.