wider usage of PeriodicController

This commit is contained in:
rusefi 2019-02-10 23:54:41 -05:00
parent d8930651e6
commit 23a8f5b526
7 changed files with 70 additions and 66 deletions

View File

@ -35,7 +35,12 @@
#define _CHIBIOS_RT_CONF_VER_5_1_ #define _CHIBIOS_RT_CONF_VER_5_1_
#define PORT_IDLE_THREAD_STACK_SIZE 1024 #define PORT_IDLE_THREAD_STACK_SIZE 1024
// rusEfi main processing happens on IRQ so PORT_INT_REQUIRED_STACK has to be pretty large.
// see also a strange comment about PORT_INT_REQUIRED_STACK in global_shared.h
// see also http://www.chibios.org/dokuwiki/doku.php?id=chibios:kb:stacks
#define PORT_INT_REQUIRED_STACK 768 #define PORT_INT_REQUIRED_STACK 768
#define CHPRINTF_USE_FLOAT TRUE #define CHPRINTF_USE_FLOAT TRUE
#if !defined(EFI_CLOCK_LOCKS) || defined(__DOXYGEN__) #if !defined(EFI_CLOCK_LOCKS) || defined(__DOXYGEN__)
@ -613,6 +618,7 @@ extern "C"
* *
* @note The default is @p FALSE. * @note The default is @p FALSE.
*/ */
// see also CH_DBG_STACK_FILL_VALUE
#if !defined(CH_DBG_FILL_THREADS) #if !defined(CH_DBG_FILL_THREADS)
#define CH_DBG_FILL_THREADS TRUE #define CH_DBG_FILL_THREADS TRUE
#endif #endif

View File

