Example program to demonstrate the use of the GnssSerial class.

Dependencies:   gnss

main.cpp

Committer:
RobMeades
Date:
2017-06-06
Revision:
0:5eb7846b73b4
Child:
1:3c41bde6d0bc

File content as of revision 0:5eb7846b73b4:

/* mbed Microcontroller Library
 * Copyright (c) 2017 u-blox
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "gnss.h"

#define _CHECK_TALKER(s) ((buffer[3] == s[0]) && (buffer[4] == s[1]) && (buffer[5] == s[2]))

// LEDs
DigitalOut ledRed(LED1, 1);
DigitalOut ledGreen(LED2, 1);
DigitalOut ledBlue(LED3, 1);

/* This example program for the u-blox C030 board instantiates
 * the gnss interface and waits for time/position to be received from a satellite.
 * Progress may be monitored with a serial terminal running at 9600 baud.
 * The LED on the C030 board will turn green when this program is
 * operating correctly, pulse blue when a time reading has been received,
 * pulse white when GNSS position has been received or turn red if there is
 * a failure.
 */

int main()
{
    GnssSerial gnss;
    int gnssReturnCode;
    int length;
    char buffer[256];
    char link[128] = "";
    
    // Initialise GNSS
    gnss.init();

    printf ("Waiting for GNSS to receive...\n");
    while (1) {
        ledGreen = 1;
        ledRed = 1;
        ledBlue = 1;
        
        gnssReturnCode = gnss.getMessage(buffer, sizeof(buffer));
        if (gnssReturnCode > 0) {
            ledGreen = 0;
            length = LENGTH(gnssReturnCode);
            
            printf("NMEA: %.*s\n", length - 2, buffer);
    
            if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) {
                // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS
                if ((buffer[0] == '$') || buffer[1] == 'G') {
                  if (_CHECK_TALKER("GLL")) {
                        double latitude = 0, longitude = 0;
                        char ch;
                        
                        if (gnss.getNmeaAngle(1, buffer, length, latitude) && 
                            gnss.getNmeaAngle(3, buffer, length, longitude) && 
                            gnss.getNmeaItem(6, buffer, length, ch) && ch == 'A')
                        {
                            ledBlue = 0;
                            ledRed = 0;
                            ledGreen = 0;
                            
                            printf("GNSS: location is %.5f %.5f.\n", latitude,
                                   longitude); 
                            sprintf(link, "I am here!\n"
                                          "https://maps.google.com/?q=%.5f,%.5f\n",
                                          latitude, longitude); 
                        }
                    } else if (_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) {
                        double altitude = 0; 
                        const char *timeString = NULL;
                        
                         // altitude msl [m]
                        if (gnss.getNmeaItem(9, buffer, length, altitude)) {
                            printf("GNSS: altitude is %.1f m.\n", altitude); 
                        }
    
                        // Retrieve the time
                        timeString = gnss.findNmeaItemPos(1, buffer, buffer +
                                                          length);
                        if (timeString != NULL) {
                            ledBlue = 0;
                            ledRed = 1;
                            ledGreen = 1;
                            
                            printf("GNSS: time is %.6s.\n", timeString);
                        }
                    } else if (_CHECK_TALKER("VTG")) {
                        double speed = 0; 
                        
                        // speed [km/h]
                        if (gnss.getNmeaItem(7, buffer, length, speed)) {
                            printf("GNSS: speed is %.1f km/h.\n", speed);
                        }
                    }
                }
            }
        }
    }
}

// End Of File