Teensy3.2 CAN to uSD logger

This forum is for the discussion of other projects on Megasquirt/Microsquirt hardware that don't fit into any of the other forums

Moderators: jsmcortina, muythaibxr

Teensy3.2 CAN to uSD logger

Postby ol boy » Wed Mar 08, 2017 5:12 pm

I've sorted the CAN message stuff from last week and started to write the data into a micro SD card. You'll need to DL SdFat lib. Adding more CAN input ID's is easy. Just name your added variables as a float or uint16_t type. Each time the power is cycled this creates a new file name. xx.csv

Thanks Ryan

Code: Select all
#include <SPI.h>
#include <SdFat.h>

#include <FlexCAN.h>
#include <kinetis_flexcan.h>

// SD chip select pin.  Be sure to disable any other SPI devices such as Enet.
const uint8_t chipSelect = SS;
// Log file base name.  Must be six characters or less.
#define FILE_BASE_NAME "Data"
//------------------------------------------------------------------------------
// File system object.
SdFat sd;
// Log file.
SdFile file;

uint16_t Time, Rpm;
float Pw1, Pw2, Baro, Map, Batv, Ego1, Ego2, Mat, Tps, Clt;


float SdValue[12] ;

const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[13] = FILE_BASE_NAME "00.csv";

// Specify CAN Baudrate. Currently 125k, 250k, 500k, 1M are supported in teensydrino 1.20
int baud = 500000;


FlexCAN CANbus(baud);
static CAN_message_t rxmsg;
//static CAN_message_t rxmsg.rtr = 1;
static CAN_filter_t filter;
static CAN_filter_t mask;

void setup(void)
{
 
  Serial.begin(115200);
  while (!Serial) ; // wait for Arduino Serial Monitor <----------- Revome in final product.**************************************
  Serial.println("Teensy 3.2 CANlisten Example and SD");

 

 // Find an unused file name.
   if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
    sd.initErrorHalt();
  }
//search known file names and creat a new file name
  while (sd.exists(fileName)) {
    if (fileName[BASE_NAME_SIZE + 1] != '9') {
      fileName[BASE_NAME_SIZE + 1]++;
    } else if (fileName[BASE_NAME_SIZE] != '9') {
      fileName[BASE_NAME_SIZE + 1] = '0';
      fileName[BASE_NAME_SIZE]++;
    }
  }
   if (!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) {
   
  }
 
writeHeader();  //wite header info to new created file before enabling CAN bus data receive
CANbus.begin(mask); // Strat CAN Bus message stuff
}


void loop(void){
 
//Poll Rx FIFO
while ( CANbus.read(rxmsg) ) {
//when message available

switch (rxmsg.id) {
case 1520:    //change to Base CAN Identifier for offset 0"Zero"
Time=( (rxmsg.buf[1]) | (rxmsg.buf[0] << 8)) ;
Pw1=( (rxmsg.buf[3]) | (rxmsg.buf[2] << 8) );
Pw2=( (rxmsg.buf[5]) | (rxmsg.buf[4] << 8) ) ;
Rpm=( (rxmsg.buf[7]) | (rxmsg.buf[6] << 8) );
break;

case 1522:  //offset 2
Baro= ( (rxmsg.buf[1]) | (rxmsg.buf[0] << 8) );
Map= ( (rxmsg.buf[3]) | (rxmsg.buf[2] << 8) ) ;
Mat= ( (rxmsg.buf[5]) | (rxmsg.buf[4] << 8) ) ;
Clt= ( (rxmsg.buf[7]) | (rxmsg.buf[6] << 8) ) ;
break;


case 1523:   //offset 3
Tps= ( (rxmsg.buf[1]) | (rxmsg.buf[0] << 8) ) ;
Batv=  ( (rxmsg.buf[3]) | (rxmsg.buf[2] << 8) ) ;
Ego1=  ( (rxmsg.buf[5]) | (rxmsg.buf[4] << 8) ) ;
Ego2=  ( (rxmsg.buf[7]) | (rxmsg.buf[6] << 8) ) ;
break;
}

/////// Change this format to match the SD card.  Some items are not needed.
SdValue[0] = Time;
SdValue[1] = Pw1 / 1000.0;
SdValue[2] = Pw2 / 1000.0;
SdValue[3] = Rpm;
SdValue[4] = Baro / 10.0;
SdValue[5] = Map / 10.0;
SdValue[6] = Mat / 10.0;
SdValue[7] = Clt / 10.0;
SdValue[8] = Tps / 10.0;
SdValue[9] = Batv / 10.0;
SdValue[10] = Ego1 / 10.0;
SdValue[11] = Ego2 / 10.0;


//int x;
//int go;
//for (x = 0; x < 12; x++){
//      go = SdValue[x];
//      //print result
//      Serial.print(go);
//       
//    }
//      Serial.println();
     
      logData();
 
}

}

// Write data header.
void writeHeader() { 
  file.println("Ryan,Test,SD Card");
  file.println("TIME,PW1,PW2,RPM,BARO,MAP,MAT,CLT,TPS,BATV,EGO1,EGO2");
  file.close();
}
//------------------------------------------------------------------------------
// Log a data record.
void logData() {
 int i;
 float go;
  // Write CAN data to CSV record.
  if (!file.open(fileName, O_WRITE | O_AT_END)){
    for (i = 0; i < 12; i++){
     go = SdValue[i];
     file.print(go);
     file.print(",");
    }
  file.println();
 file.close();
}
}
306 SBFord, Torquer II EFI intake, 60 lbs injectors, 8 LS2 coils, VS Racing 7668 turbo, 4R70W, MS2extra 3.4.x_ish
ol boy
Super MS/Extra'er
 
Posts: 1198
Joined: Mon Sep 10, 2007 3:06 am
Location: Tucson, Az

Return to Other Development

Who is online

Users browsing this forum: No registered users and 2 guests