Speeduino
Loading...
Searching...
No Matches
Functions | Variables
sensors.cpp File Reference
#include "sensors.h"
#include "crankMaths.h"
#include "globals.h"
#include "maths.h"
#include "storage.h"
#include "comms.h"
#include "idle.h"
#include "corrections.h"
#include "pages.h"
#include "decoders.h"
#include "auxiliaries.h"
#include "utilities.h"
#include "unit_testing.h"
#include "sensors_map_structs.h"
#include "units.h"
#include "atomic.h"
#include "board_definition.h"
#include "preprocessor.h"
#include "static_for.hpp"
#include "polling.hpp"

Functions

TESTABLE_INLINE_STATIC int16_t fastMap10Bit (uint16_t value, int16_t rangeMin, int16_t rangeMax)
 A specialist function to map a value in the range [0, 1023] (I.e. 10-bit) to a different range.
 
static uint16_t readAnalogPin (uint8_t pin)
 
static uint16_t readAnalogSensor (uint8_t pin)
 
static uint16_t readMAPSensor (uint8_t pin)
 
void initialiseADC (void)
 
TESTABLE_INLINE_STATIC bool instanteneousMAPReading (void)
 
static bool cycleAverageMAPReadingAccumulate (map_cycle_average_t &cycle_average, const map_adc_readings_t &sensorReadings)
 
static void reset (const statuses &current, map_cycle_average_t &cycle_average, const map_adc_readings_t &sensorReadings)
 
static bool cycleAverageEndCycle (const statuses &current, map_cycle_average_t &cycle_average, map_adc_readings_t &sensorReadings)
 
static bool isCycleCurrent (const statuses &current, uint32_t cycleStartIndex)
 
static bool isCycleCurrent (const statuses &current, const map_cycle_average_t &cycle_avg)
 
TESTABLE_INLINE_STATIC bool canUseCycleAverage (const statuses &current, const config2 &page2)
 
TESTABLE_INLINE_STATIC bool cycleAverageMAPReading (const statuses &current, const config2 &page2, map_cycle_average_t &cycle_average, map_adc_readings_t &sensorReadings)
 
static bool cycleMinimumAccumulate (map_cycle_min_t &cycle_min, const map_adc_readings_t &sensorReadings)
 
static void reset (const statuses &current, map_cycle_min_t &cycle_min, const map_adc_readings_t &sensorReadings)
 
static bool cycleMinimumEndCycle (const statuses &current, map_cycle_min_t &cycle_min, map_adc_readings_t &sensorReadings)
 
static bool isCycleCurrent (const statuses &current, const map_cycle_min_t &cycle_min)
 
TESTABLE_INLINE_STATIC bool cycleMinimumMAPReading (const statuses &current, const config2 &page2, map_cycle_min_t &cycle_min, map_adc_readings_t &sensorReadings)
 
static bool eventAverageAccumulate (map_event_average_t &eventAverage, const map_adc_readings_t &sensorReadings)
 
static bool isIgnitionEventValid (const map_event_average_t &eventAverage)
 
static void reset (map_event_average_t &eventAverage, const map_adc_readings_t &sensorReadings)
 
static bool eventAverageEndEvent (map_event_average_t &eventAverage, map_adc_readings_t &sensorReadings)
 
static bool isIgnitionEventCurrent (const map_event_average_t &eventAverage)
 
TESTABLE_INLINE_STATIC bool canUseEventAverage (const statuses &current, const config2 &page2)
 
TESTABLE_INLINE_STATIC bool eventAverageMAPReading (const statuses &current, const config2 &page2, map_event_average_t &eventAverage, map_adc_readings_t &sensorReadings)
 
static bool isValidMapSensorReading (uint16_t reading)
 
TESTABLE_INLINE_STATIC uint16_t validateFilterMapSensorReading (uint16_t reading, uint8_t alpha, uint16_t prior)
 
static uint16_t readFilteredMapADC (uint8_t pin, uint8_t alpha, uint16_t prior)
 
static map_adc_readings_t readMapSensors (const map_adc_readings_t &previousReadings, const config4 &page4, bool useEMAP)
 
static void storeLastMAPReadings (map_last_read_t &lastRead, uint16_t oldMAPValue)
 
static uint16_t mapADCToMAP (uint16_t mapADC, int8_t mapMin, uint16_t mapMax)
 
static void setMAPValuesFromReadings (const map_adc_readings_t &readings, const config2 &page2, bool useEMAP, statuses &current)
 
