rusefi/firmware/controllers/electronic_throttle.cpp

280 lines
7.9 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file electronic_throttle.cpp
* @brief Electronic Throttle Module driver L298N
*
* todo: make this more universal if/when we get other hardware options
*
2017-01-27 18:04:23 -08:00
* Jan 2017 status:
2017-05-25 09:09:07 -07:00
* Electronic throttle body with it's spring is definitely not linear - both P and I factors of PID are required to get any results
2017-01-27 18:04:23 -08:00
* PID implementation tested on a bench only
* it is believed that more than just PID would be needed, as is this is probably
* not usable on a real vehicle. Needs to be tested :)
*
2017-05-25 09:09:07 -07:00
*
*
2017-05-24 20:53:07 -07:00
* ETB is controlled according to pedal position input (pedal position sensor is a potentiometer)
* petal 0% means pedal not pressed / idle
* pedal 100% means pedal all the way down
2017-05-25 09:09:07 -07:00
* (not TPS - not the one you can calibrate in TunerStudio)
2017-05-24 20:53:07 -07:00
*
* At the moment we only control opening motor - while relying on ETB spring to move throttle butterfly
* back. Throttle position sensor inside ETB is used for closed-loop PID control of ETB.
*
2017-05-25 09:09:07 -07:00
* See also pid.cpp
*
2017-05-29 20:36:08 -07:00
* Relevant console commands:
*
* enable verbose_etb
* disable verbose_etb
* ethinfo
2017-05-24 20:53:07 -07:00
*
2017-01-27 18:04:23 -08:00
* http://rusefi.com/forum/viewtopic.php?f=5&t=592
*
2015-07-10 06:01:56 -07:00
* @date Dec 7, 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/>.
*/
#include "main.h"
#include "electronic_throttle.h"
#include "tps.h"
#include "io_pins.h"
#include "engine_configuration.h"
#include "pwm_generator_logic.h"
#include "pid.h"
#include "engine_controller.h"
#if EFI_ELECTRONIC_THROTTLE_BODY || defined(__DOXYGEN__)
2017-01-02 11:03:17 -08:00
#include "pin_repository.h"
#include "pwm_generator.h"
2017-09-21 20:21:03 -07:00
#include "pid_auto_tune.h"
2017-05-22 12:31:03 -07:00
extern TunerStudioOutputChannels tsOutputChannels;
2017-05-27 20:01:41 -07:00
static bool shouldResetPid = false;
2015-07-10 06:01:56 -07:00
2017-09-21 20:21:03 -07:00
static PID_AutoTune autoTune;
2015-07-10 06:01:56 -07:00
static LoggingWithStorage logger("ETB");
/**
* @brief Control Thread stack
*/
static THD_WORKING_AREA(etbTreadStack, UTILITY_THREAD_STACK_SIZE);
/**
* @brief Pulse-Width Modulation state
*/
2015-07-10 17:01:36 -07:00
static SimplePwm etbPwmUp CCM_OPTIONAL;
static SimplePwm etbPwmDown CCM_OPTIONAL;
static OutputPin outputDirectionOpen CCM_OPTIONAL;
static OutputPin outputDirectionClose CCM_OPTIONAL;
2015-07-10 06:01:56 -07:00
2017-05-27 20:01:41 -07:00
EXTERN_ENGINE;
2017-05-29 20:15:07 -07:00
static Pid pid(&engineConfiguration->etb);
2015-07-10 06:01:56 -07:00
2017-11-16 11:44:34 -08:00
//static float prevTps;
2015-07-10 06:01:56 -07:00
2017-09-21 20:21:03 -07:00
static percent_t currentEtbDuty;
2015-07-10 06:01:56 -07:00
2016-01-11 14:01:33 -08:00
static bool wasEtbBraking = false;
2015-07-10 17:01:36 -07:00
2015-07-10 06:01:56 -07:00
static msg_t etbThread(void *arg) {
UNUSED(arg);
while (true) {
2017-05-27 20:01:41 -07:00
if (shouldResetPid) {
pid.reset();
// alternatorPidResetCounter++;
shouldResetPid = false;
}
2017-09-21 20:21:03 -07:00
if (engine->etbAutoTune) {
autoTune.Runtime(&logger);
etbPwmUp.setSimplePwmDutyCycle(autoTune.output);
pid.sleep();
continue;
}
2017-05-27 20:01:41 -07:00
2017-11-16 11:44:34 -08:00
percent_t targetPosition = getPedalPosition(PASS_ENGINE_PARAMETER_SIGNATURE);
2015-07-10 06:01:56 -07:00
2017-11-16 11:44:34 -08:00
percent_t actualThrottlePosition = getTPS();
currentEtbDuty = pid.getValue(targetPosition, actualThrottlePosition);
2015-07-10 06:01:56 -07:00
etbPwmUp.setSimplePwmDutyCycle(currentEtbDuty / 100);
2017-11-16 11:44:34 -08:00
bool needEtbBraking = absF(targetPosition - actualThrottlePosition) < 3;
2015-07-10 17:01:36 -07:00
if (needEtbBraking != wasEtbBraking) {
scheduleMsg(&logger, "need ETB braking: %d", needEtbBraking);
wasEtbBraking = needEtbBraking;
}
outputDirectionClose.setValue(needEtbBraking);
2017-05-22 12:31:03 -07:00
if (engineConfiguration->debugMode == DBG_ELECTRONIC_THROTTLE) {
pid.postState(&tsOutputChannels);
}
2017-05-25 05:57:03 -07:00
if (engineConfiguration->isVerboseETB) {
2017-05-29 20:15:07 -07:00
pid.showPidStatus(&logger, "ETB");
2017-05-25 05:57:03 -07:00
}
2015-07-10 06:01:56 -07:00
// if (tps != prevTps) {
// prevTps = tps;
// scheduleMsg(&logger, "tps=%d", (int) tps);
// }
// this thread is activated 10 times per second
pid.sleep();
2015-07-10 06:01:56 -07:00
}
#if defined __GNUC__
return -1;
#endif
}
static void setThrottleConsole(int level) {
scheduleMsg(&logger, "setting throttle=%d", level);
float dc = 0.01 + (minI(level, 98)) / 100.0;
etbPwmUp.setSimplePwmDutyCycle(dc);
2018-01-23 09:05:14 -08:00
print("st = %.2f\r\n", dc);
2015-07-10 06:01:56 -07:00
}
static void showEthInfo(void) {
static char pinNameBuffer[16];
2017-09-21 20:21:03 -07:00
scheduleMsg(&logger, "etbAutoTune=%d",
engine->etbAutoTune);
2018-01-23 09:05:14 -08:00
scheduleMsg(&logger, "throttlePedal=%.2f %.2f/%.2f @%s",
2017-05-29 20:36:08 -07:00
getPedalPosition(),
engineConfiguration->throttlePedalUpVoltage,
engineConfiguration->throttlePedalWOTVoltage,
getPinNameByAdcChannel("tPedal", engineConfiguration->pedalPositionChannel, pinNameBuffer));
2015-07-10 06:01:56 -07:00
2018-01-23 09:05:14 -08:00
scheduleMsg(&logger, "TPS=%.2f", getTPS());
2015-07-10 06:01:56 -07:00
2018-01-23 09:05:14 -08:00
scheduleMsg(&logger, "etbControlPin1=%s duty=%.2f freq=%d",
2017-05-29 20:36:08 -07:00
hwPortname(boardConfiguration->etbControlPin1),
currentEtbDuty,
engineConfiguration->etbFreq);
2015-07-10 17:01:36 -07:00
scheduleMsg(&logger, "close dir=%s", hwPortname(boardConfiguration->etbDirectionPin2));
2017-05-29 20:36:08 -07:00
pid.showPidStatus(&logger, "ETB");
2015-07-10 06:01:56 -07:00
}
static void apply(void) {
2015-11-11 20:01:18 -08:00
pid.updateFactors(engineConfiguration->etb.pFactor, engineConfiguration->etb.iFactor, 0);
2015-07-10 06:01:56 -07:00
}
2017-01-06 14:01:28 -08:00
void setEtbPFactor(float value) {
2015-11-11 20:01:18 -08:00
engineConfiguration->etb.pFactor = value;
2015-07-10 06:01:56 -07:00
apply();
showEthInfo();
}
2017-01-06 14:01:28 -08:00
void setEtbIFactor(float value) {
2015-11-11 20:01:18 -08:00
engineConfiguration->etb.iFactor = value;
2015-07-10 06:01:56 -07:00
apply();
showEthInfo();
}
void setDefaultEtbParameters(void) {
engineConfiguration->throttlePedalUpVoltage = 0; // that's voltage, not ADC like with TPS
engineConfiguration->throttlePedalWOTVoltage = 6; // that's voltage, not ADC like with TPS
2015-11-11 20:01:18 -08:00
engineConfiguration->etb.pFactor = 1;
engineConfiguration->etb.iFactor = 0.5;
engineConfiguration->etb.period = 100;
2017-05-29 19:08:55 -07:00
engineConfiguration->etbFreq = 300;
2015-07-10 06:01:56 -07:00
}
2017-05-30 18:56:56 -07:00
bool isETBRestartNeeded(void) {
/**
* We do not want any interruption in HW pin while adjusting other properties
*/
return engineConfiguration->bc.etbControlPin1 != activeConfiguration.bc.etbControlPin1 ||
engineConfiguration->bc.etbControlPin2 != activeConfiguration.bc.etbControlPin2 ||
engineConfiguration->bc.etbDirectionPin1 != activeConfiguration.bc.etbDirectionPin1 ||
engineConfiguration->bc.etbDirectionPin2 != activeConfiguration.bc.etbDirectionPin2;
}
2017-03-19 18:44:52 -07:00
void stopETBPins(void) {
2017-03-21 18:12:51 -07:00
unmarkPin(activeConfiguration.bc.etbControlPin1);
unmarkPin(activeConfiguration.bc.etbControlPin2);
unmarkPin(activeConfiguration.bc.etbDirectionPin1);
unmarkPin(activeConfiguration.bc.etbDirectionPin2);
2017-03-19 18:44:52 -07:00
}
2015-07-10 06:01:56 -07:00
2017-05-27 20:01:41 -07:00
void onConfigurationChangeElectronicThrottleCallback(engine_configuration_s *previousConfiguration) {
2017-05-28 11:22:43 -07:00
shouldResetPid = !pid.isSame(&previousConfiguration->etb);
2017-05-27 20:01:41 -07:00
}
2017-03-19 18:44:52 -07:00
void startETBPins(void) {
2017-05-29 19:08:55 -07:00
int freq = maxI(100, engineConfiguration->etbFreq);
2015-07-10 06:01:56 -07:00
// this line used for PWM
startSimplePwmExt(&etbPwmUp, "etb1",
boardConfiguration->etbControlPin1,
2016-09-14 16:03:00 -07:00
&enginePins.etbOutput1,
2017-05-29 19:08:55 -07:00
freq,
2015-07-10 06:01:56 -07:00
0.80,
applyPinState);
startSimplePwmExt(&etbPwmDown, "etb2",
boardConfiguration->etbControlPin2,
2016-09-14 16:03:00 -07:00
&enginePins.etbOutput2,
2017-05-29 19:08:55 -07:00
freq,
2015-07-10 06:01:56 -07:00
0.80,
applyPinState);
2017-04-21 15:11:36 -07:00
outputDirectionOpen.initPin("etb dir open", boardConfiguration->etbDirectionPin1);
outputDirectionClose.initPin("etb dir close", boardConfiguration->etbDirectionPin2);
2017-03-19 18:44:52 -07:00
}
2017-09-21 20:21:03 -07:00
static void setTempOutput(float value) {
autoTune.output = value;
}
static void setTempStep(float value) {
autoTune.oStep = value;
}
2017-03-19 18:44:52 -07:00
void initElectronicThrottle(void) {
// these two lines are controlling direction
// outputPinRegister("etb1", ELECTRONIC_THROTTLE_CONTROL_1, ETB_CONTROL_LINE_1_PORT, ETB_CONTROL_LINE_1_PIN);
// outputPinRegister("etb2", ELECTRONIC_THROTTLE_CONTROL_2, ETB_CONTROL_LINE_2_PORT, ETB_CONTROL_LINE_2_PIN);
2017-05-29 19:08:55 -07:00
addConsoleAction("ethinfo", showEthInfo);
2017-03-19 18:44:52 -07:00
if (!hasPedalPositionSensor()) {
return;
}
startETBPins();
2015-07-10 17:01:36 -07:00
2017-09-21 20:21:03 -07:00
addConsoleActionI("set_etb", setThrottleConsole);
addConsoleActionF("set_etb_output", setTempOutput);
addConsoleActionF("set_etb_step", setTempStep);
2015-07-10 06:01:56 -07:00
apply();
chThdCreateStatic(etbTreadStack, sizeof(etbTreadStack), NORMALPRIO, (tfunc_t) etbThread, NULL);
}
#endif /* EFI_ELECTRONIC_THROTTLE_BODY */