Example program to demonstrate the use of the GnssSerial class.
Dependencies: gnss
main.cpp@6:881e2bbf29e4, 2017-06-14 (annotated)
- Committer:
- rob.meades@u-blox.com
- Date:
- Wed Jun 14 20:47:56 2017 +0100
- Revision:
- 6:881e2bbf29e4
- Parent:
- 1:3c41bde6d0bc
- Child:
- 7:746ae478fdf7
Corrections to printf()s and update library version.
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 | char link[128] = ""; |
RobMeades | 0:5eb7846b73b4 | 45 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 46 | printf ("Starting up...\n"); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 47 | if (gnss.init()) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 48 | printf ("Waiting for GNSS to receive something...\n"); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 49 | while (1) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 50 | gnssReturnCode = gnss.getMessage(buffer, sizeof(buffer)); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 51 | if (gnssReturnCode > 0) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 52 | ledGreen = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 53 | ledBlue = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 54 | ledRed = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 55 | length = LENGTH(gnssReturnCode); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 56 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 57 | printf("NMEA: %.*s\n", length - 2, buffer); |
RobMeades | 0:5eb7846b73b4 | 58 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 59 | if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 60 | // Talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 61 | if ((buffer[0] == '$') || buffer[1] == 'G') { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 62 | if (CHECK_TALKER("GLL")) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 63 | double latitude = 0, longitude = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 64 | char ch; |
RobMeades | 0:5eb7846b73b4 | 65 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 66 | if (gnss.getNmeaAngle(1, buffer, length, latitude) && |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 67 | gnss.getNmeaAngle(3, buffer, length, longitude) && |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 68 | gnss.getNmeaItem(6, buffer, length, ch) && (ch == 'A')) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 69 | ledBlue = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 70 | ledRed = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 71 | ledGreen = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 72 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 73 | printf("\nGNSS: location is %.5f %.5f.\n\n", latitude, longitude); |
rob.meades@u-blox.com | 6:881e2bbf29e4 | 74 | printf("I am here: https://maps.google.com/?q=%.5f,%.5f\n\n", |
rob.meades@u-blox.com | 6:881e2bbf29e4 | 75 | latitude, longitude); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 76 | } |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 77 | } else if (CHECK_TALKER("GGA") || CHECK_TALKER("GNS")) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 78 | double altitude = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 79 | const char *timeString = NULL; |
RobMeades | 0:5eb7846b73b4 | 80 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 81 | // Altitude |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 82 | if (gnss.getNmeaItem(9, buffer, length, altitude)) { |
rob.meades@u-blox.com | 6:881e2bbf29e4 | 83 | printf("\nGNSS: altitude is %.1f m.\n", altitude); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 84 | } |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 85 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 86 | // Time |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 87 | timeString = gnss.findNmeaItemPos(1, buffer, buffer + length); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 88 | if (timeString != NULL) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 89 | ledBlue = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 90 | ledRed = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 91 | ledGreen = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 92 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 93 | printf("\nGNSS: time is %.6s.\n\n", timeString); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 94 | } |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 95 | } else if (CHECK_TALKER("VTG")) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 96 | double speed = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 97 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 98 | // Speed |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 99 | if (gnss.getNmeaItem(7, buffer, length, speed)) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 100 | printf("\nGNSS: speed is %.1f km/h.\n\n", speed); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 101 | } |
RobMeades | 0:5eb7846b73b4 | 102 | } |
RobMeades | 0:5eb7846b73b4 | 103 | } |
RobMeades | 0:5eb7846b73b4 | 104 | } |
RobMeades | 0:5eb7846b73b4 | 105 | } |
RobMeades | 0:5eb7846b73b4 | 106 | } |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 107 | } else { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 108 | printf("Unable to initialise GNSS.\n"); |
RobMeades | 0:5eb7846b73b4 | 109 | } |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 110 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 111 | ledRed = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 112 | ledGreen = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 113 | ledBlue = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 114 | printf("Should never get here.\n"); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 115 | MBED_ASSERT(false); |
RobMeades | 0:5eb7846b73b4 | 116 | } |
RobMeades | 0:5eb7846b73b4 | 117 | |
RobMeades | 0:5eb7846b73b4 | 118 | // End Of File |