Rev 1910 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1000 | kribba | 1 | |
| 2 | #include "sns_nmea.h" |
||
| 3 | #include <drivers/uart/serial.h> |
||
| 4 | #include <drivers/uart/uart.h> |
||
| 5 | |||
| 6 | void nmea_send_can(uint8_t timer); |
||
| 7 | |||
| 8 | uint8_t nmea_buffer[80]; |
||
| 9 | uint8_t nmea_comindex[25]; |
||
| 10 | uint8_t nmea_buffer_index, nmea_buflen, nmea_comlen; |
||
| 11 | |||
| 12 | uint8_t a2d (uint8_t chr) { // Convert ascii to decimal |
||
| 13 | return (chr-48); |
||
| 14 | } |
||
| 15 | |||
| 16 | void nmea_clear_comindex(void) { |
||
| 17 | uint8_t i; |
||
| 18 | |||
| 19 | for (i=0;i<25;i++) { |
||
| 20 | nmea_comindex[i]=0; |
||
| 21 | } |
||
| 22 | } |
||
| 23 | |||
| 24 | void nmea_parse_GGA(void) { |
||
| 1584 | kribba | 25 | if (nmea_buffer[nmea_comindex[1]+1] != 0x2C) |
| 1000 | kribba | 26 | { |
| 27 | |||
| 28 | //Get time in UTC |
||
| 29 | NMEA_data.time_h = 10*a2d(nmea_buffer[6]) + a2d(nmea_buffer[7]); |
||
| 30 | NMEA_data.time_m = 10*a2d(nmea_buffer[8]) + a2d(nmea_buffer[9]); |
||
| 31 | NMEA_data.time_s = 10*a2d(nmea_buffer[10]) + a2d(nmea_buffer[11]); |
||
| 32 | |||
| 2032 | pengi | 33 | NMEA_data.unixtimestamp = 0 |
| 34 | + NMEA_data.time_s * 1UL |
||
| 35 | + NMEA_data.time_m * 60UL |
||
| 36 | + NMEA_data.time_h * 3600UL |
||
| 37 | + NMEA_data.date_d * 86400UL; |
||
| 38 | // + NMEA_data.time_s * 1UL |
||
| 39 | // + NMEA_data.time_s * 1UL |
||
| 40 | |||
| 1000 | kribba | 41 | //Get fixtype |
| 42 | NMEA_data.fixvalid=a2d(nmea_buffer[nmea_comindex[5]+1]); |
||
| 43 | |||
| 44 | //Get number of satellites used in fix |
||
| 45 | NMEA_data.usedsat = 10*a2d(nmea_buffer[nmea_comindex[6]+1]) + a2d(nmea_buffer[nmea_comindex[6]+2]); |
||
| 46 | |||
| 47 | // Get latitude |
||
| 1455 | eqlazer | 48 | if (nmea_comindex[2]-nmea_comindex[1] == LAT_LENGTH) { |
| 1000 | kribba | 49 | NMEA_data.lat_deg = 10*a2d(nmea_buffer[nmea_comindex[1]+1]) |
| 50 | + a2d(nmea_buffer[nmea_comindex[1]+2]); |
||
| 51 | |||
| 52 | NMEA_data.lat_min = 10*a2d(nmea_buffer[nmea_comindex[1]+3]) |
||
| 53 | + a2d(nmea_buffer[nmea_comindex[1]+4]); |
||
| 54 | |||
| 55 | NMEA_data.lat_sec_h = 10*a2d(nmea_buffer[nmea_comindex[1]+6]); |
||
| 56 | NMEA_data.lat_sec_h += a2d(nmea_buffer[nmea_comindex[1]+7]); |
||
| 57 | NMEA_data.lat_sec_l = 10*a2d(nmea_buffer[nmea_comindex[1]+8]); |
||
| 58 | NMEA_data.lat_sec_l += a2d(nmea_buffer[nmea_comindex[1]+9]); |
||
| 59 | NMEA_data.lat_sec_l = NMEA_data.lat_sec_l << 1; |
||
| 60 | |||
| 61 | //Last bit contains cardinal direction, N=0, S=1 |
||
| 62 | if (nmea_buffer[nmea_comindex[2]+1] == 'N') { |
||
| 63 | NMEA_data.lat_sec_l &=0b11111110; |
||
| 64 | } else { |
||
| 65 | NMEA_data.lat_sec_l |=1; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | |||
| 70 | // Get longitude |
||
| 1455 | eqlazer | 71 | if (nmea_comindex[4]-nmea_comindex[3] == LON_LENGTH) { |
| 1000 | kribba | 72 | NMEA_data.lon_deg = 100*a2d(nmea_buffer[nmea_comindex[3]+1]) |
| 73 | + 10*a2d(nmea_buffer[nmea_comindex[3]+2]) |
||
| 74 | + a2d(nmea_buffer[nmea_comindex[3]+3]); |
||
| 75 | |||
| 76 | NMEA_data.lon_min = 10*a2d(nmea_buffer[nmea_comindex[3]+4]) |
||
| 77 | + a2d(nmea_buffer[nmea_comindex[3]+5]); |
||
| 78 | |||
| 79 | NMEA_data.lon_sec_h = 10*a2d(nmea_buffer[nmea_comindex[3]+7]); |
||
| 80 | NMEA_data.lon_sec_h += a2d(nmea_buffer[nmea_comindex[3]+8]); |
||
| 81 | NMEA_data.lon_sec_l = 10*a2d(nmea_buffer[nmea_comindex[3]+9]); |
||
| 82 | NMEA_data.lon_sec_l += a2d(nmea_buffer[nmea_comindex[3]+10]); |
||
| 83 | NMEA_data.lon_sec_l = NMEA_data.lon_sec_l << 1; |
||
| 84 | |||
| 85 | //Last bit contains cardinal direction, E=0, W=1 |
||
| 86 | if (nmea_buffer[nmea_comindex[4]+1] == 'E') { |
||
| 87 | NMEA_data.lon_sec_l &=0b11111110; |
||
| 88 | } else { |
||
| 89 | NMEA_data.lon_sec_l |=1; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | void nmea_parse_GSA(void) { |
||
| 1584 | kribba | 96 | /* |
| 97 | Gets fixtype only, 1=No fix, 2=2D fix, 3=3D fix |
||
| 98 | */ |
||
| 99 | NMEA_data.fixtype=a2d(nmea_buffer[nmea_comindex[1]+1]); |
||
| 1000 | kribba | 100 | } |
| 101 | |||
| 102 | void nmea_parse_GSV(void) { |
||
| 1584 | kribba | 103 | //Information about tracked satellites, angle, snr etc. Ignore for now. |
| 1000 | kribba | 104 | } |
| 105 | |||
| 106 | void nmea_parse_RMC(void) { |
||
| 107 | //TODO |
||
| 1584 | kribba | 108 | /* Follows comindex length info |
| 109 | |||
| 110 | 1 1 validity, A=ok, V=invalid |
||
| 111 | 2 X latitude |
||
| 112 | 3 1 lat CD |
||
| 113 | 4 X longitude |
||
| 114 | 5 1 lon CD |
||
| 115 | 6 5 Speed over ground, knots, NNN.N |
||
| 116 | 7 5 Course made good, true |
||
| 117 | 8 6 Date of fix, DDMMYY |
||
| 118 | 9 5 Magnetic variation, degrees |
||
| 119 | 10 1 Magnetic variation, CD |
||
| 120 | |||
| 121 | */ |
||
| 122 | uint8_t varlen=0; |
||
| 123 | uint8_t validity=0; |
||
| 124 | if (nmea_buffer[nmea_comindex[1]+1]==0x41) //0x41='A', valid fix |
||
| 125 | validity=1; |
||
| 126 | NMEA_data.speed=0; |
||
| 127 | NMEA_data.cmg_h=0; |
||
| 128 | NMEA_data.cmg_l=0; |
||
| 129 | |||
| 130 | if (validity==1) { |
||
| 131 | NMEA_data.speed=0; |
||
| 132 | varlen = nmea_comindex[7]-nmea_comindex[6]; |
||
| 133 | switch(varlen-4) { |
||
| 134 | case 3: |
||
| 135 | NMEA_data.speed = 100*a2d(nmea_buffer[nmea_comindex[6]+1])+ |
||
| 136 | 10*a2d(nmea_buffer[nmea_comindex[6]+2])+ |
||
| 137 | a2d(nmea_buffer[nmea_comindex[6]+3]); |
||
| 138 | break; |
||
| 139 | case 2: |
||
| 140 | NMEA_data.speed = 10*a2d(nmea_buffer[nmea_comindex[6]+1])+ |
||
| 141 | a2d(nmea_buffer[nmea_comindex[6]+2]); |
||
| 142 | break; |
||
| 143 | case 1: |
||
| 144 | NMEA_data.speed = a2d(nmea_buffer[nmea_comindex[6]+1]); |
||
| 145 | break; |
||
| 146 | } |
||
| 147 | |||
| 148 | varlen = nmea_comindex[8]-nmea_comindex[7]; |
||
| 149 | switch (varlen-4) { |
||
| 150 | case 3: |
||
| 151 | NMEA_data.cmg_h = a2d(nmea_buffer[nmea_comindex[7]+1]); |
||
| 152 | NMEA_data.cmg_l = 10*a2d(nmea_buffer[nmea_comindex[7]+2])+ |
||
| 153 | a2d(nmea_buffer[nmea_comindex[7]+3]); |
||
| 154 | break; |
||
| 155 | case 2: |
||
| 156 | NMEA_data.cmg_l = 10*a2d(nmea_buffer[nmea_comindex[7]+1])+ |
||
| 157 | a2d(nmea_buffer[nmea_comindex[7]+2]); |
||
| 158 | break; |
||
| 159 | case 1: |
||
| 160 | NMEA_data.cmg_l = a2d(nmea_buffer[nmea_comindex[7]+1]); |
||
| 161 | break; |
||
| 162 | } |
||
| 163 | NMEA_data.date_d = 10*a2d(nmea_buffer[nmea_comindex[8]+1])+a2d(nmea_buffer[nmea_comindex[8]+2]); |
||
| 164 | NMEA_data.date_m = 10*a2d(nmea_buffer[nmea_comindex[8]+3])+a2d(nmea_buffer[nmea_comindex[8]+4]); |
||
| 165 | NMEA_data.date_y = 10*a2d(nmea_buffer[nmea_comindex[8]+5])+a2d(nmea_buffer[nmea_comindex[8]+6]); |
||
| 166 | } |
||
| 167 | #if CAN_PRINTF==1 |
||
| 168 | //printf("%02d-%02d-%02d",NMEA_data.date_y,NMEA_data.date_m,NMEA_data.date_d); |
||
| 169 | #endif |
||
| 1000 | kribba | 170 | } |
| 171 | |||
| 172 | void nmea_make_index(void) { |
||
| 173 | |||
| 174 | nmea_clear_comindex(); |
||
| 175 | |||
| 176 | uint8_t i; |
||
| 177 | uint8_t j=0; |
||
| 178 | |||
| 179 | for (i=0;i<nmea_buflen;i++) { |
||
| 180 | if (nmea_buffer[i] == ',') { |
||
| 181 | nmea_comindex[j] = i; |
||
| 182 | j++; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | nmea_comlen = j; |
||
| 186 | } |
||
| 187 | |||
| 188 | void nmea_parse(void) { |
||
| 189 | // Supported messages: GSA, GSV, GGA, RMC |
||
| 190 | nmea_make_index(); |
||
| 191 | |||
| 192 | if ( |
||
| 193 | nmea_buffer[2] == 'G' && |
||
| 194 | nmea_buffer[3] == 'S' && |
||
| 195 | nmea_buffer[4] == 'A') |
||
| 196 | { |
||
| 197 | nmea_parse_GSA(); |
||
| 198 | } else if ( |
||
| 199 | nmea_buffer[2] == 'G' && |
||
| 200 | nmea_buffer[3] == 'S' && |
||
| 201 | nmea_buffer[4] == 'V') { |
||
| 202 | nmea_parse_GSV(); |
||
| 203 | } else if ( |
||
| 204 | nmea_buffer[2] == 'G' && |
||
| 205 | nmea_buffer[3] == 'G' && |
||
| 206 | nmea_buffer[4] == 'A') { |
||
| 207 | nmea_parse_GGA(); |
||
| 208 | } else if ( |
||
| 209 | nmea_buffer[2] == 'R' && |
||
| 210 | nmea_buffer[3] == 'M' && |
||
| 211 | nmea_buffer[4] == 'C') { |
||
| 212 | nmea_parse_RMC(); |
||
| 213 | } |
||
| 214 | |||
| 215 | } |
||
| 216 | |||
| 217 | void sns_nmea_Init(void) |
||
| 1580 | kribba | 218 | { |
| 1000 | kribba | 219 | Serial_Init(); |
| 1580 | kribba | 220 | Timer_SetTimeout(sns_nmea_cansend_TIMER, sns_nmea_cansend_SEND_PERIOD , TimerTypeFreeRunning, &nmea_send_can); |
| 221 | |||
| 222 | #ifdef TSIP_AT_STARTUP |
||
| 223 | // Tell GPS to switch to NMEA and 9600 baud |
||
| 224 | //TODO: put in progmem to save space |
||
| 225 | //TODO: do not send this directly at power on, the GPS module is quite slow at startup |
||
| 226 | uint8_t tsip_command[] = {0x10, 0xBC, 0x00, 0x07, 0x07, 0x03, 0x00, 0x00, 0x00, 0x02, 0x04, 0x00, 0x10, 0x03}; |
||
| 2032 | pengi | 227 | for( uint8_t i = 0; i<14; i++){ |
| 1580 | kribba | 228 | uart_putc(tsip_command[i]); |
| 229 | } |
||
| 1455 | eqlazer | 230 | #endif |
| 1000 | kribba | 231 | } |
| 232 | |||
| 233 | void sns_nmea_Process(void) |
||
| 234 | { |
||
| 235 | uint16_t rx_uart; |
||
| 236 | uint8_t rx_char; |
||
| 237 | |||
| 238 | rx_uart = uart_getc(); |
||
| 239 | |||
| 240 | if (rx_uart != UART_NO_DATA) { |
||
| 241 | rx_char = (uint8_t) rx_uart & 0x00ff; |
||
| 242 | |||
| 243 | if (rx_char == 0x24) { |
||
| 244 | nmea_buffer_index=0; |
||
| 245 | } else if (rx_char == '\n') { |
||
| 246 | nmea_buflen = nmea_buffer_index+1; |
||
| 247 | nmea_parse(); |
||
| 248 | } else { |
||
| 249 | nmea_buffer[nmea_buffer_index] = rx_char; |
||
| 250 | nmea_buffer_index++; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | void sns_nmea_HandleMessage(StdCan_Msg_t *rxMsg) |
||
| 256 | { |
||
| 257 | /* |
||
| 258 | if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS && |
||
| 259 | StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER && |
||
| 260 | rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_NMEA && |
||
| 261 | rxMsg->Header.ModuleId == sns_nmea_ID) |
||
| 262 | { |
||
| 263 | switch (rxMsg->Header.Command) |
||
| 264 | { |
||
| 265 | case CAN_CMD_MODULE_DUMMY: |
||
| 266 | ///TODO: Do something dummy |
||
| 267 | break; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | */ |
||
| 271 | } |
||
| 272 | |||
| 273 | void sns_nmea_List(uint8_t ModuleSequenceNumber) |
||
| 274 | { |
||
| 275 | StdCan_Msg_t txMsg; |
||
| 276 | |||
| 277 | StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type |
||
| 278 | StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); |
||
| 279 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_NMEA; ///TODO: Change this to the actual module type |
||
| 280 | txMsg.Header.ModuleId = sns_nmea_ID; |
||
| 281 | txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST; |
||
| 282 | txMsg.Length = 6; |
||
| 283 | |||
| 1910 | arune | 284 | uint32_t HwId=BIOS_GetHwId(); |
| 285 | txMsg.Data[0] = HwId&0xff; |
||
| 286 | txMsg.Data[1] = (HwId>>8)&0xff; |
||
| 287 | txMsg.Data[2] = (HwId>>16)&0xff; |
||
| 288 | txMsg.Data[3] = (HwId>>24)&0xff; |
||
| 1000 | kribba | 289 | |
| 290 | txMsg.Data[4] = NUMBER_OF_MODULES; |
||
| 291 | txMsg.Data[5] = ModuleSequenceNumber; |
||
| 292 | |||
| 293 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
||
| 294 | } |
||
| 295 | |||
| 296 | void nmea_send_can(uint8_t timer) |
||
| 297 | { |
||
| 298 | StdCan_Msg_t txMsg; |
||
| 299 | |||
| 300 | StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); |
||
| 301 | StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); |
||
| 302 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_NMEA; |
||
| 303 | txMsg.Header.ModuleId = sns_nmea_ID; |
||
| 304 | txMsg.Header.Command = CAN_MODULE_CMD_NMEA_POSITION; //definiera nåt vettigt för det sen |
||
| 305 | |||
| 306 | txMsg.Length = 8; |
||
| 307 | txMsg.Data[0] = NMEA_data.lat_deg; |
||
| 308 | txMsg.Data[1] = NMEA_data.lat_min; |
||
| 309 | txMsg.Data[2] = NMEA_data.lat_sec_h; |
||
| 310 | txMsg.Data[3] = NMEA_data.lat_sec_l; |
||
| 311 | |||
| 312 | txMsg.Data[4] = NMEA_data.lon_deg; |
||
| 313 | txMsg.Data[5] = NMEA_data.lon_min; |
||
| 314 | txMsg.Data[6] = NMEA_data.lon_sec_h; |
||
| 315 | txMsg.Data[7] = NMEA_data.lon_sec_l; |
||
| 316 | |||
| 317 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
||
| 1580 | kribba | 318 | |
| 319 | txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_GPSTIME; |
||
| 320 | txMsg.Length = 4; |
||
| 321 | txMsg.Data[0] = NMEA_data.time_h; |
||
| 322 | txMsg.Data[1] = NMEA_data.time_m; |
||
| 323 | txMsg.Data[2] = NMEA_data.time_s; |
||
| 324 | txMsg.Data[3] = 0; //Timezone diff from GMT |
||
| 325 | |||
| 1584 | kribba | 326 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
| 327 | |||
| 328 | txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_GPSDATE; |
||
| 329 | txMsg.Length = 3; |
||
| 330 | txMsg.Data[0] = NMEA_data.date_y; |
||
| 331 | txMsg.Data[1] = NMEA_data.date_m; |
||
| 332 | txMsg.Data[2] = NMEA_data.date_d; |
||
| 333 | |||
| 334 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
||
| 335 | |||
| 336 | txMsg.Header.Command = CAN_MODULE_CMD_NMEA_HEADING; |
||
| 337 | txMsg.Length = 6; |
||
| 338 | txMsg.Data[0] = NMEA_data.fixvalid; |
||
| 339 | txMsg.Data[1] = NMEA_data.fixtype; |
||
| 340 | txMsg.Data[2] = NMEA_data.usedsat; |
||
| 341 | txMsg.Data[3] = NMEA_data.speed; |
||
| 342 | txMsg.Data[4] = NMEA_data.cmg_h; |
||
| 343 | txMsg.Data[5] = NMEA_data.cmg_l; |
||
| 1000 | kribba | 344 | |
| 1584 | kribba | 345 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
| 1580 | kribba | 346 | |
| 347 | #if CAN_PRINTF==1 |
||
| 348 | //printf("Lat: %02d %02d.%02d%02d, Lon: %03d %02d.%02d%02d\n",NMEA_data.lat_deg, NMEA_data.lat_min, NMEA_data.lat_sec_h, NMEA_data.lat_sec_l >> 1,NMEA_data.lon_deg, NMEA_data.lon_min, NMEA_data.lon_sec_h, NMEA_data.lon_sec_l >> 1); |
||
| 1584 | kribba | 349 | //printf("Time: %01d:%01d:%01d\n",NMEA_data.time_h,NMEA_data.time_m,NMEA_data.time_s); |
| 1580 | kribba | 350 | //printf("Fixvalid: %01d\n",NMEA_data.fixvalid); |
| 351 | //printf("Sat: %02d\n",NMEA_data.usedsat); |
||
| 1584 | kribba | 352 | //printf("Type:%01d\n",NMEA_data.fixtype); |
| 1580 | kribba | 353 | #endif |
| 1584 | kribba | 354 | |
| 355 | /* |
||
| 356 | Add to msg: |
||
| 357 | Valid fix, NMEA_data.fixvalid |
||
| 358 | # of satellites, NMEA_data.numsat |
||
| 359 | fixtype (no/2d/3d) NMEA_data.fixtype |
||
| 360 | Speed NMEA_data.speed |
||
| 361 | Course made good NMEA_data.cmg_h, NMEA_data.cmg_l |
||
| 362 | |||
| 363 | */ |
||
| 1000 | kribba | 364 | } |
| 365 |