Code to take GPS, Accelerometer and Compass readings and print to USB for data analysis.
Dependencies: CMPS03 FatFileSystemCpp GPS MMA7660 mbed C12832_lcd
main.cpp@7:2f5cc7f14269, 2014-05-07 (annotated)
- Committer:
- Alex4475
- Date:
- Wed May 07 10:44:45 2014 +0000
- Revision:
- 7:2f5cc7f14269
- Parent:
- 6:c667497a588d
Final code cleaning and syntax.
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:2f5cc7f14269 | 43 | FILE *CompassFile = fopen( "/" FSNAME "/CMP_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 | 7:2f5cc7f14269 | 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 | 7:2f5cc7f14269 | 53 | FILE *AccelFile = fopen("/" FSNAME "/MMA_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 | float refPoint = 1.0; |
Alex4475 | 0:2557081b4322 | 62 | float bearing; |
Alex4475 | 3:05d2d090b8b0 | 63 | int i = 1; |
Alex4475 | 0:2557081b4322 | 64 | |
Alex4475 | 0:2557081b4322 | 65 | while (1) |
Alex4475 | 0:2557081b4322 | 66 | { |
Alex4475 | 0:2557081b4322 | 67 | refPoint = compass.readBearing(); |
Alex4475 | 3:05d2d090b8b0 | 68 | while(i == 1) |
Alex4475 | 0:2557081b4322 | 69 | { |
Alex4475 | 7:2f5cc7f14269 | 70 | CompassFile = fopen("/" FSNAME "/CMP_file.txt", "a"); |
Alex4475 | 7:2f5cc7f14269 | 71 | GPSFile = fopen("/" FSNAME "/GPS_file.txt", "a"); |
Alex4475 | 7:2f5cc7f14269 | 72 | AccelFile = fopen("/" FSNAME "/MMA_file.txt", "a"); |
Alex4475 | 7:2f5cc7f14269 | 73 | |
Alex4475 | 0:2557081b4322 | 74 | // GPS code |
Alex4475 | 0:2557081b4322 | 75 | if(gps.sample()) |
Alex4475 | 0:2557081b4322 | 76 | { |
Alex4475 | 2:64ff9d8b2aa1 | 77 | fprintf(GPSFile, "\n%f %f\r\n", gps.latitude, gps.longitude); |
Alex4475 | 6:c667497a588d | 78 | lcd.locate(1,1); |
Alex4475 | 3:05d2d090b8b0 | 79 | lcd.printf("GPS: %.4f %.4f", gps.latitude, gps.longitude); |
Alex4475 | 0:2557081b4322 | 80 | } |
Alex4475 | 0:2557081b4322 | 81 | else |
Alex4475 | 0:2557081b4322 | 82 | { |
Alex4475 | 4:db6f9f91a04b | 83 | fprintf(GPSFile, "\nNo lock!\r\n"); |
Alex4475 | 6:c667497a588d | 84 | lcd.locate(1,1); |
Alex4475 | 3:05d2d090b8b0 | 85 | lcd.printf("GPS: No lock!"); |
Alex4475 | 0:2557081b4322 | 86 | } |
Alex4475 | 0:2557081b4322 | 87 | |
Alex4475 | 0:2557081b4322 | 88 | // Accelerometer code |
Alex4475 | 0:2557081b4322 | 89 | ax=MMA.x(); // read accelerometer values |
Alex4475 | 0:2557081b4322 | 90 | ay=MMA.y(); |
Alex4475 | 0:2557081b4322 | 91 | az=MMA.z(); |
Alex4475 | 2:64ff9d8b2aa1 | 92 | fprintf(AccelFile, "%f %f %f\r\n",ax,ay,az); |
Alex4475 | 6:c667497a588d | 93 | lcd.locate(1,11); |
Alex4475 | 3:05d2d090b8b0 | 94 | lcd.printf("MMA: %.4f %.4f %.4f",ax,ay,az); |
Alex4475 | 0:2557081b4322 | 95 | |
Alex4475 | 0:2557081b4322 | 96 | // Compass code |
Alex4475 | 0:2557081b4322 | 97 | bearing = (compass.readBearing()/10 - refPoint/10); |
Alex4475 | 0:2557081b4322 | 98 | if(bearing < 0.0) |
Alex4475 | 0:2557081b4322 | 99 | { |
Alex4475 | 0:2557081b4322 | 100 | bearing = 360 + bearing; |
Alex4475 | 2:64ff9d8b2aa1 | 101 | fprintf(CompassFile, "Bearing : %f\r\n", bearing); |
Alex4475 | 6:c667497a588d | 102 | lcd.locate(1,21); |
Alex4475 | 6:c667497a588d | 103 | lcd.printf("Ref.b: %.2f :CMP: %.2f", refPoint/10, bearing); |
Alex4475 | 0:2557081b4322 | 104 | } |
Alex4475 | 0:2557081b4322 | 105 | else |
Alex4475 | 0:2557081b4322 | 106 | { |
Alex4475 | 3:05d2d090b8b0 | 107 | fprintf(CompassFile, "Bearing : %f\r\n", bearing); |
Alex4475 | 3:05d2d090b8b0 | 108 | lcd.locate(1,21); |
Alex4475 | 6:c667497a588d | 109 | lcd.printf("Ref.b: %.2f :CMP: %.2f", refPoint/10, bearing); |
Alex4475 | 0:2557081b4322 | 110 | } |
Alex4475 | 3:05d2d090b8b0 | 111 | fclose(CompassFile); |
Alex4475 | 3:05d2d090b8b0 | 112 | fclose(AccelFile); |
Alex4475 | 3:05d2d090b8b0 | 113 | fclose(GPSFile); |
Alex4475 | 3:05d2d090b8b0 | 114 | } |
Alex4475 | 3:05d2d090b8b0 | 115 | } |
Alex4475 | 0:2557081b4322 | 116 | } |