start/stop button improvements #6483

only: a bit of encapsulation, only preparations for now
This commit is contained in:
rusefi 2024-05-08 22:14:54 -04:00
parent 4b43cc3212
commit bb92146147
4 changed files with 31 additions and 14 deletions

View File

@ -31,6 +31,7 @@
#include "injector_model.h"
#include "launch_control.h"
#include "antilag_system.h"
#include "start_stop.h"
#include "trigger_scheduler.h"
#include "fuel_pump.h"
#include "main_relay.h"
@ -90,6 +91,8 @@ class Engine final : public TriggerStateListener {
public:
Engine();
StartStopState startStopState;
// todo: technical debt: enableOverdwellProtection #3553
bool enableOverdwellProtection = true;
@ -207,8 +210,6 @@ public:
IgnitionState ignitionState;
void resetLua();
Timer startStopStateLastPush;
#if EFI_SHAFT_POSITION_INPUT
void OnTriggerStateProperState(efitick_t nowNt) override;
void OnTriggerSynchronization(bool wasSynchronized, bool isDecodingError) override;

View File

@ -5,12 +5,11 @@
*/
#include "start_stop.h"
ButtonDebounce startStopButtonDebounce("start_button");
#include "ignition_controller.h"
void initStartStopButton() {
/* startCrankingDuration is efitimesec_t, so we need to multiply it by 1000 to get milliseconds*/
startStopButtonDebounce.init((engineConfiguration->startCrankingDuration*1000),
engine->startStopState.startStopButtonDebounce.init((engineConfiguration->startCrankingDuration*1000),
engineConfiguration->startStopButtonPin,
engineConfiguration->startStopButtonMode,
engineConfiguration->startRequestPinInverted);
@ -22,7 +21,7 @@ static void onStartStopButtonToggle() {
if (engine->rpmCalculator.isStopped()) {
bool wasStarterEngaged = enginePins.starterControl.getAndSet(1);
if (!wasStarterEngaged) {
engine->startStopStateLastPush.reset();
engine->startStopState.startStopStateLastPush.reset();
efiPrintf("Let's crank this engine for up to %d seconds via %s!",
engineConfiguration->startCrankingDuration,
hwPortname(engineConfiguration->starterControlPin));
@ -41,7 +40,7 @@ static void disengageStarterIfNeeded() {
efiPrintf("Engine runs we can disengage the starter");
}
} else {
if (engine->startStopStateLastPush.hasElapsedSec(engineConfiguration->startCrankingDuration)) {
if (engine->startStopState.startStopStateLastPush.hasElapsedSec(engineConfiguration->startCrankingDuration)) {
bool wasStarterEngaged = enginePins.starterControl.getAndSet(0);
if (wasStarterEngaged) {
efiPrintf("Cranking timeout %d seconds", engineConfiguration->startCrankingDuration);
@ -51,20 +50,28 @@ static void disengageStarterIfNeeded() {
}
void slowStartStopButtonCallback() {
if (!isIgnVoltage()) {
engine->startStopState.timeSinceIgnitionPower.reset();
// return;
} else if (engine->startStopState.isFirstTime) {
engine->startStopState.isFirstTime = false;
}
if (getTimeNowMs() < engineConfiguration->startButtonSuppressOnStartUpMs) {
// where are odd cases of start button combined with ECU power source button we do not want to crank right on start
return;
}
bool startStopState = startStopButtonDebounce.readPinEvent();
bool startStopState = engine->startStopState.startStopButtonDebounce.readPinEvent();
if (startStopState && !engine->engineState.startStopState) {
// we are here on transition from 0 to 1
// TODO: huh? looks like 'stop engine' feature is broken?! we invoke 'toggle' method under "from off to on" condition?!
onStartStopButtonToggle();
}
// todo: we shall extract start_stop.txt from engine_state.txt
engine->engineState.startStopState = startStopState;
engine->engineState.startStopPhysicalState = startStopButtonDebounce.getPhysicalState();
engine->engineState.startStopPhysicalState = engine->startStopState.startStopButtonDebounce.getPhysicalState();
bool isStarterEngaged = enginePins.starterControl.getLogicValue();

View File

@ -1,3 +1,14 @@
// file start_stop.h
#pragma once
struct StartStopState {
ButtonDebounce startStopButtonDebounce{"start_button"};
Timer timeSinceIgnitionPower;
Timer startStopStateLastPush;
bool isFirstTime = true;
};
void initStartStopButton();

View File

@ -7,21 +7,19 @@
#include "pch.h"
extern ButtonDebounce startStopButtonDebounce;
TEST(start, startStop) {
std::unordered_map<SensorType, float> sensorVals = {{ SensorType::AcceleratorPedal, 0 }};
EngineTestHelper eth(engine_type_e::PROTEUS_BMW_M73, sensorVals);
eth.moveTimeForwardAndInvokeEventsSec(1); // '0' time has special meaning for implementation so let's move forward
// pull-up means inverted value
ASSERT_TRUE(startStopButtonDebounce.readPinState());
ASSERT_TRUE(engine->startStopState.startStopButtonDebounce.readPinState());
// this is a pull-up, so 'true' on start-up
setMockState(engineConfiguration->startStopButtonPin, true);
// remember about debounce?
ASSERT_TRUE(startStopButtonDebounce.readPinState());
ASSERT_TRUE(engine->startStopState.startStopButtonDebounce.readPinState());
eth.moveTimeForwardAndInvokeEventsSec(10);
ASSERT_FALSE(startStopButtonDebounce.readPinState());
ASSERT_FALSE(engine->startStopState.startStopButtonDebounce.readPinState());
ASSERT_FALSE(efiReadPin(engineConfiguration->starterControlPin));