FBRLogger final version

Dependencies:   EthernetInterface MSCAN Nanopb SDFileSystem mbed-rtos mbed

Revision:
3:32206cf84eb4
Parent:
2:2400fab06b33
Child:
4:66928695da01
--- a/main.cpp	Sun Feb 17 18:59:30 2013 +0000
+++ b/main.cpp	Sun Feb 17 19:19:19 2013 +0000
@@ -2,14 +2,18 @@
 #include "CANComms.h"
 #include "State.h"
 #include <stdint.h>
+#include <fstream>
+#include <iomanip>
+
+#define LOGGING_INTERVAL    0.1
+#define ANALOG_SCALE        3.3
 
 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);
+AnalogIn analogInputs[] = {p15, p16, p17, p18, p19, p20};
 
 Ticker sample;
 
@@ -18,31 +22,9 @@
 void writeLog_string(const char *data);
 void writeLog_data(const char *data, unsigned char length);
 
-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"))
-    {
+    if (FILE * file = fopen(filename, "r")) {
         fclose(file);
         return true;
     }
@@ -51,38 +33,46 @@
 
 void take_sample()
 {
-    float x;
-    float y;
+    ofstream out;
+
+    float value;
+
+    out.open(logFileName);
+
+    // Write the Analog Sensors
+    for(int i = 0; i < 6; i++) {
+        value = analogInputs[i].read() * ANALOG_SCALE;
+        out << setw(10) << value << ",";
+    }
+
+    //Write the ECU data (in binary form)
+    out.write(reinterpret_cast<char*>(&car), sizeof(State));
     
-    x = accX.read();
-    y = accY.read();
-    
-    printf("X: %4.3f Y:%4.3f\n", x, y);
+    out << endl;
+    out.close();
 }
 
-int main() {
+int main()
+{
     char logIndex = 0;
-    
+
     printf("FBR CAN Data Logger\n");
 
     mkdir("/sd/fbr", 0777);
-    
-    do
-    {
+
+    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");
-    
-    can = CANComms(&car);
 
-    sample.attach(&take_sample, 0.5);
+    can = new CANComms(&car, true, LOGGING_INTERVAL);
+    sample.attach(&take_sample, LOGGING_INTERVAL);
 
-    while(true)
-    {
+    while(true) {
         __wfi();
     }
-}   
+}