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