gpr

Dependencies:   MbedJSONValue QEI

main.cpp

Committer:
aldyduino
Date:
2022-09-01
Revision:
12:19b1755d34e9
Parent:
10:dc33cd3f4eb9
Child:
13:40cf48007328

File content as of revision 12:19b1755d34e9:

#include "mbed.h"
#include "stdio.h"
#include "MbedJSONValue.h"
#include "string.h"
#include "stdint.h"
#include <cstdlib>
#define NUM_FIELDS  3 
#define MAX_BUFFER 150

Thread thread;

Serial pc(USBTX,USBRX);
AnalogOut DataDAC(p18);
Serial gps(p13, p14);

char eDataBuffer[500];
int i = 0;
int h_time,m_time,s_time;
char testStringBuffer[MAX_BUFFER]={0};
bool stringComplete = false;
int len = 0;
//number of comma seperated values in the data...TODO does this remain constant?
float latitude, longitude, timefix, speed, altitude;
char ns, ew;
void Init();
void parseGPS(char *cmd, int n);
void parseTime (float timeval);
void baca_gps();
 

void clearBuffer(char* buffer , int len){
    for (int i =0; i<len ; i++){
        buffer[i] = '\0';  
    }
}
  
void onSerialInput() {
  if(pc.readable()){ 
    char inChar = pc.getc();
    //pc.printf("%c",inChar);
    testStringBuffer[len] = inChar;    
    len++;       
    if (inChar == '\n') {
                pc.printf("%s",testStringBuffer);
                stringComplete = true;
                len = 0;
     }
}
}
void Init()
{
     gps.baud(9600);
     pc.baud(115200);
     pc.attach(&onSerialInput);
     baca_gps();
     pc.printf("Init OK\n");
}

void parseTime (float timeval)
{
    //format utc time to beijing time,add 8 time zone
                float time = timeval + 80000.00f;
                h_time = int(time) / 10000;
                m_time = (int(time) % 10000) / 100;
                s_time = int(time) % 100;
}


/* 
 * NMEA sentences:  https://www.gpsinformation.org/dale/nmea.htm#nmea
 *                  http://navspark.mybigcommerce.com/content/NMEA_Format_v0.1.pdf
 */
