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@5:651dc38cc1a6, 2014-05-07 (annotated)
- Committer:
- Alex4475
- Date:
- Wed May 07 10:34:03 2014 +0000
- Revision:
- 5:651dc38cc1a6
- Parent:
- 4:db6f9f91a04b
- Child:
- 6:c667497a588d
Prints reference bearing to lcd
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 | 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 | 5:651dc38cc1a6 | 69 | lcd.locate(1,1); |
Alex4475 | 5:651dc38cc1a6 | 70 | lcd.printf("Ref bearing: %.2f", refPoint/10); |
Alex4475 | 5:651dc38cc1a6 | 71 | //pc.printf("The reference bearing: %f\n\n", refPoint/10); |
Alex4475 | 3:05d2d090b8b0 | 72 | while(i == 1) |
Alex4475 | 0:2557081b4322 | 73 | { |
Alex4475 | 4:db6f9f91a04b | 74 | CompassFile = fopen("/" FSNAME "/compass_file.txt", "a"); |
Alex4475 | 4:db6f9f91a04b | 75 | GPSFile = fopen("/" FSNAME "/gps_file.txt", "a"); |
Alex4475 | 4:db6f9f91a04b | 76 | AccelFile = fopen("/" FSNAME "/accel_file.txt", "a"); |
Alex4475 | 0:2557081b4322 | 77 | // GPS code |
Alex4475 | 0:2557081b4322 | 78 | if(gps.sample()) |
Alex4475 | 0:2557081b4322 | 79 | { |
Alex4475 | 2:64ff9d8b2aa1 | 80 | fprintf(GPSFile, "\n%f %f\r\n", gps.latitude, gps.longitude); |
Alex4475 | 5:651dc38cc1a6 | 81 | lcd.locate(1,11); |
Alex4475 | 3:05d2d090b8b0 | 82 | lcd.printf("GPS: %.4f %.4f", gps.latitude, gps.longitude); |
Alex4475 | 0:2557081b4322 | 83 | } |
Alex4475 | 0:2557081b4322 | 84 | else |
Alex4475 | 0:2557081b4322 | 85 | { |
Alex4475 | 4:db6f9f91a04b | 86 | fprintf(GPSFile, "\nNo lock!\r\n"); |
Alex4475 | 5:651dc38cc1a6 | 87 | lcd.locate(1,11); |
Alex4475 | 3:05d2d090b8b0 | 88 | lcd.printf("GPS: No lock!"); |
Alex4475 | 0:2557081b4322 | 89 | } |
Alex4475 | 0:2557081b4322 | 90 | |
Alex4475 | 0:2557081b4322 | 91 | // Accelerometer code |
Alex4475 | 0:2557081b4322 | 92 | |
Alex4475 | 0:2557081b4322 | 93 | ax=MMA.x(); // read accelerometer values |
Alex4475 | 0:2557081b4322 | 94 | ay=MMA.y(); |
Alex4475 | 0:2557081b4322 | 95 | az=MMA.z(); |
Alex4475 | 2:64ff9d8b2aa1 | 96 | fprintf(AccelFile, "%f %f %f\r\n",ax,ay,az); |
Alex4475 | 5:651dc38cc1a6 | 97 | lcd.locate(1,21); |
Alex4475 | 3:05d2d090b8b0 | 98 | lcd.printf("MMA: %.4f %.4f %.4f",ax,ay,az); |
Alex4475 | 0:2557081b4322 | 99 | |
Alex4475 | 0:2557081b4322 | 100 | // Compass code |
Alex4475 | 0:2557081b4322 | 101 | bearing = (compass.readBearing()/10 - refPoint/10); |
Alex4475 | 0:2557081b4322 | 102 | if(bearing < 0.0) |
Alex4475 | 0:2557081b4322 | 103 | { |
Alex4475 | 0:2557081b4322 | 104 | bearing = 360 + bearing; |
Alex4475 | 2:64ff9d8b2aa1 | 105 | fprintf(CompassFile, "Bearing : %f\r\n", bearing); |
Alex4475 | 5:651dc38cc1a6 | 106 | lcd.locate(1,31); |
Alex4475 | 3:05d2d090b8b0 | 107 | lcd.printf("CMP: %.2f",bearing); |
Alex4475 | 0:2557081b4322 | 108 | } |
Alex4475 | 0:2557081b4322 | 109 | else |
Alex4475 | 0:2557081b4322 | 110 | { |
Alex4475 | 3:05d2d090b8b0 | 111 | fprintf(CompassFile, "Bearing : %f\r\n", bearing); |
Alex4475 | 3:05d2d090b8b0 | 112 | lcd.locate(1,21); |
Alex4475 | 3:05d2d090b8b0 | 113 | lcd.printf("CMP: %.2f",bearing); |
Alex4475 | 0:2557081b4322 | 114 | } |
Alex4475 | 3:05d2d090b8b0 | 115 | fclose(CompassFile); |
Alex4475 | 3:05d2d090b8b0 | 116 | fclose(AccelFile); |
Alex4475 | 3:05d2d090b8b0 | 117 | fclose(GPSFile); |
Alex4475 | 3:05d2d090b8b0 | 118 | } |
Alex4475 | 3:05d2d090b8b0 | 119 | } |
Alex4475 | 0:2557081b4322 | 120 | } |