rusefi/firmware/controllers/algo/engine.h

369 lines
8.8 KiB
C
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file engine.h
*
* @date May 21, 2014
2020-01-07 21:02:40 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*/
2019-09-19 21:17:00 -07:00
#pragma once
2015-07-10 06:01:56 -07:00
2022-09-01 08:47:56 -07:00
#include "global_shared.h"
#include "engine_module.h"
2019-05-27 16:05:59 -07:00
#include "engine_state.h"
2015-07-10 06:01:56 -07:00
#include "rpm_calculator.h"
#include "event_registry.h"
#include "table_helper.h"
#include "listener_array.h"
#include "accel_enrichment.h"
2015-09-13 14:02:44 -07:00
#include "trigger_central.h"
#include "local_version_holder.h"
#include "buttonshift.h"
#include "gear_controller.h"
#include "high_pressure_fuel_pump.h"
#include "limp_manager.h"
#include "pin_repository.h"
#include "ac_control.h"
#include "knock_logic.h"
2021-10-16 20:55:29 -07:00
#include "idle_state_generated.h"
2023-02-14 17:39:25 -08:00
#include "sent_state_generated.h"
#include "dc_motors_generated.h"
#include "idle_thread.h"
#include "injector_model.h"
2021-11-15 15:57:12 -08:00
#include "launch_control.h"
2022-12-16 20:31:51 -08:00
#include "antilag_system.h"
#include "trigger_scheduler.h"
#include "fuel_pump.h"
#include "main_relay.h"
#include "ac_control.h"
#include "type_list.h"
2021-11-24 09:59:57 -08:00
#include "boost_control.h"
#include "ignition_controller.h"
#include "alternator_controller.h"
#include "harley_acr.h"
#include "dfco.h"
#include "fuel_computer.h"
#include "gear_detector.h"
2022-04-13 13:39:59 -07:00
#include "advance_map.h"
2022-04-13 14:55:31 -07:00
#include "fan_control.h"
#include "sensor_checker.h"
#include "fuel_schedule.h"
2022-09-07 13:25:50 -07:00
#include "prime_injection.h"
#include "throttle_model.h"
#include "gc_generic.h"
2023-06-28 23:49:50 -07:00
#include "lambda_monitor.h"
#include "efi_output.h"
#include "vvt.h"
2023-09-25 13:16:17 -07:00
#include "trip_odometer.h"
2015-07-10 06:01:56 -07:00
#ifndef EFI_UNIT_TEST
#error EFI_UNIT_TEST must be defined!
#endif
#ifndef EFI_SIMULATOR
#error EFI_SIMULATOR must be defined!
#endif
#ifndef EFI_PROD_CODE
#error EFI_PROD_CODE must be defined!
#endif
2021-05-14 04:17:22 -07:00
struct AirmassModelBase;
2015-07-10 06:01:56 -07:00
#define MAF_DECODING_CACHE_SIZE 256
#define MAF_DECODING_CACHE_MULT (MAF_DECODING_CACHE_SIZE / 5.0)
/**
* I am not sure if this needs to be configurable.
*
* Also technically the whole feature might be implemented as cranking fuel coefficient curve by TPS.
*/
// todo: not great location for these
#define CLEANUP_MODE_TPS 90
#define STEPPER_PARKING_TPS CLEANUP_MODE_TPS
class IEtbController;
class Engine final : public TriggerStateListener {
2015-07-10 06:01:56 -07:00
public:
2016-12-18 07:02:38 -08:00
Engine();
// todo: technical debt: enableOverdwellProtection #3553
bool enableOverdwellProtection = true;
TunerStudioOutputChannels outputChannels;
/**
* Sometimes for instance during shutdown we need to completely supress CAN TX
*/
bool allowCanTx = true;
// used by HW CI
bool isPwmEnabled = true;
const char *prevOutputName = nullptr;
2021-12-05 18:20:54 -08:00
/**
* ELM327 cannot handle both RX and TX at the same time, we have to stay quite once first ISO/TP packet was detected
* this is a pretty temporary hack only while we are trying ELM327, long term ISO/TP and rusEFI broadcast should find a way to coexists
*/
bool pauseCANdueToSerial = false;
PinRepository pinRepository;
2022-04-16 18:23:37 -07:00
IEtbController *etbControllers[ETB_COUNT] = {nullptr};
2023-05-24 08:46:07 -07:00
#if EFI_ENGINE_CONTROL
FuelComputer fuelComputer;
2023-05-24 08:46:07 -07:00
#endif // EFI_ENGINE_CONTROL
type_list<
Mockable<InjectorModelPrimary>,
Mockable<InjectorModelSecondary>,
#if EFI_IDLE_CONTROL
Mockable<IdleController>,
#endif // EFI_IDLE_CONTROL
TriggerScheduler,
#if EFI_HPFP && EFI_ENGINE_CONTROL
HpfpController,
#endif // EFI_HPFP && EFI_ENGINE_CONTROL
2023-05-24 07:31:43 -07:00
#if EFI_ENGINE_CONTROL
Mockable<ThrottleModel>,
2023-05-24 07:31:43 -07:00
#endif // EFI_ENGINE_CONTROL
#if EFI_ALTERNATOR_CONTROL
AlternatorController,
#endif /* EFI_ALTERNATOR_CONTROL */
FuelPumpController,
MainRelayController,
IgnitionController,
Mockable<AcController>,
FanControl1,
FanControl2,
PrimeController,
DfcoController,
2023-10-31 05:56:16 -07:00
#if EFI_HD_ACR
HarleyAcr,
2023-10-31 05:56:16 -07:00
#endif // EFI_HD_ACR
Mockable<WallFuelController>,
#if EFI_VEHICLE_SPEED
GearDetector,
2023-09-25 13:16:17 -07:00
TripOdometer,
#endif // EFI_VEHICLE_SPEED
KnockController,
SensorChecker,
#if EFI_ENGINE_CONTROL
LimpManager,
#endif // EFI_ENGINE_CONTROL
#if EFI_VVT_PID
VvtController1,
VvtController2,
VvtController3,
VvtController4,
#endif // EFI_VVT_PID
#if EFI_BOOST_CONTROL
BoostController,
#endif // EFI_BOOST_CONTROL
EngineModule // dummy placeholder so the previous entries can all have commas
> engineModules;
/**
* Slightly shorter helper function to keep the code looking clean.
*/
template<typename get_t>
2023-07-01 20:41:44 -07:00
constexpr auto & module() {
return engineModules.get<get_t>();
}
#if EFI_TCU
GearControllerBase *gearController;
#endif
SwitchedState clutchUpSwitchedState;
SwitchedState brakePedalSwitchedState;
SwitchedState acButtonSwitchedState;
SimpleSwitchedState luaDigitalInputState[LUA_DIGITAL_INPUT_COUNT];
#if EFI_LAUNCH_CONTROL
2021-11-15 15:57:12 -08:00
LaunchControlBase launchController;
2021-11-16 10:15:12 -08:00
SoftSparkLimiter softSparkLimiter;
2023-05-25 09:41:11 -07:00
// technically not directly related to EFI_LAUNCH_CONTROL since useful for TCU
SoftSparkLimiter hardSparkLimiter;
2022-02-06 15:46:09 -08:00
#endif // EFI_LAUNCH_CONTROL
2022-12-16 20:31:51 -08:00
#if EFI_ANTILAG_SYSTEM
AntilagSystemBase antilagController;
#endif // EFI_ANTILAG_SYSTEM
#if EFI_ANTILAG_SYSTEM
SoftSparkLimiter ALSsoftSparkLimiter;
#endif /* EFI_ANTILAG_SYSTEM */
#if EFI_SHAFT_POSITION_INPUT
2023-06-28 23:49:50 -07:00
LambdaMonitor lambdaMonitor;
#endif // EFI_ENGINE_CONTROL
2023-06-28 23:49:50 -07:00
2022-04-13 13:39:59 -07:00
IgnitionState ignitionState;
2022-07-14 09:36:55 -07:00
void resetLua();
2022-04-13 13:39:59 -07:00
Timer startStopStateLastPush;
2020-08-23 22:21:42 -07:00
#if EFI_SHAFT_POSITION_INPUT
void OnTriggerStateProperState(efitick_t nowNt) override;
2023-09-09 16:51:09 -07:00
void OnTriggerSynchronization(bool wasSynchronized, bool isDecodingError) override;
2020-01-26 09:02:54 -08:00
void OnTriggerSynchronizationLost() override;
#endif
void setConfig();
2016-12-18 07:02:38 -08:00
LocalVersionHolder versionForConfigurationListeners;
2019-12-02 21:29:12 -08:00
AuxActor auxValves[AUX_DIGITAL_VALVE_COUNT][2];
2019-11-23 19:55:21 -08:00
2019-12-23 18:58:06 -08:00
#if EFI_UNIT_TEST
bool needTdcCallback = true;
2019-12-23 18:58:06 -08:00
#endif /* EFI_UNIT_TEST */
int getGlobalConfigurationVersion(void) const;
// a pointer with interface type would make this code nicer but would carry extra runtime
// cost to resolve pointer, we use instances as a micro optimization
#if EFI_SIGNAL_EXECUTOR_ONE_TIMER
SingleTimerExecutor executor;
#endif
#if EFI_SIGNAL_EXECUTOR_SLEEP
SleepExecutor executor;
#endif
#if EFI_UNIT_TEST
TestExecutor executor;
#endif // EFI_UNIT_TEST
2016-01-25 09:01:30 -08:00
2019-04-12 19:07:03 -07:00
#if EFI_ENGINE_CONTROL
2016-12-18 09:03:48 -08:00
FuelSchedule injectionEvents;
IgnitionEventList ignitionEvents;
scheduling_s tdcScheduler[2];
2016-12-18 09:03:48 -08:00
#endif /* EFI_ENGINE_CONTROL */
2016-02-06 07:01:34 -08:00
// todo: move to electronic_throttle something?
2019-01-19 19:31:55 -08:00
bool etbAutoTune = false;
#if EFI_UNIT_TEST
bool tdcMarkEnabled = true;
#endif // EFI_UNIT_TEST
2016-01-26 20:01:44 -08:00
bool slowCallBackWasInvoked = false;
2017-05-11 05:32:08 -07:00
2015-07-10 06:01:56 -07:00
RpmCalculator rpmCalculator;
Timer configBurnTimer;
/**
2019-05-02 14:52:48 -07:00
* This counter is incremented every time user adjusts ECU parameters online (either via rusEfi console or other
* tuning software)
*/
int globalConfigurationVersion = 0;
TpsAccelEnrichment tpsAccelEnrichment;
2015-07-10 06:01:56 -07:00
2022-04-16 14:07:57 -07:00
#if EFI_SHAFT_POSITION_INPUT
2015-09-13 14:02:44 -07:00
TriggerCentral triggerCentral;
2022-04-16 14:07:57 -07:00
#endif // EFI_SHAFT_POSITION_INPUT
2015-09-13 14:02:44 -07:00
2015-07-10 06:01:56 -07:00
float stftCorrection[STFT_BANK_COUNT] = {0};
2020-05-05 05:01:40 -07:00
2023-02-28 14:36:01 -08:00
/**
* See FAST_CALLBACK_PERIOD_MS
*/
void periodicFastCallback();
2023-02-28 14:36:01 -08:00
/**
* See SLOW_CALLBACK_PERIOD_MS
*/
void periodicSlowCallback();
void updateSlowSensors();
void updateSwitchInputs();
2022-04-02 16:27:18 -07:00
void updateTriggerWaveform();
2015-07-10 06:01:56 -07:00
2019-01-19 19:31:55 -08:00
bool isRunningPwmTest = false;
// todo: encapsulate
uint16_t currentEgtValue[EGT_CHANNEL_COUNT];
2015-07-10 06:01:56 -07:00
/**
* are we running any kind of functional test? this affect
* some areas
*/
bool isFunctionalTestMode = false;
2019-12-21 17:43:11 -08:00
void resetEngineSnifferIfInTestMode();
2015-07-10 06:01:56 -07:00
EngineState engineState;
dc_motors_s dc_motors;
2023-02-14 17:39:25 -08:00
sent_state_s sent_state;
2021-10-16 21:27:12 -07:00
/**
* idle blip is a development tool: alternator PID research for instance have benefited from a repetitive change of RPM
*/
percent_t blipIdlePosition;
efitimeus_t timeToStopBlip = 0;
efitimeus_t timeToStopIdleTest = 0;
SensorsState sensors;
2015-07-10 06:01:56 -07:00
void preCalculate();
2015-07-10 06:01:56 -07:00
2022-09-09 15:02:38 -07:00
void efiWatchdog();
2023-11-17 12:58:08 -08:00
void onEngineHasStopped();
/**
* Needed by EFI_MAIN_RELAY_CONTROL to shut down the engine correctly.
* This method cancels shutdown if the ignition voltage is detected.
*/
void checkShutdown();
/**
* Allows to finish some long-term shutdown procedures (stepper motor parking etc.)
Called when the ignition switch is turned off (vBatt is too low).
Returns true if some operations are in progress on background.
*/
bool isInShutdownMode() const;
/**
* The stepper does not work if the main relay is turned off (it requires +12V).
* Needed by the stepper motor code to detect if it works.
*/
bool isMainRelayEnabled() const;
void onSparkFireKnockSense(uint8_t cylinderIndex, efitick_t nowNt);
2022-08-25 19:34:16 -07:00
#if EFI_UNIT_TEST
2020-07-26 20:33:18 -07:00
AirmassModelBase* mockAirmassModel = nullptr;
2022-08-25 19:34:16 -07:00
#endif
2020-07-26 20:33:18 -07:00
2015-07-10 06:01:56 -07:00
private:
void reset();
void injectEngineReferences();
2015-07-10 06:01:56 -07:00
};
trigger_type_e getVvtTriggerType(vvt_mode_e vvtMode);
void applyNonPersistentConfiguration();
void prepareOutputSignals();
2018-02-03 07:48:35 -08:00
// todo: huh we also have validateConfig()?!
void validateConfiguration();
void scheduleReboot();
bool isLockedFromUser();
void unlockEcu(int password);
// These externs aren't needed for unit tests - everything is injected instead
#if !EFI_UNIT_TEST
extern Engine ___engine;
static Engine * const engine = &___engine;
#else // EFI_UNIT_TEST
extern Engine *engine;
#endif // EFI_UNIT_TEST