inject interfaces to ETB, renaming (#1026)

* inject interfaces, renaming

* format

* null check

* that was a dumb typo

* fix indent
This commit is contained in:
Matthew Kennedy 2019-11-22 17:53:54 -08:00 committed by rusefi
parent c606bb27ac
commit 579219e8cc
2 changed files with 157 additions and 151 deletions

View File

@ -105,7 +105,7 @@ static bool startupPositionError = false;
#define STARTUP_NEUTRAL_POSITION_ERROR_THRESHOLD 5 #define STARTUP_NEUTRAL_POSITION_ERROR_THRESHOLD 5
class EtbControl { class EtbHardware {
private: private:
OutputPin m_pinEnable; OutputPin m_pinEnable;
OutputPin m_pinDir1; OutputPin m_pinDir1;
@ -118,9 +118,7 @@ private:
SimplePwm etbPwmUp; SimplePwm etbPwmUp;
public: public:
DECLARE_ENGINE_PTR; EtbHardware() : etbPwmUp("etbUp"), dcMotor(&m_pwmEnable, &m_pwmDir1, &m_pwmDir2) {}
EtbControl() : etbPwmUp("etbUp"), dcMotor(&m_pwmEnable, &m_pwmDir1, &m_pwmDir2) {}
TwoPinDcMotor dcMotor; TwoPinDcMotor dcMotor;
@ -135,7 +133,9 @@ public:
// since we have pointer magic here we cannot simply have value parameter // since we have pointer magic here we cannot simply have value parameter
pin_output_mode_e *pinEnableMode, pin_output_mode_e *pinEnableMode,
brain_pin_e pinDir1, brain_pin_e pinDir1,
brain_pin_e pinDir2) { brain_pin_e pinDir2,
ExecutorInterface* executor,
int frequency) {
dcMotor.setType(useTwoWires ? TwoPinDcMotor::ControlType::PwmDirectionPins : TwoPinDcMotor::ControlType::PwmEnablePin); dcMotor.setType(useTwoWires ? TwoPinDcMotor::ControlType::PwmDirectionPins : TwoPinDcMotor::ControlType::PwmEnablePin);
m_pinEnable.initPin("ETB Enable", pinEnable, pinEnableMode); m_pinEnable.initPin("ETB Enable", pinEnable, pinEnableMode);
@ -143,29 +143,29 @@ public:
m_pinDir2.initPin("ETB Dir 2", pinDir2); m_pinDir2.initPin("ETB Dir 2", pinDir2);
// Clamp to >100hz // Clamp to >100hz
int freq = maxI(100, engineConfiguration->etbFreq); int clampedFrequency = maxI(100, frequency);
// no need to complicate event queue with ETB PWM in unit tests // no need to complicate event queue with ETB PWM in unit tests
#if ! EFI_UNIT_TEST #if ! EFI_UNIT_TEST
startSimplePwm(&m_pwmEnable, "ETB Enable", startSimplePwm(&m_pwmEnable, "ETB Enable",
&engine->executor, executor,
&m_pinEnable, &m_pinEnable,
freq, clampedFrequency,
0, 0,
(pwm_gen_callback*)applyPinState); (pwm_gen_callback*)applyPinState);
startSimplePwm(&m_pwmDir1, "ETB Dir 1", startSimplePwm(&m_pwmDir1, "ETB Dir 1",
&engine->executor, executor,
&m_pinDir1, &m_pinDir1,
freq, clampedFrequency,
0, 0,
(pwm_gen_callback*)applyPinState); (pwm_gen_callback*)applyPinState);
startSimplePwm(&m_pwmDir2, "ETB Dir 2", startSimplePwm(&m_pwmDir2, "ETB Dir 2",
&engine->executor, executor,
&m_pinDir2, &m_pinDir2,
freq, clampedFrequency,
0, 0,
(pwm_gen_callback*)applyPinState); (pwm_gen_callback*)applyPinState);
#endif /* EFI_UNIT_TEST */ #endif /* EFI_UNIT_TEST */
@ -174,7 +174,7 @@ public:
#define ETB_COUNT 2 #define ETB_COUNT 2
static EtbControl etbControls[ETB_COUNT]; static EtbHardware etbHardware[ETB_COUNT];
extern percent_t mockPedalPosition; extern percent_t mockPedalPosition;
@ -187,8 +187,9 @@ static percent_t currentEtbDuty;
// this macro clamps both positive and negative percentages from about -100% to 100% // this macro clamps both positive and negative percentages from about -100% to 100%
#define ETB_PERCENT_TO_DUTY(X) (maxF(minF((X * 0.01), ETB_DUTY_LIMIT - 0.01), 0.01 - ETB_DUTY_LIMIT)) #define ETB_PERCENT_TO_DUTY(X) (maxF(minF((X * 0.01), ETB_DUTY_LIMIT - 0.01), 0.01 - ETB_DUTY_LIMIT))
EtbController::EtbController(EtbControl *etb) { EtbController::EtbController(DcMotor *motor)
this->etb = etb; : m_motor(motor)
{
} }
@ -210,8 +211,12 @@ static percent_t currentEtbDuty;
#endif /* EFI_TUNER_STUDIO */ #endif /* EFI_TUNER_STUDIO */
} }
if (!m_motor) {
return;
}
if (startupPositionError) { if (startupPositionError) {
etb->dcMotor.set(0); m_motor->set(0);
return; return;
} }
@ -221,12 +226,12 @@ static percent_t currentEtbDuty;
} }
if (!cisnan(directPwmValue)) { if (!cisnan(directPwmValue)) {
etb->dcMotor.set(directPwmValue); m_motor->set(directPwmValue);
return; return;
} }
if (boardConfiguration->pauseEtbControl) { if (boardConfiguration->pauseEtbControl) {
etb->dcMotor.set(0); m_motor->set(0);
return; return;
} }
@ -243,7 +248,7 @@ static percent_t currentEtbDuty;
autoTune.output, autoTune.output,
value); value);
scheduleMsg(&logger, "AT PID=%f", value); scheduleMsg(&logger, "AT PID=%f", value);
etb->dcMotor.set(ETB_PERCENT_TO_DUTY(value)); m_motor->set(ETB_PERCENT_TO_DUTY(value));
if (result) { if (result) {
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());
@ -275,7 +280,7 @@ static percent_t currentEtbDuty;
currentEtbDuty = engine->engineState.etbFeedForward + currentEtbDuty = engine->engineState.etbFeedForward +
etbPid.getOutput(targetPosition, actualThrottlePosition); etbPid.getOutput(targetPosition, actualThrottlePosition);
etb->dcMotor.set(ETB_PERCENT_TO_DUTY(currentEtbDuty)); m_motor->set(ETB_PERCENT_TO_DUTY(currentEtbDuty));
if (engineConfiguration->isVerboseETB) { if (engineConfiguration->isVerboseETB) {
etbPid.showPidStatus(&logger, "ETB"); etbPid.showPidStatus(&logger, "ETB");
@ -332,7 +337,7 @@ DISPLAY(DISPLAY_IF(hasEtbPedalPositionSensor))
#endif /* EFI_TUNER_STUDIO */ #endif /* EFI_TUNER_STUDIO */
} }
EtbController etbController(&etbControls[0]); EtbController etbController(&etbHardware[0].dcMotor);
/** /**
* set_etb_duty X * set_etb_duty X
@ -348,7 +353,7 @@ void setThrottleDutyCycle(percent_t level) {
float dc = ETB_PERCENT_TO_DUTY(level); float dc = ETB_PERCENT_TO_DUTY(level);
directPwmValue = dc; directPwmValue = dc;
for (int i = 0 ; i < ETB_COUNT; i++) { for (int i = 0 ; i < ETB_COUNT; i++) {
etbControls[i].dcMotor.set(dc); etbHardware[i].dcMotor.set(dc);
} }
scheduleMsg(&logger, "duty ETB duty=%f", dc); scheduleMsg(&logger, "duty ETB duty=%f", dc);
} }
@ -377,7 +382,7 @@ static void showEthInfo(void) {
scheduleMsg(&logger, "dir2=%s", hwPortname(CONFIGB(etb1.directionPin2))); scheduleMsg(&logger, "dir2=%s", hwPortname(CONFIGB(etb1.directionPin2)));
for (int i = 0 ; i < ETB_COUNT; i++) { for (int i = 0 ; i < ETB_COUNT; i++) {
EtbControl *etb = &etbControls[i]; EtbHardware *etb = &etbHardware[i];
scheduleMsg(&logger, "%d: dir=%d DC=%f", i, etb->dcMotor.isOpenDirection(), etb->dcMotor.get()); scheduleMsg(&logger, "%d: dir=%d DC=%f", i, etb->dcMotor.isOpenDirection(), etb->dcMotor.get());
} }
@ -392,7 +397,7 @@ static void setEtbFrequency(int frequency) {
engineConfiguration->etbFreq = frequency; engineConfiguration->etbFreq = frequency;
for (int i = 0 ; i < ETB_COUNT; i++) { for (int i = 0 ; i < ETB_COUNT; i++) {
etbControls[i].setFrequency(frequency); etbHardware[i].setFrequency(frequency);
} }
} }
@ -400,7 +405,7 @@ static void etbReset() {
scheduleMsg(&logger, "etbReset"); scheduleMsg(&logger, "etbReset");
for (int i = 0 ; i < ETB_COUNT; i++) { for (int i = 0 ; i < ETB_COUNT; i++) {
etbControls[i].dcMotor.set(0); etbHardware[i].dcMotor.set(0);
} }
etbPid.reset(); etbPid.reset();
@ -535,12 +540,14 @@ void onConfigurationChangeElectronicThrottleCallback(engine_configuration_s *pre
void startETBPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) { void startETBPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
// controlPinMode is a strange feature - it's simply because I am short on 5v I/O on Frankenso with Miata NB2 test mule // controlPinMode is a strange feature - it's simply because I am short on 5v I/O on Frankenso with Miata NB2 test mule
etbControls[0].start( etbHardware[0].start(
CONFIG(etb1_use_two_wires), CONFIG(etb1_use_two_wires),
CONFIGB(etb1.controlPin1), CONFIGB(etb1.controlPin1),
&CONFIGB(etb1.controlPinMode), &CONFIGB(etb1.controlPinMode),
CONFIGB(etb1.directionPin1), CONFIGB(etb1.directionPin1),
CONFIGB(etb1.directionPin2) CONFIGB(etb1.directionPin2),
&ENGINE(executor),
CONFIG(etbFreq)
); );
} }
@ -611,9 +618,6 @@ void initElectronicThrottle(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
etbPid.initPidClass(&engineConfiguration->etb); etbPid.initPidClass(&engineConfiguration->etb);
for (int i = 0 ; i < ETB_COUNT; i++) {
INJECT_ENGINE_REFERENCE(etbControls[i]);
}
INJECT_ENGINE_REFERENCE(etbController); INJECT_ENGINE_REFERENCE(etbController);
pedal2tpsMap.init(config->pedalToTpsTable, config->pedalToTpsPedalBins, config->pedalToTpsRpmBins); pedal2tpsMap.init(config->pedalToTpsTable, config->pedalToTpsPedalBins, config->pedalToTpsRpmBins);
@ -646,7 +650,7 @@ void initElectronicThrottle(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
if (engineConfiguration->etbCalibrationOnStart) { if (engineConfiguration->etbCalibrationOnStart) {
for (int i = 0 ; i < ETB_COUNT; i++) { for (int i = 0 ; i < ETB_COUNT; i++) {
EtbControl *etb = &etbControls[i]; EtbHardware *etb = &etbHardware[i];
etb->dcMotor.set(70); etb->dcMotor.set(70);
chThdSleep(600); chThdSleep(600);

View File

@ -14,16 +14,18 @@
#include "engine.h" #include "engine.h"
#include "periodic_task.h" #include "periodic_task.h"
class EtbControl; class DcMotor;
class EtbController : public PeriodicTimerController { class EtbController final : public PeriodicTimerController {
public: public:
DECLARE_ENGINE_PTR; DECLARE_ENGINE_PTR;
EtbController(EtbControl *etb); EtbController(DcMotor *etb);
EtbControl *etb = nullptr;
int getPeriodMs() override; int getPeriodMs() override;
void PeriodicTask() override; void PeriodicTask() override;
private:
DcMotor *m_motor;
}; };
void initElectronicThrottle(DECLARE_ENGINE_PARAMETER_SIGNATURE); void initElectronicThrottle(DECLARE_ENGINE_PARAMETER_SIGNATURE);