fahim alavi
/
example-gnss
Example with new gnss libraries
Fork of example-gnss by
main.cpp@7:746ae478fdf7, 2017-07-31 (annotated)
- Committer:
- rob.meades@u-blox.com
- Date:
- Mon Jul 31 10:48:08 2017 +0100
- Revision:
- 7:746ae478fdf7
- Parent:
- 6:881e2bbf29e4
- Child:
- 8:c8b7490c3fb0
Point mbed-os library at ARMmbed master and remove unused variable.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RobMeades | 0:5eb7846b73b4 | 1 | /* mbed Microcontroller Library |
RobMeades | 0:5eb7846b73b4 | 2 | * Copyright (c) 2017 u-blox |
RobMeades | 0:5eb7846b73b4 | 3 | * |
RobMeades | 0:5eb7846b73b4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
RobMeades | 0:5eb7846b73b4 | 5 | * you may not use this file except in compliance with the License. |
RobMeades | 0:5eb7846b73b4 | 6 | * You may obtain a copy of the License at |
RobMeades | 0:5eb7846b73b4 | 7 | * |
RobMeades | 0:5eb7846b73b4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
RobMeades | 0:5eb7846b73b4 | 9 | * |
RobMeades | 0:5eb7846b73b4 | 10 | * Unless required by applicable law or agreed to in writing, software |
RobMeades | 0:5eb7846b73b4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
RobMeades | 0:5eb7846b73b4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
RobMeades | 0:5eb7846b73b4 | 13 | * See the License for the specific language governing permissions and |
RobMeades | 0:5eb7846b73b4 | 14 | * limitations under the License. |
RobMeades | 0:5eb7846b73b4 | 15 | */ |
RobMeades | 0:5eb7846b73b4 | 16 | |
RobMeades | 0:5eb7846b73b4 | 17 | #include "mbed.h" |
RobMeades | 0:5eb7846b73b4 | 18 | #include "gnss.h" |
RobMeades | 0:5eb7846b73b4 | 19 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 20 | #define CHECK_TALKER(s) ((buffer[3] == s[0]) && (buffer[4] == s[1]) && (buffer[5] == s[2])) |
RobMeades | 0:5eb7846b73b4 | 21 | |
RobMeades | 0:5eb7846b73b4 | 22 | // LEDs |
RobMeades | 0:5eb7846b73b4 | 23 | DigitalOut ledRed(LED1, 1); |
RobMeades | 0:5eb7846b73b4 | 24 | DigitalOut ledGreen(LED2, 1); |
RobMeades | 0:5eb7846b73b4 | 25 | DigitalOut ledBlue(LED3, 1); |
RobMeades | 0:5eb7846b73b4 | 26 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 27 | /* This example program for the u-blox C030 and C027 boards instantiates |
RobMeades | 0:5eb7846b73b4 | 28 | * the gnss interface and waits for time/position to be received from a satellite. |
RobMeades | 0:5eb7846b73b4 | 29 | * Progress may be monitored with a serial terminal running at 9600 baud. |
RobMeades | 0:5eb7846b73b4 | 30 | * The LED on the C030 board will turn green when this program is |
RobMeades | 0:5eb7846b73b4 | 31 | * operating correctly, pulse blue when a time reading has been received, |
RobMeades | 0:5eb7846b73b4 | 32 | * pulse white when GNSS position has been received or turn red if there is |
RobMeades | 0:5eb7846b73b4 | 33 | * a failure. |
rob.meades@u-blox.com | 6:881e2bbf29e4 | 34 | * On the C027 and C030 boards the green/red (respectively) LED near the |
rob.meades@u-blox.com | 6:881e2bbf29e4 | 35 | * GNSS module will flash as the module achieves a fix. |
RobMeades | 0:5eb7846b73b4 | 36 | */ |
RobMeades | 0:5eb7846b73b4 | 37 | |
RobMeades | 0:5eb7846b73b4 | 38 | int main() |
RobMeades | 0:5eb7846b73b4 | 39 | { |
RobMeades | 0:5eb7846b73b4 | 40 | GnssSerial gnss; |
RobMeades | 0:5eb7846b73b4 | 41 | int gnssReturnCode; |
RobMeades | 0:5eb7846b73b4 | 42 | int length; |
RobMeades | 0:5eb7846b73b4 | 43 | char buffer[256]; |
RobMeades | 0:5eb7846b73b4 | 44 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 45 | printf ("Starting up...\n"); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 46 | if (gnss.init()) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 47 | printf ("Waiting for GNSS to receive something...\n"); |
rob.meades@u-blox.com | 7:746ae478fdf7 | 48 | while (1) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 49 | gnssReturnCode = gnss.getMessage(buffer, sizeof(buffer)); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 50 | if (gnssReturnCode > 0) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 51 | ledGreen = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 52 | ledBlue = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 53 | ledRed = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 54 | length = LENGTH(gnssReturnCode); |
rob.meades@u-blox.com | 7:746ae478fdf7 | 55 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 56 | printf("NMEA: %.*s\n", length - 2, buffer); |
rob.meades@u-blox.com | 7:746ae478fdf7 | 57 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 58 | if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 59 | // Talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 60 | if ((buffer[0] == '$') || buffer[1] == 'G') { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 61 | if (CHECK_TALKER("GLL")) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 62 | double latitude = 0, longitude = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 63 | char ch; |
rob.meades@u-blox.com | 7:746ae478fdf7 | 64 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 65 | if (gnss.getNmeaAngle(1, buffer, length, latitude) && |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 66 | gnss.getNmeaAngle(3, buffer, length, longitude) && |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 67 | gnss.getNmeaItem(6, buffer, length, ch) && (ch == 'A')) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 68 | ledBlue = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 69 | ledRed = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 70 | ledGreen = 0; |
rob.meades@u-blox.com | 7:746ae478fdf7 | 71 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 72 | printf("\nGNSS: location is %.5f %.5f.\n\n", latitude, longitude); |
rob.meades@u-blox.com | 6:881e2bbf29e4 | 73 | printf("I am here: https://maps.google.com/?q=%.5f,%.5f\n\n", |
rob.meades@u-blox.com | 6:881e2bbf29e4 | 74 | latitude, longitude); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 75 | } |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 76 | } else if (CHECK_TALKER("GGA") || CHECK_TALKER("GNS")) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 77 | double altitude = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 78 | const char *timeString = NULL; |
rob.meades@u-blox.com | 7:746ae478fdf7 | 79 | |
rob.meades@u-blox.com | 7:746ae478fdf7 | 80 | // Altitude |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 81 | if (gnss.getNmeaItem(9, buffer, length, altitude)) { |
rob.meades@u-blox.com | 6:881e2bbf29e4 | 82 | printf("\nGNSS: altitude is %.1f m.\n", altitude); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 83 | } |
rob.meades@u-blox.com | 7:746ae478fdf7 | 84 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 85 | // Time |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 86 | timeString = gnss.findNmeaItemPos(1, buffer, buffer + length); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 87 | if (timeString != NULL) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 88 | ledBlue = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 89 | ledRed = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 90 | ledGreen = 1; |
rob.meades@u-blox.com | 7:746ae478fdf7 | 91 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 92 | printf("\nGNSS: time is %.6s.\n\n", timeString); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 93 | } |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 94 | } else if (CHECK_TALKER("VTG")) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 95 | double speed = 0; |
rob.meades@u-blox.com | 7:746ae478fdf7 | 96 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 97 | // Speed |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 98 | if (gnss.getNmeaItem(7, buffer, length, speed)) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 99 | printf("\nGNSS: speed is %.1f km/h.\n\n", speed); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 100 | } |
RobMeades | 0:5eb7846b73b4 | 101 | } |
RobMeades | 0:5eb7846b73b4 | 102 | } |
RobMeades | 0:5eb7846b73b4 | 103 | } |
RobMeades | 0:5eb7846b73b4 | 104 | } |
RobMeades | 0:5eb7846b73b4 | 105 | } |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 106 | } else { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 107 | printf("Unable to initialise GNSS.\n"); |
RobMeades | 0:5eb7846b73b4 | 108 | } |
rob.meades@u-blox.com | 7:746ae478fdf7 | 109 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 110 | ledRed = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 111 | ledGreen = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 112 | ledBlue = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 113 | printf("Should never get here.\n"); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 114 | MBED_ASSERT(false); |
RobMeades | 0:5eb7846b73b4 | 115 | } |
RobMeades | 0:5eb7846b73b4 | 116 | |
RobMeades | 0:5eb7846b73b4 | 117 | // End Of File |