Adafruit GPS , distance and count footsteps
Dependencies: mbed SDFileSystem MBed_Adafruit-GPS-Library USBDevice
main.cpp@6:f1b3ee4199b7, 2020-01-23 (annotated)
- Committer:
- the_nabil
- Date:
- Thu Jan 23 18:09:56 2020 +0000
- Revision:
- 6:f1b3ee4199b7
- Parent:
- 5:da8e5af9d44b
- Child:
- 7:3d99469695da
gps
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
reanimationxp | 2:7166ad3f9a2a | 1 | |
reanimationxp | 2:7166ad3f9a2a | 2 | #include "mbed.h" |
reanimationxp | 2:7166ad3f9a2a | 3 | #include "MBed_Adafruit_GPS.h" |
zmoutaou | 4:e87d9fbc4869 | 4 | #include "USBSerial.h" |
the_nabil | 5:da8e5af9d44b | 5 | #include "SDFileSystem.h" |
reanimationxp | 2:7166ad3f9a2a | 6 | |
the_nabil | 5:da8e5af9d44b | 7 | USBSerial pc(0x1f00, 0x2012, 0x0001, false); |
reanimationxp | 2:7166ad3f9a2a | 8 | Serial * gps_Serial; |
the_nabil | 5:da8e5af9d44b | 9 | SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd"); |
zmoutaou | 4:e87d9fbc4869 | 10 | |
the_nabil | 5:da8e5af9d44b | 11 | PwmOut Green(PC_8); //PWM Red LED |
the_nabil | 5:da8e5af9d44b | 12 | PwmOut Red(PC_6); //PWM Green LED |
the_nabil | 5:da8e5af9d44b | 13 | PwmOut Blue(PC_9); //PWM Blue LED |
the_nabil | 6:f1b3ee4199b7 | 14 | int i = 0; |
the_nabil | 6:f1b3ee4199b7 | 15 | float lat_dd, lon_dd; |
the_nabil | 6:f1b3ee4199b7 | 16 | float lat_1, lon_1; |
the_nabil | 6:f1b3ee4199b7 | 17 | float lat_2, lon_2; |
the_nabil | 6:f1b3ee4199b7 | 18 | float height = 171; |
the_nabil | 6:f1b3ee4199b7 | 19 | float distance = 0; |
the_nabil | 6:f1b3ee4199b7 | 20 | //float distance1 = 0; |
the_nabil | 6:f1b3ee4199b7 | 21 | //float distance2 = 0; |
the_nabil | 6:f1b3ee4199b7 | 22 | float sum_distance = 0; |
the_nabil | 6:f1b3ee4199b7 | 23 | float steps = 0; |
the_nabil | 6:f1b3ee4199b7 | 24 | float avg_speed = 0; |
the_nabil | 6:f1b3ee4199b7 | 25 | float cal = 0; |
reanimationxp | 2:7166ad3f9a2a | 26 | |
reanimationxp | 2:7166ad3f9a2a | 27 | int main() { |
reanimationxp | 2:7166ad3f9a2a | 28 | |
the_nabil | 6:f1b3ee4199b7 | 29 | Blue = 0; |
the_nabil | 6:f1b3ee4199b7 | 30 | |
zmoutaou | 4:e87d9fbc4869 | 31 | //pc.baud(115200); //sets virtual COM serial communication to high rate; this is to allow more time to be spent on GPS retrieval |
reanimationxp | 2:7166ad3f9a2a | 32 | |
zmoutaou | 4:e87d9fbc4869 | 33 | gps_Serial = new Serial(PA_2,PA_3); //serial object for use w/ GPS |
reanimationxp | 2:7166ad3f9a2a | 34 | Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class |
reanimationxp | 2:7166ad3f9a2a | 35 | char c; //when read via Adafruit_GPS::read(), the class returns single character stored here |
reanimationxp | 2:7166ad3f9a2a | 36 | Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? |
reanimationxp | 2:7166ad3f9a2a | 37 | const int refresh_Time = 2000; //refresh time in ms |
the_nabil | 6:f1b3ee4199b7 | 38 | |
reanimationxp | 2:7166ad3f9a2a | 39 | |
reanimationxp | 2:7166ad3f9a2a | 40 | myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *) |
reanimationxp | 2:7166ad3f9a2a | 41 | //a list of GPS commands is available at http://www.adafruit.com/datasheets/PMTK_A08.pdf |
reanimationxp | 2:7166ad3f9a2a | 42 | |
reanimationxp | 2:7166ad3f9a2a | 43 | myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation |
reanimationxp | 2:7166ad3f9a2a | 44 | myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
reanimationxp | 2:7166ad3f9a2a | 45 | myGPS.sendCommand(PGCMD_ANTENNA); |
reanimationxp | 2:7166ad3f9a2a | 46 | |
the_nabil | 5:da8e5af9d44b | 47 | FILE *fp = fopen("/sd/GPS_Test.txt", "w"); |
the_nabil | 5:da8e5af9d44b | 48 | |
the_nabil | 5:da8e5af9d44b | 49 | pc.printf("Connection established at 115200 baud...\n"); |
reanimationxp | 2:7166ad3f9a2a | 50 | |
reanimationxp | 2:7166ad3f9a2a | 51 | wait(1); |
reanimationxp | 2:7166ad3f9a2a | 52 | |
reanimationxp | 2:7166ad3f9a2a | 53 | refresh_Timer.start(); //starts the clock on the timer |
reanimationxp | 2:7166ad3f9a2a | 54 | |
the_nabil | 5:da8e5af9d44b | 55 | while(true) |
the_nabil | 5:da8e5af9d44b | 56 | { |
reanimationxp | 2:7166ad3f9a2a | 57 | c = myGPS.read(); //queries the GPS |
reanimationxp | 2:7166ad3f9a2a | 58 | |
the_nabil | 6:f1b3ee4199b7 | 59 | Blue = 0; |
the_nabil | 6:f1b3ee4199b7 | 60 | |
zmoutaou | 4:e87d9fbc4869 | 61 | if (c) { } //this line will echo the GPS data if not paused |
reanimationxp | 2:7166ad3f9a2a | 62 | |
reanimationxp | 2:7166ad3f9a2a | 63 | //check if we recieved a new message from GPS, if so, attempt to parse it, |
reanimationxp | 2:7166ad3f9a2a | 64 | if ( myGPS.newNMEAreceived() ) { |
reanimationxp | 2:7166ad3f9a2a | 65 | if ( !myGPS.parse(myGPS.lastNMEA()) ) { |
reanimationxp | 2:7166ad3f9a2a | 66 | continue; |
reanimationxp | 2:7166ad3f9a2a | 67 | } |
reanimationxp | 2:7166ad3f9a2a | 68 | } |
reanimationxp | 2:7166ad3f9a2a | 69 | |
reanimationxp | 2:7166ad3f9a2a | 70 | //check if enough time has passed to warrant printing GPS info to screen |
reanimationxp | 2:7166ad3f9a2a | 71 | //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing |
reanimationxp | 2:7166ad3f9a2a | 72 | if (refresh_Timer.read_ms() >= refresh_Time) { |
reanimationxp | 2:7166ad3f9a2a | 73 | refresh_Timer.reset(); |
the_nabil | 5:da8e5af9d44b | 74 | fprintf(fp,"Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds); |
the_nabil | 5:da8e5af9d44b | 75 | fprintf(fp,"Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year); |
the_nabil | 5:da8e5af9d44b | 76 | fprintf(fp,"Fix: %d\n", (int) myGPS.fix); |
the_nabil | 5:da8e5af9d44b | 77 | fprintf(fp,"Quality: %d\n\n", (int) myGPS.fixquality); |
the_nabil | 5:da8e5af9d44b | 78 | |
reanimationxp | 2:7166ad3f9a2a | 79 | pc.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds); |
reanimationxp | 2:7166ad3f9a2a | 80 | pc.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year); |
reanimationxp | 2:7166ad3f9a2a | 81 | pc.printf("Fix: %d\n", (int) myGPS.fix); |
the_nabil | 5:da8e5af9d44b | 82 | pc.printf("Quality: %d\n\n", (int) myGPS.fixquality); |
the_nabil | 5:da8e5af9d44b | 83 | Blue = 0; |
the_nabil | 6:f1b3ee4199b7 | 84 | if (myGPS.fix) |
the_nabil | 6:f1b3ee4199b7 | 85 | { |
the_nabil | 6:f1b3ee4199b7 | 86 | /* |
the_nabil | 6:f1b3ee4199b7 | 87 | if (i == 0) |
the_nabil | 6:f1b3ee4199b7 | 88 | { |
the_nabil | 6:f1b3ee4199b7 | 89 | lat_1 = myGPS.latitude; |
the_nabil | 6:f1b3ee4199b7 | 90 | lon_1 = myGPS.longitude; |
the_nabil | 6:f1b3ee4199b7 | 91 | //lat_2 = myGPS.latitude; |
the_nabil | 6:f1b3ee4199b7 | 92 | //lon_2 = myGPS.longitude; |
the_nabil | 6:f1b3ee4199b7 | 93 | } |
the_nabil | 6:f1b3ee4199b7 | 94 | */ |
the_nabil | 6:f1b3ee4199b7 | 95 | |
the_nabil | 6:f1b3ee4199b7 | 96 | //lat_1 = lat_1; |
the_nabil | 6:f1b3ee4199b7 | 97 | //lon_1 = lon_1; |
the_nabil | 6:f1b3ee4199b7 | 98 | lat_dd = myGPS.coordToDegDec(myGPS.latitude); |
the_nabil | 6:f1b3ee4199b7 | 99 | lon_dd = myGPS.coordToDegDec(myGPS.longitude); |
the_nabil | 6:f1b3ee4199b7 | 100 | |
the_nabil | 6:f1b3ee4199b7 | 101 | //lat_2 = lat_dd; |
the_nabil | 6:f1b3ee4199b7 | 102 | //lon_2 = lon_dd; |
the_nabil | 6:f1b3ee4199b7 | 103 | |
the_nabil | 6:f1b3ee4199b7 | 104 | //::distance2 = myGPS.getDistance(lat_1, lon_1, lat_2, lon_2); |
the_nabil | 6:f1b3ee4199b7 | 105 | |
the_nabil | 6:f1b3ee4199b7 | 106 | if (i == 0) { |
the_nabil | 6:f1b3ee4199b7 | 107 | //lat_1 = lat_2; |
the_nabil | 6:f1b3ee4199b7 | 108 | //lon_1 = lon_2; |
the_nabil | 6:f1b3ee4199b7 | 109 | lat_1 = lat_dd; |
the_nabil | 6:f1b3ee4199b7 | 110 | lon_1 = lon_dd; |
the_nabil | 6:f1b3ee4199b7 | 111 | lat_2 = lat_1; |
the_nabil | 6:f1b3ee4199b7 | 112 | lon_2 = lon_1; |
the_nabil | 6:f1b3ee4199b7 | 113 | |
the_nabil | 6:f1b3ee4199b7 | 114 | } else { |
the_nabil | 6:f1b3ee4199b7 | 115 | lat_1 = lat_2; |
the_nabil | 6:f1b3ee4199b7 | 116 | lon_1 = lon_2; |
the_nabil | 6:f1b3ee4199b7 | 117 | lat_2 = lat_dd; |
the_nabil | 6:f1b3ee4199b7 | 118 | lon_2 = lon_dd; |
the_nabil | 6:f1b3ee4199b7 | 119 | } |
the_nabil | 6:f1b3ee4199b7 | 120 | |
the_nabil | 6:f1b3ee4199b7 | 121 | ::distance = myGPS.getDistance(lat_1, lon_1, lat_2, lon_2); |
the_nabil | 6:f1b3ee4199b7 | 122 | ::sum_distance = ::sum_distance + ::distance; |
the_nabil | 6:f1b3ee4199b7 | 123 | ::steps = myGPS.getSteps(::sum_distance, height); |
the_nabil | 6:f1b3ee4199b7 | 124 | |
the_nabil | 6:f1b3ee4199b7 | 125 | if (i != 0) { |
the_nabil | 6:f1b3ee4199b7 | 126 | ::avg_speed = myGPS.getAvgSpeed(::sum_distance, i); |
the_nabil | 6:f1b3ee4199b7 | 127 | } |
the_nabil | 6:f1b3ee4199b7 | 128 | |
the_nabil | 6:f1b3ee4199b7 | 129 | |
the_nabil | 6:f1b3ee4199b7 | 130 | fprintf(fp,"t: %d\n", i); |
the_nabil | 6:f1b3ee4199b7 | 131 | fprintf(fp,"Location (°) : %5.5f%c, %5.5f%c\n", lat_dd, myGPS.lat, lon_dd, myGPS.lon); |
the_nabil | 6:f1b3ee4199b7 | 132 | fprintf(fp,"Location1 (°) : %5.5f, %5.5f\n", lat_1, lon_1); |
the_nabil | 6:f1b3ee4199b7 | 133 | fprintf(fp,"Location2 (°) : %5.5f, %5.5f\n", lat_2, lon_2); |
the_nabil | 6:f1b3ee4199b7 | 134 | //fprintf(fp,"Speed: %5.2f knots\n", myGPS.speed); |
the_nabil | 6:f1b3ee4199b7 | 135 | //fprintf(fp,"Angle: %5.2f\n", myGPS.angle); |
the_nabil | 6:f1b3ee4199b7 | 136 | fprintf(fp,"Altitude (m) : %5.2f\n", myGPS.altitude); |
the_nabil | 6:f1b3ee4199b7 | 137 | fprintf(fp,"Satellites : %d\n", myGPS.satellites); |
the_nabil | 6:f1b3ee4199b7 | 138 | fprintf(fp,"Distance (m) : %5.2f\n", ::sum_distance); |
the_nabil | 6:f1b3ee4199b7 | 139 | fprintf(fp,"Steps taken : %5.2f\n", ::steps); |
the_nabil | 6:f1b3ee4199b7 | 140 | fprintf(fp,"Average speed (km/h) : %5.2f\n\n\n", ::avg_speed); |
the_nabil | 5:da8e5af9d44b | 141 | |
the_nabil | 6:f1b3ee4199b7 | 142 | pc.printf("t: %d\n", i); |
the_nabil | 6:f1b3ee4199b7 | 143 | pc.printf("Location (°) : %5.5f%c, %5.5f%c\n", lat_dd, myGPS.lat, lon_dd, myGPS.lon); |
the_nabil | 6:f1b3ee4199b7 | 144 | //pc.printf("Location (°) : %5.5f%c, %5.5f%c\n", lat_dd, myGPS.lat, lon_dd, myGPS.lon); |
the_nabil | 6:f1b3ee4199b7 | 145 | pc.printf("Location1 (°) : %5.5f, %5.5f\n", lat_1, lon_1); |
the_nabil | 6:f1b3ee4199b7 | 146 | pc.printf("Location2 (°) : %5.5f, %5.5f\n", lat_2, lon_2); |
the_nabil | 6:f1b3ee4199b7 | 147 | //pc.printf("Speed: %5.2f knots\n", myGPS.speed); |
the_nabil | 6:f1b3ee4199b7 | 148 | //pc.printf("Angle: %5.2f\n", myGPS.angle); |
the_nabil | 6:f1b3ee4199b7 | 149 | pc.printf("Altitude (m) : %5.2f\n", myGPS.altitude); |
the_nabil | 6:f1b3ee4199b7 | 150 | pc.printf("Satellites : %d\n", myGPS.satellites); |
the_nabil | 6:f1b3ee4199b7 | 151 | pc.printf("Distance (m) : %5.2f\n", ::sum_distance); |
the_nabil | 6:f1b3ee4199b7 | 152 | pc.printf("Steps taken : %5.2f\n", ::steps); |
the_nabil | 6:f1b3ee4199b7 | 153 | pc.printf("Average speed (km/h) : %5.10f\n\n\n", ::avg_speed); |
the_nabil | 6:f1b3ee4199b7 | 154 | |
the_nabil | 6:f1b3ee4199b7 | 155 | |
the_nabil | 5:da8e5af9d44b | 156 | |
the_nabil | 5:da8e5af9d44b | 157 | Blue = 1; |
the_nabil | 6:f1b3ee4199b7 | 158 | i++; |
the_nabil | 5:da8e5af9d44b | 159 | |
reanimationxp | 2:7166ad3f9a2a | 160 | } |
the_nabil | 5:da8e5af9d44b | 161 | wait(0.5); |
reanimationxp | 2:7166ad3f9a2a | 162 | } |
reanimationxp | 2:7166ad3f9a2a | 163 | } |
the_nabil | 5:da8e5af9d44b | 164 | fclose(fp); // Closing the file |
the_nabil | 5:da8e5af9d44b | 165 | if(fp == NULL) { error("Could not open file for write\n"); } // Error message, if there is a problem |
reanimationxp | 2:7166ad3f9a2a | 166 | } |
reanimationxp | 2:7166ad3f9a2a | 167 | |
reanimationxp | 2:7166ad3f9a2a | 168 | |
reanimationxp | 2:7166ad3f9a2a | 169 | /* |
reanimationxp | 2:7166ad3f9a2a | 170 | |
reanimationxp | 1:b100ab44119d | 171 | //gps.cpp |
reanimationxp | 1:b100ab44119d | 172 | //for use with Adafruit Ultimate GPS |
reanimationxp | 1:b100ab44119d | 173 | //Reads in and parses GPS data |
reanimationxp | 1:b100ab44119d | 174 | |
reanimationxp | 1:b100ab44119d | 175 | #include "mbed.h" |
reanimationxp | 1:b100ab44119d | 176 | #include "MBed_Adafruit_GPS.h" |
reanimationxp | 1:b100ab44119d | 177 | |
reanimationxp | 1:b100ab44119d | 178 | Serial * gps_Serial; |
reanimationxp | 1:b100ab44119d | 179 | Serial pc (USBTX, USBRX); |
reanimationxp | 1:b100ab44119d | 180 | |
reanimationxp | 1:b100ab44119d | 181 | int main() { |
reanimationxp | 1:b100ab44119d | 182 | |
reanimationxp | 1:b100ab44119d | 183 | pc.baud(115200); //sets virtual COM serial communication to high rate; this is to allow more time to be spent on GPS retrieval |
reanimationxp | 1:b100ab44119d | 184 | |
reanimationxp | 1:b100ab44119d | 185 | gps_Serial = new Serial(p28,p27); //serial object for use w/ GPS |
reanimationxp | 1:b100ab44119d | 186 | Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class |
reanimationxp | 1:b100ab44119d | 187 | char c; //when read via Adafruit_GPS::read(), the class returns single character stored here |
reanimationxp | 1:b100ab44119d | 188 | Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? |
reanimationxp | 1:b100ab44119d | 189 | const int refresh_Time = 2000; //refresh time in ms |
reanimationxp | 1:b100ab44119d | 190 | |
reanimationxp | 1:b100ab44119d | 191 | myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *) |
reanimationxp | 1:b100ab44119d | 192 | //a list of GPS commands is available at http://www.adafruit.com/datasheets/PMTK_A08.pdf |
reanimationxp | 1:b100ab44119d | 193 | |
reanimationxp | 1:b100ab44119d | 194 | myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation |
reanimationxp | 1:b100ab44119d | 195 | myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
reanimationxp | 1:b100ab44119d | 196 | myGPS.sendCommand(PGCMD_ANTENNA); |
reanimationxp | 1:b100ab44119d | 197 | |
reanimationxp | 1:b100ab44119d | 198 | pc.printf("Connection established at 115200 baud...\n"); |
reanimationxp | 1:b100ab44119d | 199 | |
reanimationxp | 1:b100ab44119d | 200 | wait(1); |
reanimationxp | 1:b100ab44119d | 201 | |
reanimationxp | 1:b100ab44119d | 202 | refresh_Timer.start(); //starts the clock on the timer |
reanimationxp | 1:b100ab44119d | 203 | |
reanimationxp | 1:b100ab44119d | 204 | while(true){ |
reanimationxp | 1:b100ab44119d | 205 | c = myGPS.read(); //queries the GPS |
reanimationxp | 1:b100ab44119d | 206 | |
reanimationxp | 1:b100ab44119d | 207 | if (c) { pc.printf("%c", c); } //this line will echo the GPS data if not paused |
reanimationxp | 1:b100ab44119d | 208 | |
reanimationxp | 1:b100ab44119d | 209 | //check if we recieved a new message from GPS, if so, attempt to parse it, |
reanimationxp | 1:b100ab44119d | 210 | if ( myGPS.newNMEAreceived() ) { |
reanimationxp | 1:b100ab44119d | 211 | if ( !myGPS.parse(myGPS.lastNMEA()) ) { |
reanimationxp | 1:b100ab44119d | 212 | continue; |
reanimationxp | 1:b100ab44119d | 213 | } |
reanimationxp | 1:b100ab44119d | 214 | } |
reanimationxp | 1:b100ab44119d | 215 | |
reanimationxp | 1:b100ab44119d | 216 | //check if enough time has passed to warrant printing GPS info to screen |
reanimationxp | 1:b100ab44119d | 217 | //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing |
reanimationxp | 1:b100ab44119d | 218 | if (refresh_Timer.read_ms() >= refresh_Time) { |
reanimationxp | 1:b100ab44119d | 219 | refresh_Timer.reset(); |
reanimationxp | 1:b100ab44119d | 220 | pc.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds); |
reanimationxp | 1:b100ab44119d | 221 | pc.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year); |
reanimationxp | 1:b100ab44119d | 222 | pc.printf("Fix: %d\n", (int) myGPS.fix); |
reanimationxp | 1:b100ab44119d | 223 | pc.printf("Quality: %d\n", (int) myGPS.fixquality); |
reanimationxp | 1:b100ab44119d | 224 | if (myGPS.fix) { |
reanimationxp | 1:b100ab44119d | 225 | pc.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); |
reanimationxp | 1:b100ab44119d | 226 | pc.printf("Speed: %5.2f knots\n", myGPS.speed); |
reanimationxp | 1:b100ab44119d | 227 | pc.printf("Angle: %5.2f\n", myGPS.angle); |
reanimationxp | 1:b100ab44119d | 228 | pc.printf("Altitude: %5.2f\n", myGPS.altitude); |
reanimationxp | 1:b100ab44119d | 229 | pc.printf("Satellites: %d\n", myGPS.satellites); |
reanimationxp | 1:b100ab44119d | 230 | } |
reanimationxp | 1:b100ab44119d | 231 | } |
reanimationxp | 1:b100ab44119d | 232 | } |
reanimationxp | 1:b100ab44119d | 233 | } |
reanimationxp | 3:e38a115af1dd | 234 | */ |
reanimationxp | 2:7166ad3f9a2a | 235 | |
reanimationxp | 1:b100ab44119d | 236 | |
reanimationxp | 1:b100ab44119d | 237 | /* |
reanimationxp | 1:b100ab44119d | 238 | |
jhey | 0:604848fcb49c | 239 | //gps.cpp |
jhey | 0:604848fcb49c | 240 | //for use with Adafruit Ultimate GPS |
jhey | 0:604848fcb49c | 241 | //Reads in and parses GPS data |
jhey | 0:604848fcb49c | 242 | |
jhey | 0:604848fcb49c | 243 | #include "mbed.h" |
jhey | 0:604848fcb49c | 244 | #include "MBed_Adafruit_GPS.h" |
jhey | 0:604848fcb49c | 245 | |
jhey | 0:604848fcb49c | 246 | Serial * gps_Serial; |
jhey | 0:604848fcb49c | 247 | Serial pct (USBTX, USBRX); |
jhey | 0:604848fcb49c | 248 | |
jhey | 0:604848fcb49c | 249 | int main() { |
jhey | 0:604848fcb49c | 250 | |
jhey | 0:604848fcb49c | 251 | pct.baud(115200); //sets virtual COM serial communication to high rate; this is to allow more time to be spent on GPS retrieval |
jhey | 0:604848fcb49c | 252 | |
jhey | 0:604848fcb49c | 253 | gps_Serial = new Serial(PTC17,PTC16); //serial object for use w/ GPS |
jhey | 0:604848fcb49c | 254 | Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class |
jhey | 0:604848fcb49c | 255 | char c; //when read via Adafruit_GPS::read(), the class returns single character stored here |
jhey | 0:604848fcb49c | 256 | Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? |
jhey | 0:604848fcb49c | 257 | const int refresh_Time = 2000; //refresh time in ms |
jhey | 0:604848fcb49c | 258 | |
jhey | 0:604848fcb49c | 259 | myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *) |
jhey | 0:604848fcb49c | 260 | //a list of GPS commands is available at http://www.adafruit.com/datasheets/PMTK_A08.pdf |
jhey | 0:604848fcb49c | 261 | |
jhey | 0:604848fcb49c | 262 | myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation |
jhey | 0:604848fcb49c | 263 | myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
jhey | 0:604848fcb49c | 264 | myGPS.sendCommand(PGCMD_ANTENNA); |
jhey | 0:604848fcb49c | 265 | |
jhey | 0:604848fcb49c | 266 | pct.printf("Connection established at 115200 baud...\n"); |
jhey | 0:604848fcb49c | 267 | |
jhey | 0:604848fcb49c | 268 | wait(1); |
jhey | 0:604848fcb49c | 269 | |
jhey | 0:604848fcb49c | 270 | refresh_Timer.start(); //starts the clock on the timer |
jhey | 0:604848fcb49c | 271 | |
jhey | 0:604848fcb49c | 272 | while(true){ |
jhey | 0:604848fcb49c | 273 | c = myGPS.read(); //queries the GPS |
jhey | 0:604848fcb49c | 274 | |
jhey | 0:604848fcb49c | 275 | if (c) { pct.printf("%c", c); } //this line will echo the GPS data if not paused |
jhey | 0:604848fcb49c | 276 | |
jhey | 0:604848fcb49c | 277 | //check if we recieved a new message from GPS, if so, attempt to parse it, |
jhey | 0:604848fcb49c | 278 | if ( myGPS.newNMEAreceived() ) { |
jhey | 0:604848fcb49c | 279 | if ( !myGPS.parse(myGPS.lastNMEA()) ) { |
jhey | 0:604848fcb49c | 280 | continue; |
jhey | 0:604848fcb49c | 281 | } |
jhey | 0:604848fcb49c | 282 | } |
jhey | 0:604848fcb49c | 283 | |
jhey | 0:604848fcb49c | 284 | //check if enough time has passed to warrant printing GPS info to screen |
jhey | 0:604848fcb49c | 285 | //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing |
jhey | 0:604848fcb49c | 286 | if (refresh_Timer.read_ms() >= refresh_Time) { |
jhey | 0:604848fcb49c | 287 | refresh_Timer.reset(); |
jhey | 0:604848fcb49c | 288 | pct.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds); |
jhey | 0:604848fcb49c | 289 | pct.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year); |
jhey | 0:604848fcb49c | 290 | pct.printf("Fix: %d\n", (int) myGPS.fix); |
jhey | 0:604848fcb49c | 291 | pct.printf("Quality: %d\n", (int) myGPS.fixquality); |
jhey | 0:604848fcb49c | 292 | if (myGPS.fix) { |
jhey | 0:604848fcb49c | 293 | pct.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); |
jhey | 0:604848fcb49c | 294 | pct.printf("Speed: %5.2f knots\n", myGPS.speed); |
jhey | 0:604848fcb49c | 295 | pct.printf("Angle: %5.2f\n", myGPS.angle); |
jhey | 0:604848fcb49c | 296 | pct.printf("Altitude: %5.2f\n", myGPS.altitude); |
jhey | 0:604848fcb49c | 297 | pct.printf("Satellites: %d\n", myGPS.satellites); |
jhey | 0:604848fcb49c | 298 | } |
jhey | 0:604848fcb49c | 299 | |
jhey | 0:604848fcb49c | 300 | } |
jhey | 0:604848fcb49c | 301 | |
jhey | 0:604848fcb49c | 302 | } |
jhey | 0:604848fcb49c | 303 | |
jhey | 0:604848fcb49c | 304 | |
reanimationxp | 1:b100ab44119d | 305 | } |
reanimationxp | 1:b100ab44119d | 306 | |
reanimationxp | 1:b100ab44119d | 307 | */ |