Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite etb autocal to support main loop #514

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 119 additions & 60 deletions firmware/controllers/actuators/electronic_throttle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,92 +619,151 @@ void EtbController::checkJam(percent_t setpoint, percent_t observation) {
}
}

void EtbController::autoCalibrateTps() {
// Only auto calibrate throttles
if (m_function == DC_Throttle1 || m_function == DC_Throttle2) {
m_isAutocal = true;
}
}

#if EFI_UNIT_TEST
#define chThdSleepMilliseconds(x) {}
#endif // EFI_UNIT_TEST

#include <utility>

template <typename TBase>
struct EtbImpl final : public TBase {
class EtbImpl final : public TBase {
private:
enum class ACPhase {
Stopped,

Start,

// Drive the motor open
Open,

// Drive the motor closed
Close,

//
TransmitPrimaryMax,
TransmitPrimaryMin,
TransmitSecondaryMax,
TransmitSecondaryMin,
};

public:
template <typename... TArgs>
EtbImpl(TArgs&&... args) : TBase(std::forward<TArgs>(args)...) { }

void update() override {
#if EFI_TUNER_STUDIO
if (TBase::m_isAutocal) {
if (m_autocalPhase != ACPhase::Stopped) {
m_autocalPhase = doAutocal(m_autocalPhase);
} else
#endif /* EFI_TUNER_STUDIO */

{
TBase::update();
}
}

void autoCalibrateTps() override {
// Only auto calibrate throttles
if (TBase::getFunction() == DC_Throttle1 || TBase::getFunction() == DC_Throttle2) {
m_autocalPhase = ACPhase::Start;
}
}

ACPhase doAutocal(ACPhase phase) {
// Don't allow if engine is running!
if (Sensor::getOrZero(SensorType::Rpm) > 0) {
TBase::m_isAutocal = false;
return;
return ACPhase::Stopped;
}

auto motor = TBase::getMotor();
if (!motor) {
TBase::m_isAutocal = false;
return;
return ACPhase::Stopped;
}

auto myFunction = TBase::getFunction();

// First grab open
motor->set(0.5f);
motor->enable();
chThdSleepMilliseconds(1000);
float primaryMax = Sensor::getRaw(functionToTpsSensorPrimary(myFunction));
float secondaryMax = Sensor::getRaw(functionToTpsSensorSecondary(myFunction));

// Let it return
motor->set(0);
chThdSleepMilliseconds(200);

// Now grab closed
motor->set(-0.5f);
chThdSleepMilliseconds(1000);
float primaryMin = Sensor::getRaw(functionToTpsSensorPrimary(myFunction));
float secondaryMin = Sensor::getRaw(functionToTpsSensorSecondary(myFunction));

// Finally disable and reset state
motor->disable("autotune");

// Check that the calibrate actually moved the throttle
if (std::abs(primaryMax - primaryMin) < 0.5f) {
firmwareError(ObdCode::OBD_TPS_Configuration, "Auto calibrate failed, check your wiring!\r\nClosed voltage: %.1fv Open voltage: %.1fv", primaryMin, primaryMax);
TBase::m_isAutocal = false;
return;
switch (phase) {
case ACPhase::Start:
// Open the throttle
motor->set(0.5f);
motor->enable();
m_autocalTimer.reset();
return ACPhase::Open;
case ACPhase::Open:
if (m_autocalTimer.hasElapsedMs(1000)) {
// Capture open position
m_primaryMax = Sensor::getRaw(functionToTpsSensorPrimary(myFunction));
m_secondaryMax = Sensor::getRaw(functionToTpsSensorSecondary(myFunction));

// Next: close the throttle
motor->set(-0.5f);
return ACPhase::Close;
}
break;
case ACPhase::Close:
if (m_autocalTimer.hasElapsedMs(1000)) {
// Capture closed position
m_primaryMin = Sensor::getRaw(functionToTpsSensorPrimary(myFunction));
m_secondaryMin = Sensor::getRaw(functionToTpsSensorSecondary(myFunction));

// Disable the motor, we're done
motor->disable("autotune");

// Check that the calibrate actually moved the throttle
if (std::abs(m_primaryMax - m_primaryMin) < 0.5f) {
firmwareError(ObdCode::OBD_TPS_Configuration, "Auto calibrate failed, check your wiring!\r\nClosed voltage: %.1fv Open voltage: %.1fv", m_primaryMin, m_primaryMax);
return ACPhase::Stopped;
}

// Next: start transmitting results
engine->outputChannels.calibrationMode = (uint8_t)functionToCalModePriMax(myFunction);
engine->outputChannels.calibrationValue = convertVoltageTo10bitADC(m_primaryMax);
return ACPhase::TransmitPrimaryMax;
}
break;
case ACPhase::TransmitPrimaryMax:
if (m_autocalTimer.hasElapsedMs(500)) {
engine->outputChannels.calibrationMode = (uint8_t)functionToCalModePriMin(myFunction);
engine->outputChannels.calibrationValue = convertVoltageTo10bitADC(m_primaryMin);
return ACPhase::TransmitPrimaryMin;
}
break;
case ACPhase::TransmitPrimaryMin:
if (m_autocalTimer.hasElapsedMs(500)) {
engine->outputChannels.calibrationMode = (uint8_t)functionToCalModeSecMax(myFunction);
engine->outputChannels.calibrationValue = convertVoltageTo10bitADC(m_secondaryMax);
return ACPhase::TransmitSecondaryMax;
}
break;
case ACPhase::TransmitSecondaryMax:
if (m_autocalTimer.hasElapsedMs(500)) {
engine->outputChannels.calibrationMode = (uint8_t)functionToCalModeSecMin(myFunction);
engine->outputChannels.calibrationValue = convertVoltageTo10bitADC(m_secondaryMin);
return ACPhase::TransmitSecondaryMin;
}
break;
case ACPhase::TransmitSecondaryMin:
if (m_autocalTimer.hasElapsedMs(500)) {
// Done!
engine->outputChannels.calibrationMode = (uint8_t)TsCalMode::None;
return ACPhase::Stopped;
}
break;
case ACPhase::Stopped: break;
}

// Write out the learned values to TS, waiting briefly after setting each to let TS grab it
engine->outputChannels.calibrationMode = (uint8_t)functionToCalModePriMax(myFunction);
engine->outputChannels.calibrationValue = convertVoltageTo10bitADC(primaryMax);
chThdSleepMilliseconds(500);
engine->outputChannels.calibrationMode = (uint8_t)functionToCalModePriMin(myFunction);
engine->outputChannels.calibrationValue = convertVoltageTo10bitADC(primaryMin);
chThdSleepMilliseconds(500);

engine->outputChannels.calibrationMode = (uint8_t)functionToCalModeSecMax(myFunction);
engine->outputChannels.calibrationValue = convertVoltageTo10bitADC(secondaryMax);
chThdSleepMilliseconds(500);
engine->outputChannels.calibrationMode = (uint8_t)functionToCalModeSecMin(myFunction);
engine->outputChannels.calibrationValue = convertVoltageTo10bitADC(secondaryMin);
chThdSleepMilliseconds(500);

engine->outputChannels.calibrationMode = (uint8_t)TsCalMode::None;

TBase::m_isAutocal = false;
return;
// by default, stay in the same phase
return phase;
}
#endif /* EFI_TUNER_STUDIO */

TBase::update();
}
private:
ACPhase m_autocalPhase = ACPhase::Stopped;
Timer m_autocalTimer;

float m_primaryMax;
float m_secondaryMax;
float m_primaryMin;
float m_secondaryMin;
};

// real implementation (we mock for some unit tests)
Expand Down
2 changes: 1 addition & 1 deletion firmware/controllers/actuators/electronic_throttle.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class IEtbController : public ClosedLoopController<percent_t, percent_t> {
virtual void setIdlePosition(percent_t pos) = 0;
virtual void setWastegatePosition(percent_t pos) = 0;
virtual void update() = 0;
virtual void autoCalibrateTps() = 0;
virtual void autoCalibrateTps() { }
virtual bool isEtbMode() const = 0;

virtual const pid_state_s& getPidState() const = 0;
Expand Down
6 changes: 0 additions & 6 deletions firmware/controllers/actuators/electronic_throttle_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ class EtbController : public IEtbController, public electronic_throttle_s {
// Used to inspect the internal PID controller's state
const pid_state_s& getPidState() const override { return m_pid; };

// Use the throttle to automatically calibrate the relevant throttle position sensor(s).
void autoCalibrateTps() override;

// Override if this throttle needs special per-throttle adjustment (bank-to-bank trim, for example)
virtual percent_t getThrottleTrim(float /*rpm*/, percent_t /*targetPosition*/) const {
return 0;
Expand All @@ -73,9 +70,6 @@ class EtbController : public IEtbController, public electronic_throttle_s {
float prevOutput = 0;

protected:
// This is set if an automatic TPS calibration should be run
bool m_isAutocal = false;

bool hadTpsError = false;
bool hadPpsError = false;

Expand Down
Loading