diff --git a/speeduino/sensors.h b/speeduino/sensors.h index 0d46b098..30736c23 100644 --- a/speeduino/sensors.h +++ b/speeduino/sensors.h @@ -3,7 +3,6 @@ #include "Arduino.h" - // 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 @@ -30,6 +29,7 @@ unsigned long MAPrunningValue; //Used for tracking either the total of all MAP r unsigned long EMAPrunningValue; //As above but for EMAP unsigned int MAPcount; //Number of samples taken in the current MAP cycle uint32_t MAPcurRev; //Tracks which revolution we're sampling on +bool auxIsEnabled; //These variables are used for tracking the number of running sensors values that appear to be errors. Once a threshold is reached, the sensor reading will go to default value and assume the sensor is faulty byte mapErrorCount = 0; diff --git a/speeduino/sensors.ino b/speeduino/sensors.ino index d8baf39e..b1b187a2 100644 --- a/speeduino/sensors.ino +++ b/speeduino/sensors.ino @@ -50,6 +50,31 @@ void initialiseADC() MAPcurRev = 0; MAPcount = 0; MAPrunningValue = 0; + + //The following checks the aux inputs and initialises pins if required + auxIsEnabled = false; + for (byte AuxinChan = 0; AuxinChan <16 ; AuxinChan++) + { + currentStatus.current_caninchannel = AuxinChan; + //currentStatus.canin[14] = ((configPage9.Auxinpinb[currentStatus.current_caninchannel]&127)+1); + //currentStatus.canin[13] = (configPage9.caninput_sel[currentStatus.current_caninchannel]&3); + if ( (configPage9.caninput_sel[currentStatus.current_caninchannel] == 1) && (configPage9.enable_candata_in > 0) && (configPage9.enable_canbus == 1)) //if current input channel is enabled as canbus + { + auxIsEnabled = true; + } + else if ((configPage9.caninput_sel[currentStatus.current_caninchannel]&3) == 2) //if current input channel is enabled as analog local pin + { + //Channel is active and analog + pinMode( (configPage9.Auxinpina[currentStatus.current_caninchannel]&127), INPUT); + auxIsEnabled = true; + } + else if ((configPage9.caninput_sel[currentStatus.current_caninchannel]&3) == 3) //if current input channel is enabled as digital local pin + { + //Channel is active and digital + pinMode( (configPage9.Auxinpinb[currentStatus.current_caninchannel]&127), INPUT); + auxIsEnabled = true; + } + } } static inline void instanteneousMAPReading() @@ -317,12 +342,12 @@ void readBat() * This value is incremented with every pulse and reset back to 0 once per second */ void flexPulse() - { - ++flexCounter; - } +{ + ++flexCounter; +} uint16_t readAuxanalog(uint8_t analogPin) - { +{ //read the Aux analog value for pin set by analogPin unsigned int tempReading; #if defined(ANALOG_ISR) @@ -332,12 +357,12 @@ uint16_t readAuxanalog(uint8_t analogPin) tempReading = fastMap1023toX(analogRead(analogPin), 511); //Get the current raw Auxanalog value #endif return tempReading; - } +} uint16_t readAuxdigital(uint8_t digitalPin) - { +{ //read the Aux digital value for pin set by digitalPin unsigned int tempReading; tempReading = digitalRead(digitalPin); return tempReading; - } +} diff --git a/speeduino/speeduino.ino b/speeduino/speeduino.ino index d6431fc6..4020c5c9 100644 --- a/speeduino/speeduino.ino +++ b/speeduino/speeduino.ino @@ -987,77 +987,54 @@ void loop() //The IAT and CLT readings can be done less frequently (4 times per second) if (BIT_CHECK(LOOP_TIMER, BIT_TIMER_4HZ)) { - BIT_CLEAR(TIMER_mask, BIT_TIMER_4HZ); - readCLT(); - readIAT(); - readO2(); - readO2_2(); - readBat(); - nitrousControl(); + BIT_CLEAR(TIMER_mask, BIT_TIMER_4HZ); + readCLT(); + readIAT(); + readO2(); + readO2_2(); + readBat(); + nitrousControl(); - if(eepromWritesPending == true) { writeAllConfig(); } //Check for any outstanding EEPROM writes. + if(eepromWritesPending == true) { writeAllConfig(); } //Check for any outstanding EEPROM writes. -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) //ATmega2561 does not have Serial3 - //check through the Aux input channels if enabed for Can or local use - for (byte AuxinChan = 0; AuxinChan <16 ; AuxinChan++) - { + if(auxIsEnabled == true) + { + //check through the Aux input channels if enabed for Can or local use + for (byte AuxinChan = 0; AuxinChan <16 ; AuxinChan++) + { currentStatus.current_caninchannel = AuxinChan; //currentStatus.canin[14] = ((configPage9.Auxinpinb[currentStatus.current_caninchannel]&127)+1); //currentStatus.canin[13] = (configPage9.caninput_sel[currentStatus.current_caninchannel]&3); if (configPage9.caninput_sel[currentStatus.current_caninchannel] == 1) //if current input channel is enabled as canbus + { + if (configPage9.enable_candata_in) //if external data input is enabled { - if (configPage9.enable_candata_in) //if external data input is enabled - { - if (configPage9.enable_canbus == 1) // megas only support can via secondary serial - { - sendCancommand(2,0,currentStatus.current_caninchannel,0,((configPage9.caninput_source_can_address[currentStatus.current_caninchannel]&2047)+0x100)); - //send an R command for data from caninput_source_address[currentStatus.current_caninchannel] - } - } + if (configPage9.enable_canbus == 1) // megas only support can via secondary serial + { + sendCancommand(2,0,currentStatus.current_caninchannel,0,((configPage9.caninput_source_can_address[currentStatus.current_caninchannel]&2047)+0x100)); + //send an R command for data from caninput_source_address[currentStatus.current_caninchannel] + } + #if defined(CORE_STM32) || defined(CORE_TEENSY) + else if (configPage9.enable_canbus == 2) // can via internal can module + { + sendCancommand(3,configPage9.speeduino_tsCanId,currentStatus.current_caninchannel,0,configPage9.caninput_source_can_address[currentStatus.current_caninchannel]); //send via localcanbus the command for data from paramgroup[currentStatus.current_caninchannel] + } + #endif } + } else if ((configPage9.caninput_sel[currentStatus.current_caninchannel]&3) == 2) //if current input channel is enabled as analog local pin - { - //read analog channel specified - currentStatus.canin[currentStatus.current_caninchannel] = readAuxanalog(configPage9.Auxinpina[currentStatus.current_caninchannel]&127); - } + { + //read analog channel specified + currentStatus.canin[currentStatus.current_caninchannel] = readAuxanalog(configPage9.Auxinpina[currentStatus.current_caninchannel]&127); + } else if ((configPage9.caninput_sel[currentStatus.current_caninchannel]&3) == 3) //if current input channel is enabled as digital local pin - { - //read digital channel specified - currentStatus.canin[currentStatus.current_caninchannel] = readAuxdigital((configPage9.Auxinpinb[currentStatus.current_caninchannel]&127)+1); - } - } + { + //read digital channel specified + currentStatus.canin[currentStatus.current_caninchannel] = readAuxdigital((configPage9.Auxinpinb[currentStatus.current_caninchannel]&127)+1); + } //Channel type + } //For loop going through each channel + } //aux channels are enabled -#elif defined(CORE_STM32) || defined(CORE_TEENSY) - //check through the Aux input channels if enabed for Can or local use - for (byte AuxinChan = 0; AuxinChan <16 ; AuxinChan++) - { - currentStatus.current_caninchannel = AuxinChan; - if ((configPage9.caninput_sel[currentStatus.current_caninchannel]&3) == 1) //if current input channel is enabled as canbus - { - if (configPage9.enable_candata_in) - { - if (configPage9.enable_canbus == 1) //can via secondary serial - { - sendCancommand(2,0,currentStatus.current_caninchannel,0,((configPage9.caninput_source_can_address[currentStatus.current_caninchannel]&2047)+0x100)); //send an R command for data from paramgroup[currentStatus.current_caninchannel] - } - else if (configPage9.enable_canbus == 2) // can via internal can module - { - sendCancommand(3,configPage9.speeduino_tsCanId,currentStatus.current_caninchannel,0,configPage9.caninput_source_can_address[currentStatus.current_caninchannel]); //send via localcanbus the command for data from paramgroup[currentStatus.current_caninchannel] - } - } - } - else if ((configPage9.caninput_sel[currentStatus.current_caninchannel]&3) == 2) //if current input channel is enabled as analog local pin - { - //read analog channel specified - currentStatus.canin[currentStatus.current_caninchannel] = readAuxanalog(configPage9.Auxinpina[currentStatus.current_caninchannel]&127); - } - else if ((configPage9.caninput_sel[currentStatus.current_caninchannel]&3) == 3) //if current input channel is enabled as digital local pin - { - //read digital channel specified - currentStatus.canin[currentStatus.current_caninchannel] = readAuxdigital((configPage9.Auxinpinb[currentStatus.current_caninchannel]&127)+1); - } - } -#endif vvtControl(); idleControl(); //Perform any idle related actions. Even at higher frequencies, running 4x per second is sufficient. } //4Hz timer