Fix average cycle MAP reading when engine is off (Also begins move of sensor readings to own file)

This commit is contained in:
Josh Stewart 2016-03-09 16:40:11 +11:00
parent 5f25a86c52
commit 2fb51787ad
5 changed files with 109 additions and 80 deletions

View File

@ -43,14 +43,6 @@ const int map_page_size = 288;
#define INJ_SEMISEQUENTIAL 1
#define INJ_SEQUENTIAL 2
// The following are alpha values for the ADC filters.
// Their values are from 0 to 255 with 0 being no filtering and 255 being maximum
#define ADCFILTER_TPS 128
#define ADCFILTER_CLT 180
#define ADCFILTER_IAT 180
#define ADCFILTER_O2 128
#define ADCFILTER_BAT 128
#define SIZE_BYTE 8
#define SIZE_INT 16

17
sensors.h Normal file
View File

@ -0,0 +1,17 @@
// The following are alpha values for the ADC filters.
// Their values are from 0 to 255 with 0 being no filtering and 255 being maximum
#define ADCFILTER_TPS 128
#define ADCFILTER_CLT 180
#define ADCFILTER_IAT 180
#define ADCFILTER_O2 128
#define ADCFILTER_BAT 128
/*
* Simple low pass IIR filter macro for the analog inputs
* This is effectively implementing the smooth filter from http://playground.arduino.cc/Main/Smooth
* But removes the use of floats and uses 8 bits of fixed precision.
*/
#define ADC_FILTER(input, alpha, prior) (((long)input * (256 - alpha) + ((long)prior * alpha))) >> 8
void instanteneousMAPReading();
void readMAP();

89
sensors.ino Normal file
View File

@ -0,0 +1,89 @@
/*
Speeduino - Simple engine management for the Arduino Mega 2560 platform
Copyright (C) Josh Stewart
A full copy of the license may be found in the projects root directory
*/
void instanteneousMAPReading()
{
//Instantaneous MAP readings
int tempReading;
tempReading = analogRead(pinMAP);
tempReading = analogRead(pinMAP);
//Error checking
if(tempReading >= VALID_MAP_MAX || tempReading <= VALID_MAP_MIN) { mapErrorCount += 1; }
else { currentStatus.mapADC = tempReading; mapErrorCount = 0; }
currentStatus.MAP = map(currentStatus.mapADC, 0, 1023, configPage1.mapMin, configPage1.mapMax); //Get the current MAP value
}
void readMAP()
{
//MAP Sampling system
int tempReading;
switch(configPage1.mapSample)
{
case 0:
//Instantaneous MAP readings
instanteneousMAPReading();
break;
case 1:
//Average of a cycle
if (currentStatus.RPM < 1) { instanteneousMAPReading(); return; } //If the engine isn't running, fall back to instantaneous reads
if( (MAPcurRev == startRevolutions) || (MAPcurRev == startRevolutions+1) ) //2 revolutions are looked at for 4 stroke. 2 stroke not currently catered for.
{
tempReading = analogRead(pinMAP);
tempReading = analogRead(pinMAP);
//Error check
if(tempReading < VALID_MAP_MAX && tempReading > VALID_MAP_MIN)
{
MAPrunningValue = MAPrunningValue + tempReading; //Add the current reading onto the total
MAPcount++;
}
else { mapErrorCount += 1; }
}
else
{
//Reaching here means that the last cylce has completed and the MAP value should be calculated
currentStatus.mapADC = ldiv(MAPrunningValue, MAPcount).quot;
currentStatus.MAP = map(currentStatus.mapADC, 0, 1023, configPage1.mapMin, configPage1.mapMax); //Get the current MAP value
MAPcurRev = startRevolutions; //Reset the current rev count
MAPrunningValue = 0;
MAPcount = 0;
}
break;
case 2:
//Minimum reading in a cycle
if (currentStatus.RPM < 1) { instanteneousMAPReading(); return; } //If the engine isn't running, fall back to instantaneous reads
if( (MAPcurRev == startRevolutions) || (MAPcurRev == startRevolutions+1) ) //2 revolutions are looked at for 4 stroke. 2 stroke not currently catered for.
{
tempReading = analogRead(pinMAP);
tempReading = analogRead(pinMAP);
//Error check
if(tempReading < VALID_MAP_MAX && tempReading > VALID_MAP_MIN)
{
if( tempReading < MAPrunningValue) { MAPrunningValue = tempReading; } //Check whether the current reading is lower than the running minimum
}
else { mapErrorCount += 1; }
}
else
{
//Reaching here means that the last cylce has completed and the MAP value should be calculated
currentStatus.mapADC = MAPrunningValue;
currentStatus.MAP = map(currentStatus.mapADC, 0, 1023, configPage1.mapMin, configPage1.mapMax); //Get the current MAP value
MAPcurRev = startRevolutions; //Reset the current rev count
MAPrunningValue = 1023; //Reset the latest value so the next reading will always be lower
}
break;
}
}

