From 69ed610d7665d4eab4cbf9237a68ecad5cdfaaa0 Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Wed, 23 Mar 2016 22:09:04 +1100 Subject: [PATCH] Movement of all sensor reads into their own file --- sensors.ino | 55 +++++++++++++++++++++++++++++++++++++++++++++++++-- speeduino.ino | 50 ++++++++++------------------------------------ 2 files changed, 63 insertions(+), 42 deletions(-) diff --git a/sensors.ino b/sensors.ino index 0134ac53..1e8cb078 100644 --- a/sensors.ino +++ b/sensors.ino @@ -3,11 +3,11 @@ 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 */ +int tempReading; void instanteneousMAPReading() { //Instantaneous MAP readings - int tempReading; tempReading = analogRead(pinMAP); tempReading = analogRead(pinMAP); @@ -21,7 +21,6 @@ void instanteneousMAPReading() void readMAP() { //MAP Sampling system - int tempReading; switch(configPage1.mapSample) { case 0: @@ -87,3 +86,55 @@ void readMAP() } } +void readTPS() +{ + currentStatus.TPSlast = currentStatus.TPS; + currentStatus.TPSlast_time = currentStatus.TPS_time; + analogRead(pinTPS); + 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); + //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 + if (currentStatus.tpsADC < configPage1.tpsMin) { tempADC = configPage1.tpsMin; } + 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_time = currentLoopTime; +} + +void readCLT() +{ + tempReading = analogRead(pinCLT); + tempReading = fastMap1023toX(analogRead(pinCLT), 0, 1023, 0, 511); //Get the current raw CLT value + 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 +} + +void readIAT() +{ + tempReading = analogRead(pinIAT); + tempReading = map(analogRead(pinIAT), 0, 1023, 0, 511); //Get the current raw IAT value + currentStatus.iatADC = ADC_FILTER(tempReading, ADCFILTER_IAT, currentStatus.iatADC); + currentStatus.IAT = iatCalibrationTable[currentStatus.iatADC] - CALIBRATION_TEMPERATURE_OFFSET; +} + +void readO2() +{ + tempReading = analogRead(pinO2); + tempReading = map(analogRead(pinO2), 0, 1023, 0, 511); //Get the current O2 value. + currentStatus.O2ADC = ADC_FILTER(tempReading, ADCFILTER_O2, currentStatus.O2ADC); + currentStatus.O2 = o2CalibrationTable[currentStatus.O2ADC]; +} + + /* 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 = ADC_FILTER(tempReading, ADCFILTER_O2, currentStatus.O2_2ADC); + currentStatus.O2_2 = o2CalibrationTable[currentStatus.O2_2ADC]; + */ + +void readBat() +{ + 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) + currentStatus.battery10 = ADC_FILTER(tempReading, ADCFILTER_BAT, currentStatus.battery10); +} + diff --git a/speeduino.ino b/speeduino.ino index 71f079d6..e272d8ec 100644 --- a/speeduino.ino +++ b/speeduino.ino @@ -137,6 +137,8 @@ volatile bool fpPrimed = false; //Tracks whether or not the fuel pump priming ha void setup() { + Serial.begin(115200); + //Setup the dummy fuel and ignition tables //dummyFuelTable(&fuelTable); //dummyIgnitionTable(&ignitionTable); @@ -415,7 +417,7 @@ void setup() previousLoopTime = 0; currentLoopTime = micros(); - Serial.begin(115200); + //This sets the ADC (Analog to Digitial Converter) to run at 1Mhz, greatly reducing analog read times (MAP/TPS) //1Mhz is the fastest speed permitted by the CPU without affecting accuracy @@ -485,7 +487,7 @@ void setup() channel1InjDegrees = 0; channel2InjDegrees = 180; } - else if (configPage1.injTiming == INJ_SEQUENTIAL) + else if (useSequentialFuel) { channel1InjDegrees = 0; channel2InjDegrees = 180; @@ -668,19 +670,9 @@ void loop() //TPS setting to be performed every 32 loops (any faster and it can upset the TPSdot sampling time) if ((mainLoopCount & 31) == 1) { - currentStatus.TPSlast = currentStatus.TPS; - currentStatus.TPSlast_time = currentStatus.TPS_time; - analogRead(pinTPS); - 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); - //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 - if (currentStatus.tpsADC < configPage1.tpsMin) { tempADC = configPage1.tpsMin; } - 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_time = currentLoopTime; + readTPS(); - //Check for launch in (clutch) can be done around here too + //Check for launching (clutch) can be done around here too currentStatus.launching = !digitalRead(pinLaunch); //And check whether the tooth log buffer is ready if(toothHistoryIndex > TOOTH_LOG_SIZE) { BIT_SET(currentStatus.squirt, BIT_SQUIRT_TOOTHLOG1READY); } @@ -689,33 +681,11 @@ void loop() //The IAT and CLT readings can be done less frequently. This still runs about 4 times per second if ((mainLoopCount & 255) == 1) { - int tempReading; - tempReading = analogRead(pinCLT); - tempReading = fastMap1023toX(analogRead(pinCLT), 0, 1023, 0, 511); //Get the current raw CLT value - 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 - - tempReading = analogRead(pinIAT); - tempReading = map(analogRead(pinIAT), 0, 1023, 0, 511); //Get the current raw IAT value - currentStatus.iatADC = ADC_FILTER(tempReading, ADCFILTER_IAT, currentStatus.iatADC); - currentStatus.IAT = iatCalibrationTable[currentStatus.iatADC] - CALIBRATION_TEMPERATURE_OFFSET; - - tempReading = analogRead(pinO2); - tempReading = map(analogRead(pinO2), 0, 1023, 0, 511); //Get the current O2 value. - currentStatus.O2ADC = ADC_FILTER(tempReading, ADCFILTER_O2, currentStatus.O2ADC); - currentStatus.O2 = o2CalibrationTable[currentStatus.O2ADC]; - - /* 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 = ADC_FILTER(tempReading, ADCFILTER_O2, currentStatus.O2_2ADC); - currentStatus.O2_2 = o2CalibrationTable[currentStatus.O2_2ADC]; - */ - - 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) - currentStatus.battery10 = ADC_FILTER(tempReading, ADCFILTER_BAT, currentStatus.battery10); - + readCLT(); + readIAT(); + readO2(); + readBat(); vvtControl(); boostControl(); //Most boost tends to run at about 30Hz, so placing it here ensures a new target time is fetched frequently enough