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
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 #endif /* EFI_UNIT_TEST */
227 
228 
229  int getGlobalConfigurationVersion(void) const;
230 
231 
232  // a pointer with interface type would make this code nicer but would carry extra runtime
233  // cost to resolve pointer, we use instances as a micro optimization
234 #if EFI_SIGNAL_EXECUTOR_ONE_TIMER
236 #endif
237 #if EFI_SIGNAL_EXECUTOR_SLEEP
238  SleepExecutor executor;
239 #endif
240 #if EFI_UNIT_TEST
241  TestExecutor executor;
242 
243  std::function<void(IgnitionEvent*, bool)> onIgnitionEvent;
244 #endif // EFI_UNIT_TEST
245 
246 #if EFI_ENGINE_CONTROL
250 #endif /* EFI_ENGINE_CONTROL */
251 
252  // todo: move to electronic_throttle something?
253  bool etbAutoTune = false;
254 
255 #if EFI_UNIT_TEST
256  bool tdcMarkEnabled = true;
257 #endif // EFI_UNIT_TEST
258 
259 
261 
263 
265 
266  /**
267  * This counter is incremented every time user adjusts ECU parameters online (either via rusEfi console or other
268  * tuning software)
269  */
271 
273 
274 #if EFI_SHAFT_POSITION_INPUT
276 #endif // EFI_SHAFT_POSITION_INPUT
277 
278 
279  float stftCorrection[STFT_BANK_COUNT] = {0};
280 
281 
282  /**
283  * See FAST_CALLBACK_PERIOD_MS
284  */
285  void periodicFastCallback();
286  /**
287  * See SLOW_CALLBACK_PERIOD_MS
288  */
289  void periodicSlowCallback();
290  void updateSlowSensors();
291  void updateSwitchInputs();
292  void updateTriggerWaveform();
293 
294  bool isRunningPwmTest = false;
295 
296  /**
297  * are we running any kind of functional test? this affect
298  * some areas
299  */
300  bool isFunctionalTestMode = false;
301 
303 
305 
308 
309  /**
310  * idle blip is a development tool: alternator PID research for instance have benefited from a repetitive change of RPM
311  */
313  efitimeus_t timeToStopBlip = 0;
314  efitimeus_t timeToStopIdleTest = 0;
315 
317 
318  void preCalculate();
319 
320  void efiWatchdog();
321  void onEngineHasStopped();
322 
323  /**
324  * Needed by EFI_MAIN_RELAY_CONTROL to shut down the engine correctly.
325  * This method cancels shutdown if the ignition voltage is detected.
326  */
327  void checkShutdown();
328 
329  /**
330  * Allows to finish some long-term shutdown procedures (stepper motor parking etc.)
331  Called when the ignition switch is turned off (vBatt is too low).
332  Returns true if some operations are in progress on background.
333  */
334  bool isInShutdownMode() const;
335 
336  /**
337  * The stepper does not work if the main relay is turned off (it requires +12V).
338  * Needed by the stepper motor code to detect if it works.
339  */
340  bool isMainRelayEnabled() const;
341 
342  void onSparkFireKnockSense(uint8_t cylinderIndex, efitick_t nowNt);
343 
344 #if EFI_UNIT_TEST
346 #endif
347 
348 private:
349  void reset();
350 
351  void injectEngineReferences();
352 };
353 
355 
357 void prepareOutputSignals();
358 
359 // todo: huh we also have validateConfig()?!
360 void validateConfiguration();
361 void scheduleReboot();
362 bool isLockedFromUser();
363 void unlockEcu(int password);
364 
365 // These externs aren't needed for unit tests - everything is injected instead
366 #if !EFI_UNIT_TEST
367 extern Engine ___engine;
368 static Engine * const engine = &___engine;
369 #else // EFI_UNIT_TEST
370 extern Engine *engine;
371 #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:275
void resetLua()
Definition: engine.cpp:312
void updateSlowSensors()
Definition: engine.cpp:215
dc_motors_s dc_motors
Definition: engine.h:306
SoftSparkLimiter softSparkLimiter
Definition: engine.h:190
bool etbAutoTune
Definition: engine.h:253
bool isFunctionalTestMode
Definition: engine.h:300
GearControllerBase * gearController
Definition: engine.h:179
bool needTdcCallback
Definition: engine.h:225
FuelSchedule injectionEvents
Definition: engine.h:247
TestExecutor executor
Definition: engine.h:241
SleepExecutor executor
Definition: engine.h:238
SoftSparkLimiter ALSsoftSparkLimiter
Definition: engine.h:200
const char * prevOutputName
Definition: engine.h:106
IgnitionEventList ignitionEvents
Definition: engine.h:248
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:279
scheduling_s tdcScheduler[2]
Definition: engine.h:249
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:307
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:313
bool slowCallBackWasInvoked
Definition: engine.h:260
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
EngineState engineState
Definition: engine.h:304
void periodicSlowCallback()
Definition: engine.cpp:142
SwitchedState brakePedalSwitchedState
Definition: engine.h:184
Timer startStopStateLastPush
Definition: engine.h:210
void OnTriggerStateProperState(efitick_t nowNt) override
Definition: engine.cpp:346
bool tdcMarkEnabled
Definition: engine.h:256
RpmCalculator rpmCalculator
Definition: engine.h:262
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:235
int globalConfigurationVersion
Definition: engine.h:270
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:312
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:243
efitimeus_t timeToStopIdleTest
Definition: engine.h:314
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:345
SoftSparkLimiter hardSparkLimiter
Definition: engine.h:192
void updateSwitchInputs()
Definition: engine.cpp:245
void reset()
Definition: engine.cpp:304
TpsAccelEnrichment tpsAccelEnrichment
Definition: engine.h:272
bool isPwmEnabled
Definition: engine.h:104
void updateTriggerWaveform()
Definition: engine.cpp:118
void OnTriggerSynchronizationLost() override
Definition: engine.cpp:350
Timer configBurnTimer
Definition: engine.h:264
bool isRunningPwmTest
Definition: engine.h:294
SensorsState sensors
Definition: engine.h:316
void scheduleReboot()
Definition: rusefi.cpp:158
static Engine *const engine
Definition: engine.h:368
void unlockEcu(int password)
Definition: engine2.cpp:265
bool isLockedFromUser()
Definition: engine2.cpp:256
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:76
constexpr auto get() -> std::enable_if_t< decltype(first)::template has< get_t >(), decltype(first.template get< get_t >())>
Definition: type_list.h:66