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  /**
291  * See FAST_CALLBACK_PERIOD_MS
292  */
293  void periodicFastCallback();
294  /**
295  * See SLOW_CALLBACK_PERIOD_MS
296  */
297  void periodicSlowCallback();
298  void updateSlowSensors();
299  void updateSwitchInputs();
300  void updateTriggerWaveform();
301 
302  bool isRunningPwmTest = false;
303 
304  /**
305  * are we running any kind of functional test? this affect
306  * some areas
307  */
308  bool isFunctionalTestMode = false;
309 
311 
313 
316 
317  /**
318  * idle blip is a development tool: alternator PID research for instance have benefited from a repetitive change of RPM
319  */
321  efitimeus_t timeToStopBlip = 0;
322  efitimeus_t timeToStopIdleTest = 0;
323 
325 
326  void preCalculate();
327 
328  void efiWatchdog();
329  void onEngineHasStopped();
330 
331  /**
332  * Needed by EFI_MAIN_RELAY_CONTROL to shut down the engine correctly.
333  * This method cancels shutdown if the ignition voltage is detected.
334  */
335  void checkShutdown();
336 
337  /**
338  * Allows to finish some long-term shutdown procedures (stepper motor parking etc.)
339  Called when the ignition switch is turned off (vBatt is too low).
340  Returns true if some operations are in progress on background.
341  */
342  bool isInShutdownMode() const;
343 
344  /**
345  * The stepper does not work if the main relay is turned off (it requires +12V).
346  * Needed by the stepper motor code to detect if it works.
347  */
348  bool isMainRelayEnabled() const;
349 
350  void onSparkFireKnockSense(uint8_t cylinderIndex, efitick_t nowNt);
351 
352 #if EFI_UNIT_TEST
354 #endif
355 
356 private:
357  void reset();
358 
359  void injectEngineReferences();
360 };
361 
363 
365 void prepareOutputSignals();
366 
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:90
FuelComputer fuelComputer
Definition: engine.h:121
bool pauseCANdueToSerial
Definition: engine.h:114
TriggerCentral triggerCentral
Definition: engine.h:286
void resetLua()
Definition: engine.cpp:327
void updateSlowSensors()
Definition: engine.cpp:219
dc_motors_s dc_motors
Definition: engine.h:314
SoftSparkLimiter softSparkLimiter
Definition: engine.h:193
bool etbAutoTune
Definition: engine.h:264
bool isFunctionalTestMode
Definition: engine.h:308
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:476
IgnitionState ignitionState
Definition: engine.h:210
void periodicFastCallback()
Definition: engine.cpp:577
scheduling_s tdcScheduler[2]
Definition: engine.h:260
bool allowCanTx
Definition: engine.h:104
void checkShutdown()
Definition: engine.cpp:489
void preCalculate()
Definition: engine.cpp:350
sent_state_s sent_state
Definition: engine.h:315
AuxActor auxValves[AUX_DIGITAL_VALVE_COUNT][2]
Definition: engine.h:223
int getGlobalConfigurationVersion(void) const
Definition: engine.cpp:315
LocalVersionHolder versionForConfigurationListeners
Definition: engine.h:221
bool enableOverdwellProtection
Definition: engine.h:97
efitimeus_t timeToStopBlip
Definition: engine.h:321
bool slowCallBackWasInvoked
Definition: engine.h:271
bool isInShutdownMode() const
Definition: engine.cpp:523
void efiWatchdog()
Definition: engine.cpp:448
bool isMainRelayEnabled() const
Definition: engine.cpp:560
LaunchControlBase launchController
Definition: engine.h:192
void incrementBailedOnDwellCount()
Definition: engine.h:231
EngineState engineState
Definition: engine.h:312
void periodicSlowCallback()
Definition: engine.cpp:146
SwitchedState brakePedalSwitchedState
Definition: engine.h:187
int bailedOnDwellCount
Definition: engine.h:228
void OnTriggerStateProperState(efitick_t nowNt) override
Definition: engine.cpp:363
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:297
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:419
LambdaMonitor lambdaMonitor
Definition: engine.h:207
void OnTriggerSynchronization(bool wasSynchronized, bool isDecodingError) override
Definition: engine.cpp:381
void injectEngineReferences()
Definition: engine.cpp:410
IEtbController * etbControllers[ETB_COUNT]
Definition: engine.h:118
percent_t blipIdlePosition
Definition: engine.h:320
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:322
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:353
SoftSparkLimiter hardSparkLimiter
Definition: engine.h:195
void updateSwitchInputs()
Definition: engine.cpp:262
void reset()
Definition: engine.cpp:319
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:122
std::function< void(const IgnitionEvent &, efitick_t, angle_t, efitick_t)> onScheduleTurnSparkPinHighStartCharging
Definition: engine.h:252
void OnTriggerSynchronizationLost() override
Definition: engine.cpp:367
StartStopState startStopState
Definition: engine.h:94
Timer configBurnTimer
Definition: engine.h:275
bool isRunningPwmTest
Definition: engine.h:302
SensorsState sensors
Definition: engine.h:324
void scheduleReboot()
Definition: rusefi.cpp:158
static Engine *const engine
Definition: engine.h:374
void unlockEcu(int password)
Definition: engine2.cpp:265
bool isLockedFromUser()
Definition: engine2.cpp:256
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:124
float angle_t
Definition: rusefi_types.h:59
float percent_t
Definition: rusefi_types.h:74
constexpr auto get() -> std::enable_if_t< decltype(first)::template has< get_t >(), decltype(first.template get< get_t >())>
Definition: type_list.h:66