2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file trigger_structure.h
|
|
|
|
*
|
|
|
|
* @date Dec 22, 2013
|
2018-12-25 19:47:29 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2018
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TRIGGER_STRUCTURE_H_
|
|
|
|
#define TRIGGER_STRUCTURE_H_
|
|
|
|
|
2018-09-16 19:26:57 -07:00
|
|
|
#include "global.h"
|
2018-12-25 19:47:29 -08:00
|
|
|
#include "engine_configuration_generated_structures.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "EfiWave.h"
|
2019-01-27 21:44:30 -08:00
|
|
|
#include "globalaccess.h"
|
2018-12-25 19:47:29 -08:00
|
|
|
|
2019-01-01 11:05:11 -08:00
|
|
|
#define FOUR_STROKE_ENGINE_CYCLE 720
|
|
|
|
|
2018-12-25 19:47:29 -08:00
|
|
|
#if EFI_ENABLE_ASSERTS
|
|
|
|
#define assertAngleRange(angle, msg, code) if(angle > 10000000 || angle < -10000000) { firmwareError(code, "angle range %s %.2f", msg, angle);angle = 0;}
|
|
|
|
#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'
|
|
|
|
*/
|
|
|
|
#define fixAngle2(angle, msg, code, engineCycle) \
|
|
|
|
{ \
|
|
|
|
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?*/ \
|
|
|
|
while (angle < 0) \
|
|
|
|
angle += engineCycleDurationLocalCopy; \
|
|
|
|
/* todo: would 'if' work as good as 'while'? */ \
|
|
|
|
while (angle >= engineCycleDurationLocalCopy) \
|
|
|
|
angle -= engineCycleDurationLocalCopy; \
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-12-18 19:03:00 -08:00
|
|
|
/**
|
|
|
|
* This structure defines an angle position within the trigger
|
|
|
|
*/
|
|
|
|
class event_trigger_position_s {
|
|
|
|
public:
|
|
|
|
event_trigger_position_s();
|
|
|
|
/**
|
|
|
|
* That's trigger event index
|
|
|
|
*/
|
|
|
|
uint32_t eventIndex;
|
|
|
|
angle_t eventAngle;
|
|
|
|
/**
|
|
|
|
* Angle offset from the trigger event
|
|
|
|
*/
|
|
|
|
angle_t angleOffset;
|
|
|
|
};
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#define TRIGGER_CHANNEL_COUNT 3
|
|
|
|
|
|
|
|
class trigger_shape_helper {
|
|
|
|
public:
|
|
|
|
trigger_shape_helper();
|
|
|
|
|
2018-12-25 07:13:00 -08:00
|
|
|
SingleWave channels[TRIGGER_CHANNEL_COUNT];
|
2015-07-10 06:01:56 -07:00
|
|
|
private:
|
|
|
|
pin_state_t pinStates[TRIGGER_CHANNEL_COUNT][PWM_PHASE_MAX_COUNT];
|
|
|
|
};
|
|
|
|
|
|
|
|
class Engine;
|
2015-09-13 13:01:38 -07:00
|
|
|
class TriggerState;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-10-23 00:47:30 -07:00
|
|
|
#define GAP_TRACKING_LENGTH 4
|
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
|
|
|
*/
|
2015-07-10 06:01:56 -07:00
|
|
|
class TriggerShape {
|
|
|
|
public:
|
|
|
|
TriggerShape();
|
2018-12-25 19:47:29 -08:00
|
|
|
void initializeTriggerShape(Logging *logger, operation_mode_e operationMode,
|
|
|
|
bool useOnlyRisingEdgeForTrigger, const trigger_config_s *triggerConfig);
|
|
|
|
void findTriggerPosition(event_trigger_position_s *position,
|
|
|
|
angle_t angleOffset DEFINE_CONFIG_PARAM(angle_t, globalTriggerAngleOffset));
|
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
|
|
|
|
* one primary channel tooth each raising (or falling depedning on configuration) front would synchronize
|
|
|
|
*/
|
2016-01-11 14:01:33 -08:00
|
|
|
bool isSynchronizationNeeded;
|
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
|
|
|
|
*/
|
2016-01-11 14:01:33 -08:00
|
|
|
bool needSecondTriggerInput;
|
2018-02-05 14:16:34 -08:00
|
|
|
/**
|
|
|
|
* true value here means that we do not have a valid trigger configuration
|
|
|
|
*/
|
2017-02-23 10:53:27 -08:00
|
|
|
bool shapeDefinitionError;
|
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
|
|
|
|
* See also
|
|
|
|
*/
|
2019-01-13 21:20:50 -08:00
|
|
|
int version = 0;
|
2018-02-03 13:06:34 -08:00
|
|
|
|
2015-09-12 14:01:24 -07:00
|
|
|
/**
|
|
|
|
* duty cycle for each individual trigger channel
|
|
|
|
*/
|
2018-02-05 14:16:34 -08:00
|
|
|
float expectedDutyCycle[PWM_PHASE_MAX_WAVE_PER_PWM];
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-09-12 14:01:24 -07:00
|
|
|
/**
|
|
|
|
* These angles are in event coordinates - with synchronization point located at angle zero.
|
|
|
|
* These values are pre-calculated for performance reasons.
|
|
|
|
*/
|
|
|
|
angle_t eventAngles[PWM_PHASE_MAX_COUNT];
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* this cache allows us to find a close-enough (with one degree precision) trigger wheel index by
|
2015-09-12 14:01:24 -07:00
|
|
|
* given angle with fast constant speed. That's a performance optimization for event scheduling.
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
int triggerIndexByAngle[720];
|
|
|
|
|
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?
|
|
|
|
*/
|
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);
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* This is used for signal validation
|
|
|
|
*/
|
|
|
|
uint32_t expectedEventCount[PWM_PHASE_MAX_WAVE_PER_PWM];
|
|
|
|
|
2015-08-30 11:01:28 -07:00
|
|
|
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
|
|
|
|
/**
|
|
|
|
* These signals are used for trigger export only
|
|
|
|
*/
|
|
|
|
int triggerSignals[PWM_PHASE_MAX_COUNT];
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
|
2018-12-08 13:59:16 -08:00
|
|
|
MultiWave 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?
|
2015-07-10 06:01:56 -07:00
|
|
|
int initialState[PWM_PHASE_MAX_WAVE_PER_PWM];
|
|
|
|
|
|
|
|
int8_t isFrontEvent[PWM_PHASE_MAX_COUNT];
|
|
|
|
/**
|
|
|
|
* this table translates trigger definition index into 'front-only' index. This translation is not so trivial
|
|
|
|
* in case of a multi-channel signal with overlapping waves, for example Ford Aspire/Mitsubishi
|
|
|
|
*/
|
|
|
|
int frontOnlyIndexes[PWM_PHASE_MAX_COUNT];
|
|
|
|
|
2015-09-12 14:01:24 -07:00
|
|
|
/**
|
|
|
|
* This is a pretty questionable option which is considered by 'addEvent' method
|
|
|
|
*/
|
2016-01-11 14:01:33 -08:00
|
|
|
bool invertOnAdd;
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* Total count of shaft events per CAM or CRANK shaft revolution.
|
|
|
|
* TODO this should be migrated to CRANKshaft revolution, this would go together
|
2018-11-21 16:40:19 -08:00
|
|
|
* 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
|
|
|
|
* Please use "getTriggerSize()" macro or "getSize()" method to read this value
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2018-11-21 16:40:19 -08:00
|
|
|
int privateTriggerDefinitionSize;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-12-25 07:33:28 -08:00
|
|
|
bool useOnlyRisingEdgeForTriggerTemp;
|
|
|
|
|
2018-12-25 07:20:13 -08:00
|
|
|
/* 0..1 angle range */
|
2018-12-25 07:13:00 -08:00
|
|
|
void addEvent(bool useOnlyRisingEdgeForTrigger, angle_t angle, trigger_wheel_e const channelIndex, trigger_value_e const state);
|
2018-12-25 07:20:13 -08:00
|
|
|
/* 0..720 angle range
|
|
|
|
* Deprecated?
|
|
|
|
*/
|
2018-12-25 07:33:28 -08:00
|
|
|
void addEvent720(angle_t angle, trigger_wheel_e const channelIndex, trigger_value_e const state);
|
2016-10-31 19:02:12 -07:00
|
|
|
|
2018-12-25 07:20:13 -08:00
|
|
|
/* 0..720 angle range
|
|
|
|
* Deprecated?
|
|
|
|
*/
|
2018-12-25 16:18:37 -08:00
|
|
|
void addEvent3(angle_t angle, trigger_wheel_e const channelIndex, trigger_value_e const stateParam, float filterLeft, float filterRight);
|
2015-07-10 06:01:56 -07:00
|
|
|
operation_mode_e getOperationMode();
|
|
|
|
|
2015-10-29 11:02:52 -07:00
|
|
|
void initialize(operation_mode_e operationMode, bool needSecondTriggerInput);
|
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
|
|
|
|
*/
|
|
|
|
uint32_t getLength() const;
|
|
|
|
int getSize() const;
|
|
|
|
|
|
|
|
int getTriggerShapeSynchPointIndex();
|
2018-12-25 19:47:29 -08:00
|
|
|
void prepareShape();
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* index of synchronization event within TriggerShape
|
|
|
|
* See findTriggerZeroEventIndex()
|
|
|
|
*/
|
|
|
|
int triggerShapeSynchPointIndex;
|
2018-12-25 19:47:29 -08:00
|
|
|
private:
|
|
|
|
trigger_shape_helper h;
|
|
|
|
|
|
|
|
int findAngleIndex(float angle);
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2015-09-12 14:01:24 -07:00
|
|
|
* Working buffer for 'wave' instance
|
2015-07-10 06:01:56 -07:00
|
|
|
* Values are in the 0..1 range
|
|
|
|
*/
|
|
|
|
float switchTimesBuffer[PWM_PHASE_MAX_COUNT];
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
2016-01-14 20:03:17 -08:00
|
|
|
angle_t getCycleDuration() const;
|
2015-07-10 06:01:56 -07:00
|
|
|
};
|
|
|
|
|
2018-12-25 17:09:35 -08:00
|
|
|
void setToothedWheelConfiguration(TriggerShape *s, int total, int skipped, operation_mode_e operationMode);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-10-28 12:07:42 -07:00
|
|
|
#define TRIGGER_SHAPE(x) ENGINE(triggerCentral.triggerShape.x)
|
|
|
|
|
2018-11-21 16:40:19 -08:00
|
|
|
#define getTriggerSize() TRIGGER_SHAPE(privateTriggerDefinitionSize)
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* TRIGGER_STRUCTURE_H_ */
|