rusEFI
The most advanced open source ECU
engine.h
Go to the documentation of this file.
1 /**
2  * @file engine.h
3  *
4  * @date May 21, 2014
5  * @author Andrey Belomutskiy, (c) 2012-2020
6  */
7 
8 #pragma once
9 
10 #include "global_shared.h"
11 #include "engine_module.h"
12 #include "engine_state.h"
13 #include "rpm_calculator.h"
14 #include "event_registry.h"
15 #include "table_helper.h"
16 #include "listener_array.h"
17 #include "accel_enrichment.h"
18 #include "trigger_central.h"
19 #include "local_version_holder.h"
20 #include "buttonshift.h"
21 #include "gear_controller.h"
23 #include "limp_manager.h"
24 #include "pin_repository.h"
25 #include "ac_control.h"
26 #include "knock_logic.h"
27 #include "idle_state_generated.h"
28 #include "sent_state_generated.h"
29 #include "dc_motors_generated.h"
30 #include "idle_thread.h"
31 #include "injector_model.h"
32 #include "launch_control.h"
33 #include "antilag_system.h"
34 #include "start_stop.h"
35 #include "trigger_scheduler.h"
36 #include "fuel_pump.h"
37 #include "main_relay.h"
38 #include "ac_control.h"
39 #include "type_list.h"
40 #include "boost_control.h"
41 #include "ignition_controller.h"
42 #include "alternator_controller.h"
43 #include "harley_acr.h"
44 #include "dfco.h"
45 #include "fuel_computer.h"
46 #include "gear_detector.h"
47 #include "advance_map.h"
48 #include "fan_control.h"
49 #include "sensor_checker.h"
50 #include "fuel_schedule.h"
51 #include "prime_injection.h"
52 #include "throttle_model.h"
53 #include "gc_generic.h"
54 #include "lambda_monitor.h"
55 #include "efi_output.h"
56 #include "vvt.h"
57 #include "trip_odometer.h"
58 
59 #include <functional>
60 
61 #ifndef EFI_UNIT_TEST
62 #error EFI_UNIT_TEST must be defined!
63 #endif
64 
65 #ifndef EFI_SIMULATOR
66 #error EFI_SIMULATOR must be defined!
67 #endif
68 
69 #ifndef EFI_PROD_CODE
70 #error EFI_PROD_CODE must be defined!
71 #endif
72 
73 struct AirmassModelBase;
74 
75 #define MAF_DECODING_CACHE_SIZE 256
76 
77 #define MAF_DECODING_CACHE_MULT (MAF_DECODING_CACHE_SIZE / 5.0)
78 
79 /**
80  * I am not sure if this needs to be configurable.
81  *
82  * Also technically the whole feature might be implemented as cranking fuel coefficient curve by TPS.
83  */
84 // todo: not great location for these
85 #define CLEANUP_MODE_TPS 90
86 #define STEPPER_PARKING_TPS CLEANUP_MODE_TPS
87 
88 class IEtbController;
89 
90 class Engine final : public TriggerStateListener {
91 public:
92  Engine();
93 
95 
96  // todo: technical debt: enableOverdwellProtection #3553
98 
100 
101  /**
102  * Sometimes for instance during shutdown we need to completely supress CAN TX
103  */
104  bool allowCanTx = true;
105 
106  // used by HW CI
107  bool isPwmEnabled = true;
108 
109  const char *prevOutputName = nullptr;
110  /**
111  * ELM327 cannot handle both RX and TX at the same time, we have to stay quite once first ISO/TP packet was detected
112  * 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
113  */
114  bool pauseCANdueToSerial = false;
115 
117 
118  IEtbController *etbControllers[ETB_COUNT] = {nullptr};
119 
120 #if EFI_ENGINE_CONTROL
122 #endif // EFI_ENGINE_CONTROL
123 
124  type_list<
127 #if EFI_IDLE_CONTROL
129 #endif // EFI_IDLE_CONTROL
131 #if EFI_HPFP && EFI_ENGINE_CONTROL
133 #endif // EFI_HPFP && EFI_ENGINE_CONTROL
134 #if EFI_ENGINE_CONTROL
136 #endif // EFI_ENGINE_CONTROL
137 #if EFI_ALTERNATOR_CONTROL
139 #endif /* EFI_ALTERNATOR_CONTROL */
144  FanControl1,
145  FanControl2,
148 #if EFI_HD_ACR
149  HarleyAcr,
150 #endif // EFI_HD_ACR
152 #if EFI_VEHICLE_SPEED
153  GearDetector,
154  TripOdometer,
155 #endif // EFI_VEHICLE_SPEED
158 #if EFI_ENGINE_CONTROL
159  LimpManager,
160 #endif // EFI_ENGINE_CONTROL
161 #if EFI_VVT_PID
166 #endif // EFI_VVT_PID
167 #if EFI_BOOST_CONTROL
169 #endif // EFI_BOOST_CONTROL
170  EngineModule // dummy placeholder so the previous entries can all have commas
172 
173  /**
174  * Slightly shorter helper function to keep the code looking clean.
175  */
176  template<typename get_t>
177  constexpr auto & module() {
178  return engineModules.get<get_t>();
179  }
180 
181 #if EFI_TCU
183 #endif
184 
185  // todo: boolean sensors should leverage sensor framework #6342
189  SimpleSwitchedState luaDigitalInputState[LUA_DIGITAL_INPUT_COUNT];
190 
191 #if EFI_LAUNCH_CONTROL
194  // technically not directly related to EFI_LAUNCH_CONTROL since useful for TCU
196 #endif // EFI_LAUNCH_CONTROL
197 
198 #if EFI_ANTILAG_SYSTEM
200 #endif // EFI_ANTILAG_SYSTEM
201 
202 #if EFI_ANTILAG_SYSTEM
203 // SoftSparkLimiter ALSsoftSparkLimiter;
204 #endif /* EFI_ANTILAG_SYSTEM */
205 
206 #if EFI_SHAFT_POSITION_INPUT
208 #endif // EFI_ENGINE_CONTROL
209 
211  void resetLua();
212 
213 #if EFI_SHAFT_POSITION_INPUT
214  void OnTriggerStateProperState(efitick_t nowNt) override;
215  void OnTriggerSynchronization(bool wasSynchronized, bool isDecodingError) override;
216  void OnTriggerSynchronizationLost() override;
217 #endif
218 
219  void setConfig();
220 
222 
223  AuxActor auxValves[AUX_DIGITAL_VALVE_COUNT][2];
224 
225 #if EFI_UNIT_TEST
226  bool needTdcCallback = true;
227 private:
229 public:
232 #endif /* EFI_UNIT_TEST */
233 
234  int getGlobalConfigurationVersion(void) const;
235 
236 
237  // a pointer with interface type would make this code nicer but would carry extra runtime
238  // cost to resolve pointer, we use instances as a micro optimization
239 #if EFI_SIGNAL_EXECUTOR_ONE_TIMER
240  // while theoretically PROD could be using EFI_SIGNAL_EXECUTOR_SLEEP, as of 2024 all PROD uses SingleTimerExecutor
242 #endif
243 #if EFI_SIGNAL_EXECUTOR_SLEEP
244  // at the moment this one is used exclusively by x86 simulator it should theoretically be possible to make it available in embedded if needed
245  SleepExecutor executor;
246 #endif
247 #if EFI_UNIT_TEST
248  TestExecutor executor;
249 
250  std::function<void(IgnitionEvent*, bool)> onIgnitionEvent;
251  std::function<void(const IgnitionEvent&, efitick_t, angle_t, efitick_t)> onScheduleTurnSparkPinHighStartCharging
252  = [](const IgnitionEvent&, efitick_t, angle_t, efitick_t) -> void {};
253  std::function<void(const IgnitionEvent&, efitick_t)> onScheduleOverFireSparkAndPrepareNextSchedule
254  = [](const IgnitionEvent&, efitick_t) -> void {};
255 #endif // EFI_UNIT_TEST
256 
257 #if EFI_ENGINE_CONTROL
261 #endif /* EFI_ENGINE_CONTROL */
262 
263  // todo: move to electronic_throttle something?
264  bool etbAutoTune = false;
265 
266 #if EFI_UNIT_TEST
267  bool tdcMarkEnabled = true;
268 #endif // EFI_UNIT_TEST
269 
270 
272 
274 
276 
277  /**
278  * This counter is incremented every time user adjusts ECU parameters online (either via rusEfi console or other
279  * tuning software)
280  */
282 
284 
285 #if EFI_SHAFT_POSITION_INPUT
287 #endif // EFI_SHAFT_POSITION_INPUT
288 
289 
290  float stftCorrection[STFT_BANK_COUNT] = {0};
291 
292 
293  /**
294  * See FAST_CALLBACK_PERIOD_MS
295  */
296  void periodicFastCallback();
297  /**
298  * See SLOW_CALLBACK_PERIOD_MS
299  */
300  void periodicSlowCallback();
301  void updateSlowSensors();
302  void updateSwitchInputs();
303  void updateTriggerWaveform();
304 
305  bool isRunningPwmTest = false;
306 
307  /**
308  * are we running any kind of functional test? this affect
309  * some areas
310  */
311  bool isFunctionalTestMode = false;
312 
314 
316 
319 
320  /**
321  * idle blip is a development tool: alternator PID research for instance have benefited from a repetitive change of RPM
322  */
324  efitimeus_t timeToStopBlip = 0;
325  efitimeus_t timeToStopIdleTest = 0;
326 
328 
329  void preCalculate();
330 
331  void efiWatchdog();
332  void onEngineHasStopped();
333 
334  /**
335  * Needed by EFI_MAIN_RELAY_CONTROL to shut down the engine correctly.
336  * This method cancels shutdown if the ignition voltage is detected.
337  */
338  void checkShutdown();
339 
340  /**
341  * Allows to finish some long-term shutdown procedures (stepper motor parking etc.)
342  Called when the ignition switch is turned off (vBatt is too low).
343  Returns true if some operations are in progress on background.
344  */
345  bool isInShutdownMode() const;
346 
347  /**
348  * The stepper does not work if the main relay is turned off (it requires +12V).
349  * Needed by the stepper motor code to detect if it works.
350  */
351  bool isMainRelayEnabled() const;
352 
353  void onSparkFireKnockSense(uint8_t cylinderIndex, efitick_t nowNt);
354 
355 #if EFI_UNIT_TEST
357 #endif
358 
359 private:
360  void reset();
361 
362  void injectEngineReferences();
363 };
364 
366 
368 void prepareOutputSignals();
369 
370 // todo: huh we also have validateConfig()?!
371 void validateConfiguration();
372 void scheduleReboot();
373 bool isLockedFromUser();
374 void unlockEcu(int password);
375 
376 // These externs aren't needed for unit tests - everything is injected instead
377 #if !EFI_UNIT_TEST
378 extern Engine ___engine;
379 static Engine * const engine = &___engine;
380 #else // EFI_UNIT_TEST
381 extern Engine *engine;
382 #endif // EFI_UNIT_TEST
Acceleration enrichment calculator.
alternator controller
Definition: engine.h:90
FuelComputer fuelComputer
Definition: engine.h:121
bool pauseCANdueToSerial
Definition: engine.h:114
TriggerCentral triggerCentral
Definition: engine.h:286
void resetLua()
Definition: engine.cpp:312
void updateSlowSensors()
Definition: engine.cpp:215
dc_motors_s dc_motors
Definition: engine.h:317
SoftSparkLimiter softSparkLimiter
Definition: engine.h:193
bool etbAutoTune
Definition: engine.h:264
bool isFunctionalTestMode
Definition: engine.h:311
GearControllerBase * gearController
Definition: engine.h:182
bool needTdcCallback
Definition: engine.h:226
FuelSchedule injectionEvents
Definition: engine.h:258
TestExecutor executor
Definition: engine.h:248
SleepExecutor executor
Definition: engine.h:245
const char * prevOutputName
Definition: engine.h:109
IgnitionEventList ignitionEvents
Definition: engine.h:259
void onEngineHasStopped()
Definition: engine.cpp:456
IgnitionState ignitionState
Definition: engine.h:210
void periodicFastCallback()
Definition: engine.cpp:557
float stftCorrection[STFT_BANK_COUNT]
Definition: engine.h:290
scheduling_s tdcScheduler[2]
Definition: engine.h:260
bool allowCanTx
Definition: engine.h:104
void checkShutdown()
Definition: engine.cpp:469
void preCalculate()
Definition: engine.cpp:333
sent_state_s sent_state
Definition: engine.h:318
AuxActor auxValves[AUX_DIGITAL_VALVE_COUNT][2]
Definition: engine.h:223
int getGlobalConfigurationVersion(void) const
Definition: engine.cpp:300
LocalVersionHolder versionForConfigurationListeners
Definition: engine.h:221
bool enableOverdwellProtection
Definition: engine.h:97
efitimeus_t timeToStopBlip
Definition: engine.h:324
bool slowCallBackWasInvoked
Definition: engine.h:271
bool isInShutdownMode() const
Definition: engine.cpp:503
void efiWatchdog()
Definition: engine.cpp:428
bool isMainRelayEnabled() const
Definition: engine.cpp:540
LaunchControlBase launchController
Definition: engine.h:192
void incrementBailedOnDwellCount()
Definition: engine.h:231
EngineState engineState
Definition: engine.h:315
void periodicSlowCallback()
Definition: engine.cpp:142
SwitchedState brakePedalSwitchedState
Definition: engine.h:187
int bailedOnDwellCount
Definition: engine.h:228
void OnTriggerStateProperState(efitick_t nowNt) override
Definition: engine.cpp:346
int getBailedOnDwellCount() const
Definition: engine.h:230
bool tdcMarkEnabled
Definition: engine.h:267
RpmCalculator rpmCalculator
Definition: engine.h:273
void resetEngineSnifferIfInTestMode()
Definition: engine.cpp:56
SwitchedState clutchUpSwitchedState
Definition: engine.h:186
Engine()
Definition: engine.cpp:282
constexpr auto & module()
Definition: engine.h:177
SingleTimerExecutor executor
Definition: engine.h:241
int globalConfigurationVersion
Definition: engine.h:281
AntilagSystemBase antilagController
Definition: engine.h:199
SwitchedState acButtonSwitchedState
Definition: engine.h:188
void setConfig()
Definition: engine.cpp:402
LambdaMonitor lambdaMonitor
Definition: engine.h:207
void OnTriggerSynchronization(bool wasSynchronized, bool isDecodingError) override
Definition: engine.cpp:364
void injectEngineReferences()
Definition: engine.cpp:393
IEtbController * etbControllers[ETB_COUNT]
Definition: engine.h:118
percent_t blipIdlePosition
Definition: engine.h:323
type_list< Mockable< InjectorModelPrimary >, Mockable< InjectorModelSecondary >,#if EFI_IDLE_CONTROL Mockable< IdleController >,#endif TriggerScheduler,#if EFI_HPFP &&EFI_ENGINE_CONTROL HpfpController,#endif #if EFI_ENGINE_CONTROL Mockable< ThrottleModel >,#endif #if EFI_ALTERNATOR_CONTROL AlternatorController,#endif FuelPumpController, MainRelayController, IgnitionController, Mockable< AcController >, FanControl1, FanControl2, PrimeController, DfcoController,#if EFI_HD_ACR HarleyAcr,#endif Mockable< WallFuelController >,#if EFI_VEHICLE_SPEED GearDetector, TripOdometer,#endif KnockController, SensorChecker,#if EFI_ENGINE_CONTROL LimpManager,#endif #if EFI_VVT_PID VvtController1, VvtController2, VvtController3, VvtController4,#endif #if EFI_BOOST_CONTROL BoostController,#endif EngineModule > engineModules
Definition: engine.h:171
std::function< void(IgnitionEvent *, bool)> onIgnitionEvent
Definition: engine.h:250
efitimeus_t timeToStopIdleTest
Definition: engine.h:325
void onSparkFireKnockSense(uint8_t cylinderIndex, efitick_t nowNt)
SimpleSwitchedState luaDigitalInputState[LUA_DIGITAL_INPUT_COUNT]
Definition: engine.h:189
TunerStudioOutputChannels outputChannels
Definition: engine.h:99
PinRepository pinRepository
Definition: engine.h:116
AirmassModelBase * mockAirmassModel
Definition: engine.h:356
SoftSparkLimiter hardSparkLimiter
Definition: engine.h:195
void updateSwitchInputs()
Definition: engine.cpp:245
void reset()
Definition: engine.cpp:304
std::function< void(const IgnitionEvent &, efitick_t)> onScheduleOverFireSparkAndPrepareNextSchedule
Definition: engine.h:254
TpsAccelEnrichment tpsAccelEnrichment
Definition: engine.h:283
bool isPwmEnabled
Definition: engine.h:107
void updateTriggerWaveform()
Definition: engine.cpp:118
std::function< void(const IgnitionEvent &, efitick_t, angle_t, efitick_t)> onScheduleTurnSparkPinHighStartCharging
Definition: engine.h:252
void OnTriggerSynchronizationLost() override
Definition: engine.cpp:350
StartStopState startStopState
Definition: engine.h:94
Timer configBurnTimer
Definition: engine.h:275
bool isRunningPwmTest
Definition: engine.h:305
SensorsState sensors
Definition: engine.h:327
void scheduleReboot()
Definition: rusefi.cpp:158
static Engine *const engine
Definition: engine.h:379
void unlockEcu(int password)
Definition: engine2.cpp:264
bool isLockedFromUser()
Definition: engine2.cpp:255
void validateConfiguration()
void prepareOutputSignals()
void applyNonPersistentConfiguration()
trigger_type_e getVvtTriggerType(vvt_mode_e vvtMode)
Definition: engine.cpp:69
Engine ___engine
One header which acts as gateway to current engine state.
trigger_type_e
Definition: engine_types.h:286
Idle Valve Control thread.
I/O pin registry header.
Shaft position sensor(s) decoder header.
vvt_mode_e
Definition: rusefi_enums.h:120
float angle_t
Definition: rusefi_types.h:58
float percent_t
Definition: rusefi_types.h:73
constexpr auto get() -> std::enable_if_t< decltype(first)::template has< get_t >(), decltype(first.template get< get_t >())>
Definition: type_list.h:66