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

Dependents:   GPS_test EM_Logger

Committer:
YSB
Date:
Fri Jul 05 04:17:36 2013 +0000
Revision:
2:7870c69fa58c
Parent:
0:42a334c405de
Child:
3:84d63345eb80
Child:
4:7be9581d0734
GPS

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