@@ -20,6 +20,18 @@ A full copy of the license may be found in the projects root directory
2020#include " auxiliaries.h"
2121#include " utilities.h"
2222#include " unit_testing.h"
23+ #include " sensors_map_structs.h"
24+
25+ bool auxIsEnabled;
26+
27+ static volatile uint32_t vssTimes[VSS_SAMPLES] = {0 };
28+ static volatile uint8_t vssIndex = 0U ;
29+
30+ volatile uint8_t flexCounter = 0U ;
31+ static volatile uint32_t flexStartTime = 0UL ;
32+ volatile uint32_t flexPulseWidth = 0U ;
33+
34+ static map_algorithm_t mapAlgorithmState;
2335
2436/* *
2537 * @brief A specialist function to map a value in the range [0, 1023] (I.e. 10-bit) to a different range.
@@ -42,14 +54,16 @@ A full copy of the license may be found in the projects root directory
4254 * @param rangeMax Maximum of the output range
4355 * @return int16_t
4456 */
45- TESTABLE_INLINE_STATIC int16_t fastMap10Bit (uint16_t value, int16_t rangeMin, int16_t rangeMax) {
57+ TESTABLE_INLINE_STATIC int16_t fastMap10Bit (uint16_t value, int16_t rangeMin, int16_t rangeMax)
58+ {
4659 uint16_t range = rangeMax-rangeMin; // Must be positive (assuming rangeMax>=rangeMin)
4760 uint16_t fromStartOfRange = (uint16_t )rshift<10 >((uint32_t )value * range);
4861 return rangeMin + (int16_t )fromStartOfRange;
4962}
5063
5164//
52- static inline uint16_t readAnalogPin (uint8_t pin) {
65+ static inline uint16_t readAnalogPin (uint8_t pin)
66+ {
5367 // Why do we read twice? Who knows.....
5468 analogRead (pin);
5569 // According to the docs, analogRead result should be in range 0-1023
@@ -60,14 +74,6 @@ static inline uint16_t readAnalogPin(uint8_t pin) {
6074 return max (0 , tmp);
6175}
6276
63- bool auxIsEnabled;
64-
65- static volatile uint32_t vssTimes[VSS_SAMPLES] = {0 };
66- static volatile uint8_t vssIndex = 0U ;
67-
68- volatile uint8_t flexCounter = 0U ;
69- static volatile uint32_t flexStartTime = 0UL ;
70- volatile uint32_t flexPulseWidth = 0U ;
7177
7278#if defined(ANALOG_ISR)
7379static volatile uint16_t AnChannel[16 ];
@@ -241,8 +247,6 @@ void initialiseADC(void)
241247static constexpr uint16_t VALID_MAP_MAX=1022U ; // The largest ADC value that is valid for the MAP sensor
242248static constexpr uint16_t VALID_MAP_MIN=2U ; // The smallest ADC value that is valid for the MAP sensor
243249
244- #include " sensors_map_structs.h"
245-
246250TESTABLE_INLINE_STATIC bool instanteneousMAPReading (void )
247251{
248252 // All we need to do it signal that the new readings should be used as-is
@@ -436,29 +440,6 @@ TESTABLE_INLINE_STATIC bool eventAverageMAPReading(const statuses ¤t, cons
436440 return instanteneousMAPReading ();
437441}
438442
439- static inline bool processMapReadings (const statuses ¤t, const config2 &page2, map_algorithm_t &state) {
440- // MAP Sampling system
441- switch (page2.mapSample )
442- {
443- case MAPSamplingCycleAverage:
444- return cycleAverageMAPReading (current, page2, state.cycle_average , state.sensorReadings );
445- break ;
446-
447- case MAPSamplingCycleMinimum:
448- return cycleMinimumMAPReading (current, page2, state.cycle_min , state.sensorReadings );
449- break ;
450-
451- case MAPSamplingIgnitionEventAverage:
452- return eventAverageMAPReading (current, page2, state.event_average , state.sensorReadings );
453- break ;
454-
455- case MAPSamplingInstantaneous:
456- default :
457- return instanteneousMAPReading ();
458- break ;
459- }
460- }
461-
462443static inline bool isValidMapSensorReading (uint16_t reading) {
463444 return (reading < VALID_MAP_MAX) && (reading > VALID_MAP_MIN);
464445}
@@ -482,7 +463,8 @@ static inline map_adc_readings_t readMapSensors(const map_adc_readings_t &previo
482463 };
483464}
484465
485- static inline void resetMAPLast (map_last_read_t &lastRead, uint16_t oldMAPValue) {
466+ static inline void storeLastMAPReadings (map_last_read_t &lastRead, uint16_t oldMAPValue)
467+ {
486468 // Update the calculation times and last value. These are used by the MAP based Accel enrich
487469 uint32_t currTime = micros ();
488470 lastRead.lastMAPValue = oldMAPValue;
@@ -491,42 +473,56 @@ static inline void resetMAPLast(map_last_read_t &lastRead, uint16_t oldMAPValue)
491473 lastRead.currentReadingTime = currTime;
492474}
493475
494- static inline uint16_t mapADCToMAP (uint16_t mapADC, int8_t mapMin, uint16_t mapMax) {
476+ static inline uint16_t mapADCToMAP (uint16_t mapADC, int8_t mapMin, uint16_t mapMax)
477+ {
495478 int16_t mapped = fastMap10Bit (mapADC, mapMin, mapMax); // Get the current MAP value
496479 return max ((int16_t )0 , mapped); // Sanity check
497480}
498481
499- static inline void setMAPValuesFromReadings (const map_adc_readings_t &readings, const config2 &page2, bool useEMAP, statuses ¤t) {
482+ static inline void setMAPValuesFromReadings (const map_adc_readings_t &readings, const config2 &page2, bool useEMAP, statuses ¤t)
483+ {
500484 current.MAP = mapADCToMAP (readings.mapADC , page2.mapMin , page2.mapMax ); // Get the current MAP value
501485 // Repeat for EMAP if it's enabled
502- if (useEMAP) {
503- current.EMAP = mapADCToMAP (readings.emapADC , page2.EMAPMin , page2.EMAPMax );
504- }
486+ if (useEMAP) { current.EMAP = mapADCToMAP (readings.emapADC , page2.EMAPMin , page2.EMAPMax ); }
505487}
506488
507- static map_algorithm_t mapAlgorithmState;
508-
509489#if defined(UNIT_TEST)
510490map_last_read_t & getMapLast (void ){
511491 return mapAlgorithmState.lastReading ;
512492}
513493#endif
514494
515- static void initialiseMAP (void ) {
516- (void )memset (&mapAlgorithmState, 0 , sizeof (mapAlgorithmState));
517- }
518-
519495void readMAP (void )
520496{
521- // Read sensor(s)
497+ // Read sensor(s). Saves filtered ADC readings. Does not set calibrated MAP and EMAP values.
522498 mapAlgorithmState.sensorReadings = readMapSensors (mapAlgorithmState.sensorReadings , configPage4, configPage6.useEMAP );
523499
524- // Process sensor readings according to user chosen sampling algorithm
525- if (processMapReadings (currentStatus, configPage2, mapAlgorithmState)) {
526- // Sampling algorithm has decided that the new readings should be used
500+ bool readingIsValid;
501+ switch (configPage2.mapSample )
502+ {
503+ case MAPSamplingCycleAverage:
504+ readingIsValid = cycleAverageMAPReading (currentStatus, configPage2, mapAlgorithmState.cycle_average , mapAlgorithmState.sensorReadings );
505+ break ;
506+
507+ case MAPSamplingCycleMinimum:
508+ readingIsValid = cycleMinimumMAPReading (currentStatus, configPage2, mapAlgorithmState.cycle_min , mapAlgorithmState.sensorReadings );
509+ break ;
527510
511+ case MAPSamplingIgnitionEventAverage:
512+ readingIsValid = eventAverageMAPReading (currentStatus, configPage2, mapAlgorithmState.event_average , mapAlgorithmState.sensorReadings );
513+ break ;
514+
515+ case MAPSamplingInstantaneous:
516+ default :
517+ readingIsValid = instanteneousMAPReading ();
518+ break ;
519+ }
520+
521+ // Process sensor readings according to user chosen sampling algorithm
522+ if (readingIsValid)
523+ {
528524 // Roll over the last reading
529- resetMAPLast (mapAlgorithmState.lastReading , currentStatus.MAP );
525+ storeLastMAPReadings (mapAlgorithmState.lastReading , currentStatus.MAP );
530526
531527 // Convert from filtered sensor readings to kPa
532528 setMAPValuesFromReadings (mapAlgorithmState.sensorReadings , configPage2, configPage6.useEMAP , currentStatus);
@@ -607,21 +603,24 @@ void readIAT(void)
607603* with record highs close to 108.5 kPa.
608604* The lowest possible baro reading is based on an altitude of 3500m above sea level.
609605*/
610- static inline bool isValidBaro (uint8_t baro) {
611- static constexpr uint16_t BARO_MIN = 65U ;
612- static constexpr uint16_t BARO_MAX = 108U ;
606+ static inline bool isValidBaro (uint8_t baro)
607+ {
608+ static constexpr uint16_t BARO_MIN = 65U ;
609+ static constexpr uint16_t BARO_MAX = 108U ;
613610
614611 return (baro >= BARO_MIN) && (baro <= BARO_MAX);
615612}
616613
617- static inline void setBaroFromSensorReading (uint16_t sensorReading) {
614+ static inline void setBaroFromSensorReading (uint16_t sensorReading)
615+ {
618616 currentStatus.baroADC = sensorReading;
619617 int16_t tempValue = fastMap10Bit (currentStatus.baroADC , configPage2.baroMin , configPage2.baroMax );
620618 currentStatus.baro = (uint8_t )max ((int16_t )0 , tempValue);
621619}
622620
623621// Should only be called when the engine isn't running.
624- static inline void setBaroFromMAP (void ) {
622+ static inline void setBaroFromMAP (void )
623+ {
625624 uint16_t tempReading = mapADCToMAP (readMAPSensor (pinMAP), configPage2.mapMin , configPage2.mapMax );
626625 if (isValidBaro (tempReading)) // Safety check to ensure the baro reading is within the physical limits
627626 {
@@ -632,7 +631,8 @@ static inline void setBaroFromMAP(void) {
632631
633632void readBaro (void )
634633{
635- if ( configPage6.useExtBaro != 0U ) {
634+ if ( configPage6.useExtBaro != 0U )
635+ {
636636 // readings
637637 setBaroFromSensorReading (LOW_PASS_FILTER (readMAPSensor (pinBaro), configPage4.ADCFILTER_BARO , currentStatus.baroADC )); // Very weak filter
638638 // If no dedicated baro sensor is available, attempt to get a reading from the MAP sensor. This can only be done if the engine is not running.
@@ -643,7 +643,12 @@ void readBaro(void)
643643 }
644644}
645645
646- static inline void initialiseBaro (void ) {
646+ void initialiseMAPBaro (void )
647+ {
648+ // Initialise MAP values to all 0's
649+ (void )memset (&mapAlgorithmState, 0 , sizeof (mapAlgorithmState));
650+
651+ // Initialise baro
647652 if ( configPage6.useExtBaro != 0U )
648653 {
649654 // Use raw unfiltered value initially
@@ -660,9 +665,11 @@ static inline void initialiseBaro(void) {
660665 }
661666}
662667
663- void initialiseMAPBaro (void ) {
664- initialiseMAP ();
665- initialiseBaro ();
668+ void resetMAPcycleAndEvent (void )
669+ {
670+ (void )memset (&mapAlgorithmState.cycle_average , 0 , sizeof (mapAlgorithmState.cycle_average ));
671+ (void )memset (&mapAlgorithmState.cycle_min , 0 , sizeof (mapAlgorithmState.cycle_min ));
672+ (void )memset (&mapAlgorithmState.event_average , 0 , sizeof (mapAlgorithmState.event_average ));
666673}
667674
668675// ========================================== O2 ==========================================
0 commit comments