2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file idle_thread.cpp
|
|
|
|
* @brief Idle Air Control valve thread.
|
|
|
|
*
|
|
|
|
* This thread looks at current RPM and decides if it should increase or decrease IAC duty cycle.
|
|
|
|
* This file is has the hardware & scheduling logic, desired idle level lives separately
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @date May 23, 2013
|
2018-01-20 17:55:31 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2018
|
2015-07-10 06:01:56 -07:00
|
|
|
*
|
|
|
|
* This file is part of rusEfi - see http://rusefi.com
|
|
|
|
*
|
|
|
|
* rusEfi is free software; you can redistribute it and/or modify it under the terms of
|
|
|
|
* the GNU General Public License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
|
|
|
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with this program.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2015-11-09 16:03:32 -08:00
|
|
|
*
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2018-09-16 19:26:57 -07:00
|
|
|
#include "global.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "rpm_calculator.h"
|
|
|
|
#include "pwm_generator.h"
|
|
|
|
#include "idle_thread.h"
|
|
|
|
#include "pin_repository.h"
|
|
|
|
#include "engine_configuration.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "stepper.h"
|
|
|
|
|
|
|
|
#if EFI_IDLE_CONTROL || defined(__DOXYGEN__)
|
2017-05-28 10:39:28 -07:00
|
|
|
#include "allsensors.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static THD_WORKING_AREA(ivThreadStack, UTILITY_THREAD_STACK_SIZE);
|
|
|
|
|
|
|
|
static Logging *logger;
|
2018-11-16 04:40:06 -08:00
|
|
|
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
|
2016-02-13 12:03:37 -08:00
|
|
|
extern TunerStudioOutputChannels tsOutputChannels;
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_TUNER_STUDIO */
|
2015-07-10 06:01:56 -07:00
|
|
|
EXTERN_ENGINE
|
|
|
|
;
|
|
|
|
|
2017-05-28 10:44:26 -07:00
|
|
|
static bool shouldResetPid = false;
|
2018-03-30 05:42:13 -07:00
|
|
|
// we might reset PID state when the state is changed, but only if needed (See autoIdle())
|
|
|
|
static bool mightResetPid = false;
|
2017-05-16 19:13:40 -07:00
|
|
|
|
2017-12-27 14:56:04 -08:00
|
|
|
#if EFI_IDLE_INCREMENTAL_PID_CIC || defined(__DOXYGEN__)
|
|
|
|
// Use new PID with CIC integrator
|
|
|
|
static PidCic idlePid(&engineConfiguration->idleRpmPid);
|
|
|
|
#else
|
2017-05-29 20:15:07 -07:00
|
|
|
static Pid idlePid(&engineConfiguration->idleRpmPid);
|
2017-12-27 14:56:04 -08:00
|
|
|
#endif /* EFI_IDLE_INCREMENTAL_PID_CIC */
|
2017-05-16 19:13:40 -07:00
|
|
|
|
2015-11-09 16:03:32 -08:00
|
|
|
// todo: extract interface for idle valve hardware, with solenoid and stepper implementations?
|
2019-01-09 05:50:51 -08:00
|
|
|
static SimplePwm idleSolenoid("idle");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static StepperMotor iacMotor;
|
|
|
|
|
2017-08-01 13:27:16 -07:00
|
|
|
static uint32_t lastCrankingCyclesCounter = 0;
|
|
|
|
static float lastCrankingIacPosition;
|
|
|
|
|
2018-07-29 09:23:55 -07:00
|
|
|
typedef enum {
|
|
|
|
INIT = 0,
|
|
|
|
TPS_THRESHOLD = 1,
|
|
|
|
RPM_DEAD_ZONE = 2,
|
|
|
|
PWM_PRETTY_CLOSE = 3,
|
|
|
|
ADJUSTING = 4,
|
2018-07-29 13:36:18 -07:00
|
|
|
BLIP = 5,
|
2018-07-29 09:23:55 -07:00
|
|
|
|
|
|
|
} idle_state_e;
|
|
|
|
|
|
|
|
idle_state_e idleState = INIT;
|
|
|
|
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2017-05-28 10:39:28 -07:00
|
|
|
* that's current position with CLT and IAT corrections
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2017-05-28 10:39:28 -07:00
|
|
|
static percent_t currentIdlePosition = -100.0f;
|
2017-12-27 14:56:04 -08:00
|
|
|
/**
|
|
|
|
* the same as currentIdlePosition, but without adjustments (iacByTpsTaper, afterCrankingIACtaperDuration)
|
|
|
|
*/
|
|
|
|
static percent_t baseIdlePosition = currentIdlePosition;
|
2018-03-30 05:42:13 -07:00
|
|
|
/**
|
|
|
|
* When the IAC position value change is insignificant (lower than this threshold), leave the poor valve alone
|
2018-07-29 13:49:42 -07:00
|
|
|
* todo: why do we have this logic? is this ever useful?
|
|
|
|
* See
|
2018-03-30 05:42:13 -07:00
|
|
|
*/
|
2018-07-29 13:49:42 -07:00
|
|
|
static percent_t idlePositionSensitivityThreshold = 0.0f;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
void idleDebug(const char *msg, percent_t value) {
|
2018-01-23 09:05:14 -08:00
|
|
|
scheduleMsg(logger, "idle debug: %s%.2f", msg, value);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void showIdleInfo(void) {
|
2017-01-28 22:04:16 -08:00
|
|
|
const char * idleModeStr = getIdle_mode_e(engineConfiguration->idleMode);
|
2018-01-23 09:05:14 -08:00
|
|
|
scheduleMsg(logger, "idleMode=%s position=%.2f isStepper=%s", idleModeStr,
|
2019-01-09 19:57:33 -08:00
|
|
|
getIdlePosition(), boolToString(CONFIGB(useStepperIdle)));
|
2017-05-16 17:52:52 -07:00
|
|
|
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(useStepperIdle)) {
|
|
|
|
scheduleMsg(logger, "directionPin=%s reactionTime=%.2f", hwPortname(CONFIGB(idle).stepperDirectionPin),
|
2015-07-10 06:01:56 -07:00
|
|
|
engineConfiguration->idleStepperReactionTime);
|
2019-01-09 19:57:33 -08:00
|
|
|
scheduleMsg(logger, "stepPin=%s steps=%d", hwPortname(CONFIGB(idle).stepperStepPin),
|
2015-07-10 06:01:56 -07:00
|
|
|
engineConfiguration->idleStepperTotalSteps);
|
2017-01-28 22:04:16 -08:00
|
|
|
scheduleMsg(logger, "enablePin=%s/%d", hwPortname(engineConfiguration->stepperEnablePin),
|
|
|
|
engineConfiguration->stepperEnablePinMode);
|
2015-07-10 06:01:56 -07:00
|
|
|
} else {
|
2019-01-09 19:57:33 -08:00
|
|
|
scheduleMsg(logger, "idle valve freq=%d on %s", CONFIGB(idle).solenoidFrequency,
|
|
|
|
hwPortname(CONFIGB(idle).solenoidPin));
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2017-05-16 17:52:52 -07:00
|
|
|
|
|
|
|
|
|
|
|
if (engineConfiguration->idleMode == IM_AUTO) {
|
2017-05-29 20:15:07 -07:00
|
|
|
idlePid.showPidStatus(logger, "idle");
|
2017-01-28 22:04:16 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-16 17:52:52 -07:00
|
|
|
void setIdleMode(idle_mode_e value) {
|
2015-07-10 06:01:56 -07:00
|
|
|
engineConfiguration->idleMode = value ? IM_AUTO : IM_MANUAL;
|
|
|
|
showIdleInfo();
|
|
|
|
}
|
|
|
|
|
2017-05-16 19:13:40 -07:00
|
|
|
static void applyIACposition(percent_t position) {
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(useStepperIdle)) {
|
2017-05-16 19:13:40 -07:00
|
|
|
iacMotor.setTargetPosition(position / 100 * engineConfiguration->idleStepperTotalSteps);
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* currently idle level is an percent value (0-100 range), and PWM takes a float in the 0..1 range
|
|
|
|
* todo: unify?
|
|
|
|
*/
|
|
|
|
idleSolenoid.setSimplePwmDutyCycle(position / 100.0);
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2018-07-29 09:23:55 -07:00
|
|
|
static percent_t manualIdleController(float cltCorrection) {
|
2016-02-15 15:02:03 -08:00
|
|
|
|
2019-01-09 19:57:33 -08:00
|
|
|
percent_t correctedPosition = cltCorrection * CONFIGB(manIdlePosition);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-16 19:13:40 -07:00
|
|
|
// let's put the value into the right range
|
|
|
|
correctedPosition = maxF(correctedPosition, 0.01);
|
|
|
|
correctedPosition = minF(correctedPosition, 99.9);
|
2016-02-13 12:03:37 -08:00
|
|
|
|
2017-05-16 19:13:40 -07:00
|
|
|
return correctedPosition;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-01-06 14:01:28 -08:00
|
|
|
void setIdleValvePosition(int positionPercent) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (positionPercent < 1 || positionPercent > 99)
|
|
|
|
return;
|
|
|
|
scheduleMsg(logger, "setting idle valve position %d", positionPercent);
|
|
|
|
showIdleInfo();
|
2017-04-29 11:40:55 -07:00
|
|
|
// todo: this is not great that we have to write into configuration here
|
2019-01-09 19:57:33 -08:00
|
|
|
CONFIGB(manIdlePosition) = positionPercent;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2018-07-29 09:23:55 -07:00
|
|
|
static percent_t blipIdlePosition;
|
2015-09-01 20:02:49 -07:00
|
|
|
static efitimeus_t timeToStopBlip = 0;
|
2015-12-24 19:01:26 -08:00
|
|
|
static efitimeus_t timeToStopIdleTest = 0;
|
2015-09-01 20:02:49 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* I use this questionable feature to tune acceleration enrichment
|
|
|
|
*/
|
|
|
|
static void blipIdle(int idlePosition, int durationMs) {
|
|
|
|
if (timeToStopBlip != 0) {
|
|
|
|
return; // already in idle blip
|
|
|
|
}
|
2017-05-16 19:13:40 -07:00
|
|
|
blipIdlePosition = idlePosition;
|
2015-09-01 20:02:49 -07:00
|
|
|
timeToStopBlip = getTimeNowUs() + 1000 * durationMs;
|
|
|
|
}
|
|
|
|
|
2015-12-24 19:01:26 -08:00
|
|
|
static void finishIdleTestIfNeeded() {
|
|
|
|
if (timeToStopIdleTest != 0 && getTimeNowUs() > timeToStopIdleTest)
|
|
|
|
timeToStopIdleTest = 0;
|
|
|
|
}
|
|
|
|
|
2015-09-01 20:02:49 -07:00
|
|
|
static void undoIdleBlipIfNeeded() {
|
|
|
|
if (timeToStopBlip != 0 && getTimeNowUs() > timeToStopBlip) {
|
|
|
|
timeToStopBlip = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 11:01:37 -07:00
|
|
|
percent_t getIdlePosition(void) {
|
2017-06-04 18:15:44 -07:00
|
|
|
return currentIdlePosition;
|
2015-10-18 11:01:37 -07:00
|
|
|
}
|
|
|
|
|
2018-07-29 09:23:55 -07:00
|
|
|
/**
|
|
|
|
* @return idle valve position percentage for automatic closed loop mode
|
|
|
|
*/
|
|
|
|
static percent_t automaticIdleController() {
|
2017-12-27 14:56:04 -08:00
|
|
|
percent_t tpsPos = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2019-01-09 19:57:33 -08:00
|
|
|
if (tpsPos > CONFIGB(idlePidDeactivationTpsThreshold)) {
|
2018-03-30 05:42:13 -07:00
|
|
|
// Don't store old I and D terms if PID doesn't work anymore.
|
|
|
|
// Otherwise they will affect the idle position much later, when the throttle is closed.
|
|
|
|
if (mightResetPid) {
|
|
|
|
mightResetPid = false;
|
|
|
|
shouldResetPid = true;
|
|
|
|
}
|
|
|
|
|
2018-07-29 09:23:55 -07:00
|
|
|
idleState = TPS_THRESHOLD;
|
2017-12-27 14:56:04 -08:00
|
|
|
// just leave IAC position as is (but don't return currentIdlePosition - it may already contain additionalAir)
|
|
|
|
return baseIdlePosition;
|
2017-07-31 11:36:01 -07:00
|
|
|
}
|
2016-06-26 18:02:40 -07:00
|
|
|
|
2017-12-24 19:05:16 -08:00
|
|
|
// get Target RPM for Auto-PID from a separate table
|
|
|
|
float clt = engine->sensors.clt;
|
|
|
|
int targetRpm;
|
|
|
|
if (cisnan(clt)) {
|
|
|
|
// error is already reported, let's take first value from the table should be good enough error handing solution
|
|
|
|
targetRpm = CONFIG(cltIdleRpm)[0];
|
|
|
|
} else {
|
|
|
|
targetRpm = interpolate2d("cltRpm", clt, CONFIG(cltIdleRpmBins), CONFIG(cltIdleRpm), CLT_CURVE_SIZE);
|
|
|
|
}
|
2019-01-19 18:10:58 -08:00
|
|
|
targetRpm += engine->fsioState.fsioIdleTargetRPMAdjustment;
|
2018-07-29 15:02:37 -07:00
|
|
|
|
2016-06-26 18:02:40 -07:00
|
|
|
|
2018-03-30 05:42:13 -07:00
|
|
|
// check if within the dead zone
|
2019-01-21 18:48:58 -08:00
|
|
|
int rpm = GET_RPM();
|
2018-07-29 09:23:55 -07:00
|
|
|
if (absI(rpm - targetRpm) <= CONFIG(idlePidRpmDeadZone)) {
|
|
|
|
idleState = RPM_DEAD_ZONE;
|
|
|
|
// current RPM is close enough, no need to change anything
|
2018-03-30 05:42:13 -07:00
|
|
|
return baseIdlePosition;
|
2018-07-29 09:23:55 -07:00
|
|
|
}
|
2018-03-30 05:42:13 -07:00
|
|
|
|
|
|
|
// When rpm < targetRpm, there's a risk of dropping RPM too low - and the engine dies out.
|
|
|
|
// So PID reaction should be increased by adding extra percent to PID-error:
|
|
|
|
percent_t errorAmpCoef = 1.0f;
|
|
|
|
if (rpm < targetRpm)
|
|
|
|
errorAmpCoef += (float)CONFIG(pidExtraForLowRpm) / PERCENT_MULT;
|
|
|
|
// If errorAmpCoef > 1.0, then PID thinks that RPM is lower than it is, and controls IAC more aggressively
|
|
|
|
idlePid.setErrorAmplification(errorAmpCoef);
|
|
|
|
|
|
|
|
percent_t newValue = idlePid.getValue(targetRpm, rpm, engineConfiguration->idleRpmPid.period);
|
|
|
|
|
|
|
|
// the state of PID has been changed, so we might reset it now, but only when needed (see idlePidDeactivationTpsThreshold)
|
|
|
|
mightResetPid = true;
|
2016-06-26 18:02:40 -07:00
|
|
|
|
2017-12-27 14:56:04 -08:00
|
|
|
#if EFI_IDLE_INCREMENTAL_PID_CIC || defined(__DOXYGEN__)
|
|
|
|
// Treat the 'newValue' as if it contains not an actual IAC position, but an incremental delta.
|
|
|
|
// So we add this delta to the base IAC position, with a smooth taper for TPS transients.
|
2019-01-09 19:57:33 -08:00
|
|
|
newValue = baseIdlePosition + interpolateClamped(0.0f, newValue, CONFIGB(idlePidDeactivationTpsThreshold), 0.0f, tpsPos);
|
2017-12-27 14:56:04 -08:00
|
|
|
|
|
|
|
// apply the PID limits
|
|
|
|
newValue = maxF(newValue, CONFIG(idleRpmPid.minValue));
|
|
|
|
newValue = minF(newValue, CONFIG(idleRpmPid.maxValue));
|
|
|
|
#endif /* EFI_IDLE_INCREMENTAL_PID_CIC */
|
|
|
|
|
2018-03-30 05:42:13 -07:00
|
|
|
// Interpolate to the manual position when RPM is close to the upper RPM limit (if idlePidRpmUpperLimit is set).
|
|
|
|
// If RPM increases and the throttle is closed, then we're in coasting mode, and we should smoothly disable auto-pid.
|
|
|
|
// If we just leave IAC at baseIdlePosition (as in case of TPS deactivation threshold), RPM would get stuck.
|
|
|
|
// That's why there's 'useIacTableForCoasting' setting which involves a separate IAC position table for coasting (iacCoasting).
|
|
|
|
// Currently it's user-defined. But eventually we'll use a real calculated and stored IAC position instead.
|
|
|
|
int idlePidLowerRpm = targetRpm + CONFIG(idlePidRpmDeadZone);
|
|
|
|
if (CONFIG(idlePidRpmUpperLimit) > 0) {
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(useIacTableForCoasting)) {
|
2018-03-30 05:42:13 -07:00
|
|
|
percent_t iacPosForCoasting = interpolate2d("iacCoasting", clt, CONFIG(iacCoastingBins), CONFIG(iacCoasting), CLT_CURVE_SIZE);
|
|
|
|
newValue = interpolateClamped(idlePidLowerRpm, newValue, idlePidLowerRpm + CONFIG(idlePidRpmUpperLimit), iacPosForCoasting, rpm);
|
|
|
|
} else {
|
|
|
|
// Well, just leave it as is, without PID regulation...
|
|
|
|
newValue = baseIdlePosition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 19:13:40 -07:00
|
|
|
return newValue;
|
2016-06-26 18:02:40 -07:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
static msg_t ivThread(int param) {
|
|
|
|
(void) param;
|
|
|
|
chRegSetThreadName("IdleValve");
|
|
|
|
|
2017-02-11 23:02:24 -08:00
|
|
|
/*
|
|
|
|
* Here we have idle logic thread - actual stepper movement is implemented in a separate
|
|
|
|
* working thread,
|
|
|
|
* @see stepper.cpp
|
|
|
|
*/
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
while (true) {
|
2017-05-28 19:32:32 -07:00
|
|
|
idlePid.sleep(); // in both manual and auto mode same period is used
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-07-29 09:23:55 -07:00
|
|
|
if (engineConfiguration->isVerboseIAC && engineConfiguration->idleMode == IM_AUTO) {
|
|
|
|
scheduleMsg(logger, "state %d", idleState);
|
|
|
|
idlePid.showPidStatus(logger, "idle");
|
|
|
|
}
|
|
|
|
|
2017-05-28 10:44:26 -07:00
|
|
|
if (shouldResetPid) {
|
|
|
|
idlePid.reset();
|
|
|
|
// alternatorPidResetCounter++;
|
|
|
|
shouldResetPid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-01 12:57:50 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
// this value is not used yet
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(clutchDownPin) != GPIO_UNASSIGNED) {
|
|
|
|
engine->clutchDownState = efiReadPin(CONFIGB(clutchDownPin));
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(clutchUpPin) != GPIO_UNASSIGNED) {
|
|
|
|
engine->clutchUpState = efiReadPin(CONFIGB(clutchUpPin));
|
2017-05-15 02:08:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (engineConfiguration->brakePedalPin != GPIO_UNASSIGNED) {
|
|
|
|
engine->brakePedalState = efiReadPin(engineConfiguration->brakePedalPin);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2018-11-01 12:57:50 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-12-24 19:01:26 -08:00
|
|
|
finishIdleTestIfNeeded();
|
2015-09-01 20:02:49 -07:00
|
|
|
undoIdleBlipIfNeeded();
|
|
|
|
|
2017-05-29 09:40:02 -07:00
|
|
|
float clt = engine->sensors.clt;
|
2017-12-27 14:56:04 -08:00
|
|
|
bool isRunning = engine->rpmCalculator.isRunning(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2018-03-30 05:42:13 -07:00
|
|
|
// cltCorrection is used only for cranking or running in manual mode
|
2017-12-27 14:56:04 -08:00
|
|
|
float cltCorrection;
|
|
|
|
if (cisnan(clt))
|
|
|
|
cltCorrection = 1.0f;
|
|
|
|
// Use separate CLT correction table for cranking
|
|
|
|
else if (engineConfiguration->overrideCrankingIacSetting && !isRunning)
|
|
|
|
cltCorrection = interpolate2d("cltCrankingT", clt, config->cltCrankingCorrBins, config->cltCrankingCorr, CLT_CRANKING_CURVE_SIZE) / PERCENT_MULT;
|
|
|
|
else
|
|
|
|
cltCorrection = interpolate2d("cltT", clt, config->cltIdleCorrBins, config->cltIdleCorr, CLT_CURVE_SIZE) / PERCENT_MULT;
|
2017-05-16 19:13:40 -07:00
|
|
|
|
2018-07-29 09:23:55 -07:00
|
|
|
percent_t iacPosition;
|
2017-05-16 19:13:40 -07:00
|
|
|
|
|
|
|
if (timeToStopBlip != 0) {
|
|
|
|
iacPosition = blipIdlePosition;
|
2017-12-27 14:56:04 -08:00
|
|
|
baseIdlePosition = iacPosition;
|
2018-07-29 13:36:18 -07:00
|
|
|
idleState = BLIP;
|
2017-12-27 14:56:04 -08:00
|
|
|
} else if (!isRunning) {
|
2017-05-16 19:13:40 -07:00
|
|
|
// during cranking it's always manual mode, PID would make no sence during cranking
|
|
|
|
iacPosition = cltCorrection * engineConfiguration->crankingIACposition;
|
2017-08-01 13:27:16 -07:00
|
|
|
// save cranking position & cycles counter for taper transition
|
|
|
|
lastCrankingIacPosition = iacPosition;
|
|
|
|
lastCrankingCyclesCounter = engine->rpmCalculator.getRevolutionCounterSinceStart();
|
2017-12-27 14:56:04 -08:00
|
|
|
baseIdlePosition = iacPosition;
|
2016-06-26 18:02:40 -07:00
|
|
|
} else {
|
2017-08-01 13:27:16 -07:00
|
|
|
if (engineConfiguration->idleMode == IM_MANUAL) {
|
|
|
|
// let's re-apply CLT correction
|
|
|
|
iacPosition = manualIdleController(cltCorrection);
|
|
|
|
} else {
|
2018-07-29 09:23:55 -07:00
|
|
|
iacPosition = automaticIdleController();
|
2017-08-01 13:27:16 -07:00
|
|
|
}
|
2017-11-03 15:04:24 -07:00
|
|
|
|
2017-12-27 14:56:04 -08:00
|
|
|
// store 'base' iacPosition without adjustments
|
|
|
|
baseIdlePosition = iacPosition;
|
|
|
|
|
2017-11-03 15:04:24 -07:00
|
|
|
percent_t tpsPos = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE);
|
|
|
|
float additionalAir = (float)engineConfiguration->iacByTpsTaper;
|
2019-01-09 19:57:33 -08:00
|
|
|
iacPosition += interpolateClamped(0.0f, 0.0f, CONFIGB(idlePidDeactivationTpsThreshold), additionalAir, tpsPos);
|
2017-11-03 15:04:24 -07:00
|
|
|
|
2017-08-01 13:27:16 -07:00
|
|
|
// taper transition from cranking to running (uint32_t to float conversion is safe here)
|
|
|
|
if (engineConfiguration->afterCrankingIACtaperDuration > 0)
|
|
|
|
iacPosition = interpolateClamped(lastCrankingCyclesCounter, lastCrankingIacPosition,
|
|
|
|
lastCrankingCyclesCounter + engineConfiguration->afterCrankingIACtaperDuration, iacPosition,
|
|
|
|
engine->rpmCalculator.getRevolutionCounterSinceStart());
|
2017-05-16 19:13:40 -07:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-22 12:30:39 -07:00
|
|
|
if (engineConfiguration->debugMode == DBG_IDLE_CONTROL) {
|
|
|
|
if (engineConfiguration->idleMode == IM_AUTO) {
|
2018-11-16 04:40:06 -08:00
|
|
|
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
|
2017-07-23 07:29:10 -07:00
|
|
|
// see also tsOutputChannels->idlePosition
|
2017-07-23 09:12:35 -07:00
|
|
|
idlePid.postState(&tsOutputChannels, 1000000);
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_TUNER_STUDIO */
|
2017-07-23 07:29:10 -07:00
|
|
|
} else {
|
2018-11-16 04:40:06 -08:00
|
|
|
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
|
2017-07-23 07:29:10 -07:00
|
|
|
tsOutputChannels.debugFloatField1 = iacPosition;
|
2018-03-30 05:42:13 -07:00
|
|
|
tsOutputChannels.debugIntField1 = iacMotor.getTargetPosition();
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_TUNER_STUDIO */
|
2017-05-22 12:30:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 13:36:18 -07:00
|
|
|
// The threshold is dependent on IAC type (see initIdleHardware())
|
|
|
|
if (absF(iacPosition - currentIdlePosition) < idlePositionSensitivityThreshold) {
|
|
|
|
idleState = PWM_PRETTY_CLOSE;
|
|
|
|
continue; // value is pretty close, let's leave the poor valve alone
|
|
|
|
}
|
|
|
|
|
2017-05-28 10:39:28 -07:00
|
|
|
currentIdlePosition = iacPosition;
|
2018-07-29 09:23:55 -07:00
|
|
|
idleState = ADJUSTING;
|
2017-05-28 10:39:28 -07:00
|
|
|
applyIACposition(currentIdlePosition);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
#if defined __GNUC__
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-01-06 14:01:28 -08:00
|
|
|
void setTargetIdleRpm(int value) {
|
2017-12-24 19:05:16 -08:00
|
|
|
setTargetRpmCurve(value PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(logger, "target idle RPM %d", value);
|
2017-05-22 20:25:34 -07:00
|
|
|
showIdleInfo();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2015-11-09 16:03:32 -08:00
|
|
|
|
2015-10-19 19:02:51 -07:00
|
|
|
static void apply(void) {
|
2017-05-22 20:25:34 -07:00
|
|
|
idlePid.updateFactors(engineConfiguration->idleRpmPid.pFactor, engineConfiguration->idleRpmPid.iFactor, engineConfiguration->idleRpmPid.dFactor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setIdleOffset(float value) {
|
|
|
|
engineConfiguration->idleRpmPid.offset = value;
|
|
|
|
showIdleInfo();
|
2015-10-19 19:02:51 -07:00
|
|
|
}
|
|
|
|
|
2017-01-06 13:03:41 -08:00
|
|
|
void setIdlePFactor(float value) {
|
2016-02-05 13:01:55 -08:00
|
|
|
engineConfiguration->idleRpmPid.pFactor = value;
|
2015-10-19 19:02:51 -07:00
|
|
|
apply();
|
|
|
|
showIdleInfo();
|
|
|
|
}
|
|
|
|
|
2017-01-06 13:03:41 -08:00
|
|
|
void setIdleIFactor(float value) {
|
2016-02-05 13:01:55 -08:00
|
|
|
engineConfiguration->idleRpmPid.iFactor = value;
|
2015-10-19 19:02:51 -07:00
|
|
|
apply();
|
|
|
|
showIdleInfo();
|
|
|
|
}
|
|
|
|
|
2017-01-06 13:03:41 -08:00
|
|
|
void setIdleDFactor(float value) {
|
2016-02-05 13:01:55 -08:00
|
|
|
engineConfiguration->idleRpmPid.dFactor = value;
|
2015-10-19 19:02:51 -07:00
|
|
|
apply();
|
|
|
|
showIdleInfo();
|
|
|
|
}
|
|
|
|
|
2017-01-06 13:03:41 -08:00
|
|
|
void setIdleDT(int value) {
|
2017-05-28 19:32:32 -07:00
|
|
|
engineConfiguration->idleRpmPid.period = value;
|
2015-10-19 19:02:51 -07:00
|
|
|
apply();
|
|
|
|
showIdleInfo();
|
|
|
|
}
|
|
|
|
|
2017-05-28 10:39:28 -07:00
|
|
|
void onConfigurationChangeIdleCallback(engine_configuration_s *previousConfiguration) {
|
2017-05-28 10:44:26 -07:00
|
|
|
shouldResetPid = !idlePid.isSame(&previousConfiguration->idleRpmPid);
|
2019-01-09 19:57:33 -08:00
|
|
|
idleSolenoid.setFrequency(CONFIGB(idle).solenoidFrequency);
|
2017-05-28 10:39:28 -07:00
|
|
|
}
|
|
|
|
|
2019-01-13 16:41:39 -08:00
|
|
|
/**
|
|
|
|
* Idle test would activate the solenoid for three seconds
|
|
|
|
*/
|
2016-12-26 18:04:16 -08:00
|
|
|
void startIdleBench(void) {
|
2015-12-24 19:01:26 -08:00
|
|
|
timeToStopIdleTest = getTimeNowUs() + MS2US(3000); // 3 seconds
|
2015-12-26 13:01:56 -08:00
|
|
|
scheduleMsg(logger, "idle valve bench test");
|
|
|
|
showIdleInfo();
|
2015-12-24 19:01:26 -08:00
|
|
|
}
|
|
|
|
|
2015-10-19 19:02:51 -07:00
|
|
|
void setDefaultIdleParameters(void) {
|
2016-02-05 13:01:55 -08:00
|
|
|
engineConfiguration->idleRpmPid.pFactor = 0.1f;
|
|
|
|
engineConfiguration->idleRpmPid.iFactor = 0.05f;
|
|
|
|
engineConfiguration->idleRpmPid.dFactor = 0.0f;
|
2017-05-28 19:32:32 -07:00
|
|
|
engineConfiguration->idleRpmPid.period = 10;
|
2015-10-19 19:02:51 -07:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static void applyIdleSolenoidPinState(PwmConfig *state, int stateIndex) {
|
2018-07-25 20:03:04 -07:00
|
|
|
efiAssertVoid(CUSTOM_ERR_6645, stateIndex < PWM_PHASE_MAX_COUNT, "invalid stateIndex");
|
|
|
|
efiAssertVoid(CUSTOM_ERR_6646, state->multiWave.waveCount == 1, "invalid idle waveCount");
|
2015-07-10 06:01:56 -07:00
|
|
|
OutputPin *output = state->outputPins[0];
|
2018-12-25 07:13:00 -08:00
|
|
|
int value = state->multiWave.getChannelState(/*channelIndex*/0, stateIndex);
|
2015-12-26 13:01:56 -08:00
|
|
|
if (!value /* always allow turning solenoid off */ ||
|
2019-01-21 17:33:21 -08:00
|
|
|
(GET_RPM_VALUE != 0 || timeToStopIdleTest != 0) /* do not run solenoid unless engine is spinning or bench testing in progress */
|
2016-02-05 13:01:55 -08:00
|
|
|
) {
|
2015-07-10 06:01:56 -07:00
|
|
|
output->setValue(value);
|
2016-02-05 13:01:55 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2015-11-09 16:03:32 -08:00
|
|
|
static void initIdleHardware() {
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(useStepperIdle)) {
|
|
|
|
iacMotor.initialize(CONFIGB(idle).stepperStepPin, CONFIGB(idle).stepperDirectionPin,
|
2017-06-13 11:32:34 -07:00
|
|
|
engineConfiguration->stepperDirectionPinMode, engineConfiguration->idleStepperReactionTime,
|
2017-08-07 04:28:21 -07:00
|
|
|
engineConfiguration->idleStepperTotalSteps, engineConfiguration->stepperEnablePin, logger);
|
2018-03-30 05:42:13 -07:00
|
|
|
// This greatly improves PID accuracy for steppers with a small number of steps
|
|
|
|
idlePositionSensitivityThreshold = 1.0f / engineConfiguration->idleStepperTotalSteps;
|
2015-07-10 06:01:56 -07:00
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Start PWM for idleValvePin
|
|
|
|
*/
|
2019-01-09 05:50:51 -08:00
|
|
|
startSimplePwmExt(&idleSolenoid, "Idle Valve",
|
|
|
|
&engine->executor,
|
2019-01-09 19:57:33 -08:00
|
|
|
CONFIGB(idle).solenoidPin, &enginePins.idleSolenoidPin,
|
|
|
|
CONFIGB(idle).solenoidFrequency, CONFIGB(manIdlePosition) / 100,
|
2015-07-10 06:01:56 -07:00
|
|
|
applyIdleSolenoidPinState);
|
2018-07-29 13:49:42 -07:00
|
|
|
idlePositionSensitivityThreshold = 0.0f;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2015-11-09 16:03:32 -08:00
|
|
|
}
|
|
|
|
|
2017-04-29 18:16:26 -07:00
|
|
|
void startIdleThread(Logging*sharedLogger) {
|
2015-11-09 16:03:32 -08:00
|
|
|
logger = sharedLogger;
|
|
|
|
|
|
|
|
// todo: re-initialize idle pins on the fly
|
|
|
|
initIdleHardware();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-16 17:18:28 -07:00
|
|
|
//scheduleMsg(logger, "initial idle %d", idlePositionController.value);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-12-27 06:40:40 -08:00
|
|
|
chThdCreateStatic(ivThreadStack, sizeof(ivThreadStack), NORMALPRIO, (tfunc_t)(void*) ivThread, NULL);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
// this is idle switch INPUT - sometimes there is a switch on the throttle pedal
|
|
|
|
// this switch is not used yet
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(clutchDownPin) != GPIO_UNASSIGNED) {
|
|
|
|
efiSetPadMode("clutch down switch", CONFIGB(clutchDownPin),
|
|
|
|
getInputMode(CONFIGB(clutchDownPinMode)));
|
2017-05-15 02:08:17 -07:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(clutchUpPin) != GPIO_UNASSIGNED) {
|
|
|
|
efiSetPadMode("clutch up switch", CONFIGB(clutchUpPin),
|
|
|
|
getInputMode(CONFIGB(clutchUpPinMode)));
|
2017-05-15 02:08:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (engineConfiguration->brakePedalPin != GPIO_UNASSIGNED) {
|
2018-11-01 12:57:50 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2017-05-15 05:40:54 -07:00
|
|
|
efiSetPadMode("brake pedal switch", engineConfiguration->brakePedalPin,
|
2017-05-15 02:08:17 -07:00
|
|
|
getInputMode(engineConfiguration->brakePedalPinMode));
|
2018-11-01 12:57:50 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2017-05-15 02:08:17 -07:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
addConsoleAction("idleinfo", showIdleInfo);
|
|
|
|
|
2015-09-01 20:02:49 -07:00
|
|
|
addConsoleActionII("blipidle", blipIdle);
|
2015-10-19 19:02:51 -07:00
|
|
|
|
2015-11-09 16:03:32 -08:00
|
|
|
// split this whole file into manual controller and auto controller? move these commands into the file
|
|
|
|
// which would be dedicated to just auto-controller?
|
2015-10-19 19:02:51 -07:00
|
|
|
|
2015-12-24 19:01:26 -08:00
|
|
|
addConsoleAction("idlebench", startIdleBench);
|
2015-10-19 19:02:51 -07:00
|
|
|
apply();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:21:50 -07:00
|
|
|
#endif /* EFI_IDLE_CONTROL */
|