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