ETB PeriodicController refactoring
This commit is contained in:
parent
dc0ae33a54
commit
de6d9d6f8d
|
@ -15,7 +15,7 @@
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* ETB is controlled according to pedal position input (pedal position sensor is a potentiometer)
|
* ETB is controlled according to pedal position input (pedal position sensor is a potentiometer)
|
||||||
* petal 0% means pedal not pressed / idle
|
* pedal 0% means pedal not pressed / idle
|
||||||
* pedal 100% means pedal all the way down
|
* pedal 100% means pedal all the way down
|
||||||
* (not TPS - not the one you can calibrate in TunerStudio)
|
* (not TPS - not the one you can calibrate in TunerStudio)
|
||||||
*
|
*
|
||||||
|
@ -62,6 +62,7 @@
|
||||||
#include "pwm_generator_logic.h"
|
#include "pwm_generator_logic.h"
|
||||||
#include "pid.h"
|
#include "pid.h"
|
||||||
#include "engine_controller.h"
|
#include "engine_controller.h"
|
||||||
|
#include "PeriodicController.h"
|
||||||
|
|
||||||
#if EFI_ELECTRONIC_THROTTLE_BODY || defined(__DOXYGEN__)
|
#if EFI_ELECTRONIC_THROTTLE_BODY || defined(__DOXYGEN__)
|
||||||
#include "pin_repository.h"
|
#include "pin_repository.h"
|
||||||
|
@ -77,10 +78,7 @@ static Pid tuneWorkingPid(&tuneWorkingPidSettings);
|
||||||
static PID_AutoTune autoTune;
|
static PID_AutoTune autoTune;
|
||||||
|
|
||||||
static LoggingWithStorage logger("ETB");
|
static LoggingWithStorage logger("ETB");
|
||||||
/**
|
|
||||||
* @brief Control Thread stack
|
|
||||||
*/
|
|
||||||
static THD_WORKING_AREA(etbTreadStack, UTILITY_THREAD_STACK_SIZE);
|
|
||||||
/**
|
/**
|
||||||
* @brief Pulse-Width Modulation state
|
* @brief Pulse-Width Modulation state
|
||||||
*/
|
*/
|
||||||
|
@ -103,9 +101,12 @@ static bool wasEtbBraking = false;
|
||||||
// todo: need to fix PWM so that it supports zero duty cycle
|
// todo: need to fix PWM so that it supports zero duty cycle
|
||||||
#define PERCENT_TO_DUTY(X) (maxF(minI(X, 99.9), 0.1) / 100.0)
|
#define PERCENT_TO_DUTY(X) (maxF(minI(X, 99.9), 0.1) / 100.0)
|
||||||
|
|
||||||
static msg_t etbThread(void *arg) {
|
class EtbController : public PeriodicController<UTILITY_THREAD_STACK_SIZE> {
|
||||||
UNUSED(arg);
|
public:
|
||||||
while (true) {
|
EtbController() : PeriodicController("ETB") { }
|
||||||
|
private:
|
||||||
|
void PeriodicTask(efitime_t nowNt) override {
|
||||||
|
|
||||||
// set debug_mode 17
|
// set debug_mode 17
|
||||||
if (engineConfiguration->debugMode == DBG_ELECTRONIC_THROTTLE_PID) {
|
if (engineConfiguration->debugMode == DBG_ELECTRONIC_THROTTLE_PID) {
|
||||||
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
|
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
|
||||||
|
@ -125,8 +126,7 @@ static msg_t etbThread(void *arg) {
|
||||||
|
|
||||||
if (!cisnan(valueOverride)) {
|
if (!cisnan(valueOverride)) {
|
||||||
etbPwmUp.setSimplePwmDutyCycle(valueOverride);
|
etbPwmUp.setSimplePwmDutyCycle(valueOverride);
|
||||||
pid.sleep();
|
return;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
percent_t actualThrottlePosition = getTPS();
|
percent_t actualThrottlePosition = getTPS();
|
||||||
|
@ -148,8 +148,7 @@ static msg_t etbThread(void *arg) {
|
||||||
scheduleMsg(&logger, "GREAT NEWS! %f/%f/%f", autoTune.GetKp(), autoTune.GetKi(), autoTune.GetKd());
|
scheduleMsg(&logger, "GREAT NEWS! %f/%f/%f", autoTune.GetKp(), autoTune.GetKi(), autoTune.GetKd());
|
||||||
}
|
}
|
||||||
|
|
||||||
tuneWorkingPid.sleep();
|
return;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -172,15 +171,10 @@ static msg_t etbThread(void *arg) {
|
||||||
if (engineConfiguration->isVerboseETB) {
|
if (engineConfiguration->isVerboseETB) {
|
||||||
pid.showPidStatus(&logger, "ETB");
|
pid.showPidStatus(&logger, "ETB");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// this thread is activated 10 times per second
|
|
||||||
pid.sleep();
|
|
||||||
}
|
|
||||||
#if defined __GNUC__
|
|
||||||
return -1;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static EtbController etbController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set_etb X
|
* set_etb X
|
||||||
|
@ -374,7 +368,9 @@ void initElectronicThrottle(void) {
|
||||||
|
|
||||||
pid.reset();
|
pid.reset();
|
||||||
|
|
||||||
chThdCreateStatic(etbTreadStack, sizeof(etbTreadStack), NORMALPRIO, (tfunc_t)(void*) etbThread, NULL);
|
int periodMs = maxI(10, engineConfiguration->etb.period);
|
||||||
|
etbController.setPeriod(periodMs);
|
||||||
|
etbController.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* EFI_ELECTRONIC_THROTTLE_BODY */
|
#endif /* EFI_ELECTRONIC_THROTTLE_BODY */
|
||||||
|
|
|
@ -35,13 +35,14 @@ template <int TStackSize>
|
||||||
class PeriodicController : public ThreadController<TStackSize>
|
class PeriodicController : public ThreadController<TStackSize>
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const systime_t m_period;
|
// time in ChibiOS time units, see CH_CFG_ST_FREQUENCY
|
||||||
|
systime_t m_period;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/**
|
/**
|
||||||
* The target period between calls to PeriodicTask.
|
* The target period between calls to PeriodicTask.
|
||||||
*/
|
*/
|
||||||
const float m_periodSeconds;
|
// const float m_periodSeconds;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Called before running the periodic task. Optionally override this method to set up.
|
* @brief Called before running the periodic task. Optionally override this method to set up.
|
||||||
|
@ -82,7 +83,18 @@ public:
|
||||||
, m_period(CH_CFG_ST_FREQUENCY / frequencyHz)
|
, m_period(CH_CFG_ST_FREQUENCY / frequencyHz)
|
||||||
// Then compute the float period off of the integer one to
|
// Then compute the float period off of the integer one to
|
||||||
// get the ACTUAL period, which may be slightly different than requested.
|
// get the ACTUAL period, which may be slightly different than requested.
|
||||||
, m_periodSeconds(m_period / (float)CH_CFG_ST_FREQUENCY)
|
// , m_periodSeconds(m_period / (float)CH_CFG_ST_FREQUENCY)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -141,7 +141,7 @@ private:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
MicrosecondTimerWatchdogController watchdogControllerInstance;
|
static MicrosecondTimerWatchdogController watchdogControllerInstance;
|
||||||
|
|
||||||
static constexpr GPTConfig gpt5cfg = { 1000000, /* 1 MHz timer clock.*/
|
static constexpr GPTConfig gpt5cfg = { 1000000, /* 1 MHz timer clock.*/
|
||||||
hwTimerCallback, /* Timer callback.*/
|
hwTimerCallback, /* Timer callback.*/
|
||||||
|
|
Loading…
Reference in New Issue