View File

@ -36,6 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "idle.h"
#include "auxiliaries.h"
#include "fastAnalog.h"
#include "sensors.h"
#include "libs/PID_v1/PID_v1.h"
#ifdef __SAM3X8E__
@ -660,72 +661,9 @@ void loop()
currentStatus.RPM = 500;
*/
//***SET STATUSES***
//***Perform sensor reads***
//-----------------------------------------------------------------------------------------------------
//MAP Sampling system
int tempReading;
switch(configPage1.mapSample)
{
case 0:
//Instantaneous MAP readings
tempReading = analogRead(pinMAP);
//Error checking
if(tempReading >= VALID_MAP_MAX || tempReading <= VALID_MAP_MIN) { mapErrorCount += 1; }
else { currentStatus.mapADC = tempReading; mapErrorCount = 0; }
currentStatus.MAP = map(currentStatus.mapADC, 0, 1023, configPage1.mapMin, configPage1.mapMax); //Get the current MAP value
break;
case 1:
//Average of a cycle
if( (MAPcurRev == startRevolutions) || (MAPcurRev == startRevolutions+1) ) //2 revolutions are looked at for 4 stroke. 2 stroke not currently catered for.
{
tempReading = analogRead(pinMAP);
//Error check
if(tempReading < VALID_MAP_MAX && tempReading > VALID_MAP_MIN)
{
MAPrunningValue = MAPrunningValue + tempReading; //Add the current reading onto the total
MAPcount++;
}
else { mapErrorCount += 1; }
}
else
{
//Reaching here means that the last cylce has completed and the MAP value should be calculated
currentStatus.mapADC = ldiv(MAPrunningValue, MAPcount).quot;
currentStatus.MAP = map(currentStatus.mapADC, 0, 1023, configPage1.mapMin, configPage1.mapMax); //Get the current MAP value
MAPcurRev = startRevolutions; //Reset the current rev count
MAPrunningValue = 0;
MAPcount = 0;
}
break;
case 2:
//Minimum reading in a cycle
if( (MAPcurRev == startRevolutions) || (MAPcurRev == startRevolutions+1) ) //2 revolutions are looked at for 4 stroke. 2 stroke not currently catered for.
{
tempReading = analogRead(pinMAP);
//Error check
if(tempReading < VALID_MAP_MAX && tempReading > VALID_MAP_MIN)
{
if( tempReading < MAPrunningValue) { MAPrunningValue = tempReading; } //Check whether the current reading is lower than the running minimum
}
else { mapErrorCount += 1; }
}
else
{
//Reaching here means that the last cylce has completed and the MAP value should be calculated
currentStatus.mapADC = MAPrunningValue;
currentStatus.MAP = map(currentStatus.mapADC, 0, 1023, configPage1.mapMin, configPage1.mapMax); //Get the current MAP value
MAPcurRev = startRevolutions; //Reset the current rev count
MAPrunningValue = 1023; //Reset the latest value so the next reading will always be lower
}
break;
}
readMAP();
//TPS setting to be performed every 32 loops (any faster and it can upset the TPSdot sampling time)
if ((mainLoopCount & 31) == 1)

View File

@ -8,13 +8,6 @@ These are some utility functions and variables used through the main code
#define MS_IN_MINUTE 60000
#define US_IN_MINUTE 60000000
/*
* Simple low pass IIR filter macro for the analog inputs
* This is effectively implementing the smooth filter from http://playground.arduino.cc/Main/Smooth
* But removes the use of floats and uses 8 bits of fixed precision.
*/
#define ADC_FILTER(input, alpha, prior) (((long)input * (256 - alpha) + ((long)prior * alpha))) >> 8
int freeRam ();
void setPinMapping(byte boardID);
unsigned int PW();