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@3:05d2d090b8b0, 2014-05-07 (annotated)
- Committer:
- Alex4475
- Date:
- Wed May 07 10:22:15 2014 +0000
- Revision:
- 3:05d2d090b8b0
- Parent:
- 2:64ff9d8b2aa1
- Child:
- 4:db6f9f91a04b
Now prints to LCD for error checking.
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 | 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 | 3:05d2d090b8b0 | 62 | int i = 1; |
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 | 3:05d2d090b8b0 | 68 | while(i == 1) |
Alex4475 | 0:2557081b4322 | 69 | { |
Alex4475 | 3:05d2d090b8b0 | 70 | FILE *CompassFile = fopen("/" FSNAME "/compass_file.txt", "a"); |
Alex4475 | 3:05d2d090b8b0 | 71 | FILE *GPSFile = fopen("/" FSNAME "/gps_file.txt", "a"); |
Alex4475 | 3:05d2d090b8b0 | 72 | FILE *AccelFile = fopen("/" FSNAME "/accel_file.txt", "a"); |
Alex4475 | 0:2557081b4322 | 73 | // GPS code |
Alex4475 | 0:2557081b4322 | 74 | if(gps.sample()) |
Alex4475 | 0:2557081b4322 | 75 | { |
Alex4475 | 2:64ff9d8b2aa1 | 76 | fprintf(GPSFile, "\n%f %f\r\n", gps.latitude, gps.longitude); |
Alex4475 | 3:05d2d090b8b0 | 77 | lcd.locate(1,1); |
Alex4475 | 3:05d2d090b8b0 | 78 | lcd.printf("GPS: %.4f %.4f", gps.latitude, gps.longitude); |
Alex4475 | 0:2557081b4322 | 79 | } |
Alex4475 | 0:2557081b4322 | 80 | else |
Alex4475 | 0:2557081b4322 | 81 | { |
Alex4475 | 3:05d2d090b8b0 | 82 | fprintf(GPSFIle, "\nNo lock!\r\n"); |
Alex4475 | 3:05d2d090b8b0 | 83 | lcd.locate(1,1); |
Alex4475 | 3:05d2d090b8b0 | 84 | lcd.printf("GPS: No lock!"); |
Alex4475 | 0:2557081b4322 | 85 | } |
Alex4475 | 0:2557081b4322 | 86 | |
Alex4475 | 0:2557081b4322 | 87 | // Accelerometer code |
Alex4475 | 0:2557081b4322 | 88 | |
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 | 3:05d2d090b8b0 | 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 | 3:05d2d090b8b0 | 102 | lcd.locate(1,21); |
Alex4475 | 3:05d2d090b8b0 | 103 | lcd.printf("CMP: %.2f",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 | 3:05d2d090b8b0 | 109 | lcd.printf("CMP: %.2f",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 | } |