void parseGPS(char *cmd, int n)
{   
    char tf, status;
    int fq, nst, fix, date;                                     // fix quality, Number of satellites being tracked, 3D fix
   
    
    // Global Positioning System Fix Data
    if(strncmp(cmd,"$GPGGA", 6) == 0) 
    {
        sscanf(cmd, "$GPGGA,%f,%f,%c,%f,%c,%d,%d,%*f,%f", &timefix, &latitude, &ns, &longitude, &ew, &fq, &nst, &altitude);
        //pc.printf("Latitude: %f %c, Longitude: %f %c \n", latitude, ns, longitude, ew);
        
        
    }
    
    // Satellite status
     else if(strncmp(cmd,"$GPGSA", 6) == 0) 
    {
        sscanf(cmd, "$GPGSA,%c,%d,%d", &tf, &fix, &nst);
       // pc.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
    }
    
    // Geographic position, Latitude and Longitude
    else if(strncmp(cmd,"$GPGLL", 6) == 0) 
    {
        sscanf(cmd, "$GPGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix);
        //pc.printf("GPGLL Latitude: %f %c, Longitude: %f %c\n", latitude, ns, longitude, ew);
    }
    
    // Geographic position, Latitude and Longitude
    else if(strncmp(cmd,"$GPRMC", 6) == 0) 
    {
        sscanf(cmd, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &timefix, &status, &latitude, &ns, &longitude, &ew, &speed, &date);
        //pc.printf("GPRMC Latitude: %f %c, Longitude: %f %c\n", latitude, ns, longitude, ew);
    }
    
    // 
    else if(strncmp(cmd,"$GNVTG", 6) == 0) 
    {
     //   pc.printf("its a Vector Track message.\n");
    }
    
    else if(strncmp(cmd,"$GNGGA", 6) == 0) 
    {
        sscanf(cmd, "$GNGGA,%f,%f,%c,%f,%c,%d,%d,%*f,%f", &timefix, &latitude, &ns, &longitude, &ew, &fq, &nst, &altitude);
       //pc.printf("GNGGA Latitude: %f %c, Longitude: %f %c \n", latitude, ns, longitude, ew);
        parseTime(timefix);
       // pc.printf("Time: %d:%d:%d\n", h_time, m_time, s_time);
    }
    
    else if(strncmp(cmd,"$GNGSA", 6) == 0) 
    {
        sscanf(cmd, "$GNGSA,%c,%d,%d", &tf, &fix, &nst);
        //pc.printf("GNGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
    }
    
    else if(strncmp(cmd,"$GPGSV", 6) == 0) 
    {
     //   pc.printf("its a Satellite details message.\n");
    }
    
    else if(strncmp(cmd,"$GNGLL", 6) == 0) 
    {
        sscanf(cmd, "$GNGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix);
       //pc.printf("GNGLL Latitude: %f %c, Longitude: %f %c \n", latitude, ns, longitude, ew);
    }
    
    else
    {
   //     pc.printf("Unknown message type\n");
    }
}

char e;
void baca_gps() 
{   
      while(gps.readable())
        { 
            
            if(gps.getc() == '$');           // wait a $
            {
                for(int i=0; i<sizeof(eDataBuffer); i++)
                {
                    e = gps.getc();
                    if( e == '\r' )
                    {
                        pc.printf("%s\n", eDataBuffer);
                        parseGPS(eDataBuffer, i);
                        i = sizeof(eDataBuffer);
                    }
                    else
                    {
                        eDataBuffer[i] = e;
                        
                    }                 
                }
            }
         } 
    
}

//int main(int argc, char* argv[])
#define TOTAL_DATA 100
bool threadFlag = false;
MbedJSONValue threadDAC;
void DAC_Thread(){
    int periode;
    double DAC_Value;
    int counterDAC = 0;
    while (true) {
        if (threadFlag){
            periode = threadDAC["periode"].get<int>();
            DAC_Value= threadDAC["value"][counterDAC].get<double>();
            DataDAC = DAC_Value;
            pc.printf("DAC: %f \n",DAC_Value);
            wait(periode/TOTAL_DATA);
            counterDAC++;
            if(counterDAC>=TOTAL_DATA){
                counterDAC = 0;    
            }
        }
        else {
            DataDAC = 0;
            counterDAC = 0;    
        }
        
    }
}
int main()
{
    //float e[5]={0};
    Init();  
    MbedJSONValue json;
    MbedJSONValue jsonGPS;
    thread.start(DAC_Thread);
    std::string buffer;
    
    while(1){
       
       if (stringComplete){
            parse(json,testStringBuffer);
            if (json.hasMember("type")){
                if (json["type"].get<std::string>() == "DAC"){
                    threadDAC = json;   
                    threadFlag = true;         
                }
                else if(json["type"].get<std::string>() == "STOP"){
                    threadFlag = false; 
                }
                else if(json["type"].get<std::string>() == "GPS"){              
                    jsonGPS["type"] = "GPS";
                    jsonGPS["lat"] = latitude;
                    jsonGPS["ns"] = ns;
                    jsonGPS["long"] = longitude;
                    jsonGPS["ew"]  = ew;
                    buffer = jsonGPS.serialize();
                    pc.printf("%s\n",buffer.c_str());
                    //pc.printf("Latitude: %f %c, Longitude: %f %c \n", latitude, ns, longitude, ew);
                    
                }
               
                else {
                     pc.printf("Type is not available\n");
                }
                stringComplete = false;
                clearBuffer(testStringBuffer,MAX_BUFFER);
            }
            else{
                pc.printf("Unknown JSON Message \n");
                stringComplete = false;
                clearBuffer(testStringBuffer,MAX_BUFFER);
            }

            
        }
        //wait(1);
        //return 0;
    
    }
}