static void readMAP (void)
 
int16_t getMAPDelta (void)
 Get the MAP change between the last 2 readings.
 
uint32_t getMAPDeltaTime (void)
 Get the time in µS between the last 2 MAP readings.
 
static bool isCTPSSensorActive (void)
 
static void readTPS (uint8_t tpsADC)
 
static void readTPS (void)
 
void initialiseTPS (void)
 Initial reading of the TPS sensor, primarily to detect flood clear state.
 
static void readCLT (void)
 
void initialiseCLT (void)
 Initial reading of the coolant sensor, primarily to make sure the priming pulsewidth is correct.
 
static void readIAT (void)
 
static bool isValidBaro (uint8_t baro)
 
static void setBaroFromSensorReading (uint16_t sensorReading)
 
static void setBaroFromMAP (void)
 
static void readBaro (void)
 
void initialiseMAPBaro (void)
 Initialize the MAP calculation & Baro values.
 
void resetMAPcycleAndEvent (void)
 
static void readO2_1 (void)
 
static void readO2_2 (void)
 
static void readO2 (void)
 
static void readBat (void)
 
static void enableAnalogIsr (void)
 
static void readSpeed (void)
 
static void readGear (void)
 
static void updateFuelPressure (void)
 
static void updateOilPressure (void)
 
void readPolledSensors (byte loopTimer)
 Read the sensors that are polled at every loop. This includes the TPS, MAP, CLT, IAT and O2 sensors.
 
uint32_t vssGetPulseGap (uint8_t historyIndex)
 Returns the VSS pulse gap for a given history point.
 
static uint16_t getSpeed (void)
 
static byte getGear (void)
 
static byte getFuelPressure (void)
 
static byte getOilPressure (void)
 
uint8_t getAnalogKnock (void)
 
void flexPulse (void)
 
void knockPulse (void)
 
void vssPulse (void)
 The ISR function for VSS pulses.
 
uint16_t readAuxanalog (uint8_t analogPin)
 
uint16_t readAuxdigital (uint8_t digitalPin)
 

Variables

uint8_t statusSensors = 0
 
static volatile uint32_t vssTimes [VSS_SAMPLES] = {0}
 
static volatile uint8_t vssIndex = 0U
 
volatile uint8_t flexCounter = 0U
 
static volatile uint32_t flexStartTime = 0UL
 
volatile uint32_t flexPulseWidth = 0U
 
static map_algorithm_t mapAlgorithmState
 
static uint16_t cltCalibration_bins [32]
 
static uint16_t cltCalibration_values [32]
 
static uint16_t iatCalibration_bins [32]
 
static uint16_t iatCalibration_values [32]
 
static uint16_t o2Calibration_bins [32]
 
static uint8_t o2Calibration_values [32]
 
static constexpr uint16_t VALID_MAP_MAX =1022U
 
static constexpr uint16_t VALID_MAP_MIN =2U
 

Detailed Description

Read sensors with appropriate timing / scheduling.

Function Documentation

◆ canUseCycleAverage()

