rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
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"
20#include "buttonshift.h"
21#include "gear_controller.h"
22#include "dynoview.h"
24#include "limp_manager.h"
25#include "pin_repository.h"
26#include "ac_control.h"
27#include "knock_logic.h"
30#include "closed_loop_idle.h"
31#include "dc_motors_generated.h"
32#include "idle_thread.h"
33#include "injector_model.h"
34#include "launch_control.h"
36#include "nitrous_controller.h"
37#include "antilag_system.h"
38#include "start_stop.h"
39#include "trigger_scheduler.h"
40#include "fuel_pump.h"
41#include "main_relay.h"
42#include "ac_control.h"
43#include "type_list.h"
44#include "boost_control.h"
45#include "ignition_controller.h"
47#include "harley_acr.h"
48#include "dfco.h"
49#include "fuel_computer.h"
50#include "advance_map.h"
51#include "ignition_state.h"
52#include "sensor_checker.h"
53#include "fuel_schedule.h"
54#include "prime_injection.h"
55#include "throttle_model.h"
56#include "gc_generic.h"
57#include "lambda_monitor.h"
58#include "efi_output.h"
59#include "vvt.h"
60#include "closed_loop_fuel.h"
61#include "long_term_fuel_trim.h"
63#include "engine_cylinder.hpp"
64
65#if ROTATIONAL_IDLE_CONTROLLER
66#include "rotational_idle.h"
67#endif
68
69#include <functional>
70
71#ifndef EFI_BOOTLOADER
73#endif
74
75static_assert(MAX_CYLINDER_COUNT > 0);
76
77#ifndef EFI_UNIT_TEST
78#error EFI_UNIT_TEST must be defined!
79#endif
80
81#ifndef EFI_SIMULATOR
82#error EFI_SIMULATOR must be defined!
83#endif
84
85#ifndef EFI_PROD_CODE
86#error EFI_PROD_CODE must be defined!
87#endif
88
89struct AirmassModelBase;
90
91#define MAF_DECODING_CACHE_SIZE 256
92
93#define MAF_DECODING_CACHE_MULT (MAF_DECODING_CACHE_SIZE / 5.0)
94
95/**
96 * I am not sure if this needs to be configurable.
97 *
98 * Also technically the whole feature might be implemented as cranking fuel coefficient curve by TPS.
99 */
100// todo: not great location for these
101#define CLEANUP_MODE_TPS 90
102#define STEPPER_PARKING_TPS CLEANUP_MODE_TPS
103
104class IEtbController;
105
106class Engine final : public TriggerStateListener {
107public:
108 Engine();
109
111
112
114
115 /**
116 * Sometimes for instance during shutdown we need to completely supress CAN TX
117 */
118 bool allowCanTx = true;
119
120 // used by HW CI
121 bool isPwmEnabled = true;
122
123 /**
124 * ELM327 cannot handle both RX and TX at the same time, we have to stay quite once first ISO/TP packet was detected
125 * 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
126 */
128
129#if EFI_ELECTRONIC_THROTTLE_BODY
130 IEtbController *etbControllers[ETB_COUNT] = {nullptr};
131#endif // EFI_ELECTRONIC_THROTTLE_BODY
132
133
134#if EFI_DYNO_VIEW
136#endif
137
138#if EFI_IDLE_CONTROL
140#endif // EFI_IDLE_CONTROL
141
142#if EFI_ENGINE_CONTROL
144#endif // EFI_ENGINE_CONTROL
145
146 type_list<
149#if EFI_IDLE_CONTROL
151#endif // EFI_IDLE_CONTROL
152
154#if EFI_HPFP && EFI_ENGINE_CONTROL
156#endif // EFI_HPFP && EFI_ENGINE_CONTROL
157#if EFI_ENGINE_CONTROL
159#endif // EFI_ENGINE_CONTROL
160#if EFI_ALTERNATOR_CONTROL
162#endif /* EFI_ALTERNATOR_CONTROL */
168#if EFI_HD_ACR
169 HarleyAcr,
170#endif // EFI_HD_ACR
174#if EFI_ENGINE_CONTROL
176#endif // EFI_ENGINE_CONTROL
177#if EFI_VVT_PID
182#endif // EFI_VVT_PID
183#if EFI_BOOST_CONTROL
185#endif // EFI_BOOST_CONTROL
187#if EFI_LAUNCH_CONTROL
189#endif // EFI_LAUNCH_CONTROL
190#if EFI_LTFT_CONTROL
192#endif
194
196
197 EngineModule // dummy placeholder so the previous entries can all have commas
199
200 /**
201 * Slightly shorter helper function to keep the code looking clean.
202 */
203 template<typename get_t>
204 constexpr auto & module() {
205 return engineModules.get<get_t>();
206 }
207
208 template<typename get_t>
209 constexpr auto const & module() const {
210 return engineModules.get<get_t>();
211 }
212
213#if EFI_TCU
215#endif
216
217 // todo: boolean sensors should leverage sensor framework #6342
221 SimpleSwitchedState luaDigitalInputState[LUA_DIGITAL_INPUT_COUNT]{};
222
223#if EFI_LAUNCH_CONTROL
227 // technically not directly related to EFI_LAUNCH_CONTROL since useful for TCU
229#endif // EFI_LAUNCH_CONTROL
230
231#if EFI_ANTILAG_SYSTEM
233#endif // EFI_ANTILAG_SYSTEM
234
235#if EFI_ANTILAG_SYSTEM
236// SoftSparkLimiter ALSsoftSparkLimiter{false};
237#endif /* EFI_ANTILAG_SYSTEM */
238
239#if EFI_SHAFT_POSITION_INPUT
241#endif // EFI_ENGINE_CONTROL
242
243#if ROTATIONAL_IDLE_CONTROLLER
245#endif // ROTATIONAL_IDLE_CONTROLLER
246
248 void resetLua();
249
250#if EFI_SHAFT_POSITION_INPUT
251 void OnTriggerStateProperState(efitick_t nowNt, size_t triggerStateIndex) override;
252 void OnTriggerSynchronization(bool wasSynchronized, bool isDecodingError) override;
253 void OnTriggerSynchronizationLost() override;
256#endif
257
258 void setConfig();
259
260#if EFI_AUX_VALVES
261 AuxActor auxValves[AUX_DIGITAL_VALVE_COUNT][2]{};
262#endif // EFI_AUX_VALVES
263
264#if EFI_UNIT_TEST
265 bool needTdcCallback = true;
266private:
268public:
271#endif /* EFI_UNIT_TEST */
272
274
275 // a pointer with interface type would make this code nicer but would carry extra runtime
276 // cost to resolve pointer, we use instances as a micro optimization
277#if EFI_SIGNAL_EXECUTOR_ONE_TIMER
278 // while theoretically PROD could be using EFI_SIGNAL_EXECUTOR_SLEEP, as of 2024 all PROD uses SingleTimerExecutor
280#endif
281#if EFI_SIGNAL_EXECUTOR_SLEEP
282 // at the moment this one is used exclusively by x86 simulator it should theoretically be possible to make it available in embedded if needed
283 SleepExecutor scheduler{};
284#endif
285#if EFI_UNIT_TEST
286 TestExecutor scheduler{};
287
288 std::function<void(IgnitionEvent*, bool)> onIgnitionEvent;
289 std::function<void(const IgnitionEvent&, efitick_t, angle_t, efitick_t)> onScheduleTurnSparkPinHighStartCharging
290 = [](const IgnitionEvent&, efitick_t, angle_t, efitick_t) -> void {};
292 = [](const IgnitionEvent&, efitick_t) -> void {};
293#endif // EFI_UNIT_TEST
294
295#if EFI_ENGINE_CONTROL
299 OneCylinder cylinders[MAX_CYLINDER_COUNT]{};
300#endif /* EFI_ENGINE_CONTROL */
301
302#if EFI_ELECTRONIC_THROTTLE_BODY
303 // todo: move to electronic_throttle something?
304 bool etbAutoTune = false;
306#endif // EFI_ELECTRONIC_THROTTLE_BODY
307
308#if EFI_UNIT_TEST
309 bool tdcMarkEnabled = true;
310#endif // EFI_UNIT_TEST
311
313
315
318
319 /**
320 * This counter is incremented every time user adjusts ECU parameters online (either via rusEfi console or other
321 * tuning software)
322 */
324
325#if EFI_SHAFT_POSITION_INPUT
327#endif // EFI_SHAFT_POSITION_INPUT
328
329 /**
330 * See FAST_CALLBACK_PERIOD_MS
331 */
333 /**
334 * See SLOW_CALLBACK_PERIOD_MS
335 */
337 void onEngineStopped();
338 void updateSlowSensors();
339 void updateSwitchInputs();
341
342 bool isRunningPwmTest = false;
343
344 /**
345 * are we running any kind of functional test? this affect
346 * some areas
347 */
349
351
353
355#if EFI_SENT_SUPPORT
357#endif
358
359 efitimeus_t timeToStopIdleTest{};
360
362
363 void preCalculate();
364
365 void efiWatchdog();
366 void onEngineHasStopped();
367
368 /**
369 * Needed by EFI_MAIN_RELAY_CONTROL to shut down the engine correctly.
370 * This method cancels shutdown if the ignition voltage is detected.
371 */
372 void checkShutdown();
373
374 /**
375 * Allows to finish some long-term shutdown procedures (stepper motor parking etc.)
376 Called when the ignition switch is turned off (vBatt is too low).
377 Returns true if some operations are in progress on background.
378 */
379 bool isInShutdownMode() const;
380
381 /**
382 * The stepper does not work if the main relay is turned off (it requires +12V).
383 * Needed by the stepper motor code to detect if it works.
384 */
385 bool isMainRelayEnabled() const;
386
387 void onSparkFireKnockSense(uint8_t cylinderIndex, efitick_t nowNt);
388
389#if EFI_UNIT_TEST
391#endif
392
393private:
394 void reset();
395
397};
398
400
403
404void scheduleReboot();
405bool isLockedFromUser();
406void unlockEcu(int password);
407
408// These externs aren't needed for unit tests - everything is injected instead
409#if !EFI_UNIT_TEST
410extern Engine ___engine;
411static constexpr Engine* engine_ptr = &___engine;
412
414 constexpr Engine* operator->() { return engine_ptr; }
415 constexpr Engine const* operator->() const { return engine_ptr; }
416 constexpr operator Engine*() { return engine_ptr; }
417 constexpr operator Engine const*() const { return engine_ptr; }
418 constexpr operator bool() const { return true; }
419};
420
421[[maybe_unused]] static EngineAccessor engine;
422
423#else // EFI_UNIT_TEST
424extern Engine* engine;
425#endif // EFI_UNIT_TEST
Acceleration enrichment calculator.
alternator controller
FuelComputer fuelComputer
Definition engine.h:143
bool pauseCANdueToSerial
Definition engine.h:127
TriggerCentral triggerCentral
Definition engine.h:326
void resetLua()
Definition engine.cpp:311
void updateSlowSensors()
Definition engine.cpp:218
dc_motors_s dc_motors
Definition engine.h:354
int getGlobalConfigurationVersion() const
Definition engine.cpp:299
SoftSparkLimiter softSparkLimiter
Definition engine.h:226
bool etbAutoTune
Definition engine.h:304
bool isFunctionalTestMode
Definition engine.h:348
GearControllerBase * gearController
Definition engine.h:214
bool needTdcCallback
Definition engine.h:265
bool etbIgnoreJamProtection
Definition engine.h:305
FuelSchedule injectionEvents
Definition engine.h:296
LongTermIdleTrim m_ltit
Definition engine.h:139
IgnitionEventList ignitionEvents
Definition engine.h:297
void onEngineHasStopped()
Definition engine.cpp:466
IgnitionState ignitionState
Definition engine.h:247
void periodicFastCallback()
Definition engine.cpp:567
void onEngineStopped()
Definition engine.cpp:581
scheduling_s tdcScheduler[2]
Definition engine.h:298
bool allowCanTx
Definition engine.h:118
void checkShutdown()
Definition engine.cpp:479
void preCalculate()
Definition engine.cpp:334
sent_state_s sent_state
Definition engine.h:356
AuxActor auxValves[AUX_DIGITAL_VALVE_COUNT][2]
Definition engine.h:261
bool slowCallBackWasInvoked
Definition engine.h:312
RotationalIdle rotationalIdleController
Definition engine.h:244
SingleTimerExecutor scheduler
Definition engine.h:279
bool isInShutdownMode() const
Definition engine.cpp:513
void efiWatchdog()
Definition engine.cpp:437
bool isMainRelayEnabled() const
Definition engine.cpp:550
LaunchControlBase launchController
Definition engine.h:224
void incrementBailedOnDwellCount()
Definition engine.h:270
EngineState engineState
Definition engine.h:352
void periodicSlowCallback()
Definition engine.cpp:159
SwitchedState brakePedalSwitchedState
Definition engine.h:219
int bailedOnDwellCount
Definition engine.h:267
int getBailedOnDwellCount() const
Definition engine.h:269
bool tdcMarkEnabled
Definition engine.h:309
RpmCalculator rpmCalculator
Definition engine.h:314
void resetEngineSnifferIfInTestMode()
Definition engine.cpp:53
TriggerStateListener * nextListener() override
Definition engine.cpp:351
SwitchedState clutchUpSwitchedState
Definition engine.h:218
Engine()
Definition engine.cpp:293
int globalConfigurationVersion
Definition engine.h:323
AntilagSystemBase antilagController
Definition engine.h:232
SwitchedState acButtonSwitchedState
Definition engine.h:220
void setConfig()
Definition engine.cpp:407
constexpr auto const & module() const
Definition engine.h:209
LambdaMonitor lambdaMonitor
Definition engine.h:240
void OnTriggerSynchronization(bool wasSynchronized, bool isDecodingError) override
Definition engine.cpp:369
void injectEngineReferences()
Definition engine.cpp:398
TriggerStateListener * secondListener
Definition engine.h:255
IEtbController * etbControllers[ETB_COUNT]
Definition engine.h:130
Timer engineTypeChangeTimer
Definition engine.h:317
std::function< void(IgnitionEvent *, bool)> onIgnitionEvent
Definition engine.h:288
efitimeus_t timeToStopIdleTest
Definition engine.h:359
DynoView dynoInstance
Definition engine.h:135
void onSparkFireKnockSense(uint8_t cylinderIndex, efitick_t nowNt)
SimpleSwitchedState luaDigitalInputState[LUA_DIGITAL_INPUT_COUNT]
Definition engine.h:221
TunerStudioOutputChannels outputChannels
Definition engine.h:113
void updateTriggerConfiguration()
Definition engine.cpp:133
AirmassModelBase * mockAirmassModel
Definition engine.h:390
void OnTriggerStateProperState(efitick_t nowNt, size_t triggerStateIndex) override
Definition engine.cpp:345
OneCylinder cylinders[MAX_CYLINDER_COUNT]
Definition engine.h:299
SoftSparkLimiter hardSparkLimiter
Definition engine.h:228
void updateSwitchInputs()
Definition engine.cpp:264
constexpr auto & module()
Definition engine.h:204
void reset()
Definition engine.cpp:303
ShiftTorqueReductionController shiftTorqueReductionController
Definition engine.h:225
std::function< void(const IgnitionEvent &, efitick_t)> onScheduleOverFireSparkAndPrepareNextSchedule
Definition engine.h:292
bool isPwmEnabled
Definition engine.h:121
std::function< void(const IgnitionEvent &, efitick_t, angle_t, efitick_t)> onScheduleTurnSparkPinHighStartCharging
Definition engine.h:290
void OnTriggerSynchronizationLost() override
Definition engine.cpp:355
type_list< Mockable< InjectorModelPrimary >, Mockable< InjectorModelSecondary >,#if EFI_IDLE_CONTROL Mockable< IdleController >,#endif TriggerScheduler,#if EFI_HPFP &&EFI_ENGINE_CONTROL Mockable< HpfpController >,#endif #if EFI_ENGINE_CONTROL Mockable< ThrottleModel >,#endif #if EFI_ALTERNATOR_CONTROL AlternatorController,#endif MainRelayController, Mockable< IgnitionController >, Mockable< AcController >, PrimeController, DfcoController,#if EFI_HD_ACR HarleyAcr,#endif Mockable< WallFuelController >, KnockController, SensorChecker,#if EFI_ENGINE_CONTROL Mockable< LimpManager >,#endif #if EFI_VVT_PID VvtController1, VvtController2, VvtController3, VvtController4,#endif #if EFI_BOOST_CONTROL BoostController,#endif TpsAccelEnrichment,#if EFI_LAUNCH_CONTROL NitrousController,#endif #if EFI_LTFT_CONTROL LongTermFuelTrim,#endif ShortTermFuelTrim,#include "modules_list_generated.h" EngineModule > engineModules
Definition engine.h:198
StartStopState startStopState
Definition engine.h:110
Timer configBurnTimer
Definition engine.h:316
bool isRunningPwmTest
Definition engine.h:342
SensorsState sensors
Definition engine.h:361
void scheduleReboot()
Definition rusefi.cpp:158
static EngineAccessor engine
Definition engine.h:421
void unlockEcu(int password)
Definition engine2.cpp:320
bool isLockedFromUser()
Definition engine2.cpp:311
void prepareOutputSignals()
static constexpr Engine * engine_ptr
Definition engine.h:411
void applyNonPersistentConfiguration()
trigger_type_e getVvtTriggerType(vvt_mode_e vvtMode)
Definition engine.cpp:72
Engine ___engine
One header which acts as gateway to current engine state.
trigger_type_e
Idle Valve Control thread.
I/O pin registry header.
Shaft position sensor(s) decoder header.
vvt_mode_e
float angle_t
triggerStateIndex("triggerStateIndex", SensorCategory.SENSOR_INPUTS, FieldType.INT8, 1600, 1.0, -1.0, -1.0, "")
constexpr Engine const * operator->() const
Definition engine.h:415
constexpr Engine * operator->()
Definition engine.h:414