FBRLogger final version
Dependencies: EthernetInterface MSCAN Nanopb SDFileSystem mbed-rtos mbed
Diff: main.cpp
- Revision:
- 1:5f51069d5e09
- Parent:
- 0:6f69ea2839fa
- Child:
- 2:2400fab06b33
--- a/main.cpp Sun Oct 07 17:00:03 2012 +0000 +++ b/main.cpp Sun Feb 10 13:19:37 2013 +0000 @@ -1,58 +1,144 @@ -#include "SDHCFileSystem.h" - -SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board -FILE *logFile; - -CAN can1(p30, p29); //Needs to be CAN1 - SD takes CAN0 - -void can_interrupt() -{ - CANMessage msg; - - if(can1.read(msg)) - { - fprintf(logFile, "Message Received\r\n"); - } -} - -bool file_exists(const char * filename) -{ - if (FILE * file = fopen(filename, "r")) - { - fclose(file); - return true; - } - return false; -} - -int main() { - char logIndex = 0; - char logFileName[50]; - - printf("Hello World!\r\n"); - - mkdir("/sd/fbr", 0777); - - can1.attach(&can_interrupt); - - do - { - sprintf(&logFileName[0], "/sd/fbr/log.%d", logIndex); - logIndex++; - } while(file_exists(&logFileName[0])); - - printf(&logFileName[0]); - - logFile = fopen(&logFileName[0], "w"); - - if(logFile == NULL) { - error("Could not open file for write\r\n"); - } - - fprintf(logFile, "FBR CAN Log File"); - - while(true) - { - __wfi(); - } -} +#include "SDHCFileSystem.h" +#include <stdint.h> + +//SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board +CAN can1(p30, p29); //Needs to be CAN1 - SD takes CAN0 + +char logFileName[50]; +Timer messageTimer; + +void writeLog_string(const char *data); +void writeLog_data(const char *data, unsigned char length); + +Ticker canPoll; + +void can_interrupt() +{ + int time; + + uint16_t tps; + uint16_t battery; + + CANMessage msg; + + time = messageTimer.read_us(); + + char logMessage[17]; + + while(can1.read(msg)) + { + printf("CAN Message %08X %d %02X%02X%02X%02X%02X%02X%02X%02X\n", msg.id, msg.len, + msg.data[0], + msg.data[1], + msg.data[2], + msg.data[3], + msg.data[4], + msg.data[5], + msg.data[6], + msg.data[7] + ); + + tps = (msg.data[0] << 8) | msg.data[1]; + battery = (msg.data[2] << 8) | msg.data[3]; + + printf("TPS: %f\n", tps / 10.0); + printf("Bat: %f\n", battery / 10.0); + //printf("Bat: %fV\n", (((int)msg.data[2] << 8) & (int)msg.data[3]) / 10.0); + + memcpy(&logMessage[0], &time, 4); + memcpy(&logMessage[4], &msg.id, 4); + memcpy(&logMessage[8], &msg.len, 1); + memcpy(&logMessage[9], &msg.data, 8); + + writeLog_data(&logMessage[0], sizeof(logMessage)); + } +} + +void poll_can() +{ + //MS encodes most of the request params in the message ID. + //Alignment is weird because the ID really includes some non-data bits. MS worked round these, mBed just skips them + + //0-2 2 bits Spare on MSII + //3-6 4 bits Block to get data from. 7 = outpc, all the stuff that MS sends to the PC via serial + //7-10 4 bits To ID - 0 for MS + //11-14 4 bits From ID - Using 3 here for no good reason + //15-17 3 bits Message type. 1 to get data, response has 2 + //18-28 11 bits Offset into the block in bits 3-6. Look in the INI file for Tuner Studio to find these, + // [OutputChannels] section starting line 3036 + + CANMessage msg(0x00609838, 0, 3, CANData, CANExtended); + //CANMessage msg(); + + msg.data[0] = 0; //Where to put the response - used when communicating between MS's to tell them where they wanted the data put + msg.data[1] = 0; //Offset into the table specified in data[0] + msg.data[2] = 8; //How many bytes of the source table we want + + //CANMessage msg(0); + + if(can1.write(msg)) + { + printf("Polled\n"); + } +} + +void writeLog_string(const char *data) +{ + writeLog_data(data, strlen(data)); +} + +void writeLog_data(const char *data, unsigned char length) +{ + /*FILE *logFile; + + logFile = fopen(&logFileName[0], "a"); + + if(logFile == NULL) + { + error("Could not open file for write\n"); + } + + fwrite(data, 1, length, logFile); + + fclose(logFile);*/ +} + +bool file_exists(const char * filename) +{ + if (FILE * file = fopen(filename, "r")) + { + fclose(file); + return true; + } + return false; +} + +int main() { + char logIndex = 0; + + printf("FBR CAN Data Logger\n"); + + /*mkdir("/sd/fbr", 0777); + + do + { + sprintf(&logFileName[0], "/sd/fbr/log.%d", logIndex); + logIndex++; + } while(file_exists(&logFileName[0])); + + printf("Log File: %s\n", &logFileName[0]);*/ + + printf("Listening Started\n"); + + messageTimer.start(); + + can1.frequency(500000); + can1.attach(&can_interrupt); + + canPoll.attach(&poll_can, 1.0); + + while(true) + { + __wfi(); + } +}