FBRLogger final version
Dependencies: EthernetInterface MSCAN Nanopb SDFileSystem mbed-rtos mbed
Diff: main.cpp
- Revision:
- 2:2400fab06b33
- Parent:
- 1:5f51069d5e09
- Child:
- 3:32206cf84eb4
--- a/main.cpp Sun Feb 10 13:19:37 2013 +0000 +++ b/main.cpp Sun Feb 17 18:59:30 2013 +0000 @@ -1,87 +1,23 @@ #include "SDHCFileSystem.h" +#include "CANComms.h" +#include "State.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 +State car; +CANComms* can; + +SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board + +AnalogIn accX(p19); +AnalogIn accY(p20); + +Ticker sample; 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)); @@ -113,12 +49,23 @@ return false; } +void take_sample() +{ + float x; + float y; + + x = accX.read(); + y = accY.read(); + + printf("X: %4.3f Y:%4.3f\n", x, y); +} + int main() { char logIndex = 0; printf("FBR CAN Data Logger\n"); - /*mkdir("/sd/fbr", 0777); + mkdir("/sd/fbr", 0777); do { @@ -126,16 +73,13 @@ logIndex++; } while(file_exists(&logFileName[0])); - printf("Log File: %s\n", &logFileName[0]);*/ + printf("Log File: %s\n", &logFileName[0]); printf("Listening Started\n"); - messageTimer.start(); - - can1.frequency(500000); - can1.attach(&can_interrupt); + can = CANComms(&car); - canPoll.attach(&poll_can, 1.0); + sample.attach(&take_sample, 0.5); while(true) {