エレキジャック Web版 mbedで初めてのマイコン開発 センサを使ってみよう Rapid PrototypingでGPSロガーをサクサク作るの記事のGPS Loggerのプログラムです。とても簡単にGPS Loggerを作ることができます。 http://www.eleki-jack.com/arm/2011/03/mbed-rapid-prototypinggps-1.html

Dependencies:   TextLCD mbed SDFileSystem

Files at this revision

API Documentation at this revision

Comitter:
sunifu
Date:
Tue Mar 08 08:13:49 2011 +0000
Commit message:

Changed in this revision

FATFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
MyGPS/MyGPS.cpp Show annotated file Show diff for this revision Revisions of this file
MyGPS/MyGPS.h Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FATFileSystem.lib	Tue Mar 08 08:13:49 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_unsupported/code/fatfilesystem/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MyGPS/MyGPS.cpp	Tue Mar 08 08:13:49 2011 +0000
@@ -0,0 +1,201 @@
+#include "MyGPS.h"
+
+MyGPS::MyGPS(PinName tx, PinName rx) : _gps(tx,rx)
+{
+    _gps.baud(4800) ;
+    setTimeLocMode(1); // UTC->JST Change TimeLocationMode 
+    setLongitude(0.0) ;
+    setLatitude(0.0) ;
+    setStatus(0) ;
+    setNS('-');
+    setEW('-');
+    setUpdateTime(10);
+}
+
+float MyGPS::getLongitude()
+{
+    return longitude;
+}
+
+float MyGPS::getLatitude()
+{
+    return latitude;
+}
+
+void MyGPS::setLatitude(float lat)
+{
+    float deg, min;
+    
+    if ( lat != 0.0 ){
+        deg = trunc(lat /100.0) ;
+        min = lat - ( deg * 100.0);
+        latitude = (deg + min / 60.0);
+    }else{
+        latitude = 0.0;
+    }    
+}
+
+void MyGPS::setLongitude(float lon)
+{
+    float deg, min;
+
+    if ( lon != 0.0 ){
+        deg = trunc(lon/100.0);
+        min = lon - ( deg * 100.0) ;
+        longitude = (deg + min / 60.0);
+    }else{
+        longitude = 0.0 ;
+    }
+}
+
+void MyGPS::setNS(char lns)
+{
+    if(lns == 'N' || lns =='S')
+        ns = lns;
+    else
+        ns = '-' ;
+}
+char MyGPS::getNS()
+{
+    return ns;
+}
+void MyGPS::setEW(char lew)
+{
+    if (lew == 'E' || lew == 'W' )
+        ew = lew ;
+    else
+        ew = '-' ;
+}
+char MyGPS::getEW()
+{
+    return ew;
+}
+
+int MyGPS::getStatus()
+{
+    return status;
+}    
+
+char *MyGPS::getNMEA()
+{
+    return msg;
+}    
+void MyGPS::setStatus(int stat)
+{
+    status = stat;
+}
+
+void MyGPS::setTime()
+{
+    struct tm s_time;
+    int i ;
+    float lat,lon,ltime;
+    char lns,lew;
+            
+    while(1){
+        if (_gps.readable()) {
+            //_gps.getc(); // $ cut
+            for ( i = 0 ; (msg[i]=_gps.getc())!='\r' ; i++);
+            _gps.getc();
+            msg[i] ='\0' ;
+        }
+        printf("%s \r\n",msg); // Debug 
+        if(strstr(msg,"GPRMC") != NULL ){
+            char statc;
+            float d1,d2;
+            int date;
+            sscanf(msg, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,%f,%d",&ltime,&statc,&lat,&lns,&lon,&lew,&d1,&d2,&date);
+//ltime=161229.00; statc='A' ; date=120511; lat=3612.2475;lon=12158.34;lew='W';lns='N';
+            //printf("GPRMC=>[%c] Date[%d] Time[%f]\r\n",statc); // Debug  
+            
+            if(statc == 'A'){
+                s_time.tm_mday = (int)(date / 10000 );
+                s_time.tm_mon = (int)((date - (s_time.tm_mday) * 10000) /100 ) - 1 ;
+                s_time.tm_year = date%100+100;
+                
+                s_time.tm_hour = (int)((int)ltime / 10000 );
+                s_time.tm_min  = (int)(((int)ltime - (s_time.tm_hour * 10000 )) / 100 ) ;
+                s_time.tm_sec  =  (int)ltime%100 ;
+                set_time(mktime(&s_time)) ;
+                setStatus(1);
+                break;
+            }
+        }
+    }                    
+}
+
+time_t MyGPS::getTime()
+{
+     if( getTimeLocMode() == 0 )
+        return time(NULL); // UTC
+     else if ( getTimeLocMode() == 1 )
+        return ( time(NULL) + 32400 ) ; // JST Add 9Hour
+     else
+        return time(NULL) ;
+}
+
+int MyGPS::getTimeLocMode()
+{
+    return timeLocMode;
+}
+
+void MyGPS::setTimeLocMode(int tlm)
+{
+    timeLocMode = tlm ;
+}
+void MyGPS::setUpdateTime(int update)
+{
+    updateTime = update;
+}
+int MyGPS::getUpdateTime()
+{
+    return updateTime;
+}        
+float MyGPS::trunc(float v)
+{
+    if ( v < 0.0 ){
+        v *= -1.0;
+        v = floor(v);
+        v *= -1.0;
+    }else{
+        v = floor(v);
+    }
+    return v;    
+}
+
+int MyGPS::sample(){
+    int i ;
+    float lat,lon,ltime;
+    char lns,lew;
+          
+    while(1){
+        if (_gps.readable()) {
+            //_gps.getc(); // $ cut
+            for ( i = 0 ; (msg[i]=_gps.getc())!='\r' ; i++);
+            _gps.getc();
+            msg[i] ='\0' ;
+        }
+        printf("%s \r\n",msg); // Debug 
+        if(strstr(msg,"GPRMC") != NULL ){
+            char statc;
+
+            sscanf(msg, "$GPRMC,%f,%c,%f,%c,%f,%c",&ltime,&statc,&lat,&lns,&lon,&lew);
+//ltime=161229.00; statc='A' ; date=120511; lat=3612.2475;lon=12158.34;lew='W';lns='N';
+            //printf("GPRMC=>[%c]\r\n",statc); // Debug  
+            
+            if(statc == 'A'){
+
+                setLatitude(lat) ;
+                setNS(lns);                
+                setLongitude(lon) ;     
+                setEW(lew);
+          
+                setStatus(1);
+                return 0;
+            }else{
+                setStatus(0) ;
+                return -1;
+            }
+        }                           
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MyGPS/MyGPS.h	Tue Mar 08 08:13:49 2011 +0000
@@ -0,0 +1,44 @@
+#ifndef MBED_MYGPS_H
+#define MBED_MYGPS_H
+
+#include "mbed.h"
+
+class MyGPS {
+    
+public :   
+    MyGPS(PinName tx,PinName rx);
+    
+    
+    float getLongitude();
+    float getLatitude();
+    char  getNS();
+    char  getEW();
+    int   getStatus() ;
+    int   sample();
+    time_t getTime();
+    char* getNMEA();
+    void setUpdateTime(int);
+    int getUpdateTime();   
+    void setTime();
+       
+private:
+    float longitude ;
+    float latitude ;    
+    int status ;
+    Serial _gps;
+    char msg[256] ;
+    char ns, ew;
+    int timeLocMode ; 
+    int updateTime;
+     
+    float trunc(float);
+    void setLongitude(float);
+    void setLatitude(float);
+    void setStatus(int);
+    void setNS(char);
+    void setEW(char);
+    int getTimeLocMode() ;
+    void setTimeLocMode(int) ;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Tue Mar 08 08:13:49 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/SDFileSystem/#b1ddfc9a9b25
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Tue Mar 08 08:13:49 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Mar 08 08:13:49 2011 +0000
@@ -0,0 +1,94 @@
+#include "mbed.h"
+#include "MyGPS.h"
+#include "TextLCD.h"
+#include "string.h"
+#include "SDFileSystem.h"
+
+TextLCD lcd(p24, p26, p27, p28, p29, p30) ;
+MyGPS gps(p9, p10);  // tx, rx
+SDFileSystem sd(p5, p6, p7, p8, "sd1") ;
+DigitalOut led1(LED1);
+
+void writeLocation(void) ;
+void ledFlash(void);
+
+int main() {
+    time_t oldTime = 0 ,nowTime = 0;
+    char strTimeMsg[16];
+    time_t t_time;
+    
+    gps.setUpdateTime(10);
+    lcd.cls();
+       
+    lcd.locate(0,0); 
+    lcd.printf("Please Wait....");
+    gps.setTime();
+
+    t_time = gps.getTime() ;
+    
+    strftime(strTimeMsg,16,"%m/%d %H:%M:%S",localtime(&t_time));
+    lcd.locate(0,0); 
+    lcd.printf("%s",strTimeMsg);
+    wait(2.0);
+       
+    while (1) {
+        gps.sample();
+        lcd.cls();
+        if ( gps.getStatus() ==1 ){
+            lcd.locate(0,0);
+            lcd.printf("lat:%10.6f %c",gps.getLatitude(),gps.getNS());
+            lcd.locate(0,1);
+            lcd.printf("lon:%10.6f %c",gps.getLongitude(),gps.getEW());  
+                     
+            nowTime = gps.getTime();        
+            if( (oldTime + gps.getUpdateTime()) < nowTime ){
+                writeLocation();
+                oldTime = nowTime;
+            }        
+        }else{
+            lcd.locate(0,1); 
+            lcd.printf("data not valid!");
+        }
+    }   
+}
+
+void writeLocation(void)
+{
+    FILE *fp,*fp1;
+    char strTimeMsg[32];
+    time_t t_time;
+    
+    if ( gps.getStatus() == 1 ){
+        if ( (fp = fopen("/sd1/gps.txt","a"))== NULL ) {
+            lcd.cls();
+            lcd.locate(0,0);
+            lcd.printf("gps Open Failed.") ;
+            wait(0.5);
+        }else{
+            t_time = gps.getTime() ;
+            strftime(strTimeMsg,32,"%Y/%m/%d %H:%M:%S ",localtime(&t_time));
+            fprintf(fp,"%s",strTimeMsg);
+            fprintf(fp,"Latitude:%f Longitude:%f\n",gps.getLatitude(),gps.getLongitude());    
+         
+            ledFlash(); 
+            fclose(fp);             
+        }
+       
+        if ( (fp1 = fopen("/sd1/NMEA.nme","a"))== NULL ) {
+            lcd.cls();
+            lcd.locate(0,1);
+            lcd.printf("NMEA Open Failed.") ;
+            wait(0.5);
+        }else{    
+            fprintf(fp1,"%s\n",gps.getNMEA());
+            fclose(fp1); 
+        }    
+    }
+}
+
+void ledFlash(void)
+{
+    led1=1;
+    wait(0.3);
+    led1=0; 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Mar 08 08:13:49 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9a9732ce53a1