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"
|
2023-12-19 12:34:53 -08:00
|
|
|
#include "generated_lookup_engine_configuration.h"
|
2022-09-15 18:27:20 -07:00
|
|
|
#include <rusefi/isnan.h>
|
2023-10-19 15:21:44 -07:00
|
|
|
#include "engine_state.h"
|
2018-12-25 19:47:29 -08:00
|
|
|
|
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
|
2024-05-11 13:24:38 -07:00
|
|
|
#define assertAngleRange(angle, msg, code) if (angle > 10000000 || angle < -10000000) { firmwareError(code, "angle range %s %d", msg, (int)angle);angle = 0;}
|
2018-12-25 19:47:29 -08:00
|
|
|
#else
|
2023-11-02 06:55:22 -07:00
|
|
|
#define assertAngleRange(angle, msg, code) {UNUSED(code);}
|
2018-12-25 19:47:29 -08:00
|
|
|
#endif
|
|
|
|
|
2023-10-19 15:21:44 -07:00
|
|
|
// Shifts angle into the [0..720) range for four stroke and [0..360) for two stroke
|
|
|
|
// See also wrapVvt
|
2024-03-28 18:20:40 -07:00
|
|
|
inline void wrapAngle(angle_t& angle, const char* msg, ObdCode code) {
|
2023-10-19 15:21:44 -07:00
|
|
|
if (cisnan(angle)) {
|
|
|
|
firmwareError(ObdCode::CUSTOM_ERR_ANGLE, "a NaN %s", msg);
|
|
|
|
angle = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertAngleRange(angle, msg, code);
|
|
|
|
float engineCycle = getEngineState()->engineCycle;
|
|
|
|
|
|
|
|
while (angle < 0) {
|
|
|
|
angle += engineCycle;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (angle >= engineCycle) {
|
|
|
|
angle -= engineCycle;
|
2018-12-25 19:47:29 -08:00
|
|
|
}
|
2023-10-19 15:21:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// proper method avoids un-wrapped state of variables
|
2024-03-28 18:20:40 -07:00
|
|
|
inline angle_t wrapAngleMethod(angle_t param, const char *msg = "", ObdCode code = ObdCode::OBD_PCM_Processor_Fault) {
|
2023-10-19 15:21:44 -07:00
|
|
|
wrapAngle(param, msg, code);
|
|
|
|
return param;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
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
|
|
|
|
2022-09-23 17:39:41 -07:00
|
|
|
#include "sync_edge.h"
|
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();
|
2023-03-02 21:21:40 -08:00
|
|
|
void initializeTriggerWaveform(operation_mode_e triggerOperationMode, const trigger_config_s &triggerType);
|
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
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2023-09-09 16:53:18 -07:00
|
|
|
float synchronizationRatioFrom[GAP_TRACKING_LENGTH];
|
|
|
|
float synchronizationRatioTo[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;
|
2022-09-23 17:39:41 -07:00
|
|
|
|
|
|
|
// Which edge(s) to consider for finding the sync point: rise, fall, or both
|
|
|
|
SyncEdge syncEdge;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2022-09-25 15:49:54 -07:00
|
|
|
// If true, falling edges should be fully ignored on this trigger shape.
|
|
|
|
bool useOnlyRisingEdges;
|
|
|
|
|
|
|
|
void calculateExpectedEventCounts();
|
2018-12-24 20:16:33 -08:00
|
|
|
|
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'
|
2023-09-15 20:40:31 -07:00
|
|
|
// todo: reuse doesTriggerImplyOperationMode instead of separate field only which is only used for metadata anyway?
|
2022-04-03 09:22:47 -07:00
|
|
|
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
|
|
|
|
2019-02-02 22:49:41 -08:00
|
|
|
bool isRiseEvent[PWM_PHASE_MAX_COUNT];
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2022-11-16 12:23:42 -08:00
|
|
|
/**
|
|
|
|
* @param angle (0..1]
|
|
|
|
*/
|
2022-11-16 11:28:43 -08:00
|
|
|
void addEvent(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex = TriggerWheel::T_PRIMARY);
|
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-11-16 11:28:43 -08:00
|
|
|
void addEvent720(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex = TriggerWheel::T_PRIMARY);
|
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-11-16 11:28:43 -08:00
|
|
|
void addEvent360(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex = TriggerWheel::T_PRIMARY);
|
2021-07-16 21:45:47 -07:00
|
|
|
|
2023-05-13 11:26:31 -07:00
|
|
|
void addToothRiseFall(angle_t angle, angle_t width = 10, TriggerWheel const channelIndex = TriggerWheel::T_PRIMARY);
|
|
|
|
|
2020-04-18 17:28:03 -07:00
|
|
|
/**
|
2022-11-16 12:23:42 -08:00
|
|
|
* This version of the method is best when same wheel could be mounted either on crank or cam
|
|
|
|
*
|
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
|
2022-11-16 12:23:42 -08:00
|
|
|
*
|
|
|
|
* @param angle (0..360] or (0..720] depending on configuration
|
2020-04-18 17:28:03 -07:00
|
|
|
*/
|
2022-11-16 11:28:43 -08:00
|
|
|
void addEventAngle(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex = TriggerWheel::T_PRIMARY);
|
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-11-16 11:28:43 -08:00
|
|
|
void addEventClamped(angle_t angle, TriggerValue const state, TriggerWheel const channelIndex, 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
|
|
|
|
2022-09-23 17:39:41 -07:00
|
|
|
void initialize(operation_mode_e operationMode, SyncEdge syncEdge);
|
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;
|
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;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|