NMEA0813フォーマットのGPSから情報を取り出すプログラムです。
Dependents: GPS_test EM_Logger
nmea0813.cpp@0:42a334c405de, 2013-03-29 (annotated)
- Committer:
- YSB
- Date:
- Fri Mar 29 05:55:53 2013 +0000
- Revision:
- 0:42a334c405de
- Child:
- 1:f4d3c59a4917
- Child:
- 2:7870c69fa58c
ver.1.0
Who changed what in which revision?
User | Revision | Line number | New 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 | //time_str[8] = {'0','0',':','0','0',':','0','0'}; |
YSB | 0:42a334c405de | 5 | //latitude_str[9] = {'0','0','0','0','.','0','0','0','0'}; |
YSB | 0:42a334c405de | 6 | //longitude_str[10]= {'0','0','0','0','0','.','0','0','0','0'}; |
YSB | 0:42a334c405de | 7 | flg = 0; |
YSB | 0:42a334c405de | 8 | count = 0; |
YSB | 0:42a334c405de | 9 | attach(this, &GPS::rxHandler,Serial::RxIrq); |
YSB | 0:42a334c405de | 10 | T.attach(this,&GPS::update_infomation,1.0); |
YSB | 0:42a334c405de | 11 | } |
YSB | 0:42a334c405de | 12 | |
YSB | 0:42a334c405de | 13 | |
YSB | 0:42a334c405de | 14 | void GPS::rxHandler(void){ |
YSB | 0:42a334c405de | 15 | char rxbuf; |
YSB | 0:42a334c405de | 16 | rxbuf = getc(); |
YSB | 0:42a334c405de | 17 | GPSdata[count] = rxbuf; |
YSB | 0:42a334c405de | 18 | if(rxbuf == '$'){ |
YSB | 0:42a334c405de | 19 | count++; |
YSB | 0:42a334c405de | 20 | } |
YSB | 0:42a334c405de | 21 | else if(rxbuf == LF){ |
YSB | 0:42a334c405de | 22 | count++; |
YSB | 0:42a334c405de | 23 | flg++; |
YSB | 0:42a334c405de | 24 | } |
YSB | 0:42a334c405de | 25 | else{ |
YSB | 0:42a334c405de | 26 | count++; |
YSB | 0:42a334c405de | 27 | } |
YSB | 0:42a334c405de | 28 | if(flg == 7){ |
YSB | 0:42a334c405de | 29 | flg = 0; |
YSB | 0:42a334c405de | 30 | count=0; |
YSB | 0:42a334c405de | 31 | } |
YSB | 0:42a334c405de | 32 | } |
YSB | 0:42a334c405de | 33 | |
YSB | 0:42a334c405de | 34 | void GPS::update_infomation() { //repeatedlly called function |
YSB | 0:42a334c405de | 35 | get_GGA_RMC(GPSdata); |
YSB | 0:42a334c405de | 36 | get_infomation(GPGGA,GPRMC); |
YSB | 0:42a334c405de | 37 | } |
YSB | 0:42a334c405de | 38 | |
YSB | 0:42a334c405de | 39 | void GPS::get_GGA_RMC(char* str){ |
YSB | 0:42a334c405de | 40 | int nullflg=0; |
YSB | 0:42a334c405de | 41 | char *sp; |
YSB | 0:42a334c405de | 42 | |
YSB | 0:42a334c405de | 43 | sp = (char*) strstr(str,"$GPGGA"); |
YSB | 0:42a334c405de | 44 | for(int i=0;i<80;i++){ |
YSB | 0:42a334c405de | 45 | if(nullflg ==0){ |
YSB | 0:42a334c405de | 46 | if(sp[i] != '\n'){ |
YSB | 0:42a334c405de | 47 | GPGGA[i] = sp[i]; |
YSB | 0:42a334c405de | 48 | }else{ |
YSB | 0:42a334c405de | 49 | GPGGA[i] = '\n'; |
YSB | 0:42a334c405de | 50 | nullflg = 1; |
YSB | 0:42a334c405de | 51 | } |
YSB | 0:42a334c405de | 52 | }else{ |
YSB | 0:42a334c405de | 53 | GPGGA[i] = '\n'; |
YSB | 0:42a334c405de | 54 | } |
YSB | 0:42a334c405de | 55 | } |
YSB | 0:42a334c405de | 56 | nullflg = 0; |
YSB | 0:42a334c405de | 57 | sp = (char*) strstr(str,"$GPRMC"); |
YSB | 0:42a334c405de | 58 | for(int i=0;i<80;i++){ |
YSB | 0:42a334c405de | 59 | if(nullflg ==0){ |
YSB | 0:42a334c405de | 60 | if(sp[i] != '\n'){ |
YSB | 0:42a334c405de | 61 | GPRMC[i] = sp[i]; |
YSB | 0:42a334c405de | 62 | }else{ |
YSB | 0:42a334c405de | 63 | GPRMC[i] = '\n'; |
YSB | 0:42a334c405de | 64 | nullflg = 1; |
YSB | 0:42a334c405de | 65 | } |
YSB | 0:42a334c405de | 66 | }else{ |
YSB | 0:42a334c405de | 67 | GPRMC[i] = '\n'; |
YSB | 0:42a334c405de | 68 | } |
YSB | 0:42a334c405de | 69 | } |
YSB | 0:42a334c405de | 70 | nullflg = 0; |
YSB | 0:42a334c405de | 71 | } |
YSB | 0:42a334c405de | 72 | |
YSB | 0:42a334c405de | 73 | void GPS::get_infomation(char* gga,char* rmc){ |
YSB | 0:42a334c405de | 74 | if(gga[8]=='0'){ |
YSB | 0:42a334c405de | 75 | time_str[0]=gga[7]; |
YSB | 0:42a334c405de | 76 | time_str[1]=gga[8]+0x09; |
YSB | 0:42a334c405de | 77 | }else{ |
YSB | 0:42a334c405de | 78 | time_str[0]=gga[7]+0x01; |
YSB | 0:42a334c405de | 79 | time_str[1]=gga[8]-0x01; |
YSB | 0:42a334c405de | 80 | } |
YSB | 0:42a334c405de | 81 | time_str[2]=':'; |
YSB | 0:42a334c405de | 82 | time_str[3]=gga[9]; |
YSB | 0:42a334c405de | 83 | time_str[4]=gga[10]; |
YSB | 0:42a334c405de | 84 | time_str[5]=':'; |
YSB | 0:42a334c405de | 85 | time_str[6]=gga[11]; |
YSB | 0:42a334c405de | 86 | time_str[7]=gga[12]; |
YSB | 0:42a334c405de | 87 | |
YSB | 0:42a334c405de | 88 | states = rmc[18]; |
YSB | 0:42a334c405de | 89 | |
YSB | 0:42a334c405de | 90 | for(int i=0;i<9;i++){ |
YSB | 0:42a334c405de | 91 | latitude_str[i]=gga[18+i]; |
YSB | 0:42a334c405de | 92 | } |
YSB | 0:42a334c405de | 93 | for(int i=0;i<10;i++){ |
YSB | 0:42a334c405de | 94 | longitude_str[i]=gga[30+i]; |
YSB | 0:42a334c405de | 95 | } |
YSB | 0:42a334c405de | 96 | for(int i=0;i<5;i++){ |
YSB | 0:42a334c405de | 97 | speed_str[i]=rmc[51+i]; |
YSB | 0:42a334c405de | 98 | } |
YSB | 0:42a334c405de | 99 | speed = (float)(speed_str[0]-0x30)*100.0+\ |
YSB | 0:42a334c405de | 100 | (float)(speed_str[1]-0x30)*10.0+\ |
YSB | 0:42a334c405de | 101 | (float)(speed_str[2]-0x30)*1.0+\ |
YSB | 0:42a334c405de | 102 | (float)(speed_str[4]-0x30)*0.1; |
YSB | 0:42a334c405de | 103 | latitude = (float)(latitude_str[0]-0x30)*10.0+ \ |
YSB | 0:42a334c405de | 104 | (float)(latitude_str[1]-0x30)+ \ |
YSB | 0:42a334c405de | 105 | ((float)(latitude_str[2]-0x30)*10.0+ \ |
YSB | 0:42a334c405de | 106 | (float)(latitude_str[3]-0x30)+ \ |
YSB | 0:42a334c405de | 107 | (float)(latitude_str[5]-0x30)*0.1+ \ |
YSB | 0:42a334c405de | 108 | (float)(latitude_str[6]-0x30)*0.01+ \ |
YSB | 0:42a334c405de | 109 | (float)(latitude_str[7]-0x30)*0.001+ \ |
YSB | 0:42a334c405de | 110 | (float)(latitude_str[8]-0x30)*0.0001)/60.0; |
YSB | 0:42a334c405de | 111 | longitude = (float)(longitude_str[0]-0x30)*100.0+ \ |
YSB | 0:42a334c405de | 112 | (float)(longitude_str[1]-0x30)*10.0+ \ |
YSB | 0:42a334c405de | 113 | (float)(longitude_str[2]-0x30)+ \ |
YSB | 0:42a334c405de | 114 | ((float)(longitude_str[3]-0x30)*10.0+ \ |
YSB | 0:42a334c405de | 115 | (float)(longitude_str[4]-0x30)+ \ |
YSB | 0:42a334c405de | 116 | (float)(longitude_str[6]-0x30)*0.1+ \ |
YSB | 0:42a334c405de | 117 | (float)(longitude_str[7]-0x30)*0.01+ \ |
YSB | 0:42a334c405de | 118 | (float)(longitude_str[8]-0x30)*0.001+ \ |
YSB | 0:42a334c405de | 119 | (float)(longitude_str[9]-0x30)*0.0001)/60.0; |
YSB | 0:42a334c405de | 120 | } |
YSB | 0:42a334c405de | 121 | |
YSB | 0:42a334c405de | 122 | char* GPS::get_time(){ |
YSB | 0:42a334c405de | 123 | return time_str; |
YSB | 0:42a334c405de | 124 | } |
YSB | 0:42a334c405de | 125 | |
YSB | 0:42a334c405de | 126 | float GPS::get_latitude(){ |
YSB | 0:42a334c405de | 127 | return latitude; |
YSB | 0:42a334c405de | 128 | } |
YSB | 0:42a334c405de | 129 | |
YSB | 0:42a334c405de | 130 | char* GPS::get_str_latitude(){ |
YSB | 0:42a334c405de | 131 | return latitude_str; |
YSB | 0:42a334c405de | 132 | } |
YSB | 0:42a334c405de | 133 | |
YSB | 0:42a334c405de | 134 | float GPS::get_longitude(){ |
YSB | 0:42a334c405de | 135 | return longitude; |
YSB | 0:42a334c405de | 136 | } |
YSB | 0:42a334c405de | 137 | |
YSB | 0:42a334c405de | 138 | char* GPS::get_str_longitude(){ |
YSB | 0:42a334c405de | 139 | return longitude_str; |
YSB | 0:42a334c405de | 140 | } |
YSB | 0:42a334c405de | 141 | |
YSB | 0:42a334c405de | 142 | char GPS::get_states(){ |
YSB | 0:42a334c405de | 143 | return states; |
YSB | 0:42a334c405de | 144 | } |
YSB | 0:42a334c405de | 145 | |
YSB | 0:42a334c405de | 146 | float GPS::get_speed(){ |
YSB | 0:42a334c405de | 147 | return speed; |
YSB | 0:42a334c405de | 148 | } |