fahim alavi
/
example-gnss
Example with new gnss libraries
Fork of example-gnss by
main.cpp@8:c8b7490c3fb0, 2018-07-20 (annotated)
- Committer:
- fahim.alavi@u-blox.com
- Date:
- Fri Jul 20 14:38:50 2018 +0500
- Revision:
- 8:c8b7490c3fb0
- Parent:
- 7:746ae478fdf7
New gnss library pass through enabled
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" |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 18 | #include "gnss_operations.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 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 38 | GnssOperations gnss; |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 39 | Serial host_serial(USBTX, USBRX); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 40 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 41 | bool print_hex_buffer(char *buffer, uint32_t length) { |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 42 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 43 | char log[256] = ""; |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 44 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 45 | for (int i =0; i < length; i++) { |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 46 | sprintf(log, "%s%02X ", log, buffer[i]); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 47 | } |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 48 | printf(log); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 49 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 50 | return true; |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 51 | } |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 52 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 53 | void passThroughThreadHandler() { |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 54 | while (1) { |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 55 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 56 | gnss.send_to_gnss(host_serial.getc()); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 57 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 58 | } |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 59 | } |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 60 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 61 | |
RobMeades | 0:5eb7846b73b4 | 62 | int main() |
RobMeades | 0:5eb7846b73b4 | 63 | { |
RobMeades | 0:5eb7846b73b4 | 64 | int gnssReturnCode; |
RobMeades | 0:5eb7846b73b4 | 65 | int length; |
RobMeades | 0:5eb7846b73b4 | 66 | char buffer[256]; |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 67 | bool enable_pass_through = true; |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 68 | Thread passThroughThread; |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 69 | host_serial.baud(115200); |
RobMeades | 0:5eb7846b73b4 | 70 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 71 | printf ("Starting up...\n"); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 72 | if (gnss.init()) { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 73 | printf ("Waiting for GNSS to receive something...\n"); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 74 | passThroughThread.start(callback(passThroughThreadHandler)); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 75 | gnss.enable_ubx_odo(); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 76 | gnss.enable_ubx_nav_odo(); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 77 | gnss.enable_ubx_nav_pvt(); |
rob.meades@u-blox.com | 7:746ae478fdf7 | 78 | while (1) { |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 79 | double lat = 0.0, lon = 0.0; |
rob.meades@u-blox.com | 7:746ae478fdf7 | 80 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 81 | gnssReturnCode = gnss.getMessage(buffer, sizeof(buffer)); |
rob.meades@u-blox.com | 7:746ae478fdf7 | 82 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 83 | if (gnssReturnCode > 0) { |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 84 | length = LENGTH(gnssReturnCode); |
rob.meades@u-blox.com | 7:746ae478fdf7 | 85 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 86 | if (enable_pass_through){ |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 87 | for (int i=0; i<length; i++) |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 88 | printf("%c", buffer[i]); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 89 | } |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 90 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 91 | if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) { |
rob.meades@u-blox.com | 7:746ae478fdf7 | 92 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 93 | // Talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 94 | if ((buffer[0] == '$') || buffer[1] == 'G') { |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 95 | if (CHECK_TALKER("GLL")) { |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 96 | char ch; |
rob.meades@u-blox.com | 7:746ae478fdf7 | 97 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 98 | if (gnss.getNmeaAngle(1, buffer, length, lat) && |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 99 | gnss.getNmeaAngle(3, buffer, length, lon) && |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 100 | gnss.getNmeaItem(6, buffer, length, ch) && (ch == 'A')) { |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 101 | } |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 102 | } |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 103 | } |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 104 | buffer[length] = '\0'; |
rob.meades@u-blox.com | 7:746ae478fdf7 | 105 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 106 | printf(buffer); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 107 | } |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 108 | else if ((PROTOCOL(gnssReturnCode) == GnssParser::UBX) && (length > 6)) { |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 109 | eUBX_MESSAGE ubx_message_type = gnss.get_ubx_message(buffer); |
rob.meades@u-blox.com | 7:746ae478fdf7 | 110 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 111 | uint16_t payload_length = buffer[4] | (buffer[5] << 8); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 112 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 113 | //print_hex_buffer(buffer, payload_length + UBX_FRAME_SIZE); |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 114 | } |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 115 | } |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 116 | } |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 117 | } else { |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 118 | printf("Unable to initialise GNSS.\n"); |
RobMeades | 0:5eb7846b73b4 | 119 | } |
rob.meades@u-blox.com | 7:746ae478fdf7 | 120 | |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 121 | ledRed = 0; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 122 | ledGreen = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 123 | ledBlue = 1; |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 124 | printf("Should never get here.\n"); |
rob.meades@u-blox.com | 1:3c41bde6d0bc | 125 | MBED_ASSERT(false); |
RobMeades | 0:5eb7846b73b4 | 126 | } |
RobMeades | 0:5eb7846b73b4 | 127 | |
fahim.alavi@u-blox.com | 8:c8b7490c3fb0 | 128 | // End Of File |