@ -17,6 +17,7 @@
#include "fsio_impl.h" #include "fsio_impl.h"
#include "engine_math.h" #include "engine_math.h"
#include "pin_repository.h" #include "pin_repository.h"
#include "PeriodicController.h"
EXTERN_ENGINE EXTERN_ENGINE
; ;
@ -30,8 +31,6 @@ extern fsio8_Map3D_f32t fsioTable1;
extern TunerStudioOutputChannels tsOutputChannels; extern TunerStudioOutputChannels tsOutputChannels;
#endif /* EFI_TUNER_STUDIO */ #endif /* EFI_TUNER_STUDIO */
static THD_WORKING_AREA(auxPidThreadStack, UTILITY_THREAD_STACK_SIZE);
static LocalVersionHolder parametersVersion; static LocalVersionHolder parametersVersion;
static SimplePwm auxPidPwm[AUX_PID_COUNT]; static SimplePwm auxPidPwm[AUX_PID_COUNT];
static OutputPin auxPidPin[AUX_PID_COUNT]; static OutputPin auxPidPin[AUX_PID_COUNT];
@ -58,11 +57,12 @@ static void pidReset(void) {
auxPid.reset(); auxPid.reset();
} }
static msg_t auxPidThread(int param) { class AuxPidController : public PeriodicController<UTILITY_THREAD_STACK_SIZE> {
UNUSED(param); public:
chRegSetThreadName("AuxPidController"); AuxPidController() : PeriodicController("AuxPidController") { }
while (true) { private:
auxPid.sleep(); void PeriodicTask(efitime_t nowNt) override {
setPeriod(NOT_TOO_OFTEN(10 /* ms */, engineConfiguration->auxPid[0].periodMs));
if (parametersVersion.isOld(engine->getGlobalConfigurationVersion())) { if (parametersVersion.isOld(engine->getGlobalConfigurationVersion())) {
pidReset(); pidReset();
@ -76,7 +76,7 @@ static msg_t auxPidThread(int param) {
if (!enabledAtCurrentRpm) { if (!enabledAtCurrentRpm) {
// we need to avoid accumulating iTerm while engine is not running // we need to avoid accumulating iTerm while engine is not running
pidReset(); pidReset();
continue; return;
} }
@ -101,10 +101,9 @@ static msg_t auxPidThread(int param) {
} }
#if defined __GNUC__ };
return -1;
#endif static AuxPidController instance;
}
static void turnAuxPidOn(int index) { static void turnAuxPidOn(int index) {
if (!isEnabled(index)) { if (!isEnabled(index)) {
@ -135,12 +134,10 @@ void stopAuxPins(void) {
} }
void initAuxPid(Logging *sharedLogger) { void initAuxPid(Logging *sharedLogger) {
chThdCreateStatic(auxPidThreadStack, sizeof(auxPidThreadStack), LOWPRIO,
(tfunc_t)(void*) auxPidThread, NULL);
logger = sharedLogger; logger = sharedLogger;
startAuxPins(); startAuxPins();
instance.Start();
} }
#endif #endif

View File

@ -13,6 +13,7 @@
#include "voltage.h" #include "voltage.h"
#include "pid.h" #include "pid.h"
#include "LocalVersionHolder.h" #include "LocalVersionHolder.h"
#include "PeriodicController.h"
#if EFI_ALTERNATOR_CONTROL || defined(__DOXYGEN__) #if EFI_ALTERNATOR_CONTROL || defined(__DOXYGEN__)
#include "pwm_generator.h" #include "pwm_generator.h"
@ -28,8 +29,6 @@ static SimplePwm alternatorControl("alt");
static pid_s *altPidS = &persistentState.persistentConfiguration.engineConfiguration.alternatorControl; static pid_s *altPidS = &persistentState.persistentConfiguration.engineConfiguration.alternatorControl;
static Pid altPid(altPidS); static Pid altPid(altPidS);
static THD_WORKING_AREA(alternatorControlThreadStack, UTILITY_THREAD_STACK_SIZE);
static percent_t currentAltDuty; static percent_t currentAltDuty;
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__) #if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
@ -43,10 +42,13 @@ static void pidReset(void) {
altPid.reset(); altPid.reset();
} }
static msg_t AltCtrlThread(int param) { class AlternatorController : public PeriodicController<UTILITY_THREAD_STACK_SIZE> {
UNUSED(param); public:
chRegSetThreadName("AlternatorController"); AlternatorController() : PeriodicController("AlternatorController") { }
while (true) { private:
void PeriodicTask(efitime_t nowNt) override {
setPeriod(NOT_TOO_OFTEN(10 /* ms */, engineConfiguration->alternatorControl.periodMs));
#if ! EFI_UNIT_TEST || defined(__DOXYGEN__) #if ! EFI_UNIT_TEST || defined(__DOXYGEN__)
if (shouldResetPid) { if (shouldResetPid) {
pidReset(); pidReset();
@ -54,8 +56,6 @@ static msg_t AltCtrlThread(int param) {
} }
#endif #endif
altPid.sleep();
if (engineConfiguration->debugMode == DBG_ALTERNATOR_PID) { if (engineConfiguration->debugMode == DBG_ALTERNATOR_PID) {
// this block could be executed even in on/off alternator control mode // this block could be executed even in on/off alternator control mode
// but at least we would reflect latest state // but at least we would reflect latest state
@ -71,7 +71,7 @@ static msg_t AltCtrlThread(int param) {
if (!engine->isAlternatorControlEnabled) { if (!engine->isAlternatorControlEnabled) {
// we need to avoid accumulating iTerm while engine is not running // we need to avoid accumulating iTerm while engine is not running
pidReset(); pidReset();
continue; return;
} }
float vBatt = getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE); float vBatt = getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE);
@ -88,7 +88,7 @@ static msg_t AltCtrlThread(int param) {
#endif /* EFI_TUNER_STUDIO */ #endif /* EFI_TUNER_STUDIO */
} }
continue; return;
} }
@ -101,11 +101,9 @@ static msg_t AltCtrlThread(int param) {
alternatorControl.setSimplePwmDutyCycle(currentAltDuty / 100); alternatorControl.setSimplePwmDutyCycle(currentAltDuty / 100);
} }
#if defined __GNUC__ };
return -1;
#endif
}
static AlternatorController instance;
void showAltInfo(void) { void showAltInfo(void) {
scheduleMsg(logger, "alt=%s @%s t=%dms", boolToString(engineConfiguration->isAlternatorControlEnabled), scheduleMsg(logger, "alt=%s @%s t=%dms", boolToString(engineConfiguration->isAlternatorControlEnabled),
@ -169,8 +167,7 @@ void initAlternatorCtrl(Logging *sharedLogger) {
&enginePins.alternatorPin, &enginePins.alternatorPin,
engineConfiguration->alternatorPwmFrequency, 0.1, applyAlternatorPinState); engineConfiguration->alternatorPwmFrequency, 0.1, applyAlternatorPinState);
} }
chThdCreateStatic(alternatorControlThreadStack, sizeof(alternatorControlThreadStack), LOWPRIO, instance.Start();
(tfunc_t)(void*) AltCtrlThread, NULL);
} }
#endif /* EFI_ALTERNATOR_CONTROL */ #endif /* EFI_ALTERNATOR_CONTROL */

View File

@ -106,6 +106,8 @@ public:
EtbController() : PeriodicController("ETB") { } EtbController() : PeriodicController("ETB") { }
private: private:
void PeriodicTask(efitime_t nowNt) override { void PeriodicTask(efitime_t nowNt) override {
setPeriod(NOT_TOO_OFTEN(10 /* ms */, engineConfiguration->etb.periodMs));
// set debug_mode 17 // set debug_mode 17
if (engineConfiguration->debugMode == DBG_ELECTRONIC_THROTTLE_PID) { if (engineConfiguration->debugMode == DBG_ELECTRONIC_THROTTLE_PID) {
@ -368,8 +370,6 @@ void initElectronicThrottle(void) {
pid.reset(); pid.reset();
int periodMs = maxI(10, engineConfiguration->etb.periodMs);
etbController.setPeriod(periodMs);
etbController.Start(); etbController.Start();
} }

View File

@ -34,6 +34,7 @@
#include "efiGpio.h" #include "efiGpio.h"
#include "settings.h" #include "settings.h"
#include "idle_thread.h" #include "idle_thread.h"
#include "PeriodicController.h"
EXTERN_ENGINE EXTERN_ENGINE
; ;
@ -214,25 +215,22 @@ void dizzyBench(void) {
pinbench("300", "5", "400", "3", &enginePins.dizzyOutput, engineConfiguration->dizzySparkOutputPin); pinbench("300", "5", "400", "3", &enginePins.dizzyOutput, engineConfiguration->dizzySparkOutputPin);
} }
class BenchController : public PeriodicController<UTILITY_THREAD_STACK_SIZE> {
public:
BenchController() : PeriodicController("BenchThread") { }
private:
void PeriodicTask(efitime_t nowNt) override {
setPeriod(NOT_TOO_OFTEN(10 /* ms */, engineConfiguration->auxPid[0].periodMs));
static THD_WORKING_AREA(benchThreadStack, UTILITY_THREAD_STACK_SIZE);
static msg_t benchThread(int param) {
(void) param;
chRegSetThreadName("BenchThread");
while (true) {
// naive inter-thread communication - waiting for a flag // naive inter-thread communication - waiting for a flag
while (!isBenchTestPending) { if (isBenchTestPending) {
chThdSleepMilliseconds(200);
}
isBenchTestPending = false; isBenchTestPending = false;
runBench(brainPin, pinX, delayMs, onTime, offTime, count); runBench(brainPin, pinX, delayMs, onTime, offTime, count);
} }
#if defined __GNUC__
return 0;
#endif
} }
};
static BenchController instance;
void OutputPin::unregisterOutput(brain_pin_e oldPin, brain_pin_e newPin) { void OutputPin::unregisterOutput(brain_pin_e oldPin, brain_pin_e newPin) {
if (oldPin != GPIO_UNASSIGNED && oldPin != newPin) { if (oldPin != GPIO_UNASSIGNED && oldPin != newPin) {
@ -276,7 +274,6 @@ void runBenchTest(uint16_t subsystem, uint16_t index) {
void initInjectorCentral(Logging *sharedLogger) { void initInjectorCentral(Logging *sharedLogger) {
logger = sharedLogger; logger = sharedLogger;
chThdCreateStatic(benchThreadStack, sizeof(benchThreadStack), NORMALPRIO, (tfunc_t)(void*) benchThread, NULL);
for (int i = 0; i < INJECTION_PIN_COUNT; i++) { for (int i = 0; i < INJECTION_PIN_COUNT; i++) {
is_injector_enabled[i] = true; is_injector_enabled[i] = true;
@ -301,6 +298,9 @@ void initInjectorCentral(Logging *sharedLogger) {
addConsoleActionSSSSS("fuelbench2", fuelbench2); addConsoleActionSSSSS("fuelbench2", fuelbench2);
addConsoleActionSSSSS("sparkbench2", sparkbench2); addConsoleActionSSSSS("sparkbench2", sparkbench2);
instance.setPeriod(200 /*ms*/);
instance.Start();
} }
#endif #endif

View File

@ -30,9 +30,12 @@
#include "malfunction_central.h" #include "malfunction_central.h"
#include "malfunction_indicator.h" #include "malfunction_indicator.h"
#include "efiGpio.h" #include "efiGpio.h"
#include "PeriodicController.h"
#if EFI_MALFUNCTION_INDICATOR || defined(__DOXYGEN__) #if EFI_MALFUNCTION_INDICATOR || defined(__DOXYGEN__)
#define TEST_MIL_CODE FALSE
EXTERN_ENGINE; EXTERN_ENGINE;
#define MFI_LONG_BLINK 1500 #define MFI_LONG_BLINK 1500
@ -40,8 +43,6 @@ EXTERN_ENGINE;
#define MFI_BLINK_SEPARATOR 400 #define MFI_BLINK_SEPARATOR 400
#define MFI_CHECKENGINE_LIGHT 10000 #define MFI_CHECKENGINE_LIGHT 10000
static THD_WORKING_AREA(mfiThreadStack, UTILITY_THREAD_STACK_SIZE); // declare thread
static void blink_digits(int digit, int duration) { static void blink_digits(int digit, int duration) {
for (int iter = 0; iter < digit; iter++) { for (int iter = 0; iter < digit; iter++) {
enginePins.checkEnginePin.setValue(0); enginePins.checkEnginePin.setValue(0);
@ -78,18 +79,12 @@ static void DisplayErrorCode(int length, int code) {
} }
} }
// our main thread for show check engine error class MILController : public PeriodicController<UTILITY_THREAD_STACK_SIZE> {
#if defined __GNUC__ public:
__attribute__((noreturn)) static msg_t mfiThread(void) MILController() : PeriodicController("MFIndicator") { }
#else private:
static msg_t mfiThread(void) void PeriodicTask(efitime_t nowNt) override {
#endif static error_codes_set_s localErrorCopy;
{
chRegSetThreadName("MFIndicator");
error_codes_set_s localErrorCopy;
while (true) {
chThdSleepSeconds(10);
getErrorCodes(&localErrorCopy); getErrorCodes(&localErrorCopy);
for (int p = 0; p < localErrorCopy.count; p++) { for (int p = 0; p < localErrorCopy.count; p++) {
@ -98,12 +93,16 @@ __attribute__((noreturn)) static msg_t mfiThread(void)
DisplayErrorCode(DigitLength(code), code); DisplayErrorCode(DigitLength(code), code);
} }
} }
} };
static MILController instance;
#if TEST_MIL_CODE
static void testMil(void) { static void testMil(void) {
addError(OBD_Engine_Coolant_Temperature_Circuit_Malfunction); addError(OBD_Engine_Coolant_Temperature_Circuit_Malfunction);
addError(OBD_Intake_Air_Temperature_Circuit_Malfunction); addError(OBD_Intake_Air_Temperature_Circuit_Malfunction);
} }
#endif /* TEST_MIL_CODE */
bool isMilEnabled() { bool isMilEnabled() {
return CONFIGB(malfunctionIndicatorPin) != GPIO_UNASSIGNED; return CONFIGB(malfunctionIndicatorPin) != GPIO_UNASSIGNED;
@ -113,10 +112,12 @@ void initMalfunctionIndicator(void) {
if (!isMilEnabled()) { if (!isMilEnabled()) {
return; return;
} }
// create static thread instance.setPeriod(10);
chThdCreateStatic(mfiThreadStack, sizeof(mfiThreadStack), LOWPRIO, (tfunc_t)(void*) mfiThread, NULL); instance.Start();
#if TEST_MIL_CODE
addConsoleAction("testmil", testMil); addConsoleAction("testmil", testMil);
#endif /* TEST_MIL_CODE */
} }
#endif /* EFI_MALFUNCTION_INDICATOR */ #endif /* EFI_MALFUNCTION_INDICATOR */

View File

@ -98,3 +98,6 @@ public:
this->m_period = CH_CFG_ST_FREQUENCY / frequencyHz; this->m_period = CH_CFG_ST_FREQUENCY / frequencyHz;
} }
}; };
// let's make sure period is not below specified threshold
#define NOT_TOO_OFTEN(threshold, value) maxI(threshold, value)