Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

main.cpp

Committer:
MAA
Date:
2017-03-09
Revision:
6:6d1683c8b26b
Parent:
5:11782a2008c2
Child:
7:872984a67d5b

File content as of revision 6:6d1683c8b26b:

#include "main.h"


//Global GPS variables
bool GPS_Data_Rdy = false;
bool GPS_Data_Valid = false;
bool GGA_Fix_Present = false;
bool firstLineWritten = false;
bool fileNameUpdated = false;

//global system variables
bool run = true;

//global tmpGpsRxString
char tmpGpsRxString[128];

//Write to file prototype
bool writeToUsb(string line, FILE * f);

int main(void){
    //initializing watchdog
    Watchdog wd;
    wd.init(10.0);   
    
    string currentFilename, nextFilename; 
    
    currentFilename = "/usb/tempFile.sps";
    
    //Initializing string buffer for GPS data
    string GPS_String_Buff;
    GPS_String_Buff.resize(128);
    memset(tmpGpsRxString,'\0',128);
    
    //debug comm setup
    dbg.baud(115200);
    dbg.printf("Init...\r\n");
    
    //setting up USB device
    USBHostMSD msd("usb");
    
    while(!msd.connect()){
        dbg.printf("Trying to connect to usb flash disk\r\n");
        wait_ms(500);      
    }
    
    //Opening a file on usb disk
    FILE * fp = fopen(currentFilename.c_str(), "a");
    wait_ms(100);
    
    //initializing SPS generation 
    SPS spsGen;
    
    //GPS communication init
    gps.baud(9600);
    gps.attach(&rxCallback, MODSERIAL::RxIrq);
    
    //Start comms with the GPS, Enabling GPRMC and  GPGGA
    gps.printf("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n");   
    
    dbg.printf("Init.... Done!\r\n");        
    //infinite loop running after initialization
    while(run) {
        
        //if the gps rx buffer is full, flush the buffer
        if(gps.rxBufferFull()){
            gps.rxBufferFlush();    
        }
        
        if(GPS_Data_Rdy){
            
            //update filename when date is available with gga fix
            if(!fileNameUpdated && GGA_Fix_Present){
                spsGen.generateSpsFilename(gpsNMEA.currentDATEFromGPRMC);
                nextFilename.assign(spsGen.getSpsFileName());
                fclose(fp);
                currentFilename.assign(nextFilename);
                nextFilename = "";
                
                fopen(currentFilename.c_str(), "a");
                
                fileNameUpdated = true;                            
                                                    
            }
            
            gps.scanf("%s", &tmpGpsRxString);
            
            //copy c_string to string
            GPS_String_Buff.assign(tmpGpsRxString);
            
            //clear tmpRxBuffer
            memset(tmpGpsRxString,'\0',128);
            
            //printing GPS string buffer
            dbg.printf(GPS_String_Buff.c_str());
            dbg.printf("\r\n");
 
            //Validate CRC
            GPS_Data_Valid = gpsNMEA.ValidateData(GPS_String_Buff);
         
            //store valid string, either gga or rmc
            if(GPS_Data_Valid){
                 gpsNMEA.StoreString(GPS_String_Buff/*, &dbg*/);   
            }
            
            //Get gga fix flag
            GGA_Fix_Present = gpsNMEA.GGAFixVerification();
            
            //Clearing RX buffer.
            GPS_String_Buff = "";
            GPS_String_Buff.resize(128);
            
            //parse current date 
            gpsNMEA.ParseCurrentDateFromGPRMC();
            
            //parse current time
            gpsNMEA.ParseCurrentUTCFromGPRMC();
            
            //parse gps coordinates
            gpsNMEA.ParseCurrentLatitudeFromGPRMC();
            gpsNMEA.ParseCurrentLongitudeFromGPRMC();
            
            //clearing flags
            GPS_Data_Valid = false;
            GPS_Data_Rdy = false;
            
        
            if(GGA_Fix_Present){
                
                spsGen.UpdateCurrentString(TAG, IDENTIFIERID, GROUP, gpsNMEA.currentDATEFromGPRMC, gpsNMEA.currentUTCFromGPRMC, TIMEZONE, ENCODING, SOURCEIDENTIFICATION, INTERPRETERID, DATALINEVERSION, FWSRCVERSION, FWIVERSION, gpsNMEA.currentLatitude, gpsNMEA.currentLongitude, GGA_Fix_Present, "magtime", "mag_nt", "mag_sq", &dbg);
                                
                //write data strings to sps file
                if(firstLineWritten){ 
                    writeToUsb(spsGen.getCurrentString(), fp);   
                }
        
                if(!firstLineWritten){ 
                    writeToUsb(spsGen.getHeaderString(), fp);
                    writeToUsb(spsGen.getCurrentString(), fp);
                    firstLineWritten = true;    
                }                
            }    
        }   
        

        //If connection to USB flash disk is lost, reconnect to the flash disk
        if(!msd.connected()){
            //reestablish usb connection
            while(!msd.connect()){
                dbg.printf("Trying to reconnect to usb flash disk\r\n");
                wait_ms(200);
                msd.connect();
            }
                
            //Reopening a file on usb disk
            fp = fopen(currentFilename.c_str(), "a");
                     
        }
        
    
        //kick / feed watchdog 
        wd.kick();
    
    }    
   
    return 0;    
}

//Thanks to MODSERIAL!
// Called everytime a new character goes into
// the RX buffer. Test that character for \n
// Note, rxGetLastChar() gets the last char that
// we received but it does NOT remove it from
// the RX buffer. 
void rxCallback(MODSERIAL_IRQ_INFO *q) {
    MODSERIAL *serial = q->serial;
    if (serial->rxGetLastChar() == '\n') {
        GPS_Data_Rdy = true;
    }
}

//Write to file prototype
bool writeToUsb(string line, FILE * f){

    if (f != NULL) {
        
        fprintf(f, line.c_str());
        fprintf(f, "\r\n");
        
    }
                 
    if(f == NULL){
        return false;
    }
    
    return true;      
}