rusEFI
The most advanced open source ECU
trigger_structure.h
Go to the documentation of this file.
1 /**
2  * @file trigger_structure.h
3  *
4  * rusEFI defines trigger shape programmatically in C code
5  * For integration we have exportAllTriggers export
6  *
7  * @date Dec 22, 2013
8  * @author Andrey Belomutskiy, (c) 2012-2020
9  */
10 
11 #pragma once
12 
13 #include "state_sequence.h"
15 #include "engine_state.h"
16 
17 #define FOUR_STROKE_ENGINE_CYCLE 720
18 
19 #define TRIGGER_GAP_DEVIATION 0.25f
20 #define TRIGGER_GAP_DEVIATION_LOW (1.0f - TRIGGER_GAP_DEVIATION)
21 #define TRIGGER_GAP_DEVIATION_HIGH (1.0f + TRIGGER_GAP_DEVIATION)
22 
23 #if EFI_ENABLE_ASSERTS
24 #define assertAngleRange(angle, msg, code) if (angle > 10000000 || angle < -10000000) { firmwareError(code, "angle range %s %d", msg, (int)angle);angle = 0;}
25 #else
26 #define assertAngleRange(angle, msg, code) {UNUSED(code);}
27 #endif
28 
29 // Shifts angle into the [0..720) range for four stroke and [0..360) for two stroke
30 // See also wrapVvt
31 inline void wrapAngle(angle_t& angle, const char* msg, ObdCode code) {
32  if (std::isnan(angle)) {
33  firmwareError(ObdCode::CUSTOM_ERR_ANGLE, "a NaN %s", msg);
34  angle = 0;
35  }
36 
37  assertAngleRange(angle, msg, code);
38  float engineCycle = getEngineState()->engineCycle;
39 
40  while (angle < 0) {
41  angle += engineCycle;
42  }
43 
44  while (angle >= engineCycle) {
45  angle -= engineCycle;
46  }
47 }
48 
49 // proper method avoids un-wrapped state of variables
51  wrapAngle(param, msg, code);
52  return param;
53 }
54 
55 class TriggerDecoderBase;
56 class TriggerFormDetails;
58 
59 #include "sync_edge.h"
60 
61 /**
62  * @brief Trigger shape has all the fields needed to describe and decode trigger signal.
63  * @see TriggerState for trigger decoder state which works based on this trigger shape model
64  */
66 public:
69  void setShapeDefinitionError(bool value);
70 
71  /**
72  * Simplest trigger shape does not require any synchronization - for example if there is only
73  * one primary channel tooth each raising (or falling depending on configuration) front would synchronize
74  */
76 
77  /**
78  * trigger meta information: is second wheel mounted on crank shaft ('false') or cam shaft ('true')
79  */
81  /**
82  * number of consecutive trigger gaps needed to synchronize
83  */
85  /**
86  * special case for triggers which do not provide exact TDC location
87  * For example pick-up in distributor with mechanical ignition firing order control.
88  */
89  bool shapeWithoutTdc = false;
90  /**
91  * this flag tells us if we should ignore events on second input channel
92  * that's the way to ignore noise from the disconnected wire
93  */
94  bool needSecondTriggerInput = false;
95  /**
96  * true value here means that we do not have a valid trigger configuration
97  */
98  bool shapeDefinitionError = false;
99 
100  /**
101  * this variable is incremented after each trigger shape redefinition
102  */
103  int version = 0;
104 
105  /**
106  * Depending on trigger shape, we use betweeb one and three previous gap ranges to detect synchronizaiton.
107  *
108  * Usually second or third gap is not needed, but some crazy triggers like 36-2-2-2 require two consecutive
109  * gaps ratios to sync
110  */
111 
112  float synchronizationRatioFrom[GAP_TRACKING_LENGTH];
113  float synchronizationRatioTo[GAP_TRACKING_LENGTH];
114 
115 
116  /**
117  * used by NoiselessTriggerDecoder (See TriggerCentral::handleShaftSignal())
118  */
120 
121 
122  /**
123  * Trigger indexes within trigger cycle are counted from synchronization point, and all
124  * engine processes are defined in angles from TDC.
125  *
126  * That's the angle distance from trigger event #0 and actual engine TDC
127  *
128  * see also globalTriggerAngleOffset
129  */
131 
132  /**
133  * In case of a multi-channel trigger, do we want to sync based on primary channel only?
134  * See also gapBothDirections
135  */
137 
138  // Which edge(s) to consider for finding the sync point: rise, fall, or both
140 
141  // If true, falling edges should be fully ignored on this trigger shape.
143 
145 
146  size_t getExpectedEventCount(TriggerWheel channelIndex) const;
147 
148  /**
149  * This is used for signal validation
150  */
151  size_t expectedEventCount[PWM_PHASE_MAX_WAVE_PER_PWM];
152 
153 #if EFI_UNIT_TEST
154  /**
155  * These signals are used for trigger export only
156  */
157  TriggerWheel triggerSignalIndeces[PWM_PHASE_MAX_COUNT];
158  TriggerValue triggerSignalStates[PWM_PHASE_MAX_COUNT];
159  // see also 'doesTriggerImplyOperationMode'
160  // todo: reuse doesTriggerImplyOperationMode instead of separate field only which is only used for metadata anyway?
161  bool knownOperationMode = true;
162 #endif
163 
164  /**
165  * wave.phaseCount is total count of shaft events per CAM or CRANK shaft revolution.
166  * TODO this should be migrated to CRANKshaft revolution, this would go together
167  * this variable is public for performance reasons (I want to avoid costs of method if it's not inlined)
168  * but name is supposed to hint at the fact that decoders should not be assigning to it
169  * Please use "getSize()" function to read this value
170  */
172 
173  bool isRiseEvent[PWM_PHASE_MAX_COUNT];
174 
175  /**
176  * @param angle (0..1]
177  */
178  void addEvent(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex = TriggerWheel::T_PRIMARY);
179  /* (0..720] angle range
180  * Deprecated! many usages should be replaced by addEvent360
181  */
182  void addEvent720(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex = TriggerWheel::T_PRIMARY);
183 
184  /**
185  * this method helps us use real world 360 degrees shape for FOUR_STROKE_CAM_SENSOR and FOUR_STROKE_CRANK_SENSOR
186  */
187  void addEvent360(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex = TriggerWheel::T_PRIMARY);
188 
189  void addToothRiseFall(angle_t angle, angle_t width = 10, TriggerWheel const channelIndex = TriggerWheel::T_PRIMARY);
190 
191  /**
192  * This version of the method is best when same wheel could be mounted either on crank or cam
193  *
194  * This version of 'addEvent...' family considers the angle duration of operationMode in this trigger
195  * For example, (0..180] for FOUR_STROKE_SYMMETRICAL_CRANK_SENSOR
196  *
197  * TODO: one day kill all usages with FOUR_STROKE_CAM_SENSOR 720 cycle and add runtime prohibition
198  * TODO: for FOUR_STROKE_CAM_SENSOR addEvent360 is the way to go
199  *
200  * @param angle (0..360] or (0..720] depending on configuration
201  */
202  void addEventAngle(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex = TriggerWheel::T_PRIMARY);
203 
204  /* (0..720] angle range
205  * Deprecated?
206  */
207  void addEventClamped(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex, float filterLeft, float filterRight);
209 
211  void setTriggerSynchronizationGap(float syncRatio);
212  void setTriggerSynchronizationGap3(int index, float syncRatioFrom, float syncRatioTo);
213  void setTriggerSynchronizationGap2(float syncRatioFrom, float syncRatioTo);
214  void setSecondTriggerSynchronizationGap(float syncRatio);
215  void setSecondTriggerSynchronizationGap2(float syncRatioFrom, float syncRatioTo);
216  void setThirdTriggerSynchronizationGap(float syncRatio);
217  /**
218  * this one is per CRANKshaft revolution
219  */
220  size_t getLength() const;
221  size_t getSize() const;
222 
224 
225  /**
226  * This private method should only be used to prepare the array of pre-calculated values
227  * See eventAngles array
228  */
229  angle_t getAngle(int phaseIndex) const;
230 
231  angle_t getCycleDuration() const;
232 
233  // Returns true if this trigger alone can fully sync the current engine for sequential mode.
234  bool needsDisambiguation() const;
235 
236  /**
237  * index of synchronization event within TriggerWaveform
238  * See findTriggerZeroEventIndex()
239  */
241 
242  void initializeSyncPoint(
244  const TriggerConfiguration& triggerConfiguration
245  );
246 
247  uint16_t findAngleIndex(TriggerFormDetails *details, angle_t angle) const;
248 
249 private:
250  /**
251  * These angles are in trigger DESCRIPTION coordinates - i.e. the way you add events while declaring trigger shape
252  */
253  angle_t getSwitchAngle(int index) const;
254 
255  /**
256  * This variable is used to confirm that events are added in the right order.
257  * todo: this variable is probably not needed, could be reimplemented by accessing by index
258  */
260  /**
261  * this is part of performance optimization
262  */
264 };
265 
266 /**
267  * Misc values calculated from TriggerWaveform
268  */
270 public:
271  void prepareEventAngles(TriggerWaveform *shape);
272 
273  /**
274  * These angles are in event coordinates - with synchronization point located at angle zero.
275  * These values are pre-calculated for performance reasons.
276  */
277  angle_t eventAngles[2 * PWM_PHASE_MAX_COUNT];
278 };
uint8_t code
Definition: bluetooth.cpp:40
angle_t engineCycle
Definition: engine_state.h:27
angle_t eventAngles[2 *PWM_PHASE_MAX_COUNT]
void prepareEventAngles(TriggerWaveform *shape)
Trigger shape has all the fields needed to describe and decode trigger signal.
void setShapeDefinitionError(bool value)
int getTriggerWaveformSynchPointIndex() const
void initialize(operation_mode_e operationMode, SyncEdge syncEdge)
bool isRiseEvent[PWM_PHASE_MAX_COUNT]
void setSecondTriggerSynchronizationGap(float syncRatio)
bool needsDisambiguation() const
angle_t getCycleDuration() const
void setTriggerSynchronizationGap(float syncRatio)
void initializeSyncPoint(TriggerDecoderBase &state, const TriggerConfiguration &triggerConfiguration)
void addToothRiseFall(angle_t angle, angle_t width=10, TriggerWheel const channelIndex=TriggerWheel::T_PRIMARY)
void addEventAngle(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex=TriggerWheel::T_PRIMARY)
uint16_t findAngleIndex(TriggerFormDetails *details, angle_t angle) const
float synchronizationRatioFrom[GAP_TRACKING_LENGTH]
void initializeTriggerWaveform(operation_mode_e triggerOperationMode, const trigger_config_s &triggerType)
TriggerValue triggerSignalStates[PWM_PHASE_MAX_COUNT]
size_t getLength() const
void addEvent720(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex=TriggerWheel::T_PRIMARY)
float synchronizationRatioTo[GAP_TRACKING_LENGTH]
void addEvent360(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex=TriggerWheel::T_PRIMARY)
void setSecondTriggerSynchronizationGap2(float syncRatioFrom, float syncRatioTo)
void setThirdTriggerSynchronizationGap(float syncRatio)
operation_mode_e getWheelOperationMode() const
angle_t getAngle(int phaseIndex) const
angle_t getSwitchAngle(int index) const
void setTriggerSynchronizationGap2(float syncRatioFrom, float syncRatioTo)
void setTriggerSynchronizationGap3(int index, float syncRatioFrom, float syncRatioTo)
operation_mode_e operationMode
MultiChannelStateSequenceWithData< PWM_PHASE_MAX_COUNT > wave
TriggerWheel triggerSignalIndeces[PWM_PHASE_MAX_COUNT]
size_t getExpectedEventCount(TriggerWheel channelIndex) const
size_t getSize() const
void addEvent(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex=TriggerWheel::T_PRIMARY)
void calculateExpectedEventCounts()
void addEventClamped(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex, float filterLeft, float filterRight)
size_t expectedEventCount[PWM_PHASE_MAX_WAVE_PER_PWM]
EngineState * getEngineState()
Definition: engine.cpp:596
One header which acts as gateway to current engine state.
void firmwareError(ObdCode code, const char *fmt,...)
ObdCode
@ CUSTOM_ERR_ANGLE
@ OBD_PCM_Processor_Fault
operation_mode_e
Definition: rusefi_enums.h:251
TriggerWheel
Definition: rusefi_enums.h:47
float angle_t
Definition: rusefi_types.h:59
static ScState state
TriggerValue
SyncEdge
Definition: sync_edge.h:3
triggerType
angle_t wrapAngleMethod(angle_t param, const char *msg="", ObdCode code=ObdCode::OBD_PCM_Processor_Fault)
void wrapAngle(angle_t &angle, const char *msg, ObdCode code)
static tstrWifiInitParam param