rusefi-1/firmware/controllers/trigger/trigger_decoder.h

143 lines
4.0 KiB
C
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file trigger_decoder.h
*
* @date Dec 24, 2013
2017-01-03 03:05:22 -08:00
* @author Andrey Belomutskiy, (c) 2012-2017
2015-07-10 06:01:56 -07:00
*/
#ifndef TRIGGER_DECODER_H_
#define TRIGGER_DECODER_H_
#include "main.h"
#include "trigger_structure.h"
#include "engine_configuration.h"
class TriggerState;
typedef void (*TriggerStateCallback)(TriggerState *);
2015-09-08 20:01:07 -07:00
typedef struct {
/**
* index within trigger revolution, from 0 to trigger event count
*/
uint32_t current_index;
/**
2015-09-12 13:01:43 -07:00
* Number of actual events of each channel within current trigger cycle, these
* values are used to detect trigger signal errors.
2015-09-08 20:01:07 -07:00
* see TriggerShape
*/
uint32_t eventCount[PWM_PHASE_MAX_WAVE_PER_PWM];
2015-09-12 13:01:43 -07:00
/**
* This array is used to calculate duty cycle of each trigger channel.
* Current implementation is a bit funny - it does not really consider if an event
* is a rise or a fall, it works based on the event order within synchronization cycle.
*
* 32 bit value is good enough here, overflows will happen but they would work just fine.
*/
uint32_t timeOfPreviousEventNt[PWM_PHASE_MAX_WAVE_PER_PWM];
/**
* Here we accumulate the amount of time this signal was ON within current trigger cycle
*/
uint32_t totalTimeNt[PWM_PHASE_MAX_WAVE_PER_PWM];
2015-09-08 20:01:07 -07:00
} current_cycle_state_s;
2015-07-10 06:01:56 -07:00
class TriggerState {
public:
TriggerState();
int getCurrentIndex();
int getTotalRevolutionCounter();
2016-08-23 19:02:18 -07:00
/**
* this is important for crank-based virtual trigger and VVT magic
*/
bool isEvenRevolution();
void intTotalEventCounter();
2015-07-10 06:01:56 -07:00
efitime_t getTotalEventCounter();
efitime_t getStartOfRevolutionIndex();
2017-05-15 20:33:22 -07:00
void decodeTriggerEvent(trigger_event_e const signal, efitime_t nowUs DECLARE_ENGINE_PARAMETER_SUFFIX);
2015-07-10 06:01:56 -07:00
2017-05-15 20:33:22 -07:00
bool isValidIndex(DECLARE_ENGINE_PARAMETER_SIGNATURE);
2015-07-10 06:01:56 -07:00
float getTriggerDutyCycle(int index);
TriggerStateCallback cycleCallback;
/**
* TRUE if we know where we are
*/
bool shaft_is_synchronized;
2015-12-27 13:02:44 -08:00
uint32_t toothed_previous_duration; // todo: unify Camel_notation, what a mess :(
uint32_t durationBeforePrevious; // this one is before 'toothed_previous_duration'
uint32_t thirdPreviousDuration; // this one is before durationBeforePrevious, todo: better field names?
2015-07-10 06:01:56 -07:00
/**
* this could be a local variable, but it's better for debugging to have it as a field
*/
uint32_t currentDuration;
efitime_t toothed_previous_time;
2015-09-08 20:01:07 -07:00
current_cycle_state_s currentCycle;
2017-12-12 15:04:54 -08:00
2015-07-10 06:01:56 -07:00
int expectedTotalTime[PWM_PHASE_MAX_WAVE_PER_PWM];
2015-09-12 14:01:24 -07:00
2015-08-17 20:02:01 -07:00
/**
* how many times since ECU reboot we had unexpected number of teeth in trigger cycle
*/
2015-07-10 06:01:56 -07:00
uint32_t totalTriggerErrorCounter;
uint32_t runningTriggerErrorCounter;
uint32_t orderingErrorCounter;
uint32_t runningOrderingErrorCounter;
2015-09-13 07:01:39 -07:00
void reset();
2015-07-10 06:01:56 -07:00
void resetRunningCounters();
2017-12-03 21:04:47 -08:00
virtual void runtimeStatistics(efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX);
2017-05-18 13:16:55 -07:00
2015-07-10 06:01:56 -07:00
uint32_t runningRevolutionCounter;
2016-08-22 20:04:55 -07:00
/**
* this is start of real trigger cycle
* for virtual double trigger see timeAtVirtualZeroNt
*/
2016-08-20 19:02:12 -07:00
efitick_t startOfCycleNt;
2015-07-10 06:01:56 -07:00
private:
2015-09-08 20:01:07 -07:00
void resetCurrentCycleState();
2015-07-10 06:01:56 -07:00
trigger_event_e curSignal;
trigger_event_e prevSignal;
efitime_t totalEventCountBase;
uint32_t totalRevolutionCounter;
bool isFirstEvent;
2017-12-12 14:51:44 -08:00
2015-07-10 06:01:56 -07:00
};
2017-05-18 13:39:04 -07:00
/**
* the reason for sub-class is simply to save RAM but not having statisics in the trigger initialization instance
*/
2017-05-18 13:16:55 -07:00
class TriggerStateWithRunningStatistics : public TriggerState {
public:
2017-12-13 18:08:34 -08:00
TriggerStateWithRunningStatistics();
float instantRpm;
2017-12-03 20:58:48 -08:00
/**
* timestamp of each trigger wheel tooth
*/
2017-05-18 13:16:55 -07:00
uint32_t timeOfLastEvent[PWM_PHASE_MAX_COUNT];
2017-12-03 20:58:48 -08:00
/**
* instant RPM calculated at this trigger wheel tooth
*/
2017-05-18 13:16:55 -07:00
float instantRpmValue[PWM_PHASE_MAX_COUNT];
2017-12-03 20:58:48 -08:00
float calculateInstantRpm(int *prevIndex, efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX);
2017-12-03 21:04:47 -08:00
virtual void runtimeStatistics(efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX);
2017-05-18 13:16:55 -07:00
};
2016-01-14 21:01:42 -08:00
angle_t getEngineCycle(operation_mode_e operationMode);
2017-05-15 20:33:22 -07:00
uint32_t findTriggerZeroEventIndex(TriggerState *state, TriggerShape * shape, trigger_config_s const*triggerConfig DECLARE_ENGINE_PARAMETER_SUFFIX);
2015-07-10 06:01:56 -07:00
class Engine;
void initTriggerDecoder(void);
void initTriggerDecoderLogger(Logging *sharedLogger);
2016-01-11 14:01:33 -08:00
bool isTriggerDecoderError(void);
2015-07-10 06:01:56 -07:00
#endif /* TRIGGER_DECODER_H_ */