GPS test
Dependencies: MBed_Adafruit-GPS-Library mbed
main.cpp@0:02573593bcdc, 2016-06-29 (annotated)
- Committer:
- s1210160
- Date:
- Wed Jun 29 13:04:49 2016 +0000
- Revision:
- 0:02573593bcdc
GPS test;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
s1210160 | 0:02573593bcdc | 1 | #include "mbed.h" |
s1210160 | 0:02573593bcdc | 2 | #include "MBed_Adafruit_GPS.h" |
s1210160 | 0:02573593bcdc | 3 | |
s1210160 | 0:02573593bcdc | 4 | int main() |
s1210160 | 0:02573593bcdc | 5 | { |
s1210160 | 0:02573593bcdc | 6 | |
s1210160 | 0:02573593bcdc | 7 | int flag = 0; |
s1210160 | 0:02573593bcdc | 8 | |
s1210160 | 0:02573593bcdc | 9 | Serial* gps_Serial = new Serial(p28,p27); |
s1210160 | 0:02573593bcdc | 10 | Adafruit_GPS myGPS(gps_Serial); |
s1210160 | 0:02573593bcdc | 11 | myGPS.begin(9600); |
s1210160 | 0:02573593bcdc | 12 | |
s1210160 | 0:02573593bcdc | 13 | const double target_x = 37.524988, target_y = 139.938585; |
s1210160 | 0:02573593bcdc | 14 | double robot_x = 0.0, robot_y = 0.0; |
s1210160 | 0:02573593bcdc | 15 | double sub_x = 0.0, sub_y = 0.0; |
s1210160 | 0:02573593bcdc | 16 | double distance = 0.0; |
s1210160 | 0:02573593bcdc | 17 | Timer GPS_Timer; |
s1210160 | 0:02573593bcdc | 18 | GPS_Timer.start(); |
s1210160 | 0:02573593bcdc | 19 | const int GPS_Time = 1000; |
s1210160 | 0:02573593bcdc | 20 | |
s1210160 | 0:02573593bcdc | 21 | myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); |
s1210160 | 0:02573593bcdc | 22 | myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
s1210160 | 0:02573593bcdc | 23 | myGPS.sendCommand(PGCMD_ANTENNA); |
s1210160 | 0:02573593bcdc | 24 | |
s1210160 | 0:02573593bcdc | 25 | while(true) { |
s1210160 | 0:02573593bcdc | 26 | |
s1210160 | 0:02573593bcdc | 27 | myGPS.read(); |
s1210160 | 0:02573593bcdc | 28 | if ( myGPS.newNMEAreceived() ) { |
s1210160 | 0:02573593bcdc | 29 | if ( !myGPS.parse(myGPS.lastNMEA()) ) { |
s1210160 | 0:02573593bcdc | 30 | continue; |
s1210160 | 0:02573593bcdc | 31 | } |
s1210160 | 0:02573593bcdc | 32 | } |
s1210160 | 0:02573593bcdc | 33 | |
s1210160 | 0:02573593bcdc | 34 | if (GPS_Timer.read_ms() >= GPS_Time) { |
s1210160 | 0:02573593bcdc | 35 | GPS_Timer.reset(); |
s1210160 | 0:02573593bcdc | 36 | if(myGPS.fix) { |
s1210160 | 0:02573593bcdc | 37 | robot_x = (double)myGPS.latitudeH + (double)(myGPS.latitudeL / 10000.0 / 60.0); |
s1210160 | 0:02573593bcdc | 38 | robot_y = (double)myGPS.longitudeH +(double)(myGPS.longitudeL / 10000.0 / 60.0); |
s1210160 | 0:02573593bcdc | 39 | } |
s1210160 | 0:02573593bcdc | 40 | |
s1210160 | 0:02573593bcdc | 41 | sub_x = (abs(target_x) - abs(robot_x)) * 111135.0; |
s1210160 | 0:02573593bcdc | 42 | sub_y = (abs(target_y) - abs(robot_y)) * 91191.0; |
s1210160 | 0:02573593bcdc | 43 | distance = sqrt(sub_x*sub_x - sub_y*sub_y); |
s1210160 | 0:02573593bcdc | 44 | printf("latitude:%f, longitude:%f, distance:%f\n", robot_x, robot_y, distance); |
s1210160 | 0:02573593bcdc | 45 | |
s1210160 | 0:02573593bcdc | 46 | } |
s1210160 | 0:02573593bcdc | 47 | } |
s1210160 | 0:02573593bcdc | 48 | return 0; |
s1210160 | 0:02573593bcdc | 49 | } |