#include 
#include 
#include 
#include 
#include 

Linkit ONE 讀取GPS紀錄並寫入SPI flash

Linkit ONE 讀取GPS紀錄並寫入SPI flash



#include "LGPS.h"
#include "LTask.h"
#include "LFlash.h"
#include "LSD.h"
#include "LStorage.h"


#define Drv LFlash          // use Internal 10M Flash
// #define Drv LSD           // use SD card


gpsSentenceInfoStruct info;

//Global define the GPS parameters
char buff[256];

double latitude;
double longitude;
double height=0;
int tmp, year, month, day, hour, minute, second, num ;
char datestamp[12];
char timestamp[12];
double bearing=0 ; 
double velocity=0 ;


static unsigned char getComma(unsigned char num,const char *str)
{
  unsigned char i,j = 0;
  int len=strlen(str);
  for(i = 0;i < len;i ++)
  {
     if(str[i] == ',')
      j++;
     if(j == num)
      return i + 1; 
  }
  return 0; 
}

static double getDoubleNumber(const char *s)
{
  char buf[10];
  unsigned char i;
  double rev;
  
  i=getComma(1, s);
  i = i - 1;
  strncpy(buf, s, i);
  buf[i] = 0;
  rev=atof(buf);
  return rev; 
}

static double getIntNumber(const char *s)
{
  char buf[10];
  unsigned char i;
  int rev;
  
  i=getComma(1, s);
  i = i - 1;
  strncpy(buf, s, i);
  buf[i] = 0;
  rev=atoi(buf);
  return rev; 
}

void parseGPGGA(const char* GPGGAstr)
{
  /* Refer to http://www.gpsinformation.org/dale/nmea.htm#GGA
   * Sample data: $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
   * Where:
   *  GGA          Global Positioning System Fix Data
   *  123519       Fix taken at 12:35:19 UTC
   *  4807.038,N   Latitude 48 deg 07.038' N
   *  01131.000,E  Longitude 11 deg 31.000' E
   *  1            Fix quality: 0 = invalid
   *                            1 = GPS fix (SPS)
   *                            2 = DGPS fix
   *                            3 = PPS fix
   *                            4 = Real Time Kinematic
   *                            5 = Float RTK
   *                            6 = estimated (dead reckoning) (2.3 feature)
   *                            7 = Manual input mode
   *                            8 = Simulation mode
   *  08           Number of satellites being tracked
   *  0.9          Horizontal dilution of position
   *  545.4,M      Altitude, Meters, above mean sea level
   *  46.9,M       Height of geoid (mean sea level) above WGS84
   *                   ellipsoid
   *  (empty field) time in seconds since last DGPS update
   *  (empty field) DGPS station ID number
   *  *47          the checksum data, always begins with *
      Recommended Minimum Specific GPS/TRANSIT Data(RMC)推薦定位資訊

   */

  if(GPGGAstr[0] == '$')
  {
    tmp = getComma(2, GPGGAstr);
    latitude = getDoubleNumber(&GPGGAstr[tmp])*10000;
  
    tmp = getComma(4, GPGGAstr);
    longitude = getDoubleNumber(&GPGGAstr[tmp])*10000;

    tmp = getComma(1, GPGGAstr);
    hour     = (GPGGAstr[tmp + 0] - '0') * 10 + (GPGGAstr[tmp + 1] - '0');
    minute   = (GPGGAstr[tmp + 2] - '0') * 10 + (GPGGAstr[tmp + 3] - '0');
    second    = (GPGGAstr[tmp + 4] - '0') * 10 + (GPGGAstr[tmp + 5] - '0');
    sprintf(timestamp, "%2d:%2d:%2d", hour, minute, second);
        

        
    tmp = getComma(7, GPGGAstr);
    num = getIntNumber(&GPGGAstr[tmp]);    
    
    tmp = getComma(9, GPGGAstr);
    height = getDoubleNumber(&GPGGAstr[tmp]);
        
  }
  else
  {
    Serial.println("Not get data"); 
  }
}

