Miscellaneous Library, read Encoder etc.
GPS.cpp@1:c680da75a614, 2019-03-06 (annotated)
- Committer:
- altb
- Date:
- Wed Mar 06 14:19:10 2019 +0000
- Revision:
- 1:c680da75a614
- Parent:
- 0:3312872854c4
dropped Signal
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
altb | 0:3312872854c4 | 1 | #include "GPS.h" |
altb | 0:3312872854c4 | 2 | #include "math.h" |
altb | 0:3312872854c4 | 3 | #include "string" |
altb | 0:3312872854c4 | 4 | #include "mbed.h" |
altb | 0:3312872854c4 | 5 | |
altb | 0:3312872854c4 | 6 | using namespace std; |
altb | 0:3312872854c4 | 7 | |
altb | 0:3312872854c4 | 8 | GPS::GPS(PinName P1,PinName P2,float TS) : logGPS(P1, P2),thread(osPriorityLow, 4096) { |
altb | 0:3312872854c4 | 9 | |
altb | 0:3312872854c4 | 10 | // start thread and timer interrupt |
altb | 0:3312872854c4 | 11 | logGPS.baud(9600); |
altb | 0:3312872854c4 | 12 | logGPS.attach(callback(this, &GPS::Rx_interrupt), Serial::RxIrq); |
altb | 0:3312872854c4 | 13 | thread.start(callback(this, &GPS::get_data)); |
altb | 0:3312872854c4 | 14 | ticker.attach(callback(this, &GPS::sendSignal), TS); |
altb | 0:3312872854c4 | 15 | buffer_filled = false; |
altb | 0:3312872854c4 | 16 | rx_in = 0; |
altb | 0:3312872854c4 | 17 | } |
altb | 0:3312872854c4 | 18 | |
altb | 0:3312872854c4 | 19 | GPS::~GPS() {} |
altb | 0:3312872854c4 | 20 | |
altb | 0:3312872854c4 | 21 | void GPS::get_data(void){ |
altb | 0:3312872854c4 | 22 | float dum; |
altb | 0:3312872854c4 | 23 | string str2; |
altb | 0:3312872854c4 | 24 | while(1) { |
altb | 0:3312872854c4 | 25 | thread.signal_wait(signal); |
altb | 0:3312872854c4 | 26 | //string str="blabla $GNGGA,124720.00,4729.84871,N,00843.83588,E,1,06,2.77,406.1,M,47.4,M,,*4B"; |
altb | 0:3312872854c4 | 27 | str=string(buf); |
altb | 0:3312872854c4 | 28 | size_t found=str.find("GNGGA"); |
altb | 0:3312872854c4 | 29 | if(found!=string::npos){ |
altb | 0:3312872854c4 | 30 | int cou=1; |
altb | 0:3312872854c4 | 31 | str.replace(0,found+6, "\0"); |
altb | 0:3312872854c4 | 32 | do{ |
altb | 0:3312872854c4 | 33 | int en=str.find(","); |
altb | 0:3312872854c4 | 34 | str2=str.substr(0,en); |
altb | 0:3312872854c4 | 35 | switch(cou){ |
altb | 0:3312872854c4 | 36 | case 2:{ |
altb | 0:3312872854c4 | 37 | float north = atof(str2.c_str()); |
altb | 0:3312872854c4 | 38 | dum=floor(north/100.0); |
altb | 0:3312872854c4 | 39 | north=dum+((north/100.0)-dum)/0.6; |
altb | 0:3312872854c4 | 40 | pos0_xyz[0]=north; |
altb | 0:3312872854c4 | 41 | break; |
altb | 0:3312872854c4 | 42 | } |
altb | 0:3312872854c4 | 43 | case 4:{ |
altb | 0:3312872854c4 | 44 | float ew = atof(str2.c_str()); |
altb | 0:3312872854c4 | 45 | dum=floor(ew/100.0); |
altb | 0:3312872854c4 | 46 | ew=dum+((ew/100.0)-dum)/0.6; |
altb | 0:3312872854c4 | 47 | pos0_xyz[1]=ew; |
altb | 0:3312872854c4 | 48 | break; |
altb | 0:3312872854c4 | 49 | } |
altb | 0:3312872854c4 | 50 | case 6:{ |
altb | 0:3312872854c4 | 51 | int isvalid = atof(str2.c_str()); |
altb | 0:3312872854c4 | 52 | break; |
altb | 0:3312872854c4 | 53 | } |
altb | 0:3312872854c4 | 54 | case 7:{ |
altb | 0:3312872854c4 | 55 | int numSat = atof(str2.c_str()); |
altb | 0:3312872854c4 | 56 | break; |
altb | 0:3312872854c4 | 57 | } |
altb | 0:3312872854c4 | 58 | case 9:{ |
altb | 0:3312872854c4 | 59 | float alt = atof(str2.c_str()); |
altb | 0:3312872854c4 | 60 | pos0_xyz[2]=alt; |
altb | 0:3312872854c4 | 61 | break; |
altb | 0:3312872854c4 | 62 | } |
altb | 0:3312872854c4 | 63 | } |
altb | 0:3312872854c4 | 64 | str.replace(0,en+1,"\0"); |
altb | 0:3312872854c4 | 65 | cou++; |
altb | 0:3312872854c4 | 66 | }while(cou<10 && str.length()>2); |
altb | 0:3312872854c4 | 67 | }// if found |
altb | 0:3312872854c4 | 68 | }//while(1) |
altb | 0:3312872854c4 | 69 | } |
altb | 0:3312872854c4 | 70 | void GPS::sendSignal() { |
altb | 0:3312872854c4 | 71 | |
altb | 0:3312872854c4 | 72 | thread.signal_set(signal); |
altb | 0:3312872854c4 | 73 | } |
altb | 0:3312872854c4 | 74 | |
altb | 0:3312872854c4 | 75 | void GPS::get_position(void){ |
altb | 0:3312872854c4 | 76 | pos_xyz[0]=pos0_xyz[0]; |
altb | 0:3312872854c4 | 77 | pos_xyz[1]=pos0_xyz[1]; |
altb | 0:3312872854c4 | 78 | pos_xyz[2]=pos0_xyz[2]; |
altb | 0:3312872854c4 | 79 | } |
altb | 0:3312872854c4 | 80 | |
altb | 0:3312872854c4 | 81 | |
altb | 0:3312872854c4 | 82 | |
altb | 0:3312872854c4 | 83 | void GPS::Rx_interrupt() { |
altb | 0:3312872854c4 | 84 | while ((logGPS.readable()) ){ |
altb | 0:3312872854c4 | 85 | buf[rx_in++] = logGPS.getc(); |
altb | 0:3312872854c4 | 86 | if(rx_in==255) |
altb | 0:3312872854c4 | 87 | buffer_filled = true; |
altb | 0:3312872854c4 | 88 | } |
altb | 0:3312872854c4 | 89 | } |
altb | 0:3312872854c4 | 90 | |
altb | 0:3312872854c4 | 91 | void GPS::return_string(string *st){ |
altb | 0:3312872854c4 | 92 | *st = string(buf); |
altb | 0:3312872854c4 | 93 | return; |
altb | 0:3312872854c4 | 94 | |
altb | 0:3312872854c4 | 95 | } |