GPS program used with the STM32 L152RE and any NMEA GPS

Dependencies:   SerialGPS mbed

Fork of Sparkfun_GPS_Shield by Shields

Committer:
VictorMC
Date:
Wed Jan 13 15:32:23 2016 +0000
Revision:
2:5eb57d412bf2
Parent:
1:6bba24a04008
1st version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 1:6bba24a04008 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
screamer 1:6bba24a04008 2 *
screamer 1:6bba24a04008 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
screamer 1:6bba24a04008 4 * and associated documentation files (the "Software"), to deal in the Software without
screamer 1:6bba24a04008 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
screamer 1:6bba24a04008 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
screamer 1:6bba24a04008 7 * Software is furnished to do so, subject to the following conditions:
screamer 1:6bba24a04008 8 *
screamer 1:6bba24a04008 9 * The above copyright notice and this permission notice shall be included in all copies or
screamer 1:6bba24a04008 10 * substantial portions of the Software.
screamer 1:6bba24a04008 11 *
screamer 1:6bba24a04008 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
screamer 1:6bba24a04008 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
screamer 1:6bba24a04008 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
screamer 1:6bba24a04008 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
screamer 1:6bba24a04008 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
screamer 1:6bba24a04008 17 */
screamer 1:6bba24a04008 18
screamer 0:2465168eb818 19 #include "mbed.h"
screamer 0:2465168eb818 20 #include "SerialGPS.h"
VictorMC 2:5eb57d412bf2 21 #include "math.h"
VictorMC 2:5eb57d412bf2 22
VictorMC 2:5eb57d412bf2 23 #ifndef M_PI
VictorMC 2:5eb57d412bf2 24 #define M_PI 3.14159265358979323846
VictorMC 2:5eb57d412bf2 25 #endif
VictorMC 2:5eb57d412bf2 26
VictorMC 2:5eb57d412bf2 27 //Various interruption process
VictorMC 2:5eb57d412bf2 28 Ticker toggler; // periodic interrupt routines
VictorMC 2:5eb57d412bf2 29 //Timer debounce; // define debounce timer
screamer 0:2465168eb818 30
screamer 0:2465168eb818 31 /** On many platforms USBTX/USBRX overlap with serial on D1/D0 pins and enabling the below will interrupt the communication.
screamer 0:2465168eb818 32 * You can use an LCD display to print the values or store them on an SD card etc.
screamer 0:2465168eb818 33 */
VictorMC 2:5eb57d412bf2 34 Serial pc(USBTX, USBRX);
screamer 0:2465168eb818 35
screamer 0:2465168eb818 36 /**
VictorMC 2:5eb57d412bf2 37 * PC_10 - TX pin (RX on the GPS module side)
VictorMC 2:5eb57d412bf2 38 * PC_11 - RX pin (TX on the GPS module side)
VictorMC 2:5eb57d412bf2 39 * 9600 - GPS baud rate
screamer 0:2465168eb818 40 */
VictorMC 2:5eb57d412bf2 41 SerialGPS gps(PC_10, PC_11, 9600);
VictorMC 2:5eb57d412bf2 42
VictorMC 2:5eb57d412bf2 43 void toggle_gps(){
VictorMC 2:5eb57d412bf2 44 int i=0;
VictorMC 2:5eb57d412bf2 45 while(1){
VictorMC 2:5eb57d412bf2 46 //pc.printf("gps.sample?\n\r");
VictorMC 2:5eb57d412bf2 47 if (gps.sample()) {
VictorMC 2:5eb57d412bf2 48 i++;
VictorMC 2:5eb57d412bf2 49 //pc.printf("true\n\r");
VictorMC 2:5eb57d412bf2 50 pc.printf("sats %d, lat %f, lont %f, alt %f, geoid %f, time %f\n\r", gps.sats, gps.latitude, gps.longitude, gps.alt, gps.geoid, gps.time);
VictorMC 2:5eb57d412bf2 51 break;
VictorMC 2:5eb57d412bf2 52 }
VictorMC 2:5eb57d412bf2 53 }
VictorMC 2:5eb57d412bf2 54
VictorMC 2:5eb57d412bf2 55 }
screamer 0:2465168eb818 56
VictorMC 2:5eb57d412bf2 57 float DegToRad(float valDeg){
VictorMC 2:5eb57d412bf2 58 return M_PI*valDeg/180;
VictorMC 2:5eb57d412bf2 59 }
VictorMC 2:5eb57d412bf2 60
VictorMC 2:5eb57d412bf2 61 double distance(float rxLat, float rxLong){
VictorMC 2:5eb57d412bf2 62 double a=0, c=0;
VictorMC 2:5eb57d412bf2 63 int R=6371000;
VictorMC 2:5eb57d412bf2 64
VictorMC 2:5eb57d412bf2 65 double deltaLong=abs(DegToRad(rxLong)-DegToRad(gps.longitude));
VictorMC 2:5eb57d412bf2 66 double deltaLat=abs(DegToRad(rxLat)-DegToRad(gps.latitude));
VictorMC 2:5eb57d412bf2 67
VictorMC 2:5eb57d412bf2 68 a=sin(deltaLat/2)*sin(deltaLat/2)+cos((double)DegToRad(rxLat))*cos((double)DegToRad(gps.latitude))*sin(deltaLong/2)*sin(deltaLong/2);
VictorMC 2:5eb57d412bf2 69 c=2*atan2(sqrt(a), sqrt(1-a));
VictorMC 2:5eb57d412bf2 70
VictorMC 2:5eb57d412bf2 71 return R*c;
VictorMC 2:5eb57d412bf2 72 }
screamer 0:2465168eb818 73
screamer 0:2465168eb818 74 int main() {
VictorMC 2:5eb57d412bf2 75 /*
VictorMC 2:5eb57d412bf2 76 int i=0,k;
VictorMC 2:5eb57d412bf2 77 double distance;
VictorMC 2:5eb57d412bf2 78 float sleep=20.0;
VictorMC 2:5eb57d412bf2 79 toggler.attach(&toggle_gps,sleep);
VictorMC 2:5eb57d412bf2 80 */
VictorMC 2:5eb57d412bf2 81 gps.latitude=43.615659;
VictorMC 2:5eb57d412bf2 82 gps.longitude=7.072609;
VictorMC 2:5eb57d412bf2 83 pc.printf("La distance entre les deux points est de: %f", distance(43.616541,7.070429));
VictorMC 2:5eb57d412bf2 84 while(1);
VictorMC 2:5eb57d412bf2 85
screamer 0:2465168eb818 86 }