Program to read data from sensors, write them to a file which can then be interpreted by software to help show the path of a bicycle as it travels around a field

Dependencies:   C12832_lcd FatFileSystemCpp MMA7660 CMPS03 GPS

Fork of MSCUsbHost by Igor Skochinsky

Program to link a compass and a GPS to an MBED to produce a CSV file which can be used to track a bicycle around a field

Results can be found here

Revision:
4:c64b742e9388
Parent:
0:e294af8d0e07
Child:
5:b4d5a68654bb
--- a/main.cpp	Mon Jul 30 13:49:56 2012 +0000
+++ b/main.cpp	Sat Mar 11 18:06:06 2017 +0000
@@ -1,69 +1,93 @@
-#include "mbed.h"
+#include "mbed.h" // mbed library
+#include "MMA7660.h" // Accelerometer library
+#include "GPS.h" // GPS library
+#include "CMPS03.h" // Compass library
+#include "C12832_lcd.h" // LCD screen library
 #include "MSCFileSystem.h"
-//#include <stat.h>
+
+#define FSNAME "USB"
+#define LIM 10
 
-#define FSNAME "msc"
-MSCFileSystem msc(FSNAME);
+GPS gps(p9, p10); // GPS Connection
+CMPS03 compass(p28, p27, CMPS03_DEFAULT_I2C_ADDRESS); // Compass connection
+MMA7660 MMA(p28, p27); // Accelerometer connection
+C12832_LCD lcd;
+Serial pc(USBTX, USBRX);
+
+
+// Declarations
+float ax, ay, az;
+DigitalOut connectionLed(LED1);
+MSCFileSystem msc("USB"); // Mount USB stick under the name "USB"
 
 int main()
 {
-	DIR *d;
-	struct dirent *p;
-	//struct stat st;
-	//char path[PATH_MAX];
+    /*
+    DIR *d;
+    struct dirent *p; // For printing externally (USB Stick)
+    d = opendir("/" FSNAME);
+    */
+    if (MMA.testConnection())
+        connectionLed = 1; // Checking MMA connected correctly
+   
+    FILE *Accel = fopen("/USB/Accelerometer.txt", "w"); // Opens text file for Accelerometer                
+    FILE *GPS = fopen("/USB/GPS.txt", "w"); // Opens text file for GPS                      
+    FILE *Comp = fopen("/USB/Compass.txt", "w"); // Opens text file for Compass   
+    
+    float Offset, GenBearing;
     
-    printf("\n\n================================\n");
-    printf("USB Mass storage demo program for mbed LPC1768\n");
-    printf("================================\n\n");
-    
-	d = opendir("/" FSNAME);
-    
-    printf("\nList of files on the flash drive:\n");
-    if ( d != NULL )
+    while(1)
     {
-        while ( (p = readdir(d)) != NULL )
+        //Accelerometer
+        Accel = fopen("/USB/Accelerometer.txt", "a");
+        ax=MMA.x();
+        ay=MMA.y();
+        az=MMA.z();
+        fprintf(Accel,"%f, %f, %f,\r\n",ax,ay,az);
+        lcd.locate(1,21);
+        lcd.printf("Accel: %.2f, %.2f, %.2f", ax, ay, az);
+                
+        //GPS
+        GPS = fopen("/USB/GPS.txt", "a");
+        if (gps.sample())
+        {
+            fprintf(GPS, "\n%f, %f\r\n", gps.latitude, gps.longitude);
+            lcd.locate(1,1);
+            lcd.printf("GPS: %.4f, %.4f, %.4f", gps.latitude, gps.longitude);
+        }       
+        else // For no signal
         {
-        	printf(" - %s\n", p->d_name);
-        	/* no <stat.h> on mbed, it seems :/
-        	sprintf(path, "/"FSNAME"/%s", p->d_name);
-        	if ( stat(path, &st) == 0 )
-        	{
-        	  if ( S_ISDIR(st.st_mode) )
-        	    printf(" <directory>\n");
-        	  else
-        	    printf(" %d\n", st.st_size);
-        	}
-        	else
-        	{
-        	  printf(" ???\n");
-        	}*/
+            fprintf(GPS, "\nNo Lock!\n\n");
+            lcd.locate(1,1);
+            lcd.printf("GPS: No Lock!");
         }
-    }
-    else
-    {
-    	error("Could not open directory!");
+        
+        // Compass
+        Offset = compass.readBearing();  // Initial definitions
+        GenBearing = (compass.readBearing()/10 - Offset/10);
+        Comp = fopen("/USB/Compass.txt", "a");
+        if (GenBearing < 0)
+        {
+            GenBearing = 0 - GenBearing;
+        }
+        fprintf(Comp, "Bearing: %.2f\r\n", GenBearing);
+        lcd.locate(1,11);
+        lcd.printf("Offset: %.2f, CMP: %.2f", Offset/10, GenBearing);
+        
+        fclose(Accel);
+        fclose(GPS);
+        fclose(Comp);
     }
-    printf("\nTesting file write:\n");
-    FILE *fp = fopen( "/" FSNAME "/msctest.txt", "w");
-    if ( fp == NULL )
-    {
-        error("Could not open file for write\n");
-    }
-    fprintf(fp, "Hello mass storage!");
-    fclose(fp); 
-    printf("\n - OK\n");
-
-    printf("\nTesting file read:\n");
-    fp = fopen( "/" FSNAME "/msctest.txt", "r");
-    if ( fp == NULL )
-    {
-        error("Could not open file for read\n");
-    }
-    char buf[256];
-    if ( NULL == fgets(buf, sizeof(buf), fp) )
-    {
-        error("Error reading from file\n");
-    }
-    fclose(fp); 
-    printf("\n - OK, read string: '%s'\n\n", buf);
 }
+        
+        /*lcd.locate(3,3);
+        lcd.printf("X: %.2f\t\n",MMA.x());
+        lcd.locate(38,3);
+        lcd.printf("Y: %.2f\t\n",MMA.y());
+        lcd.locate(78,3);
+        lcd.printf("Z: %.2f\t\n",MMA.z());
+        //lcd.cls();*/
+        
+        //wait(0.5);
+        //i++;
+        
\ No newline at end of file