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#include <functional>
66
67#ifndef EFI_BOOTLOADER
69#endif
70
71static_assert(MAX_CYLINDER_COUNT > 0);
72
73#ifndef EFI_UNIT_TEST
74#error EFI_UNIT_TEST must be defined!
75#endif
76
77#ifndef EFI_SIMULATOR
78#error EFI_SIMULATOR must be defined!
79#endif
80
81#ifndef EFI_PROD_CODE
82#error EFI_PROD_CODE must be defined!
83#endif
84
85struct AirmassModelBase;
86
87#define MAF_DECODING_CACHE_SIZE 256
88
89#define MAF_DECODING_CACHE_MULT (MAF_DECODING_CACHE_SIZE / 5.0)
90
91/**
92 * I am not sure if this needs to be configurable.
93 *
94 * Also technically the whole feature might be implemented as cranking fuel coefficient curve by TPS.
95 */
96// todo: not great location for these
97#define CLEANUP_MODE_TPS 90
98#define STEPPER_PARKING_TPS CLEANUP_MODE_TPS
99
100class IEtbController;
101
102class Engine final : public TriggerStateListener {
103public:
104 Engine();
105
107
108
110
111 /**
112 * Sometimes for instance during shutdown we need to completely supress CAN TX
113 */
114 bool allowCanTx = true;
115
116 // used by HW CI
117 bool isPwmEnabled = true;
118
119 /**
120 * ELM327 cannot handle both RX and TX at the same time, we have to stay quite once first ISO/TP packet was detected
121 * 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
122 */
124
125#if EFI_ELECTRONIC_THROTTLE_BODY
126 IEtbController *etbControllers[ETB_COUNT] = {nullptr};
127#endif // EFI_ELECTRONIC_THROTTLE_BODY
128
129
130#if EFI_DYNO_VIEW
132#endif
133
134#if EFI_IDLE_CONTROL
136#endif // EFI_IDLE_CONTROL
137
138#if EFI_ENGINE_CONTROL
140#endif // EFI_ENGINE_CONTROL
141
142 type_list<
145#if EFI_IDLE_CONTROL
147#endif // EFI_IDLE_CONTROL
148
150#if EFI_HPFP && EFI_ENGINE_CONTROL
152#endif // EFI_HPFP && EFI_ENGINE_CONTROL
153#if EFI_ENGINE_CONTROL
155#endif // EFI_ENGINE_CONTROL
156#if EFI_ALTERNATOR_CONTROL
158#endif /* EFI_ALTERNATOR_CONTROL */
164#if EFI_HD_ACR
165 HarleyAcr,
166#endif // EFI_HD_ACR
170#if EFI_ENGINE_CONTROL
172#endif // EFI_ENGINE_CONTROL
173#if EFI_VVT_PID
178#endif // EFI_VVT_PID
179#if EFI_BOOST_CONTROL
181#endif // EFI_BOOST_CONTROL
183#if EFI_LAUNCH_CONTROL
185#endif // EFI_LAUNCH_CONTROL
186#if EFI_LTFT_CONTROL
188#endif
190
192
193 EngineModule // dummy placeholder so the previous entries can all have commas
195
196 /**
197 * Slightly shorter helper function to keep the code looking clean.
198 */
199 template<typename get_t>
200 constexpr auto & module() {
201 return engineModules.get<get_t>();
202 }
203
204 template<typename get_t>
205 constexpr auto const & module() const {
206 return engineModules.get<get_t>();
207 }
208
209#if EFI_TCU
211#endif
212
213 // todo: boolean sensors should leverage sensor framework #6342
217 SimpleSwitchedState luaDigitalInputState[LUA_DIGITAL_INPUT_COUNT]{};
218
219#if EFI_LAUNCH_CONTROL
223 // technically not directly related to EFI_LAUNCH_CONTROL since useful for TCU
225#endif // EFI_LAUNCH_CONTROL
226
227#if EFI_ANTILAG_SYSTEM
229#endif // EFI_ANTILAG_SYSTEM
230
231#if EFI_ANTILAG_SYSTEM
232// SoftSparkLimiter ALSsoftSparkLimiter{false};
233#endif /* EFI_ANTILAG_SYSTEM */
234
235#if EFI_SHAFT_POSITION_INPUT
237#endif // EFI_ENGINE_CONTROL
238
240 void resetLua();
241
242#if EFI_SHAFT_POSITION_INPUT
243 void OnTriggerStateProperState(efitick_t nowNt, size_t triggerStateIndex) override;
244 void OnTriggerSynchronization(bool wasSynchronized, bool isDecodingError) override;
245 void OnTriggerSynchronizationLost() override;
248#endif
249
250 void setConfig();
251
252#if EFI_AUX_VALVES
253 AuxActor auxValves[AUX_DIGITAL_VALVE_COUNT][2]{};
254#endif // EFI_AUX_VALVES
255
256#if EFI_UNIT_TEST
257 bool needTdcCallback = true;
258private:
260public:
263#endif /* EFI_UNIT_TEST */
264
266
267 // a pointer with interface type would make this code nicer but would carry extra runtime
268 // cost to resolve pointer, we use instances as a micro optimization
269#if EFI_SIGNAL_EXECUTOR_ONE_TIMER
270 // while theoretically PROD could be using EFI_SIGNAL_EXECUTOR_SLEEP, as of 2024 all PROD uses SingleTimerExecutor
272#endif
273#if EFI_SIGNAL_EXECUTOR_SLEEP
274 // at the moment this one is used exclusively by x86 simulator it should theoretically be possible to make it available in embedded if needed
275 SleepExecutor scheduler{};
276#endif
277#if EFI_UNIT_TEST
278 TestExecutor scheduler{};
279
280 std::function<void(IgnitionEvent*, bool)> onIgnitionEvent;
281 std::function<void(const IgnitionEvent&, efitick_t, angle_t, efitick_t)> onScheduleTurnSparkPinHighStartCharging
282 = [](const IgnitionEvent&, efitick_t, angle_t, efitick_t) -> void {};
284 = [](const IgnitionEvent&, efitick_t) -> void {};
285#endif // EFI_UNIT_TEST
286
287#if EFI_ENGINE_CONTROL
291 OneCylinder cylinders[MAX_CYLINDER_COUNT]{};
292#endif /* EFI_ENGINE_CONTROL */
293
294#if EFI_ELECTRONIC_THROTTLE_BODY
295 // todo: move to electronic_throttle something?
296 bool etbAutoTune = false;
298#endif // EFI_ELECTRONIC_THROTTLE_BODY
299
300#if EFI_UNIT_TEST
301 bool tdcMarkEnabled = true;
302#endif // EFI_UNIT_TEST
303
305
307
310
311 /**
312 * This counter is incremented every time user adjusts ECU parameters online (either via rusEfi console or other
313 * tuning software)
314 */
316
317#if EFI_SHAFT_POSITION_INPUT
319#endif // EFI_SHAFT_POSITION_INPUT
320
321 /**
322 * See FAST_CALLBACK_PERIOD_MS
323 */
325 /**
326 * See SLOW_CALLBACK_PERIOD_MS
327 */
329 void onEngineStopped();
330 void updateSlowSensors();
331 void updateSwitchInputs();
333
334 bool isRunningPwmTest = false;
335
336 /**
337 * are we running any kind of functional test? this affect
338 * some areas
339 */
341
343
345
347#if EFI_SENT_SUPPORT
349#endif
350
351 efitimeus_t timeToStopIdleTest{};
352
354
355 void preCalculate();
356
357 void efiWatchdog();
358 void onEngineHasStopped();
359
360 /**
361 * Needed by EFI_MAIN_RELAY_CONTROL to shut down the engine correctly.
362 * This method cancels shutdown if the ignition voltage is detected.
363 */
364 void checkShutdown();
365
366 /**
367 * Allows to finish some long-term shutdown procedures (stepper motor parking etc.)
368 Called when the ignition switch is turned off (vBatt is too low).
369 Returns true if some operations are in progress on background.
370 */
371 bool isInShutdownMode() const;
372
373 /**
374 * The stepper does not work if the main relay is turned off (it requires +12V).
375 * Needed by the stepper motor code to detect if it works.
376 */
377 bool isMainRelayEnabled() const;
378
379 void onSparkFireKnockSense(uint8_t cylinderIndex, efitick_t nowNt);
380
381#if EFI_UNIT_TEST
383#endif
384
385private:
386 void reset();
387
389};
390
392
395
396void scheduleReboot();
397bool isLockedFromUser();
398void unlockEcu(int password);
399
400// These externs aren't needed for unit tests - everything is injected instead
401#if !EFI_UNIT_TEST
402extern Engine ___engine;
403static constexpr Engine* engine_ptr = &___engine;
404
406 constexpr Engine* operator->() { return engine_ptr; }
407 constexpr Engine const* operator->() const { return engine_ptr; }
408 constexpr operator Engine*() { return engine_ptr; }
409 constexpr operator Engine const*() const { return engine_ptr; }
410 constexpr operator bool() const { return true; }
411};
412
413[[maybe_unused]] static EngineAccessor engine;
414
415#else // EFI_UNIT_TEST
416extern Engine* engine;
417#endif // EFI_UNIT_TEST
Acceleration enrichment calculator.
alternator controller
FuelComputer fuelComputer
Definition engine.h:139
bool pauseCANdueToSerial
Definition engine.h:123
TriggerCentral triggerCentral
Definition engine.h:318
void resetLua()
Definition engine.cpp:301
void updateSlowSensors()
Definition engine.cpp:208
dc_motors_s dc_motors
Definition engine.h:346
int getGlobalConfigurationVersion() const
Definition engine.cpp:289
SoftSparkLimiter softSparkLimiter
Definition engine.h:222
bool etbAutoTune
Definition engine.h:296
bool isFunctionalTestMode
Definition engine.h:340
GearControllerBase * gearController
Definition engine.h:210
bool needTdcCallback
Definition engine.h:257
bool etbIgnoreJamProtection
Definition engine.h:297
FuelSchedule injectionEvents
Definition engine.h:288
LongTermIdleTrim m_ltit
Definition engine.h:135
IgnitionEventList ignitionEvents
Definition engine.h:289
void onEngineHasStopped()
Definition engine.cpp:455
IgnitionState ignitionState
Definition engine.h:239
void periodicFastCallback()
Definition engine.cpp:556
void onEngineStopped()
Definition engine.cpp:569
scheduling_s tdcScheduler[2]
Definition engine.h:290
bool allowCanTx
Definition engine.h:114
void checkShutdown()
Definition engine.cpp:468
void preCalculate()
Definition engine.cpp:324
sent_state_s sent_state
Definition engine.h:348
AuxActor auxValves[AUX_DIGITAL_VALVE_COUNT][2]
Definition engine.h:253
bool slowCallBackWasInvoked
Definition engine.h:304
SingleTimerExecutor scheduler
Definition engine.h:271
bool isInShutdownMode() const
Definition engine.cpp:502
void efiWatchdog()
Definition engine.cpp:427
bool isMainRelayEnabled() const
Definition engine.cpp:539
LaunchControlBase launchController
Definition engine.h:220
void incrementBailedOnDwellCount()
Definition engine.h:262
EngineState engineState
Definition engine.h:344
void periodicSlowCallback()
Definition engine.cpp:150
SwitchedState brakePedalSwitchedState
Definition engine.h:215
int bailedOnDwellCount
Definition engine.h:259
int getBailedOnDwellCount() const
Definition engine.h:261
bool tdcMarkEnabled
Definition engine.h:301
RpmCalculator rpmCalculator
Definition engine.h:306
void resetEngineSnifferIfInTestMode()
Definition engine.cpp:53
TriggerStateListener * nextListener() override
Definition engine.cpp:341
SwitchedState clutchUpSwitchedState
Definition engine.h:214
Engine()
Definition engine.cpp:283
int globalConfigurationVersion
Definition engine.h:315
AntilagSystemBase antilagController
Definition engine.h:228
SwitchedState acButtonSwitchedState
Definition engine.h:216
void setConfig()
Definition engine.cpp:397
constexpr auto const & module() const
Definition engine.h:205
LambdaMonitor lambdaMonitor
Definition engine.h:236
void OnTriggerSynchronization(bool wasSynchronized, bool isDecodingError) override
Definition engine.cpp:359
void injectEngineReferences()
Definition engine.cpp:388
TriggerStateListener * secondListener
Definition engine.h:247
IEtbController * etbControllers[ETB_COUNT]
Definition engine.h:126
Timer engineTypeChangeTimer
Definition engine.h:309
std::function< void(IgnitionEvent *, bool)> onIgnitionEvent
Definition engine.h:280
efitimeus_t timeToStopIdleTest
Definition engine.h:351
DynoView dynoInstance
Definition engine.h:131
void onSparkFireKnockSense(uint8_t cylinderIndex, efitick_t nowNt)
SimpleSwitchedState luaDigitalInputState[LUA_DIGITAL_INPUT_COUNT]
Definition engine.h:217
TunerStudioOutputChannels outputChannels
Definition engine.h:109
void updateTriggerConfiguration()
Definition engine.cpp:133
AirmassModelBase * mockAirmassModel
Definition engine.h:382
void OnTriggerStateProperState(efitick_t nowNt, size_t triggerStateIndex) override
Definition engine.cpp:335
OneCylinder cylinders[MAX_CYLINDER_COUNT]
Definition engine.h:291
SoftSparkLimiter hardSparkLimiter
Definition engine.h:224
void updateSwitchInputs()
Definition engine.cpp:254
constexpr auto & module()
Definition engine.h:200
void reset()
Definition engine.cpp:293
ShiftTorqueReductionController shiftTorqueReductionController
Definition engine.h:221
std::function< void(const IgnitionEvent &, efitick_t)> onScheduleOverFireSparkAndPrepareNextSchedule
Definition engine.h:284
bool isPwmEnabled
Definition engine.h:117
std::function< void(const IgnitionEvent &, efitick_t, angle_t, efitick_t)> onScheduleTurnSparkPinHighStartCharging
Definition engine.h:282
void OnTriggerSynchronizationLost() override
Definition engine.cpp:345
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:194
StartStopState startStopState
Definition engine.h:106
Timer configBurnTimer
Definition engine.h:308
bool isRunningPwmTest
Definition engine.h:334
SensorsState sensors
Definition engine.h:353
void scheduleReboot()
Definition rusefi.cpp:158
static EngineAccessor engine
Definition engine.h:413
void unlockEcu(int password)
Definition engine2.cpp:319
bool isLockedFromUser()
Definition engine2.cpp:310
void prepareOutputSignals()
static constexpr Engine * engine_ptr
Definition engine.h:403
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, 1584, 1.0, -1.0, -1.0, "")
constexpr Engine const * operator->() const
Definition engine.h:407
constexpr Engine * operator->()
Definition engine.h:406