2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file trigger_structure.h
|
|
|
|
*
|
2020-12-09 09:19:25 -08:00
|
|
|
* rusEFI defines trigger shape programmatically in C code
|
|
|
|
* For integration we have exportAllTriggers export
|
|
|
|
*
|
2015-07-10 06:01:56 -07:00
|
|
|
* @date Dec 22, 2013
|
2020-01-07 21:02:40 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2019-12-05 21:07:27 -08:00
|
|
|
#pragma once
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
#include "state_sequence.h"
|
2018-12-25 19:47:29 -08:00
|
|
|
#include "engine_configuration_generated_structures.h"
|
|
|
|
|
2019-01-01 11:05:11 -08:00
|
|
|
#define FOUR_STROKE_ENGINE_CYCLE 720
|
|
|
|
|
2020-12-04 13:24:19 -08:00
|
|
|
#define TRIGGER_GAP_DEVIATION 0.25f
|
|
|
|
#define TRIGGER_GAP_DEVIATION_LOW (1.0f - TRIGGER_GAP_DEVIATION)
|
|
|
|
#define TRIGGER_GAP_DEVIATION_HIGH (1.0f + TRIGGER_GAP_DEVIATION)
|
|
|
|
|
2018-12-25 19:47:29 -08:00
|
|
|
#if EFI_ENABLE_ASSERTS
|
2019-11-19 15:17:03 -08:00
|
|
|
#define assertAngleRange(angle, msg, code) if (angle > 10000000 || angle < -10000000) { firmwareError(code, "angle range %s %.2f", msg, angle);angle = 0;}
|
2018-12-25 19:47:29 -08:00
|
|
|
#else
|
|
|
|
#define assertAngleRange(angle, msg, code) {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Shifts angle into the [0..720) range for four stroke and [0..360) for two stroke
|
|
|
|
* I guess this implementation would be faster than 'angle % engineCycle'
|
|
|
|
*/
|
2021-12-02 14:05:47 -08:00
|
|
|
#define wrapAngle2(angle, msg, code, engineCycle) \
|
2018-12-25 19:47:29 -08:00
|
|
|
{ \
|
|
|
|
if (cisnan(angle)) { \
|
|
|
|
firmwareError(CUSTOM_ERR_ANGLE, "aNaN%s", msg); \
|
|
|
|
angle = 0; \
|
|
|
|
} \
|
|
|
|
assertAngleRange(angle, msg, code); \
|
|
|
|
float engineCycleDurationLocalCopy = engineCycle; \
|
|
|
|
/* todo: split this method into 'fixAngleUp' and 'fixAngleDown'*/ \
|
|
|
|
/* as a performance optimization?*/ \
|
2021-10-28 16:26:59 -07:00
|
|
|
while (angle < 0) \
|
|
|
|
angle += engineCycleDurationLocalCopy; \
|
|
|
|
/* todo: would 'if' work as good as 'while'? */ \
|
|
|
|
while (angle >= engineCycleDurationLocalCopy) \
|
|
|
|
angle -= engineCycleDurationLocalCopy; \
|
2018-12-25 19:47:29 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-12-18 19:03:00 -08:00
|
|
|
/**
|
2019-10-07 22:03:57 -07:00
|
|
|
* This structure defines an angle position in relation to specific tooth within trigger shape
|
2016-12-18 19:03:00 -08:00
|
|
|
*/
|
|
|
|
class event_trigger_position_s {
|
|
|
|
public:
|
2020-03-03 05:37:02 -08:00
|
|
|
size_t triggerEventIndex = 0;
|
2019-10-07 22:03:57 -07:00
|
|
|
|
|
|
|
angle_t angleOffsetFromTriggerEvent = 0;
|
2019-11-23 21:15:44 -08:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void setAngle(angle_t angle);
|
2016-12-18 19:03:00 -08:00
|
|
|
};
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
class Engine;
|
2022-05-10 01:41:39 -07:00
|
|
|
class TriggerDecoderBase;
|
2020-08-24 21:59:07 -07:00
|
|
|
class TriggerFormDetails;
|
2020-08-26 17:57:11 -07:00
|
|
|
class TriggerConfiguration;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-10-21 06:31:58 -07:00
|
|
|
|
2015-09-12 14:01:24 -07:00
|
|
|
/**
|
|
|
|
* @brief Trigger shape has all the fields needed to describe and decode trigger signal.
|
2018-02-05 14:16:34 -08:00
|
|
|
* @see TriggerState for trigger decoder state which works based on this trigger shape model
|
2015-09-12 14:01:24 -07:00
|
|
|
*/
|
2019-12-07 22:09:39 -08:00
|
|
|
class TriggerWaveform {
|
2015-07-10 06:01:56 -07:00
|
|
|
public:
|
2019-12-07 22:09:39 -08:00
|
|
|
TriggerWaveform();
|
2022-05-31 21:55:34 -07:00
|
|
|
void initializeTriggerWaveform(operation_mode_e triggerOperationMode, const TriggerConfiguration& triggerConfig);
|
2019-08-07 19:02:08 -07:00
|
|
|
void setShapeDefinitionError(bool value);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-01-13 21:20:50 -08:00
|
|
|
/**
|
|
|
|
* Simplest trigger shape does not require any synchronization - for example if there is only
|
2019-05-10 19:43:03 -07:00
|
|
|
* one primary channel tooth each raising (or falling depending on configuration) front would synchronize
|
2019-01-13 21:20:50 -08:00
|
|
|
*/
|
2016-01-11 14:01:33 -08:00
|
|
|
bool isSynchronizationNeeded;
|
2022-04-01 18:10:08 -07:00
|
|
|
|
2022-04-02 12:30:37 -07:00
|
|
|
/**
|
|
|
|
* trigger meta information: is second wheel mounted on crank shaft ('false') or cam shaft ('true')
|
|
|
|
*/
|
2022-04-01 18:10:08 -07:00
|
|
|
bool isSecondWheelCam;
|
2020-10-04 10:05:49 -07:00
|
|
|
/**
|
|
|
|
* number of consecutive trigger gaps needed to synchronize
|
|
|
|
*/
|
|
|
|
int gapTrackingLength = 1;
|
2020-01-14 00:54:46 -08:00
|
|
|
/**
|
|
|
|
* special case for triggers which do not provide exact TDC location
|
|
|
|
* For example pick-up in distributor with mechanical ignition firing order control.
|
|
|
|
*/
|
|
|
|
bool shapeWithoutTdc = false;
|
2018-02-05 14:16:34 -08:00
|
|
|
/**
|
|
|
|
* this flag tells us if we should ignore events on second input channel
|
|
|
|
* that's the way to ignore noise from the disconnected wire
|
|
|
|
*/
|
2021-09-05 02:56:59 -07:00
|
|
|
bool needSecondTriggerInput = false;
|
2018-02-05 14:16:34 -08:00
|
|
|
/**
|
|
|
|
* true value here means that we do not have a valid trigger configuration
|
|
|
|
*/
|
2021-09-05 02:56:59 -07:00
|
|
|
bool shapeDefinitionError = false;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-08-08 19:57:22 -07:00
|
|
|
/**
|
|
|
|
* https://github.com/rusefi/rusefi/issues/898
|
|
|
|
* User can choose for example Miata trigger which is not compatible with useOnlyRisingEdgeForTrigger option
|
|
|
|
* Such contradictory configuration causes a very hard to identify issue and for the sake of usability it's better to
|
|
|
|
* just crash with a very visible fatal error
|
|
|
|
*
|
|
|
|
* One day a nicer implementation could be simply ignoring 'useOnlyRisingEdgeForTrigger' in case of 'bothFrontsRequired'
|
|
|
|
*/
|
2021-09-05 02:56:59 -07:00
|
|
|
bool bothFrontsRequired = false;
|
2019-08-08 19:57:22 -07:00
|
|
|
|
2018-02-03 13:06:34 -08:00
|
|
|
/**
|
|
|
|
* this variable is incremented after each trigger shape redefinition
|
|
|
|
*/
|
2019-01-13 21:20:50 -08:00
|
|
|
int version = 0;
|
2018-02-03 13:06:34 -08:00
|
|
|
|
2018-10-21 06:31:58 -07:00
|
|
|
/**
|
|
|
|
* Depending on trigger shape, we use betweeb one and three previous gap ranges to detect synchronizaiton.
|
|
|
|
*
|
|
|
|
* Usually second or third gap is not needed, but some crazy triggers like 36-2-2-2 require two consecutive
|
|
|
|
* gaps ratios to sync
|
|
|
|
*/
|
|
|
|
|
2018-10-23 00:47:30 -07:00
|
|
|
float syncronizationRatioFrom[GAP_TRACKING_LENGTH];
|
|
|
|
float syncronizationRatioTo[GAP_TRACKING_LENGTH];
|
2018-10-21 06:31:58 -07:00
|
|
|
|
2018-10-21 08:17:47 -07:00
|
|
|
|
2018-04-25 23:11:51 -07:00
|
|
|
/**
|
|
|
|
* used by NoiselessTriggerDecoder (See TriggerCentral::handleShaftSignal())
|
|
|
|
*/
|
|
|
|
int syncRatioAvg;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-12-27 13:02:44 -08:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2015-10-29 11:02:52 -07:00
|
|
|
* Trigger indexes within trigger cycle are counted from synchronization point, and all
|
|
|
|
* engine processes are defined in angles from TDC.
|
|
|
|
*
|
|
|
|
* That's the angle distance from trigger event #0 and actual engine TDC
|
|
|
|
*
|
2015-07-10 06:01:56 -07:00
|
|
|
* see also globalTriggerAngleOffset
|
|
|
|
*/
|
|
|
|
angle_t tdcPosition;
|
|
|
|
|
2015-09-12 14:01:24 -07:00
|
|
|
/**
|
|
|
|
* In case of a multi-channel trigger, do we want to sync based on primary channel only?
|
2015-09-21 19:02:29 -07:00
|
|
|
* See also gapBothDirections
|
2015-09-12 14:01:24 -07:00
|
|
|
*/
|
2016-01-11 14:01:33 -08:00
|
|
|
bool useOnlyPrimaryForSync;
|
2015-09-21 20:01:35 -07:00
|
|
|
/**
|
|
|
|
* Should we use falls or rises for gap ratio detection?
|
2022-08-22 15:44:44 -07:00
|
|
|
* See also useOnlyRisingEdgeForTrigger
|
2015-09-21 20:01:35 -07:00
|
|
|
*/
|
2016-01-11 14:01:33 -08:00
|
|
|
bool useRiseEdge;
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2016-06-11 20:02:58 -07:00
|
|
|
* This is about selecting signal edges within particular trigger channels.
|
2015-09-21 19:02:29 -07:00
|
|
|
* Should we measure gaps with both fall and rise signal edges?
|
|
|
|
* See also useOnlyPrimaryForSync
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2017-03-18 17:59:52 -07:00
|
|
|
bool gapBothDirections;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-12-24 20:16:33 -08:00
|
|
|
void calculateExpectedEventCounts(bool useOnlyRisingEdgeForTrigger);
|
|
|
|
|
2022-09-11 00:46:50 -07:00
|
|
|
size_t getExpectedEventCount(TriggerWheel channelIndex) const;
|
2021-06-26 19:07:26 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* This is used for signal validation
|
|
|
|
*/
|
2021-07-13 11:50:10 -07:00
|
|
|
size_t expectedEventCount[PWM_PHASE_MAX_WAVE_PER_PWM];
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_UNIT_TEST
|
2015-08-30 11:01:28 -07:00
|
|
|
/**
|
|
|
|
* These signals are used for trigger export only
|
|
|
|
*/
|
2022-09-11 00:46:50 -07:00
|
|
|
TriggerWheel triggerSignalIndeces[PWM_PHASE_MAX_COUNT];
|
2022-09-10 23:57:35 -07:00
|
|
|
TriggerValue triggerSignalStates[PWM_PHASE_MAX_COUNT];
|
2022-04-03 09:22:47 -07:00
|
|
|
// see also 'doesTriggerImplyOperationMode'
|
|
|
|
bool knownOperationMode = true;
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
|
2021-11-10 16:47:27 -08:00
|
|
|
/**
|
|
|
|
* wave.phaseCount is total count of shaft events per CAM or CRANK shaft revolution.
|
|
|
|
* TODO this should be migrated to CRANKshaft revolution, this would go together
|
|
|
|
* this variable is public for performance reasons (I want to avoid costs of method if it's not inlined)
|
|
|
|
* but name is supposed to hint at the fact that decoders should not be assigning to it
|
2022-05-29 10:49:00 -07:00
|
|
|
* Please use "getSize()" function to read this value
|
2021-11-10 16:47:27 -08:00
|
|
|
*/
|
2021-11-21 01:56:07 -08:00
|
|
|
MultiChannelStateSequenceWithData<PWM_PHASE_MAX_COUNT> wave;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
// todo: add a runtime validation which would verify that this field was set properly
|
2019-02-02 22:04:24 -08:00
|
|
|
// todo: maybe even automate this flag calculation?
|
2019-02-02 22:19:16 -08:00
|
|
|
pin_state_t initialState[PWM_PHASE_MAX_WAVE_PER_PWM];
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-02-02 22:49:41 -08:00
|
|
|
bool isRiseEvent[PWM_PHASE_MAX_COUNT];
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-12-25 07:33:28 -08:00
|
|
|
bool useOnlyRisingEdgeForTriggerTemp;
|
|
|
|
|
2021-02-01 20:44:14 -08:00
|
|
|
/* (0..1] angle range */
|
2022-09-11 00:46:50 -07:00
|
|
|
void addEvent(angle_t angle, TriggerWheel const channelIndex, TriggerValue const state);
|
2021-02-01 20:44:14 -08:00
|
|
|
/* (0..720] angle range
|
2021-11-14 12:35:11 -08:00
|
|
|
* Deprecated! many usages should be replaced by addEvent360
|
2018-12-25 07:20:13 -08:00
|
|
|
*/
|
2022-09-11 00:46:50 -07:00
|
|
|
void addEvent720(angle_t angle, TriggerWheel const channelIndex, TriggerValue const state);
|
2016-10-31 19:02:12 -07:00
|
|
|
|
2021-07-16 21:45:47 -07:00
|
|
|
/**
|
2021-11-06 23:51:05 -07:00
|
|
|
* this method helps us use real world 360 degrees shape for FOUR_STROKE_CAM_SENSOR and FOUR_STROKE_CRANK_SENSOR
|
2021-07-16 21:45:47 -07:00
|
|
|
*/
|
2022-09-11 00:46:50 -07:00
|
|
|
void addEvent360(angle_t angle, TriggerWheel const channelIndex, TriggerValue const state);
|
2021-07-16 21:45:47 -07:00
|
|
|
|
2020-04-18 17:28:03 -07:00
|
|
|
/**
|
|
|
|
* This version of 'addEvent...' family considers the angle duration of operationMode in this trigger
|
2021-02-01 20:44:14 -08:00
|
|
|
* For example, (0..180] for FOUR_STROKE_SYMMETRICAL_CRANK_SENSOR
|
2021-11-19 20:56:52 -08:00
|
|
|
*
|
|
|
|
* TODO: one day kill all usages with FOUR_STROKE_CAM_SENSOR 720 cycle and add runtime prohibition
|
|
|
|
* TODO: for FOUR_STROKE_CAM_SENSOR addEvent360 is the way to go
|
2020-04-18 17:28:03 -07:00
|
|
|
*/
|
2022-09-11 00:46:50 -07:00
|
|
|
void addEventAngle(angle_t angle, TriggerWheel const channelIndex, TriggerValue const state);
|
2020-04-18 17:28:03 -07:00
|
|
|
|
2021-02-01 20:44:14 -08:00
|
|
|
/* (0..720] angle range
|
2018-12-25 07:20:13 -08:00
|
|
|
* Deprecated?
|
|
|
|
*/
|
2022-09-11 00:46:50 -07:00
|
|
|
void addEventClamped(angle_t angle, TriggerWheel const channelIndex, TriggerValue const stateParam, float filterLeft, float filterRight);
|
2022-09-04 22:16:24 -07:00
|
|
|
operation_mode_e getWheelOperationMode() const;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-01-12 07:43:02 -08:00
|
|
|
void initialize(operation_mode_e operationMode);
|
2015-12-27 13:02:44 -08:00
|
|
|
void setTriggerSynchronizationGap(float syncRatio);
|
2018-10-21 10:41:01 -07:00
|
|
|
void setTriggerSynchronizationGap3(int index, float syncRatioFrom, float syncRatioTo);
|
2015-09-12 12:02:40 -07:00
|
|
|
void setTriggerSynchronizationGap2(float syncRatioFrom, float syncRatioTo);
|
2015-12-27 13:02:44 -08:00
|
|
|
void setSecondTriggerSynchronizationGap(float syncRatio);
|
2015-09-12 12:02:40 -07:00
|
|
|
void setSecondTriggerSynchronizationGap2(float syncRatioFrom, float syncRatioTo);
|
2015-12-27 13:02:44 -08:00
|
|
|
void setThirdTriggerSynchronizationGap(float syncRatio);
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* this one is per CRANKshaft revolution
|
|
|
|
*/
|
2020-03-03 05:37:02 -08:00
|
|
|
size_t getLength() const;
|
|
|
|
size_t getSize() const;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-12-07 22:09:39 -08:00
|
|
|
int getTriggerWaveformSynchPointIndex() const;
|
2022-05-29 10:49:00 -07:00
|
|
|
void prepareShape(TriggerFormDetails& details);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-12-25 19:47:29 -08:00
|
|
|
/**
|
|
|
|
* This private method should only be used to prepare the array of pre-calculated values
|
|
|
|
* See eventAngles array
|
|
|
|
*/
|
|
|
|
angle_t getAngle(int phaseIndex) const;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-04-19 11:23:01 -07:00
|
|
|
angle_t getCycleDuration() const;
|
|
|
|
|
2022-06-09 14:21:22 -07:00
|
|
|
// Returns true if this trigger alone can fully sync the current engine for sequential mode.
|
|
|
|
bool needsDisambiguation() const;
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2019-12-07 22:09:39 -08:00
|
|
|
* index of synchronization event within TriggerWaveform
|
2015-07-10 06:01:56 -07:00
|
|
|
* See findTriggerZeroEventIndex()
|
|
|
|
*/
|
|
|
|
int triggerShapeSynchPointIndex;
|
2020-08-26 17:57:11 -07:00
|
|
|
|
2020-10-05 11:22:59 -07:00
|
|
|
void initializeSyncPoint(
|
2022-05-10 01:41:39 -07:00
|
|
|
TriggerDecoderBase& state,
|
2022-05-30 16:36:47 -07:00
|
|
|
const TriggerConfiguration& triggerConfiguration
|
|
|
|
);
|
2020-08-26 17:57:11 -07:00
|
|
|
|
2021-07-17 14:47:13 -07:00
|
|
|
uint16_t findAngleIndex(TriggerFormDetails *details, angle_t angle) const;
|
|
|
|
|
2018-12-25 19:47:29 -08:00
|
|
|
private:
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* These angles are in trigger DESCRIPTION coordinates - i.e. the way you add events while declaring trigger shape
|
|
|
|
*/
|
2016-01-14 20:03:17 -08:00
|
|
|
angle_t getSwitchAngle(int index) const;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-01-14 20:03:17 -08:00
|
|
|
/**
|
|
|
|
* This variable is used to confirm that events are added in the right order.
|
2018-12-25 07:20:13 -08:00
|
|
|
* todo: this variable is probably not needed, could be reimplemented by accessing by index
|
2016-01-14 20:03:17 -08:00
|
|
|
*/
|
|
|
|
angle_t previousAngle;
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* this is part of performance optimization
|
|
|
|
*/
|
|
|
|
operation_mode_e operationMode;
|
|
|
|
};
|
|
|
|
|
2021-11-06 22:34:16 -07:00
|
|
|
#ifndef MAX
|
2021-11-06 21:03:16 -07:00
|
|
|
#define MAX(a,b) (((a)>(b))?(a):(b))
|
2021-11-06 22:28:28 -07:00
|
|
|
#endif
|
2021-11-06 21:03:16 -07:00
|
|
|
|
2020-08-24 21:59:07 -07:00
|
|
|
/**
|
|
|
|
* Misc values calculated from TriggerWaveform
|
|
|
|
*/
|
|
|
|
class TriggerFormDetails {
|
|
|
|
public:
|
2022-05-29 10:49:00 -07:00
|
|
|
void prepareEventAngles(TriggerWaveform *shape);
|
|
|
|
|
2020-08-24 21:59:07 -07:00
|
|
|
/**
|
|
|
|
* These angles are in event coordinates - with synchronization point located at angle zero.
|
|
|
|
* These values are pre-calculated for performance reasons.
|
|
|
|
*/
|
2021-11-19 22:38:39 -08:00
|
|
|
angle_t eventAngles[2 * PWM_PHASE_MAX_COUNT];
|
2020-08-24 21:59:07 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
void findTriggerPosition(
|
|
|
|
TriggerWaveform *shape,
|
|
|
|
TriggerFormDetails *details,
|
|
|
|
event_trigger_position_s *position,
|
2021-11-16 01:15:29 -08:00
|
|
|
angle_t angle);
|
2020-08-24 21:59:07 -07:00
|
|
|
|
2019-12-07 22:09:39 -08:00
|
|
|
void setToothedWheelConfiguration(TriggerWaveform *s, int total, int skipped, operation_mode_e operationMode);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-11-17 00:54:21 -08:00
|
|
|
#define TRIGGER_WAVEFORM(x) engine->triggerCentral.triggerShape.x
|
2018-10-28 12:07:42 -07:00
|
|
|
|
2021-11-21 01:56:07 -08:00
|
|
|
#define getTriggerSize() TRIGGER_WAVEFORM(wave.phaseCount)
|