Merge pull request #35 from classifiedperformance/master

Tidying up
This commit is contained in:
Josh Stewart 2016-06-11 20:00:21 +10:00 committed by GitHub
commit 77158ead74
3 changed files with 93 additions and 94 deletions

View File

@ -11,7 +11,7 @@ void initialiseSchedulers()
{ {
// Much help in this from http://arduinomega.blogspot.com.au/2011/05/timer2-and-overflow-interrupt-lets-get.html // Much help in this from http://arduinomega.blogspot.com.au/2011/05/timer2-and-overflow-interrupt-lets-get.html
//Fuel Schedules, which uses timer 3 //Fuel Schedules, which uses timer 3
TCCR3B = 0x00; //Disbale Timer3 while we set it up TCCR3B = 0x00; //Disable Timer3 while we set it up
TCNT3 = 0; //Reset Timer Count TCNT3 = 0; //Reset Timer Count
TIFR3 = 0x00; //Timer3 INT Flag Reg: Clear Timer Overflow Flag TIFR3 = 0x00; //Timer3 INT Flag Reg: Clear Timer Overflow Flag
TCCR3A = 0x00; //Timer3 Control Reg A: Wave Gen Mode normal TCCR3A = 0x00; //Timer3 Control Reg A: Wave Gen Mode normal
@ -22,7 +22,7 @@ void initialiseSchedulers()
fuelSchedule3.Status = OFF; fuelSchedule3.Status = OFF;
//Ignition Schedules, which uses timer 5 //Ignition Schedules, which uses timer 5
TCCR5B = 0x00; //Disbale Timer3 while we set it up TCCR5B = 0x00; //Disable Timer3 while we set it up
TCNT5 = 0; //Reset Timer Count TCNT5 = 0; //Reset Timer Count
TIFR5 = 0x00; //Timer5 INT Flag Reg: Clear Timer Overflow Flag TIFR5 = 0x00; //Timer5 INT Flag Reg: Clear Timer Overflow Flag
TCCR5A = 0x00; //Timer5 Control Reg A: Wave Gen Mode normal TCCR5A = 0x00; //Timer5 Control Reg A: Wave Gen Mode normal
@ -33,7 +33,7 @@ void initialiseSchedulers()
ignitionSchedule3.Status = OFF; ignitionSchedule3.Status = OFF;
//The remaining Schedules (Schedules 4 for fuel and ignition) use Timer4 //The remaining Schedules (Schedules 4 for fuel and ignition) use Timer4
TCCR4B = 0x00; //Disbale Timer4 while we set it up TCCR4B = 0x00; //Disable Timer4 while we set it up
TCNT4 = 0; //Reset Timer Count TCNT4 = 0; //Reset Timer Count
TIFR4 = 0x00; //Timer4 INT Flag Reg: Clear Timer Overflow Flag TIFR4 = 0x00; //Timer4 INT Flag Reg: Clear Timer Overflow Flag
TCCR4A = 0x00; //Timer4 Control Reg A: Wave Gen Mode normal TCCR4A = 0x00; //Timer4 Control Reg A: Wave Gen Mode normal

View File

@ -3,6 +3,7 @@ Speeduino - Simple engine management for the Arduino Mega 2560 platform
Copyright (C) Josh Stewart Copyright (C) Josh Stewart
A full copy of the license may be found in the projects root directory A full copy of the license may be found in the projects root directory
*/ */
int tempReading; int tempReading;
void instanteneousMAPReading() void instanteneousMAPReading()
@ -15,126 +16,124 @@ void instanteneousMAPReading()
if(tempReading >= VALID_MAP_MAX || tempReading <= VALID_MAP_MIN) { mapErrorCount += 1; } if(tempReading >= VALID_MAP_MAX || tempReading <= VALID_MAP_MIN) { mapErrorCount += 1; }
else { currentStatus.mapADC = tempReading; mapErrorCount = 0; } else { currentStatus.mapADC = tempReading; mapErrorCount = 0; }
currentStatus.MAP = map(currentStatus.mapADC, 0, 1023, configPage1.mapMin, configPage1.mapMax); //Get the current MAP value currentStatus.MAP = fastMap1023toX(currentStatus.mapADC, 0, 1023, configPage1.mapMin, configPage1.mapMax); //Get the current MAP value
} }
void readMAP() void readMAP()
{ {
//MAP Sampling system //MAP Sampling system
switch(configPage1.mapSample) switch(configPage1.mapSample)
{ {
case 0: case 0:
//Instantaneous MAP readings //Instantaneous MAP readings
instanteneousMAPReading(); instanteneousMAPReading();
break; 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: case 1:
//Minimum reading in a cycle //Average of a cycle
if (currentStatus.RPM < 1) { instanteneousMAPReading(); return; } //If the engine isn't running, fall back to instantaneous reads 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);
if( (MAPcurRev == startRevolutions) || (MAPcurRev == startRevolutions+1) ) //2 revolutions are looked at for 4 stroke. 2 stroke not currently catered for. //Error check
if(tempReading < VALID_MAP_MAX && tempReading > VALID_MAP_MIN)
{ {
tempReading = analogRead(pinMAP); MAPrunningValue = MAPrunningValue + tempReading; //Add the current reading onto the total
tempReading = analogRead(pinMAP); MAPcount++;
//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 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 = fastMap1023toX(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)
{ {
//Reaching here means that the last cylce has completed and the MAP value should be calculated if( tempReading < MAPrunningValue) { MAPrunningValue = tempReading; } //Check whether the current reading is lower than the running minimum
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; 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 = fastMap1023toX(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;
}
} }
void readTPS() void readTPS()
{ {
currentStatus.TPSlast = currentStatus.TPS; currentStatus.TPSlast = currentStatus.TPS;
currentStatus.TPSlast_time = currentStatus.TPS_time; currentStatus.TPSlast_time = currentStatus.TPS_time;
analogRead(pinTPS); analogRead(pinTPS);
byte tempTPS = fastMap1023toX(analogRead(pinTPS), 0, 1023, 0, 255); //Get the current raw TPS ADC value and map it into a byte byte tempTPS = fastMap1023toX(analogRead(pinTPS), 0, 1023, 0, 255); //Get the current raw TPS ADC value and map it into a byte
currentStatus.tpsADC = ADC_FILTER(tempTPS, ADCFILTER_TPS, currentStatus.tpsADC); currentStatus.tpsADC = ADC_FILTER(tempTPS, ADCFILTER_TPS, currentStatus.tpsADC);
//Check that the ADC values fall within the min and max ranges (Should always be the case, but noise can cause these to fluctuate outside the defined range). //Check that the ADC values fall within the min and max ranges (Should always be the case, but noise can cause these to fluctuate outside the defined range).
byte tempADC = currentStatus.tpsADC; //The tempADC value is used in order to allow TunerStudio to recover and redo the TPS calibration if this somehow gets corrupted byte tempADC = currentStatus.tpsADC; //The tempADC value is used in order to allow TunerStudio to recover and redo the TPS calibration if this somehow gets corrupted
if (currentStatus.tpsADC < configPage1.tpsMin) { tempADC = configPage1.tpsMin; } if (currentStatus.tpsADC < configPage1.tpsMin) { tempADC = configPage1.tpsMin; }
else if(currentStatus.tpsADC > configPage1.tpsMax) { tempADC = configPage1.tpsMax; } else if(currentStatus.tpsADC > configPage1.tpsMax) { tempADC = configPage1.tpsMax; }
currentStatus.TPS = map(tempADC, configPage1.tpsMin, configPage1.tpsMax, 0, 100); //Take the raw TPS ADC value and convert it into a TPS% based on the calibrated values currentStatus.TPS = map(tempADC, configPage1.tpsMin, configPage1.tpsMax, 0, 100); //Take the raw TPS ADC value and convert it into a TPS% based on the calibrated values
currentStatus.TPS_time = currentLoopTime; currentStatus.TPS_time = currentLoopTime;
} }
void readCLT() void readCLT()
{ {
tempReading = analogRead(pinCLT); tempReading = analogRead(pinCLT);
tempReading = fastMap1023toX(analogRead(pinCLT), 0, 1023, 0, 511); //Get the current raw CLT value tempReading = fastMap1023toX(analogRead(pinCLT), 0, 1023, 0, 511); //Get the current raw CLT value
currentStatus.cltADC = ADC_FILTER(tempReading, ADCFILTER_CLT, currentStatus.cltADC); currentStatus.cltADC = ADC_FILTER(tempReading, ADCFILTER_CLT, currentStatus.cltADC);
currentStatus.coolant = cltCalibrationTable[currentStatus.cltADC] - CALIBRATION_TEMPERATURE_OFFSET; //Temperature calibration values are stored as positive bytes. We subtract 40 from them to allow for negative temperatures currentStatus.coolant = cltCalibrationTable[currentStatus.cltADC] - CALIBRATION_TEMPERATURE_OFFSET; //Temperature calibration values are stored as positive bytes. We subtract 40 from them to allow for negative temperatures
} }
void readIAT() void readIAT()
{ {
tempReading = analogRead(pinIAT); tempReading = analogRead(pinIAT);
tempReading = map(analogRead(pinIAT), 0, 1023, 0, 511); //Get the current raw IAT value tempReading = fastMap1023toX(analogRead(pinIAT), 0, 1023, 0, 511); //Get the current raw IAT value
currentStatus.iatADC = ADC_FILTER(tempReading, ADCFILTER_IAT, currentStatus.iatADC); currentStatus.iatADC = ADC_FILTER(tempReading, ADCFILTER_IAT, currentStatus.iatADC);
currentStatus.IAT = iatCalibrationTable[currentStatus.iatADC] - CALIBRATION_TEMPERATURE_OFFSET; currentStatus.IAT = iatCalibrationTable[currentStatus.iatADC] - CALIBRATION_TEMPERATURE_OFFSET;
} }
void readO2() void readO2()
{ {
tempReading = analogRead(pinO2); tempReading = analogRead(pinO2);
tempReading = map(analogRead(pinO2), 0, 1023, 0, 511); //Get the current O2 value. tempReading = fastMap1023toX(analogRead(pinO2), 0, 1023, 0, 511); //Get the current O2 value.
currentStatus.O2ADC = ADC_FILTER(tempReading, ADCFILTER_O2, currentStatus.O2ADC); currentStatus.O2ADC = ADC_FILTER(tempReading, ADCFILTER_O2, currentStatus.O2ADC);
currentStatus.O2 = o2CalibrationTable[currentStatus.O2ADC]; currentStatus.O2 = o2CalibrationTable[currentStatus.O2ADC];
} }
/* Second O2 currently disabled as its not being used /* Second O2 currently disabled as its not being used
currentStatus.O2_2ADC = map(analogRead(pinO2_2), 0, 1023, 0, 511); //Get the current O2 value. currentStatus.O2_2ADC = map(analogRead(pinO2_2), 0, 1023, 0, 511); //Get the current O2 value.
currentStatus.O2_2ADC = ADC_FILTER(tempReading, ADCFILTER_O2, currentStatus.O2_2ADC); currentStatus.O2_2ADC = ADC_FILTER(tempReading, ADCFILTER_O2, currentStatus.O2_2ADC);
currentStatus.O2_2 = o2CalibrationTable[currentStatus.O2_2ADC]; currentStatus.O2_2 = o2CalibrationTable[currentStatus.O2_2ADC];
*/ */
void readBat() void readBat()
{ {
tempReading = analogRead(pinBat); tempReading = analogRead(pinBat);
tempReading = fastMap1023toX(analogRead(pinBat), 0, 1023, 0, 245); //Get the current raw Battery value. Permissible values are from 0v to 24.5v (245) tempReading = fastMap1023toX(analogRead(pinBat), 0, 1023, 0, 245); //Get the current raw Battery value. Permissible values are from 0v to 24.5v (245)
currentStatus.battery10 = ADC_FILTER(tempReading, ADCFILTER_BAT, currentStatus.battery10); currentStatus.battery10 = ADC_FILTER(tempReading, ADCFILTER_BAT, currentStatus.battery10);
} }

View File

@ -718,7 +718,7 @@ void loop()
if(toothHistoryIndex > TOOTH_LOG_SIZE) { BIT_SET(currentStatus.squirt, BIT_SQUIRT_TOOTHLOG1READY); } if(toothHistoryIndex > TOOTH_LOG_SIZE) { BIT_SET(currentStatus.squirt, BIT_SQUIRT_TOOTHLOG1READY); }
} }
//The IAT and CLT readings can be done less frequently. This still runs about 4 times per secondl //The IAT and CLT readings can be done less frequently. This still runs about 4 times per second
if ((mainLoopCount & 255) == 1) if ((mainLoopCount & 255) == 1)
{ {