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

Committer:
Alex4475
Date:
2014-05-07
Revision:
3:05d2d090b8b0
Parent:
2:64ff9d8b2aa1
Child:
4:db6f9f91a04b

File content as of revision 3:05d2d090b8b0:

#include "mbed.h"
#include "GPS.h"
#include "MMA7660.h"
#include "CMPS03.h"
#include "MSCFileSystem.h"

#define FSNAME "USB"

// Initialisation
Serial pc(USBTX, USBRX);
GPS gps(p9, p10);                                       // GPS connections
MMA7660 MMA(p28, p27);                                  // Accelerometer connections 
CMPS03 compass(p28, p27, CMPS03_DEFAULT_I2C_ADDRESS);   // Compass connections

// Declarations
float ax, ay, az; // acceleration values 
DigitalOut connectionLed(LED1); 
MSCFileSystem msc(FSNAME);


int main()
{
    DIR *d;
    struct dirent *p;
     
    d = opendir("/" FSNAME);
    
    pc.printf("\nList of files on the flash drive:\n");
    if ( d != NULL )
    {
        while ( (p = readdir(d)) != NULL )
        {
            pc.printf(" - %s\n", p->d_name);
        }
    }
    else
    {
        error("Could not open directory!");
    }
    //printf("\nTesting file write:\n");
    FILE *CompassFile = fopen( "/" FSNAME "/compass_file.txt", "w");
    if ( CompassFile == NULL )
    {
        error("Could not open compass file for write\n");
    }
    FILE *GPSFile = fopen("/" FSNAME "/gps_file.txt", "w");
    if ( GPSFile == NULL )
    {
        error("Could not open GPS file for write\n");
    }
    FILE *AccelFile = fopen("/" FSNAME "/accel_file.txt", "w");
    if ( AccelFile == NULL )
    {
        error("Could not open acceleration file for write\n");
    }
    if (MMA.testConnection()) 
        connectionLed = 1; // check that MMA is connected and display on LED1
        
    pc.printf("Starting CMPS03 test...\n");
    float refPoint = 1.0; 
    float bearing;
    int i = 1;
    
    while (1)
    {
        refPoint = compass.readBearing();
        pc.printf("The reference bearing: %f\n\n", refPoint/10);
        while(i == 1)
        {
            FILE *CompassFile = fopen("/" FSNAME "/compass_file.txt", "a");
            FILE *GPSFile = fopen("/" FSNAME "/gps_file.txt", "a");
            FILE *AccelFile = fopen("/" FSNAME "/accel_file.txt", "a");
        // GPS code
            if(gps.sample()) 
            {
                fprintf(GPSFile, "\n%f %f\r\n", gps.latitude, gps.longitude);
                lcd.locate(1,1);
                lcd.printf("GPS: %.4f %.4f", gps.latitude, gps.longitude);
            } 
            else 
            {
                fprintf(GPSFIle, "\nNo lock!\r\n");
                lcd.locate(1,1);
                lcd.printf("GPS: No lock!");
            }
            
        // Accelerometer code
        
            ax=MMA.x(); // read accelerometer values 
            ay=MMA.y(); 
            az=MMA.z(); 
            fprintf(AccelFile, "%f %f %f\r\n",ax,ay,az); 
            lcd.locate(1,11);
            lcd.printf("MMA: %.4f %.4f %.4f",ax,ay,az);
            
        // Compass code
            bearing = (compass.readBearing()/10 - refPoint/10);
            if(bearing < 0.0)
            {
                bearing = 360 + bearing;
                fprintf(CompassFile, "Bearing : %f\r\n", bearing);
                lcd.locate(1,21);
                lcd.printf("CMP: %.2f",bearing);
            }
            else
            {
                fprintf(CompassFile, "Bearing : %f\r\n", bearing);
                lcd.locate(1,21);
                lcd.printf("CMP: %.2f",bearing);
            }
            fclose(CompassFile);
            fclose(AccelFile);
            fclose(GPSFile);  
        }        
    } 
}