TESTABLE_INLINE_STATIC bool canUseCycleAverage ( const statuses current,
const config2 page2 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ canUseEventAverage()

TESTABLE_INLINE_STATIC bool canUseEventAverage ( const statuses current,
const config2 page2 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cycleAverageEndCycle()

static bool cycleAverageEndCycle ( const statuses current,
map_cycle_average_t cycle_average,
map_adc_readings_t sensorReadings 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cycleAverageMAPReading()

TESTABLE_INLINE_STATIC bool cycleAverageMAPReading ( const statuses current,
const config2 page2,
map_cycle_average_t cycle_average,
map_adc_readings_t sensorReadings 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cycleAverageMAPReadingAccumulate()

static bool cycleAverageMAPReadingAccumulate ( map_cycle_average_t cycle_average,
const map_adc_readings_t sensorReadings 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cycleMinimumAccumulate()

static bool cycleMinimumAccumulate ( map_cycle_min_t cycle_min,
const map_adc_readings_t sensorReadings 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cycleMinimumEndCycle()

static bool cycleMinimumEndCycle ( const statuses current,
map_cycle_min_t cycle_min,
map_adc_readings_t sensorReadings 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cycleMinimumMAPReading()

TESTABLE_INLINE_STATIC bool cycleMinimumMAPReading ( const statuses current,
const config2 page2,
map_cycle_min_t cycle_min,
map_adc_readings_t sensorReadings 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ enableAnalogIsr()

static void enableAnalogIsr ( void  )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ eventAverageAccumulate()

static bool eventAverageAccumulate ( map_event_average_t eventAverage,
const map_adc_readings_t sensorReadings 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ eventAverageEndEvent()

static bool eventAverageEndEvent ( map_event_average_t eventAverage,
map_adc_readings_t sensorReadings 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ eventAverageMAPReading()

TESTABLE_INLINE_STATIC bool eventAverageMAPReading ( const statuses current,
const config2 page2,
map_event_average_t eventAverage,
map_adc_readings_t sensorReadings 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fastMap10Bit()

TESTABLE_INLINE_STATIC int16_t fastMap10Bit ( uint16_t  value,
int16_t  rangeMin,
int16_t  rangeMax 
)

A specialist function to map a value in the range [0, 1023] (I.e. 10-bit) to a different range.

Mostly used for analog input voltage level to real world value conversions.

analogRead returns a number in the range [0, 1023], representing the pin input voltage from min to max (typically 0V - 5V) We need to convert that value to the real world value the sensor is reading (pressure, temperature etc.) If:

  • rangeMin is the real world value when the sensor is reading 0V
  • rangeMax is the real world measurement when the sensor is reading 5V
  • There is a linear relationship between voltage output and the real world value.

then this function will return the real world measurement (kPa, °C etc)

Parameters
valueValue to map (should be in range [0, 1023])
rangeMinMinimum of the output range
rangeMaxMaximum of the output range
Returns
int16_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ flexPulse()

void flexPulse ( void  )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getAnalogKnock()

uint8_t getAnalogKnock ( void  )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFuelPressure()

static byte getFuelPressure ( void  )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getGear()

static byte getGear ( void  )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getMAPDelta()

int16_t getMAPDelta ( void  )

Get the MAP change between the last 2 readings.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getMAPDeltaTime()

uint32_t getMAPDeltaTime ( void  )

Get the time in µS between the last 2 MAP readings.

Here is the caller graph for this function:

◆ getOilPressure()

static byte getOilPressure ( void  )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getSpeed()

static uint16_t getSpeed ( void  )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ initialiseADC()

void initialiseADC ( void  )

Init all ADC conversions by setting resolutions, etc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initialiseCLT()

void initialiseCLT ( void  )

Initial reading of the coolant sensor, primarily to make sure the priming pulsewidth is correct.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initialiseMAPBaro()

void initialiseMAPBaro ( void  )

Initialize the MAP calculation & Baro values.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initialiseTPS()

void initialiseTPS ( void  )

Initial reading of the TPS sensor, primarily to detect flood clear state.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ instanteneousMAPReading()

TESTABLE_INLINE_STATIC bool instanteneousMAPReading ( void  )
Here is the caller graph for this function:

◆ isCTPSSensorActive()

static bool isCTPSSensorActive ( void  )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isCycleCurrent() [1/3]

static bool isCycleCurrent ( const statuses current,
const map_cycle_average_t cycle_avg 
)
inlinestatic
Here is the call graph for this function:

◆ isCycleCurrent() [2/3]

static bool isCycleCurrent ( const statuses current,
const map_cycle_min_t cycle_min 
)
inlinestatic
Here is the call graph for this function:

◆ isCycleCurrent() [3/3]

static bool isCycleCurrent ( const statuses current,
uint32_t  cycleStartIndex 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isIgnitionEventCurrent()

static bool isIgnitionEventCurrent ( const map_event_average_t eventAverage)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isIgnitionEventValid()

static bool isIgnitionEventValid ( const map_event_average_t eventAverage)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isValidBaro()

static bool isValidBaro ( uint8_t  baro)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isValidMapSensorReading()

static bool isValidMapSensorReading ( uint16_t  reading)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ knockPulse()

void knockPulse ( void  )
Here is the caller graph for this function:

◆ mapADCToMAP()

static uint16_t mapADCToMAP ( uint16_t  mapADC,
int8_t  mapMin,
uint16_t  mapMax 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readAnalogPin()

static uint16_t readAnalogPin ( uint8_t  pin)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readAnalogSensor()

static uint16_t readAnalogSensor ( uint8_t  pin)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readAuxanalog()

uint16_t readAuxanalog ( uint8_t  analogPin)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readAuxdigital()

uint16_t readAuxdigital ( uint8_t  digitalPin)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readBaro()

static void readBaro ( void  )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readBat()

static void readBat ( void  )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readCLT()

static void readCLT ( void  )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readFilteredMapADC()

static uint16_t readFilteredMapADC ( uint8_t  pin,
uint8_t  alpha,
uint16_t  prior 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readGear()

static void readGear ( void  )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readIAT()

static void readIAT ( void  )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readMAP()

static void readMAP ( void  )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readMAPSensor()

static uint16_t readMAPSensor ( uint8_t  pin)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readMapSensors()

static map_adc_readings_t readMapSensors ( const map_adc_readings_t previousReadings,
const config4 page4,
bool  useEMAP 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readO2()

static void readO2 ( void  )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readO2_1()

static void readO2_1 ( void  )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readO2_2()

static void readO2_2 ( void  )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readPolledSensors()

void readPolledSensors ( byte  loopTimer)

Read the sensors that are polled at every loop. This includes the TPS, MAP, CLT, IAT and O2 sensors.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readSpeed()

static void readSpeed ( void  )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readTPS() [1/2]

static void readTPS ( uint8_t  tpsADC)
inlinestatic
Here is the call graph for this function:

◆ readTPS() [2/2]

static void readTPS ( void  )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset() [1/3]

static void reset ( const statuses current,
map_cycle_average_t cycle_average,
const map_adc_readings_t sensorReadings 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset() [2/3]

static void reset ( const statuses current,
map_cycle_min_t cycle_min,
const map_adc_readings_t sensorReadings 
)
inlinestatic
Here is the call graph for this function:

◆ reset() [3/3]

static void reset ( map_event_average_t eventAverage,
const map_adc_readings_t sensorReadings 
)
inlinestatic
Here is the call graph for this function:

◆ resetMAPcycleAndEvent()

void resetMAPcycleAndEvent ( void  )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setBaroFromMAP()

static void setBaroFromMAP ( void  )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setBaroFromSensorReading()

static void setBaroFromSensorReading ( uint16_t  sensorReading)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setMAPValuesFromReadings()

static void setMAPValuesFromReadings ( const map_adc_readings_t readings,
const config2 page2,
bool  useEMAP,
statuses current 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ storeLastMAPReadings()

static void storeLastMAPReadings ( map_last_read_t lastRead,
uint16_t  oldMAPValue 
)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateFuelPressure()

static void updateFuelPressure ( void  )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateOilPressure()

static void updateOilPressure ( void  )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ validateFilterMapSensorReading()

TESTABLE_INLINE_STATIC uint16_t validateFilterMapSensorReading ( uint16_t  reading,
uint8_t  alpha,
uint16_t  prior 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vssGetPulseGap()

uint32_t vssGetPulseGap ( uint8_t  historyIndex)

Returns the VSS pulse gap for a given history point.

Parameters
historyIndexThe gap number that is wanted. EG: historyIndex = 0 = Latest entry historyIndex = 1 = 2nd entry entry
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vssPulse()

void vssPulse ( void  )

The ISR function for VSS pulses.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ cltCalibration_bins

table2D_u16_u16_32 cltCalibrationTable & cltCalibration_bins
static

◆ cltCalibration_values

uint16_t cltCalibration_values[32]
static

◆ flexCounter

volatile uint8_t flexCounter = 0U

◆ flexPulseWidth

volatile uint32_t flexPulseWidth = 0U

◆ flexStartTime

volatile uint32_t flexStartTime = 0UL
static

◆ iatCalibration_bins

table2D_u16_u16_32 iatCalibrationTable & iatCalibration_bins
static

◆ iatCalibration_values

uint16_t iatCalibration_values[32]
static

◆ mapAlgorithmState

map_algorithm_t mapAlgorithmState
static

◆ o2Calibration_bins

table2D_u16_u8_32 o2CalibrationTable & o2Calibration_bins
static

◆ o2Calibration_values

uint8_t o2Calibration_values[32]
static

◆ statusSensors

uint8_t statusSensors = 0

◆ VALID_MAP_MAX

constexpr uint16_t VALID_MAP_MAX =1022U
staticconstexpr

◆ VALID_MAP_MIN

constexpr uint16_t VALID_MAP_MIN =2U
staticconstexpr

◆ vssIndex

volatile uint8_t vssIndex = 0U
static

◆ vssTimes

volatile uint32_t vssTimes[VSS_SAMPLES] = {0}
static