void parseGPRMC(const char* GPRMCstr)
{
   /*  
    $GPRMC,(1),(2),(3),(4),(5),(6),(*7),(*8),(*9),(10),(11),(12)*hh
    (1) UTC時間,hhmmss(時分秒)格式 
    (2) 定位狀態,A=有效定位,V=無效定位 
    (3) 緯度ddmm.mmmm(度分)格式(前面的0也將被傳輸) 
    (4) 緯度半球N(北半球)或S(南半球) 
    (5) 經度dddmm.mmmm(度分)格式(前面的0也將被傳輸) 
    (6) 經度半球E(東經)或W(西經) 
    (7) 地面速率(000.0~999.9節,前面的0也將被傳輸) 
    (8) 地面航向(000.0~359.9度,以真北為參考基準,前面的0也將被傳輸) 
    (9) UTC日期,ddmmyy(日月年)格式 
    (10) 磁偏角(000.0~180.0度,前面的0也將被傳輸) 
    (11) 磁偏角方向,E(東)或W(西) 
    (12) 模式指示(僅NMEA0183 3.00版本輸出,A=自主定位,D=差分,E=估算,N=資料無效)
   */<1><2><3><4><5><6><10><11><12><1><2><3><4><5><6><7><8><9><10><11><12>

  if(GPRMCstr[0] == '$')
  {
   
    tmp = getComma(7, GPRMCstr);
    velocity = getDoubleNumber(&GPRMCstr[tmp])*0.5144444;  // translated knot to m/sec
    tmp = getComma(8, GPRMCstr);
    bearing = getDoubleNumber(&GPRMCstr[tmp]);
    
    tmp = getComma(9, GPRMCstr);
    day     = (GPRMCstr[tmp + 0] - '0') * 10 + (GPRMCstr[tmp + 1] - '0');
    month   = (GPRMCstr[tmp + 2] - '0') * 10 + (GPRMCstr[tmp + 3] - '0');
    year    = (GPRMCstr[tmp + 4] - '0') * 10 + (GPRMCstr[tmp + 5] - '0')  + 2000;
    sprintf(datestamp, "%4d/%2d/%2d", year, month, day);
   
  }
  else
  {
    Serial.println("Not get data"); 
  }
}

double Coordinate_transform( double GPS_temp )
{
  // To transform GPS(dd°mm'ss.ss") to GPS decimal format
  char gps_datum[14];
  int   GPS_degree  = GPS_temp / 1000000                    ; // modulo down the residue for degree
        GPS_temp    = GPS_temp - GPS_degree * 1000000       ; // take back the residue
  int   GPS_minutes = GPS_temp / 10000                      ; // modulo down the residue for minute
  float GPS_second  = (GPS_temp - GPS_minutes * 10000 ) /100; // take back the residue and calculate second

  float GPS_return = GPS_degree / 1.0 + GPS_minutes / 60.0 + GPS_second / 3600.0 ;
  Serial.print("GPS_return=  ");Serial.println(GPS_return,8);
  return GPS_return ; 
}



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.print("Initializing SD card...");
    // make sure that the default chip select pin is set to
    // output, even if you don't use it:
  pinMode(10, OUTPUT);

    // see if the card is present and can be initialized:
  Drv.begin();
  Serial.println("card initialized.");
  
  LGPS.powerOn();
  Serial.println("LGPS Power on, and waiting ..."); 
  delay(60000);
}

void loop() 
{
  char dataString[400];
  // put your main code here, to run repeatedly:
  Serial.println("LGPS loop"); 
  LGPS.getData(&info);
  Serial.println((char*)info.GPGGA); 
  parseGPGGA((const char*)info.GPGGA);
  Serial.println((char*)info.GPRMC);
  parseGPRMC((const char*)info.GPRMC);

  latitude = Coordinate_transform(latitude);
  longitude = Coordinate_transform(longitude) - 0.005 ;  // WGS84 transfer to Google map had about 500m right shift.

  Serial.println();Serial.println();
  Serial.println("-------------------------------------------------------------");
  Serial.print("GPS datum:"); Serial.print(datestamp);Serial.print(" ");Serial.println(timestamp);
  Serial.print("Location :"); Serial.print(latitude,7); Serial.print(","); Serial.println(longitude,7);
  Serial.print("Elevation:"); Serial.print(height);Serial.println(" m");
  Serial.print("satellites number:"); Serial.println(num);
  Serial.print("Bearing:"); Serial.print(bearing);Serial.println(" degree");
  Serial.print("Velocity:"); Serial.print(velocity);Serial.println(" m/sec");
  Serial.println("-------------------------------------------------------------");

  sprintf(dataString, "%s,%s,%.7f,%.7f,%.1f,%d,%.2f,%.2f", 
          datestamp,
          timestamp,
          latitude,
          longitude,
          height,
          num,
          bearing,
          velocity 
          );
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  Serial.println(dataString);
  
  LFile dataFile = Drv.open("GPSlogger.csv", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) 
  {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else 
  {
    Serial.println("error opening datalog.txt");
  }

  delay(10000);

}


留言