NMEA0813フォーマットのGPSから情報を取り出すプログラムです。

Dependents:   GPS_test EM_Logger

Committer:
YSB
Date:
Fri Aug 16 08:22:08 2013 +0000
Revision:
4:7be9581d0734
Parent:
2:7870c69fa58c
20130816ver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YSB 0:42a334c405de 1 #include "nmea0813.h"
YSB 0:42a334c405de 2
YSB 0:42a334c405de 3 GPS::GPS(PinName tx,PinName rx) : Serial(tx,rx){
YSB 0:42a334c405de 4 flg = 0;
YSB 0:42a334c405de 5 count = 0;
YSB 0:42a334c405de 6 attach(this, &GPS::rxHandler,Serial::RxIrq);
YSB 0:42a334c405de 7 T.attach(this,&GPS::update_infomation,1.0);
YSB 0:42a334c405de 8 }
YSB 0:42a334c405de 9
YSB 0:42a334c405de 10
YSB 0:42a334c405de 11 void GPS::rxHandler(void){
YSB 0:42a334c405de 12 rxbuf = getc();
YSB 0:42a334c405de 13 GPSdata[count] = rxbuf;
YSB 4:7be9581d0734 14 if(rxbuf == LF){
YSB 0:42a334c405de 15 flg++;
YSB 4:7be9581d0734 16 }else{}
YSB 4:7be9581d0734 17 count++;
YSB 0:42a334c405de 18 if(flg == 7){
YSB 0:42a334c405de 19 flg = 0;
YSB 4:7be9581d0734 20 GPSdata[count+1]='\0';
YSB 0:42a334c405de 21 count=0;
YSB 0:42a334c405de 22 }
YSB 0:42a334c405de 23 }
YSB 0:42a334c405de 24
YSB 0:42a334c405de 25 void GPS::update_infomation() { //repeatedlly called function
YSB 4:7be9581d0734 26 set_GGA_RMC(GPSdata);
YSB 0:42a334c405de 27 }
YSB 0:42a334c405de 28
YSB 4:7be9581d0734 29 void GPS::set_GGA_RMC(char* str){
YSB 0:42a334c405de 30 char *sp;
YSB 0:42a334c405de 31 sp = (char*) strstr(str,"$GPGGA");
YSB 4:7be9581d0734 32 if(sp != NULL){
YSB 4:7be9581d0734 33 for(int i=0;i<100;i++){
YSB 4:7be9581d0734 34 if(sp[i] != '*'){
YSB 0:42a334c405de 35 GPGGA[i] = sp[i];
YSB 0:42a334c405de 36 }else{
YSB 4:7be9581d0734 37 GPGGA[i] = '*';
YSB 4:7be9581d0734 38 GPGGA[i+1] = '\0';
YSB 4:7be9581d0734 39 //gga_checksum = sp[i+1]*16+sp[i+2];
YSB 4:7be9581d0734 40 i = 100;
YSB 4:7be9581d0734 41 }
YSB 4:7be9581d0734 42 }
YSB 0:42a334c405de 43 }
YSB 0:42a334c405de 44 sp = (char*) strstr(str,"$GPRMC");
YSB 4:7be9581d0734 45 if(sp != NULL){
YSB 4:7be9581d0734 46 for(int i=0;i<100;i++){
YSB 4:7be9581d0734 47 if(sp[i] != '*'){
YSB 0:42a334c405de 48 GPRMC[i] = sp[i];
YSB 0:42a334c405de 49 }else{
YSB 4:7be9581d0734 50 GPRMC[i] = '*';
YSB 4:7be9581d0734 51 GPRMC[i+1] = '\0';
YSB 4:7be9581d0734 52 //rmc_checksum = sp[i+1]*16+sp[i+2];
YSB 4:7be9581d0734 53 i = 100;
YSB 4:7be9581d0734 54 }
YSB 4:7be9581d0734 55 }
YSB 4:7be9581d0734 56 }
YSB 2:7870c69fa58c 57 }
YSB 2:7870c69fa58c 58
YSB 2:7870c69fa58c 59 char* GPS::get_time(){
YSB 4:7be9581d0734 60 time_str[0]=GPGGA[7];
YSB 4:7be9581d0734 61 time_str[1]=GPGGA[8];
YSB 4:7be9581d0734 62 time_str[2]=':';
YSB 4:7be9581d0734 63 time_str[3]=GPGGA[9];
YSB 4:7be9581d0734 64 time_str[4]=GPGGA[10];
YSB 4:7be9581d0734 65 time_str[5]=':';
YSB 4:7be9581d0734 66 time_str[6]=GPGGA[11];
YSB 4:7be9581d0734 67 time_str[7]=GPGGA[12];
YSB 4:7be9581d0734 68 time_str[8]='\0';
YSB 4:7be9581d0734 69
YSB 2:7870c69fa58c 70 return time_str;
YSB 2:7870c69fa58c 71 }
YSB 2:7870c69fa58c 72
YSB 2:7870c69fa58c 73 float GPS::get_latitude(){
YSB 4:7be9581d0734 74 for(int i=0;i<9;i++){latitude_str[i]=GPGGA[18+i];}
YSB 4:7be9581d0734 75 latitude_str[9]='\0';
YSB 0:42a334c405de 76 latitude = (float)(latitude_str[0]-0x30)*10.0+ \
YSB 0:42a334c405de 77 (float)(latitude_str[1]-0x30)+ \
YSB 0:42a334c405de 78 ((float)(latitude_str[2]-0x30)*10.0+ \
YSB 0:42a334c405de 79 (float)(latitude_str[3]-0x30)+ \
YSB 0:42a334c405de 80 (float)(latitude_str[5]-0x30)*0.1+ \
YSB 0:42a334c405de 81 (float)(latitude_str[6]-0x30)*0.01+ \
YSB 0:42a334c405de 82 (float)(latitude_str[7]-0x30)*0.001+ \
YSB 0:42a334c405de 83 (float)(latitude_str[8]-0x30)*0.0001)/60.0;
YSB 2:7870c69fa58c 84 return latitude;
YSB 2:7870c69fa58c 85 }
YSB 2:7870c69fa58c 86
YSB 2:7870c69fa58c 87 char* GPS::get_str_latitude(){
YSB 4:7be9581d0734 88 for(int i=0;i<9;i++){latitude_str[i]=GPGGA[18+i];}
YSB 4:7be9581d0734 89 latitude_str[9]='\0';
YSB 2:7870c69fa58c 90 return latitude_str;
YSB 2:7870c69fa58c 91 }
YSB 2:7870c69fa58c 92
YSB 2:7870c69fa58c 93 float GPS::get_longitude(){
YSB 4:7be9581d0734 94 for(int i=0;i<10;i++){longitude_str[i]=GPGGA[30+i];}
YSB 4:7be9581d0734 95 longitude_str[10]='\0';
YSB 0:42a334c405de 96 longitude = (float)(longitude_str[0]-0x30)*100.0+ \
YSB 0:42a334c405de 97 (float)(longitude_str[1]-0x30)*10.0+ \
YSB 0:42a334c405de 98 (float)(longitude_str[2]-0x30)+ \
YSB 0:42a334c405de 99 ((float)(longitude_str[3]-0x30)*10.0+ \
YSB 0:42a334c405de 100 (float)(longitude_str[4]-0x30)+ \
YSB 0:42a334c405de 101 (float)(longitude_str[6]-0x30)*0.1+ \
YSB 0:42a334c405de 102 (float)(longitude_str[7]-0x30)*0.01+ \
YSB 0:42a334c405de 103 (float)(longitude_str[8]-0x30)*0.001+ \
YSB 0:42a334c405de 104 (float)(longitude_str[9]-0x30)*0.0001)/60.0;
YSB 0:42a334c405de 105 return longitude;
YSB 0:42a334c405de 106 }
YSB 0:42a334c405de 107
YSB 0:42a334c405de 108 char* GPS::get_str_longitude(){
YSB 4:7be9581d0734 109 for(int i=0;i<10;i++){longitude_str[i]=GPGGA[30+i];}
YSB 4:7be9581d0734 110 longitude_str[10]='\0';
YSB 0:42a334c405de 111 return longitude_str;
YSB 0:42a334c405de 112 }
YSB 0:42a334c405de 113
YSB 2:7870c69fa58c 114 char GPS::get_status(){
YSB 4:7be9581d0734 115 status = GPRMC[18];
YSB 2:7870c69fa58c 116 return status;
YSB 0:42a334c405de 117 }
YSB 0:42a334c405de 118
YSB 0:42a334c405de 119 float GPS::get_speed(){
YSB 4:7be9581d0734 120 for(int i=0;i<5;i++){speed_str[i]=GPRMC[51+i];}
YSB 4:7be9581d0734 121 speed_str[5]='\0';
YSB 2:7870c69fa58c 122 speed = (float)(speed_str[0]-0x30)*100.0+\
YSB 2:7870c69fa58c 123 (float)(speed_str[1]-0x30)*10.0+\
YSB 2:7870c69fa58c 124 (float)(speed_str[2]-0x30)*1.0+\
YSB 2:7870c69fa58c 125 (float)(speed_str[4]-0x30)*0.1;
YSB 0:42a334c405de 126 return speed;
YSB 2:7870c69fa58c 127 }
YSB 2:7870c69fa58c 128
YSB 4:7be9581d0734 129 int GPS::get_satelite_number(){
YSB 4:7be9581d0734 130 number_of_satelite = (int)(GPGGA[45]-0x30)*10 + (int)(GPGGA[46]-0x30);
YSB 2:7870c69fa58c 131 return number_of_satelite;
YSB 0:42a334c405de 132 }