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:27:35 2014 +0000
Revision:
4:db6f9f91a04b
Parent:
3:05d2d090b8b0
Child:
5:651dc38cc1a6
NOW prints to lcd

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