Testing u-blox modules

Committer:
fskorup
Date:
Thu Apr 18 07:41:02 2019 +0000
Revision:
0:2f5afc4289aa
Testing u-blox modules

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fskorup 0:2f5afc4289aa 1 /*
fskorup 0:2f5afc4289aa 2 Copyright (c) 2019 Flow design labs, Project RAMA R01
fskorup 0:2f5afc4289aa 3
fskorup 0:2f5afc4289aa 4 Permission is hereby granted, free of charge, to any person obtaining a copy
fskorup 0:2f5afc4289aa 5 of this software and associated documentation files (the "Software"), to deal
fskorup 0:2f5afc4289aa 6 in the Software without restriction, including without limitation the rights
fskorup 0:2f5afc4289aa 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
fskorup 0:2f5afc4289aa 8 copies of the Software, and to permit persons to whom the Software is
fskorup 0:2f5afc4289aa 9 furnished to do so, subject to the following conditions:
fskorup 0:2f5afc4289aa 10
fskorup 0:2f5afc4289aa 11 The above copyright notice and this permission notice shall be included in
fskorup 0:2f5afc4289aa 12 all copies or substantial portions of the Software.
fskorup 0:2f5afc4289aa 13
fskorup 0:2f5afc4289aa 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
fskorup 0:2f5afc4289aa 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
fskorup 0:2f5afc4289aa 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
fskorup 0:2f5afc4289aa 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
fskorup 0:2f5afc4289aa 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
fskorup 0:2f5afc4289aa 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
fskorup 0:2f5afc4289aa 20 THE SOFTWARE.
fskorup 0:2f5afc4289aa 21 */
fskorup 0:2f5afc4289aa 22
fskorup 0:2f5afc4289aa 23 #include "mbed.h"
fskorup 0:2f5afc4289aa 24 #include "RamaGNSS.h"
fskorup 0:2f5afc4289aa 25 #include "Other/DebuggerColors.h"
fskorup 0:2f5afc4289aa 26
fskorup 0:2f5afc4289aa 27 RamaGNSS::RamaGNSS(Serial &gnss/*, Serial &debugger*/) : _gnss(gnss)/*, _debugger(debugger)*/
fskorup 0:2f5afc4289aa 28 {
fskorup 0:2f5afc4289aa 29 _gnss.baud(9600);
fskorup 0:2f5afc4289aa 30 //_debugger.baud(9600);
fskorup 0:2f5afc4289aa 31 }
fskorup 0:2f5afc4289aa 32
fskorup 0:2f5afc4289aa 33 bool RamaGNSS::parseNMEA(char *nmeaSentence)
fskorup 0:2f5afc4289aa 34 {
fskorup 0:2f5afc4289aa 35 if (NULL != strstr(nmeaSentence, "$GNGGA")) {
fskorup 0:2f5afc4289aa 36 char *p = nmeaSentence;
fskorup 0:2f5afc4289aa 37
fskorup 0:2f5afc4289aa 38 // parse time
fskorup 0:2f5afc4289aa 39 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 40 float timef = atof(p);
fskorup 0:2f5afc4289aa 41 uint32_t time = timef;
fskorup 0:2f5afc4289aa 42 hour = time / 10000;
fskorup 0:2f5afc4289aa 43 minute = (time % 10000) / 100;
fskorup 0:2f5afc4289aa 44 seconds = (time % 100);
fskorup 0:2f5afc4289aa 45 milliseconds = fmod((double) timef, 1.0) * 1000;
fskorup 0:2f5afc4289aa 46
fskorup 0:2f5afc4289aa 47 // parse latitude
fskorup 0:2f5afc4289aa 48 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 49 latitude = atof(p);
fskorup 0:2f5afc4289aa 50 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 51 if (p[0] == 'N') headingLatitude = 'N';
fskorup 0:2f5afc4289aa 52 else if (p[0] == 'S') headingLatitude = 'S';
fskorup 0:2f5afc4289aa 53 else if (p[0] == ',') headingLatitude = 0;
fskorup 0:2f5afc4289aa 54 else return false;
fskorup 0:2f5afc4289aa 55
fskorup 0:2f5afc4289aa 56 // parse longitude
fskorup 0:2f5afc4289aa 57 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 58 longitude = atof(p);
fskorup 0:2f5afc4289aa 59 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 60 if (p[0] == 'W') headingLongitude = 'W';
fskorup 0:2f5afc4289aa 61 else if (p[0] == 'E') headingLongitude = 'E';
fskorup 0:2f5afc4289aa 62 else if (p[0] == ',') headingLongitude = 0;
fskorup 0:2f5afc4289aa 63 else return false;
fskorup 0:2f5afc4289aa 64
fskorup 0:2f5afc4289aa 65 // parse fixquality
fskorup 0:2f5afc4289aa 66 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 67 fixquality = atoi(p);
fskorup 0:2f5afc4289aa 68
fskorup 0:2f5afc4289aa 69 // parse satellites
fskorup 0:2f5afc4289aa 70 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 71 satelliteCount = atoi(p);
fskorup 0:2f5afc4289aa 72
fskorup 0:2f5afc4289aa 73 // parse HDOP
fskorup 0:2f5afc4289aa 74 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 75 HDOP = atof(p);
fskorup 0:2f5afc4289aa 76
fskorup 0:2f5afc4289aa 77 // parse altitude
fskorup 0:2f5afc4289aa 78 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 79 altitude = atof(p);
fskorup 0:2f5afc4289aa 80
fskorup 0:2f5afc4289aa 81 // parse geoidheight
fskorup 0:2f5afc4289aa 82 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 83 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 84 geoidHeight = atof(p);
fskorup 0:2f5afc4289aa 85 return true;
fskorup 0:2f5afc4289aa 86 }
fskorup 0:2f5afc4289aa 87
fskorup 0:2f5afc4289aa 88 if (NULL != strstr(nmeaSentence, "$GNRMC")) {
fskorup 0:2f5afc4289aa 89 char *p = nmeaSentence;
fskorup 0:2f5afc4289aa 90
fskorup 0:2f5afc4289aa 91 // parse time
fskorup 0:2f5afc4289aa 92 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 93 float timef = atof(p);
fskorup 0:2f5afc4289aa 94 uint32_t time = timef;
fskorup 0:2f5afc4289aa 95 hour = time / 10000;
fskorup 0:2f5afc4289aa 96 minute = (time % 10000) / 100;
fskorup 0:2f5afc4289aa 97 seconds = (time % 100);
fskorup 0:2f5afc4289aa 98 milliseconds = fmod((double) timef, 1.0) * 1000;
fskorup 0:2f5afc4289aa 99
fskorup 0:2f5afc4289aa 100 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 101 satelliteFixValue = p[0];
fskorup 0:2f5afc4289aa 102 if (p[0] == 'A')
fskorup 0:2f5afc4289aa 103 isFixed = true;
fskorup 0:2f5afc4289aa 104 else if (p[0] == 'V')
fskorup 0:2f5afc4289aa 105 isFixed = false;
fskorup 0:2f5afc4289aa 106 else
fskorup 0:2f5afc4289aa 107 return false;
fskorup 0:2f5afc4289aa 108
fskorup 0:2f5afc4289aa 109 // parse latitude
fskorup 0:2f5afc4289aa 110 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 111 latitude = atof(p);
fskorup 0:2f5afc4289aa 112
fskorup 0:2f5afc4289aa 113 // parse heading 01
fskorup 0:2f5afc4289aa 114 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 115 if (p[0] == 'N') headingLatitude = 'N';
fskorup 0:2f5afc4289aa 116 else if (p[0] == 'S') headingLatitude = 'S';
fskorup 0:2f5afc4289aa 117 else if (p[0] == ',') headingLatitude = 0;
fskorup 0:2f5afc4289aa 118 else return false;
fskorup 0:2f5afc4289aa 119
fskorup 0:2f5afc4289aa 120 // parse longitude
fskorup 0:2f5afc4289aa 121 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 122 longitude = atof(p);
fskorup 0:2f5afc4289aa 123
fskorup 0:2f5afc4289aa 124 // parse heading 02
fskorup 0:2f5afc4289aa 125 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 126 if (p[0] == 'W') headingLongitude = 'W';
fskorup 0:2f5afc4289aa 127 else if (p[0] == 'E') headingLongitude = 'E';
fskorup 0:2f5afc4289aa 128 else if (p[0] == ',') headingLongitude = 0;
fskorup 0:2f5afc4289aa 129 else return false;
fskorup 0:2f5afc4289aa 130
fskorup 0:2f5afc4289aa 131 // parse speed
fskorup 0:2f5afc4289aa 132 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 133 speed = atof(p);
fskorup 0:2f5afc4289aa 134
fskorup 0:2f5afc4289aa 135 // parse angle
fskorup 0:2f5afc4289aa 136 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 137 angle = atof(p);
fskorup 0:2f5afc4289aa 138
fskorup 0:2f5afc4289aa 139 // parse time
fskorup 0:2f5afc4289aa 140 p = strchr(p, ',')+1;
fskorup 0:2f5afc4289aa 141 uint32_t fulldate = atof(p);
fskorup 0:2f5afc4289aa 142 day = fulldate / 10000;
fskorup 0:2f5afc4289aa 143 month = (fulldate % 10000) / 100;
fskorup 0:2f5afc4289aa 144 year = (fulldate % 100);
fskorup 0:2f5afc4289aa 145
fskorup 0:2f5afc4289aa 146 // we dont parse the remaining, yet!
fskorup 0:2f5afc4289aa 147 return true;
fskorup 0:2f5afc4289aa 148 }
fskorup 0:2f5afc4289aa 149
fskorup 0:2f5afc4289aa 150 return false;
fskorup 0:2f5afc4289aa 151 }
fskorup 0:2f5afc4289aa 152
fskorup 0:2f5afc4289aa 153 bool RamaGNSS::newNMEAReceived()
fskorup 0:2f5afc4289aa 154 {
fskorup 0:2f5afc4289aa 155 return receivedFlag;
fskorup 0:2f5afc4289aa 156 }
fskorup 0:2f5afc4289aa 157
fskorup 0:2f5afc4289aa 158 char *RamaGNSS::lastNMEA()
fskorup 0:2f5afc4289aa 159 {
fskorup 0:2f5afc4289aa 160 receivedFlag = false;
fskorup 0:2f5afc4289aa 161 return (char *)lastLine;
fskorup 0:2f5afc4289aa 162 }
fskorup 0:2f5afc4289aa 163
fskorup 0:2f5afc4289aa 164 float RamaGNSS::googleReadable(float coordinate)
fskorup 0:2f5afc4289aa 165 {
fskorup 0:2f5afc4289aa 166 float input = coordinate;
fskorup 0:2f5afc4289aa 167 int firstTwo = ((int)input)/100.0f;
fskorup 0:2f5afc4289aa 168 float nextTwo = input - (float)(firstTwo*100.0f);
fskorup 0:2f5afc4289aa 169 float finalConversion = (float)(firstTwo + nextTwo/60.0f);
fskorup 0:2f5afc4289aa 170
fskorup 0:2f5afc4289aa 171 return finalConversion;
fskorup 0:2f5afc4289aa 172 }
fskorup 0:2f5afc4289aa 173
fskorup 0:2f5afc4289aa 174 char RamaGNSS::readNMEA()
fskorup 0:2f5afc4289aa 175 {
fskorup 0:2f5afc4289aa 176 char receivedChar = 0;
fskorup 0:2f5afc4289aa 177
fskorup 0:2f5afc4289aa 178 if(!_gnss.readable()) return receivedChar;
fskorup 0:2f5afc4289aa 179 receivedChar = _gnss.getc();
fskorup 0:2f5afc4289aa 180
fskorup 0:2f5afc4289aa 181 if (receivedChar == '$') {
fskorup 0:2f5afc4289aa 182 currentLine[lineIndex] = 0;
fskorup 0:2f5afc4289aa 183 lineIndex = 0;
fskorup 0:2f5afc4289aa 184 }
fskorup 0:2f5afc4289aa 185
fskorup 0:2f5afc4289aa 186 if (receivedChar == '\n') {
fskorup 0:2f5afc4289aa 187 currentLine[lineIndex] = 0;
fskorup 0:2f5afc4289aa 188
fskorup 0:2f5afc4289aa 189 if (currentLine == line01) {
fskorup 0:2f5afc4289aa 190 currentLine = line02;
fskorup 0:2f5afc4289aa 191 lastLine = line01;
fskorup 0:2f5afc4289aa 192 } else {
fskorup 0:2f5afc4289aa 193 currentLine = line01;
fskorup 0:2f5afc4289aa 194 lastLine = line02;
fskorup 0:2f5afc4289aa 195 }
fskorup 0:2f5afc4289aa 196
fskorup 0:2f5afc4289aa 197 lineIndex = 0;
fskorup 0:2f5afc4289aa 198 receivedFlag = true;
fskorup 0:2f5afc4289aa 199 }
fskorup 0:2f5afc4289aa 200
fskorup 0:2f5afc4289aa 201 currentLine[lineIndex++] = receivedChar;
fskorup 0:2f5afc4289aa 202
fskorup 0:2f5afc4289aa 203 if (lineIndex >= MAX_LINE_SIZE)
fskorup 0:2f5afc4289aa 204 lineIndex = MAX_LINE_SIZE - 1;
fskorup 0:2f5afc4289aa 205
fskorup 0:2f5afc4289aa 206 return receivedChar;
fskorup 0:2f5afc4289aa 207 }