Adafruit GPS , distance and count footsteps

Dependencies:   mbed SDFileSystem MBed_Adafruit-GPS-Library USBDevice

Committer:
zmoutaou
Date:
Mon Jan 27 10:52:53 2020 +0000
Revision:
8:eee9069df8bb
Parent:
7:3d99469695da
Adafruit GPS

Who changed what in which revision?

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