Example program to demonstrate the use of the GnssSerial class.

Dependencies:   gnss

Committer:
rob.meades@u-blox.com
Date:
Tue Jun 06 21:06:04 2017 +0100
Revision:
1:3c41bde6d0bc
Parent:
0:5eb7846b73b4
Child:
6:881e2bbf29e4
Flesh out example properly.

Who changed what in which revision?

UserRevisionLine numberNew 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.
RobMeades 0:5eb7846b73b4 34 */
RobMeades 0:5eb7846b73b4 35
RobMeades 0:5eb7846b73b4 36 int main()
RobMeades 0:5eb7846b73b4 37 {
RobMeades 0:5eb7846b73b4 38 GnssSerial gnss;
RobMeades 0:5eb7846b73b4 39 int gnssReturnCode;
RobMeades 0:5eb7846b73b4 40 int length;
RobMeades 0:5eb7846b73b4 41 char buffer[256];
RobMeades 0:5eb7846b73b4 42 char link[128] = "";
RobMeades 0:5eb7846b73b4 43
rob.meades@u-blox.com 1:3c41bde6d0bc 44 printf ("Starting up...\n");
rob.meades@u-blox.com 1:3c41bde6d0bc 45 if (gnss.init()) {
rob.meades@u-blox.com 1:3c41bde6d0bc 46 printf ("Waiting for GNSS to receive something...\n");
rob.meades@u-blox.com 1:3c41bde6d0bc 47 while (1) {
rob.meades@u-blox.com 1:3c41bde6d0bc 48 gnssReturnCode = gnss.getMessage(buffer, sizeof(buffer));
rob.meades@u-blox.com 1:3c41bde6d0bc 49 if (gnssReturnCode > 0) {
rob.meades@u-blox.com 1:3c41bde6d0bc 50 ledGreen = 0;
rob.meades@u-blox.com 1:3c41bde6d0bc 51 ledBlue = 1;
rob.meades@u-blox.com 1:3c41bde6d0bc 52 ledRed = 1;
rob.meades@u-blox.com 1:3c41bde6d0bc 53 length = LENGTH(gnssReturnCode);
rob.meades@u-blox.com 1:3c41bde6d0bc 54
rob.meades@u-blox.com 1:3c41bde6d0bc 55 printf("NMEA: %.*s\n", length - 2, buffer);
RobMeades 0:5eb7846b73b4 56
rob.meades@u-blox.com 1:3c41bde6d0bc 57 if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) {
rob.meades@u-blox.com 1:3c41bde6d0bc 58 // Talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS
rob.meades@u-blox.com 1:3c41bde6d0bc 59 if ((buffer[0] == '$') || buffer[1] == 'G') {
rob.meades@u-blox.com 1:3c41bde6d0bc 60 if (CHECK_TALKER("GLL")) {
rob.meades@u-blox.com 1:3c41bde6d0bc 61 double latitude = 0, longitude = 0;
rob.meades@u-blox.com 1:3c41bde6d0bc 62 char ch;
RobMeades 0:5eb7846b73b4 63
rob.meades@u-blox.com 1:3c41bde6d0bc 64 if (gnss.getNmeaAngle(1, buffer, length, latitude) &&
rob.meades@u-blox.com 1:3c41bde6d0bc 65 gnss.getNmeaAngle(3, buffer, length, longitude) &&
rob.meades@u-blox.com 1:3c41bde6d0bc 66 gnss.getNmeaItem(6, buffer, length, ch) && (ch == 'A')) {
rob.meades@u-blox.com 1:3c41bde6d0bc 67 ledBlue = 0;
rob.meades@u-blox.com 1:3c41bde6d0bc 68 ledRed = 0;
rob.meades@u-blox.com 1:3c41bde6d0bc 69 ledGreen = 0;
rob.meades@u-blox.com 1:3c41bde6d0bc 70
rob.meades@u-blox.com 1:3c41bde6d0bc 71 printf("\nGNSS: location is %.5f %.5f.\n\n", latitude, longitude);
rob.meades@u-blox.com 1:3c41bde6d0bc 72 sprintf(link, "I am here!\n"
rob.meades@u-blox.com 1:3c41bde6d0bc 73 "https://maps.google.com/?q=%.5f,%.5f\n",
rob.meades@u-blox.com 1:3c41bde6d0bc 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;
RobMeades 0:5eb7846b73b4 79
rob.meades@u-blox.com 1:3c41bde6d0bc 80 // Altitude
rob.meades@u-blox.com 1:3c41bde6d0bc 81 if (gnss.getNmeaItem(9, buffer, length, altitude)) {
rob.meades@u-blox.com 1:3c41bde6d0bc 82 printf("\nGNSS: altitude is %.1f m.\n\n", altitude);
rob.meades@u-blox.com 1:3c41bde6d0bc 83 }
rob.meades@u-blox.com 1:3c41bde6d0bc 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 1:3c41bde6d0bc 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 1:3c41bde6d0bc 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 1:3c41bde6d0bc 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