0

I have a Innovate LC-2 wideband and Megasquirt ECU. The Innovate wideband has RS232 serial output I would like to use instead of the default 0-5V analog output.

So I designed and built a board using a MAX3232, MCP2515/MCP2561 CANbus, and ATMEGA328PB. I also have a MAX31856 for EGT, LIS3DH for accel, and BMP280 for barometer. I also mucked about on forums and threads until I understood enough to decode the format. What I have now works, but I would like someone more experienced to check it for errors!

The serial format is described here... https://www.innovatemotorsports.com/support/downloads/Seriallog-2.pdf

My code looks a bit like this, it is part of a main loop reading data from other sensors as data is ready, so the loop is non-blocking. The main loop also transmits data over canbus at set intervals.

This does work, but is there anything I can do to make it more resilient?

Thank you in advance!

    if (Serial.available() > 0) {
      EGO_rx_byte=Serial.read();                                            // pull data from serial
      if ( (EGO_rx_byte & 0xA2) == 0xA2 ) {                                 // synchronization byte - bits 15-8 1x1xxx1x 
         EGO_packet[0] = EGO_rx_byte;                                       // Store buffer data to Packet
         EGO_packet_index = 0 ;                                             // This is the start of the header packet, bits 15-8      
      }
      else if ((EGO_packet_index == 0) && ((EGO_rx_byte & 0x80) == 0x80)) { // If index = 0 & bit 7 = 1 = 2nd syncro byte
         EGO_packet_index=1;                                                // this is second half of header packet, bits 7-0
         EGO_packet[1]=EGO_rx_byte;                                         // store buffer data to packet
      }
      else if ((EGO_packet_index == 1) && ((EGO_rx_byte & 0x42) == 0x42)) { // If index = 1 & 0x42 is set on next packet likely valid LC2 packet...
         EGO_packet_index=2;                                                // This was the third packet in six - Zero indexed               
         EGO_packet[2]=EGO_rx_byte;                                         // Store buffer data to packet
      }
      else {
        EGO_packet_index++;                                                 // all else are data packets, increment the packet counter by one
        EGO_packet[EGO_packet_index]=EGO_rx_byte;                           // Store buffer data to packet in series
      }
      if (EGO_packet_index == 5) {                                          // Done recieving packets, ready to construct data... 2 words per EGO, 1 header packet        
         EGO_status = (EGO_packet[2] & B00011100) >> 2;                     // status bits are in word 1 bits 12-10                
         EGO_data = (EGO_packet[2] & B00000001) << 7;                       // AFR Multiplier - think this is programmed with lmprogrammer - bits 8, 6-0
         EGO_data += EGO_packet[3] & B01111111;                             // these are the 6-0 bits         
         EGO_lambda = EGO_packet[4] & B00111111;                            // 13 bit lambda value, data depends on status bits
         EGO_lambda = EGO_lambda << 7;                                      // shift bits from first half of data packet
         EGO_lambda += (EGO_packet[5] & B01111111);                         // add bits from second half of packet       
         switch (EGO_status) {                                              // Output data depends on Status
         case 0: Lambda = EGO_lambda; EGO_conn = 13;                        // EGO Output is valid - Lambda in 10 bit scale
         if (Lambda > 1023){Lambda = 1023;}break;                           // Megasquirt expects 0-1023 input value                       
         case 1:Lambda = 1023;EGO_conn = 13;break;                          // EGO Output is valid - reporting O2% in 1/10%
         case 2:EGO_conn = 10;Lambda =  500;break;                          // Free air calibration 
         case 3:EGO_conn = 11;Lambda =  500;break;                          // Need Calibration
         case 4:EGO_conn = 12;Lambda =  500;break;                          // Warmup
         case 5:EGO_conn = 10;Lambda =  500;break;                          // Heater Calibration
         case 6:EGO_conn = EGO_lambda;Lambda =  500;break;                  // Error code 1-9    
         case 7:EGO_conn = 0;Lambda =  500;break;                           // Reserved 
         default:Lambda =  507;EGO_conn = 0;break;                          // Something else is happening, output 14.7 and error 
         }                                                                            
    } 
}
  • That looks pretty robust to me as it is. You might like to make use if the length attribute of the header though to receive the right number of bytes for the payload though. – Majenko Nov 26 '19 at 9:42
  • Thank you for the reply! Oh, you mean pulling the header length from the first packet to determine the number of following packets? I have some very poorly written code that does so, but I lack the hardware to test more than one LC-2 on the serial bus at the time! – Moonie223 Nov 27 '19 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.