Example program to demonstrate the use of the GnssSerial class.

Dependencies:   gnss

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 u-blox
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "gnss.h"
00019 
00020 #define CHECK_TALKER(s) ((buffer[3] == s[0]) && (buffer[4] == s[1]) && (buffer[5] == s[2]))
00021 
00022 // LEDs
00023 DigitalOut ledRed(LED1, 1);
00024 DigitalOut ledGreen(LED2, 1);
00025 DigitalOut ledBlue(LED3, 1);
00026 
00027 /* This example program for the u-blox C030 and C027 boards instantiates
00028  * the gnss interface and waits for time/position to be received from a satellite.
00029  * Progress may be monitored with a serial terminal running at 9600 baud.
00030  * The LED on the C030 board will turn green when this program is
00031  * operating correctly, pulse blue when a time reading has been received,
00032  * pulse white when GNSS position has been received or turn red if there is
00033  * a failure.
00034  * On the C027 and C030 boards the green/red (respectively) LED near the
00035  * GNSS module will flash as the module achieves a fix.
00036  */
00037 
00038 int main()
00039 {
00040     GnssSerial gnss;
00041     int gnssReturnCode;
00042     int length;
00043     char buffer[256];
00044     
00045     printf ("Starting up...\n");
00046     if (gnss.init()) {
00047         printf ("Waiting for GNSS to receive something...\n");
00048         while (1) {
00049             gnssReturnCode = gnss.getMessage(buffer, sizeof(buffer));
00050             if (gnssReturnCode > 0) {
00051                 ledGreen = 0;
00052                 ledBlue = 1;
00053                 ledRed = 1;
00054                 length = LENGTH(gnssReturnCode);
00055 
00056                 printf("NMEA: %.*s\n", length - 2, buffer);
00057 
00058                 if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) {
00059                     // Talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS
00060                     if ((buffer[0] == '$') || buffer[1] == 'G') {
00061                       if (CHECK_TALKER("GLL")) {
00062                             double latitude = 0, longitude = 0;
00063                             char ch;
00064 
00065                             if (gnss.getNmeaAngle(1, buffer, length, latitude) && 
00066                                 gnss.getNmeaAngle(3, buffer, length, longitude) && 
00067                                 gnss.getNmeaItem(6, buffer, length, ch) && (ch == 'A')) {
00068                                 ledBlue = 0;
00069                                 ledRed = 0;
00070                                 ledGreen = 0;
00071 
00072                                 printf("\nGNSS: location is %.5f %.5f.\n\n", latitude, longitude); 
00073                                 printf("I am here: https://maps.google.com/?q=%.5f,%.5f\n\n",
00074                                        latitude, longitude); 
00075                             }
00076                         } else if (CHECK_TALKER("GGA") || CHECK_TALKER("GNS")) {
00077                             double altitude = 0; 
00078                             const char *timeString = NULL;
00079 
00080                             // Altitude
00081                             if (gnss.getNmeaItem(9, buffer, length, altitude)) {
00082                                 printf("\nGNSS: altitude is %.1f m.\n", altitude); 
00083                             }
00084 
00085                             // Time
00086                             timeString = gnss.findNmeaItemPos(1, buffer, buffer + length);
00087                             if (timeString != NULL) {
00088                                 ledBlue = 0;
00089                                 ledRed = 1;
00090                                 ledGreen = 1;
00091 
00092                                 printf("\nGNSS: time is %.6s.\n\n", timeString);
00093                             }
00094                         } else if (CHECK_TALKER("VTG")) {
00095                             double speed = 0; 
00096 
00097                             // Speed
00098                             if (gnss.getNmeaItem(7, buffer, length, speed)) {
00099                                 printf("\nGNSS: speed is %.1f km/h.\n\n", speed);
00100                             }
00101                         }
00102                     }
00103                 }
00104             }
00105         }
00106     } else {
00107         printf("Unable to initialise GNSS.\n");
00108     }
00109 
00110     ledRed = 0;
00111     ledGreen = 1;
00112     ledBlue = 1;
00113     printf("Should never get here.\n");
00114     MBED_ASSERT(false);
00115 }
00116 
00117 // End Of File