Generation 2 of the Harp project

Dependencies:   Servo TMP36 GZ chan_fatfs_sd buffered-serial1 nmea_parser watchdog mbed-rtos mbed

Committer:
tylerjw
Date:
Wed Feb 22 03:52:43 2012 +0000
Revision:
0:ce5f06c3895f
Child:
1:2ace7946a246
Version 0.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerjw 0:ce5f06c3895f 1 #include "GPS.h"
tylerjw 0:ce5f06c3895f 2
tylerjw 0:ce5f06c3895f 3 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
tylerjw 0:ce5f06c3895f 4 _gps.baud(4800);
tylerjw 0:ce5f06c3895f 5 nmea_longitude = 0.0;
tylerjw 0:ce5f06c3895f 6 nmea_latitude = 0.0;
tylerjw 0:ce5f06c3895f 7 utc_time = 0;
tylerjw 0:ce5f06c3895f 8 ns = ' ';
tylerjw 0:ce5f06c3895f 9 ew = ' ';
tylerjw 0:ce5f06c3895f 10 lock = 0;
tylerjw 0:ce5f06c3895f 11 satelites = 0;
tylerjw 0:ce5f06c3895f 12 hdop = 0.0;
tylerjw 0:ce5f06c3895f 13 msl_altitude = 0.0;
tylerjw 0:ce5f06c3895f 14 msl_units = ' ';
tylerjw 0:ce5f06c3895f 15
tylerjw 0:ce5f06c3895f 16 rmc_status = ' ';
tylerjw 0:ce5f06c3895f 17 ground_speed_k = 0.0;
tylerjw 0:ce5f06c3895f 18 ground_course_d = 0.0;
tylerjw 0:ce5f06c3895f 19 date = 0;
tylerjw 0:ce5f06c3895f 20
tylerjw 0:ce5f06c3895f 21 dec_longitude = 0.0;
tylerjw 0:ce5f06c3895f 22 dec_latitude = 0.0;
tylerjw 0:ce5f06c3895f 23 }
tylerjw 0:ce5f06c3895f 24
tylerjw 0:ce5f06c3895f 25 float GPS::nmea_to_dec(float deg_coord, char nsew) {
tylerjw 0:ce5f06c3895f 26 int degree = (int)(deg_coord/100);
tylerjw 0:ce5f06c3895f 27 float minutes = deg_coord - degree*100;
tylerjw 0:ce5f06c3895f 28 float dec_deg = minutes / 60;
tylerjw 0:ce5f06c3895f 29 float decimal = degree + dec_deg;
tylerjw 0:ce5f06c3895f 30 if(nsew == 'S' || nsew == 'W') // return negative
tylerjw 0:ce5f06c3895f 31 {
tylerjw 0:ce5f06c3895f 32 decimal *= -1;
tylerjw 0:ce5f06c3895f 33 }
tylerjw 0:ce5f06c3895f 34 return decimal;
tylerjw 0:ce5f06c3895f 35 }
tylerjw 0:ce5f06c3895f 36
tylerjw 0:ce5f06c3895f 37 int GPS::sample() {
tylerjw 0:ce5f06c3895f 38
tylerjw 0:ce5f06c3895f 39 while(1) {
tylerjw 0:ce5f06c3895f 40 getline();
tylerjw 0:ce5f06c3895f 41
tylerjw 0:ce5f06c3895f 42 // Check if it is a GPGGA msg (matches both locked and non-locked msg)
tylerjw 0:ce5f06c3895f 43 if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c", &utc_time, &nmea_latitude, &ns, &nmea_longitude, &ew, &lock, &satelites, &hdop, &msl_altitude, &msl_units) >= 1) {
tylerjw 0:ce5f06c3895f 44 if(!lock) {
tylerjw 0:ce5f06c3895f 45 return 0;
tylerjw 0:ce5f06c3895f 46 } else {
tylerjw 0:ce5f06c3895f 47 dec_latitude = nmea_to_dec(nmea_latitude, ns);
tylerjw 0:ce5f06c3895f 48 dec_longitude = nmea_to_dec(nmea_longitude, ew);
tylerjw 0:ce5f06c3895f 49 return 1;
tylerjw 0:ce5f06c3895f 50 }
tylerjw 0:ce5f06c3895f 51 }
tylerjw 0:ce5f06c3895f 52 // Check if it is a GPRMC msg)
tylerjw 0:ce5f06c3895f 53 else if(sscanf(msg, "GPRMC,%f,%c,%f,%c,%f,%c,%f,%f,%d", &utc_time, &rmc_status, &nmea_latitude, &ns, &nmea_longitude, &ew, &ground_speed_k, &ground_course_d, &date) >= 1) {
tylerjw 0:ce5f06c3895f 54 if(!lock) {
tylerjw 0:ce5f06c3895f 55 return 0;
tylerjw 0:ce5f06c3895f 56 } else {
tylerjw 0:ce5f06c3895f 57 dec_latitude = nmea_to_dec(nmea_latitude, ns);
tylerjw 0:ce5f06c3895f 58 dec_longitude = nmea_to_dec(nmea_longitude, ew);
tylerjw 0:ce5f06c3895f 59 return 1;
tylerjw 0:ce5f06c3895f 60 }
tylerjw 0:ce5f06c3895f 61 }
tylerjw 0:ce5f06c3895f 62 }
tylerjw 0:ce5f06c3895f 63 }
tylerjw 0:ce5f06c3895f 64
tylerjw 0:ce5f06c3895f 65 float GPS::trunc(float v) {
tylerjw 0:ce5f06c3895f 66 if(v < 0.0) {
tylerjw 0:ce5f06c3895f 67 v*= -1.0;
tylerjw 0:ce5f06c3895f 68 v = floor(v);
tylerjw 0:ce5f06c3895f 69 v*=-1.0;
tylerjw 0:ce5f06c3895f 70 } else {
tylerjw 0:ce5f06c3895f 71 v = floor(v);
tylerjw 0:ce5f06c3895f 72 }
tylerjw 0:ce5f06c3895f 73 return v;
tylerjw 0:ce5f06c3895f 74 }
tylerjw 0:ce5f06c3895f 75
tylerjw 0:ce5f06c3895f 76 void GPS::getline() {
tylerjw 0:ce5f06c3895f 77 while(_gps.getc() != '$'); // wait for the start of a line
tylerjw 0:ce5f06c3895f 78 for(int i=0; i<256; i++) {
tylerjw 0:ce5f06c3895f 79 msg[i] = _gps.getc();
tylerjw 0:ce5f06c3895f 80 if(msg[i] == '\r') {
tylerjw 0:ce5f06c3895f 81 msg[i] = 0;
tylerjw 0:ce5f06c3895f 82 return;
tylerjw 0:ce5f06c3895f 83 }
tylerjw 0:ce5f06c3895f 84 }
tylerjw 0:ce5f06c3895f 85 error("Overflowed message limit");
tylerjw 0:ce5f06c3895f 86 }
tylerjw 0:ce5f06c3895f 87
tylerjw 0:ce5f06c3895f 88 float GPS::get_msl_altitude() {
tylerjw 0:ce5f06c3895f 89 if(!lock)
tylerjw 0:ce5f06c3895f 90 return 0.0;
tylerjw 0:ce5f06c3895f 91 else
tylerjw 0:ce5f06c3895f 92 return msl_altitude;
tylerjw 0:ce5f06c3895f 93 }
tylerjw 0:ce5f06c3895f 94
tylerjw 0:ce5f06c3895f 95 int GPS::get_satelites() {
tylerjw 0:ce5f06c3895f 96 if(!lock)
tylerjw 0:ce5f06c3895f 97 return 0;
tylerjw 0:ce5f06c3895f 98 else
tylerjw 0:ce5f06c3895f 99 return satelites;
tylerjw 0:ce5f06c3895f 100 }
tylerjw 0:ce5f06c3895f 101
tylerjw 0:ce5f06c3895f 102 float GPS::get_nmea_longitude() {
tylerjw 0:ce5f06c3895f 103 if(!lock)
tylerjw 0:ce5f06c3895f 104 return 0.0;
tylerjw 0:ce5f06c3895f 105 else
tylerjw 0:ce5f06c3895f 106 return nmea_longitude;
tylerjw 0:ce5f06c3895f 107 }
tylerjw 0:ce5f06c3895f 108
tylerjw 0:ce5f06c3895f 109 float GPS::get_dec_longitude() {
tylerjw 0:ce5f06c3895f 110 if(!lock)
tylerjw 0:ce5f06c3895f 111 return 0.0;
tylerjw 0:ce5f06c3895f 112 else
tylerjw 0:ce5f06c3895f 113 return dec_longitude;
tylerjw 0:ce5f06c3895f 114 }
tylerjw 0:ce5f06c3895f 115
tylerjw 0:ce5f06c3895f 116 float GPS::get_nmea_latitude() {
tylerjw 0:ce5f06c3895f 117 if(!lock)
tylerjw 0:ce5f06c3895f 118 return 0.0;
tylerjw 0:ce5f06c3895f 119 else
tylerjw 0:ce5f06c3895f 120 return nmea_latitude;
tylerjw 0:ce5f06c3895f 121 }
tylerjw 0:ce5f06c3895f 122
tylerjw 0:ce5f06c3895f 123 float GPS::get_dec_latitude() {
tylerjw 0:ce5f06c3895f 124 if(!lock)
tylerjw 0:ce5f06c3895f 125 return 0.0;
tylerjw 0:ce5f06c3895f 126 else
tylerjw 0:ce5f06c3895f 127 return nmea_latitude;
tylerjw 0:ce5f06c3895f 128 }