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:
2:64ff9d8b2aa1
Parent:
1:cdfeb3e3d25d
Child:
3:05d2d090b8b0

File content as of revision 2:64ff9d8b2aa1:

#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 = 0;
    
    while (1)
    {
        refPoint = compass.readBearing();
        pc.printf("The reference bearing: %f\n\n", refPoint/10);
        while(i < 20)
        {
        // GPS code
            if(gps.sample()) 
            {
                fprintf(GPSFile, "\n%f %f\r\n", gps.latitude, gps.longitude);
            } 
            else 
            {
                pc.printf("\nOh Dear! No lock :(\n") && fprintf(GPSFile, "\n0.000 0.000\r\n");
            }
            
        // Accelerometer code
        
            ax=MMA.x(); // read accelerometer values 
            ay=MMA.y(); 
            az=MMA.z(); 
            fprintf(AccelFile, "%f %f %f\r\n",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);
            }
            else
            {
                fprintf(CompassFile, "Bearing : %f\r\n" << endl, bearing);
            }
            i = i + 1;
        }
        fclose(CompassFile);
        fclose(AccelFile);
        fclose(GPSFile);        
    }
    
        
   /* fclose(compassFile); 
    printf("\n - OK\n");

    printf("\nTesting file read:\n");
    compassFile = fopen( "/" FSNAME "/compassfile.txt", "r");
    if ( compassFile == NULL )
    {
        error("Could not open compass file for read\n");
    }
    char buf[256];
    if ( NULL == fgets(buf, sizeof(buf), compassFile) )
    {
        error("Error reading from file\n");
    }
    fclose(compassFile); 
    printf("\n - OK, read string: '%s'\n\n", buf);
   */ 
    
    
}