Code to take GPS, Accelerometer and Compass readings and print to USB for data analysis.

Dependencies:   CMPS03 FatFileSystemCpp GPS MMA7660 mbed C12832_lcd

Committer:
Alex4475
Date:
Wed May 07 10:07:45 2014 +0000
Revision:
2:64ff9d8b2aa1
Parent:
1:cdfeb3e3d25d
Child:
3:05d2d090b8b0
Added changes;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Alex4475 0:2557081b4322 1 #include "mbed.h"
Alex4475 0:2557081b4322 2 #include "GPS.h"
Alex4475 0:2557081b4322 3 #include "MMA7660.h"
Alex4475 0:2557081b4322 4 #include "CMPS03.h"
Alex4475 0:2557081b4322 5 #include "MSCFileSystem.h"
Alex4475 0:2557081b4322 6
Alex4475 0:2557081b4322 7 #define FSNAME "USB"
Alex4475 0:2557081b4322 8
Alex4475 0:2557081b4322 9 // Initialisation
Alex4475 0:2557081b4322 10 Serial pc(USBTX, USBRX);
Alex4475 0:2557081b4322 11 GPS gps(p9, p10); // GPS connections
Alex4475 0:2557081b4322 12 MMA7660 MMA(p28, p27); // Accelerometer connections
Alex4475 0:2557081b4322 13 CMPS03 compass(p28, p27, CMPS03_DEFAULT_I2C_ADDRESS); // Compass connections
Alex4475 0:2557081b4322 14
Alex4475 0:2557081b4322 15 // Declarations
Alex4475 0:2557081b4322 16 float ax, ay, az; // acceleration values
Alex4475 0:2557081b4322 17 DigitalOut connectionLed(LED1);
Alex4475 0:2557081b4322 18 MSCFileSystem msc(FSNAME);
Alex4475 0:2557081b4322 19
Alex4475 0:2557081b4322 20
Alex4475 0:2557081b4322 21 int main()
Alex4475 0:2557081b4322 22 {
Alex4475 0:2557081b4322 23 DIR *d;
Alex4475 0:2557081b4322 24 struct dirent *p;
Alex4475 0:2557081b4322 25
Alex4475 0:2557081b4322 26 d = opendir("/" FSNAME);
Alex4475 0:2557081b4322 27
Alex4475 0:2557081b4322 28 pc.printf("\nList of files on the flash drive:\n");
Alex4475 0:2557081b4322 29 if ( d != NULL )
Alex4475 0:2557081b4322 30 {
Alex4475 0:2557081b4322 31 while ( (p = readdir(d)) != NULL )
Alex4475 0:2557081b4322 32 {
Alex4475 0:2557081b4322 33 pc.printf(" - %s\n", p->d_name);
Alex4475 0:2557081b4322 34 }
Alex4475 0:2557081b4322 35 }
Alex4475 0:2557081b4322 36 else
Alex4475 0:2557081b4322 37 {
Alex4475 0:2557081b4322 38 error("Could not open directory!");
Alex4475 0:2557081b4322 39 }
Alex4475 0:2557081b4322 40 //printf("\nTesting file write:\n");
Alex4475 0:2557081b4322 41 FILE *CompassFile = fopen( "/" FSNAME "/compass_file.txt", "w");
Alex4475 0:2557081b4322 42 if ( CompassFile == NULL )
Alex4475 0:2557081b4322 43 {
Alex4475 0:2557081b4322 44 error("Could not open compass file for write\n");
Alex4475 0:2557081b4322 45 }
Alex4475 0:2557081b4322 46 FILE *GPSFile = fopen("/" FSNAME "/gps_file.txt", "w");
Alex4475 0:2557081b4322 47 if ( GPSFile == NULL )
Alex4475 0:2557081b4322 48 {
Alex4475 0:2557081b4322 49 error("Could not open GPS file for write\n");
Alex4475 0:2557081b4322 50 }
Alex4475 0:2557081b4322 51 FILE *AccelFile = fopen("/" FSNAME "/accel_file.txt", "w");
Alex4475 0:2557081b4322 52 if ( AccelFile == NULL )
Alex4475 0:2557081b4322 53 {
Alex4475 0:2557081b4322 54 error("Could not open acceleration file for write\n");
Alex4475 0:2557081b4322 55 }
Alex4475 0:2557081b4322 56 if (MMA.testConnection())
Alex4475 0:2557081b4322 57 connectionLed = 1; // check that MMA is connected and display on LED1
Alex4475 0:2557081b4322 58
Alex4475 0:2557081b4322 59 pc.printf("Starting CMPS03 test...\n");
Alex4475 0:2557081b4322 60 float refPoint = 1.0;
Alex4475 0:2557081b4322 61 float bearing;
Alex4475 1:cdfeb3e3d25d 62 int i = 0;
Alex4475 0:2557081b4322 63
Alex4475 0:2557081b4322 64 while (1)
Alex4475 0:2557081b4322 65 {
Alex4475 0:2557081b4322 66 refPoint = compass.readBearing();
Alex4475 1:cdfeb3e3d25d 67 pc.printf("The reference bearing: %f\n\n", refPoint/10);
Alex4475 1:cdfeb3e3d25d 68 while(i < 20)
Alex4475 0:2557081b4322 69 {
Alex4475 0:2557081b4322 70 // GPS code
Alex4475 0:2557081b4322 71 if(gps.sample())
Alex4475 0:2557081b4322 72 {
Alex4475 2:64ff9d8b2aa1 73 fprintf(GPSFile, "\n%f %f\r\n", gps.latitude, gps.longitude);
Alex4475 0:2557081b4322 74 }
Alex4475 0:2557081b4322 75 else
Alex4475 0:2557081b4322 76 {
Alex4475 2:64ff9d8b2aa1 77 pc.printf("\nOh Dear! No lock :(\n") && fprintf(GPSFile, "\n0.000 0.000\r\n");
Alex4475 0:2557081b4322 78 }
Alex4475 0:2557081b4322 79
Alex4475 0:2557081b4322 80 // Accelerometer code
Alex4475 0:2557081b4322 81
Alex4475 0:2557081b4322 82 ax=MMA.x(); // read accelerometer values
Alex4475 0:2557081b4322 83 ay=MMA.y();
Alex4475 0:2557081b4322 84 az=MMA.z();
Alex4475 2:64ff9d8b2aa1 85 fprintf(AccelFile, "%f %f %f\r\n",ax,ay,az);
Alex4475 0:2557081b4322 86
Alex4475 0:2557081b4322 87 // Compass code
Alex4475 0:2557081b4322 88 bearing = (compass.readBearing()/10 - refPoint/10);
Alex4475 0:2557081b4322 89 if(bearing < 0.0)
Alex4475 0:2557081b4322 90 {
Alex4475 0:2557081b4322 91 bearing = 360 + bearing;
Alex4475 2:64ff9d8b2aa1 92 fprintf(CompassFile, "Bearing : %f\r\n", bearing);
Alex4475 0:2557081b4322 93 }
Alex4475 0:2557081b4322 94 else
Alex4475 0:2557081b4322 95 {
Alex4475 2:64ff9d8b2aa1 96 fprintf(CompassFile, "Bearing : %f\r\n" << endl, bearing);
Alex4475 0:2557081b4322 97 }
Alex4475 1:cdfeb3e3d25d 98 i = i + 1;
Alex4475 1:cdfeb3e3d25d 99 }
Alex4475 1:cdfeb3e3d25d 100 fclose(CompassFile);
Alex4475 1:cdfeb3e3d25d 101 fclose(AccelFile);
Alex4475 1:cdfeb3e3d25d 102 fclose(GPSFile);
Alex4475 0:2557081b4322 103 }
Alex4475 1:cdfeb3e3d25d 104
Alex4475 0:2557081b4322 105
Alex4475 0:2557081b4322 106 /* fclose(compassFile);
Alex4475 0:2557081b4322 107 printf("\n - OK\n");
Alex4475 0:2557081b4322 108
Alex4475 0:2557081b4322 109 printf("\nTesting file read:\n");
Alex4475 0:2557081b4322 110 compassFile = fopen( "/" FSNAME "/compassfile.txt", "r");
Alex4475 0:2557081b4322 111 if ( compassFile == NULL )
Alex4475 0:2557081b4322 112 {
Alex4475 0:2557081b4322 113 error("Could not open compass file for read\n");
Alex4475 0:2557081b4322 114 }
Alex4475 0:2557081b4322 115 char buf[256];
Alex4475 0:2557081b4322 116 if ( NULL == fgets(buf, sizeof(buf), compassFile) )
Alex4475 0:2557081b4322 117 {
Alex4475 0:2557081b4322 118 error("Error reading from file\n");
Alex4475 0:2557081b4322 119 }
Alex4475 0:2557081b4322 120 fclose(compassFile);
Alex4475 0:2557081b4322 121 printf("\n - OK, read string: '%s'\n\n", buf);
Alex4475 0:2557081b4322 122 */
Alex4475 0:2557081b4322 123
Alex4475 0:2557081b4322 124
Alex4475 0:2557081b4322 125 }