2013-05-29 20:52:21 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2012-12-14 22:47:35 -04:00
|
|
|
//
|
|
|
|
// DIYDrones Custom Mediatek GPS driver for ArduPilot and ArduPilotMega.
|
2012-12-20 19:47:09 -04:00
|
|
|
// Code by Michael Smith, Jordi Munoz and Jose Julio, Craig Elder, DIYDrones.com
|
2012-12-14 22:47:35 -04:00
|
|
|
//
|
2012-12-20 19:47:09 -04:00
|
|
|
// This library is free software; you can redistribute it and / or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
2012-12-14 22:47:35 -04:00
|
|
|
//
|
2012-12-20 19:47:09 -04:00
|
|
|
// GPS configuration : Custom protocol per "DIYDrones Custom Binary Sentence Specification V1.6, v1.7, v1.8, v1.9"
|
2012-12-14 22:47:35 -04:00
|
|
|
//
|
2013-02-16 07:03:53 -04:00
|
|
|
// Note that this driver supports both the 1.6 and 1.9 protocol varients
|
|
|
|
//
|
2012-12-14 22:47:35 -04:00
|
|
|
|
2013-01-01 18:53:55 -04:00
|
|
|
#include <AP_HAL.h>
|
2012-12-14 22:47:35 -04:00
|
|
|
#include "AP_GPS_MTK19.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
void
|
2013-01-01 18:53:55 -04:00
|
|
|
AP_GPS_MTK19::init(AP_HAL::UARTDriver *s, enum GPS_Engine_Setting nav_setting)
|
2012-12-14 22:47:35 -04:00
|
|
|
{
|
2013-01-01 18:53:55 -04:00
|
|
|
_port = s;
|
2012-12-14 22:47:35 -04:00
|
|
|
_port->flush();
|
|
|
|
|
2012-12-21 20:04:02 -04:00
|
|
|
// initialize serial port for binary protocol use
|
|
|
|
// XXX should assume binary, let GPS_AUTO handle dynamic config?
|
|
|
|
_port->print(MTK_SET_BINARY);
|
|
|
|
|
|
|
|
// set 5Hz update rate
|
|
|
|
_port->print(MTK_OUTPUT_5HZ);
|
|
|
|
|
2012-12-20 19:47:09 -04:00
|
|
|
// set SBAS on
|
2012-12-14 22:47:35 -04:00
|
|
|
_port->print(SBAS_ON);
|
2012-12-21 20:04:02 -04:00
|
|
|
|
|
|
|
// set WAAS on
|
|
|
|
_port->print(WAAS_ON);
|
|
|
|
|
2012-12-20 19:47:09 -04:00
|
|
|
// Set Nav Threshold to 0 m/s
|
2012-12-14 22:47:35 -04:00
|
|
|
_port->print(MTK_NAVTHRES_OFF);
|
|
|
|
|
|
|
|
// set initial epoch code
|
|
|
|
_epoch = TIME_OF_DAY;
|
|
|
|
_time_offset = 0;
|
|
|
|
_offset_calculated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process bytes available from the stream
|
|
|
|
//
|
|
|
|
// The stream is assumed to contain only our custom message. If it
|
|
|
|
// contains other messages, and those messages contain the preamble bytes,
|
|
|
|
// it is possible for this code to become de-synchronised. Without
|
|
|
|
// buffering the entire message and re-processing it from the top,
|
|
|
|
// this is unavoidable.
|
|
|
|
//
|
|
|
|
// The lack of a standard header length field makes it impossible to skip
|
|
|
|
// unrecognised messages.
|
|
|
|
//
|
|
|
|
bool
|
|
|
|
AP_GPS_MTK19::read(void)
|
|
|
|
{
|
|
|
|
uint8_t data;
|
|
|
|
int16_t numc;
|
|
|
|
bool parsed = false;
|
|
|
|
|
|
|
|
numc = _port->available();
|
|
|
|
for (int16_t i = 0; i < numc; i++) { // Process bytes received
|
|
|
|
|
|
|
|
// read the next byte
|
|
|
|
data = _port->read();
|
|
|
|
|
|
|
|
restart:
|
2012-12-20 19:47:09 -04:00
|
|
|
switch(_step) {
|
2012-12-14 22:47:35 -04:00
|
|
|
|
|
|
|
// Message preamble, class, ID detection
|
|
|
|
//
|
|
|
|
// If we fail to match any of the expected bytes, we
|
|
|
|
// reset the state machine and re-consider the failed
|
|
|
|
// byte as the first byte of the preamble. This
|
|
|
|
// improves our chances of recovering from a mismatch
|
|
|
|
// and makes it less likely that we will be fooled by
|
|
|
|
// the preamble appearing as data in some other message.
|
|
|
|
//
|
|
|
|
case 0:
|
2012-12-20 19:47:09 -04:00
|
|
|
if (data == PREAMBLE1_V16) {
|
2013-01-01 23:43:33 -04:00
|
|
|
_mtk_revision = MTK_GPS_REVISION_V16;
|
2012-12-14 22:47:35 -04:00
|
|
|
_step++;
|
2013-01-01 23:43:33 -04:00
|
|
|
} else if (data == PREAMBLE1_V19) {
|
|
|
|
_mtk_revision = MTK_GPS_REVISION_V19;
|
2012-12-20 19:47:09 -04:00
|
|
|
_step++;
|
|
|
|
}
|
|
|
|
break;
|
2012-12-14 22:47:35 -04:00
|
|
|
case 1:
|
2013-01-01 23:43:33 -04:00
|
|
|
if (data == PREAMBLE2) {
|
2012-12-20 19:47:09 -04:00
|
|
|
_step++;
|
2013-01-01 19:12:55 -04:00
|
|
|
} else {
|
2013-01-01 23:43:33 -04:00
|
|
|
_step = 0;
|
2013-01-01 19:12:55 -04:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
break;
|
2012-12-14 22:47:35 -04:00
|
|
|
case 2:
|
|
|
|
if (sizeof(_buffer) == data) {
|
|
|
|
_step++;
|
2012-12-21 20:04:02 -04:00
|
|
|
_ck_b = _ck_a = data; // reset the checksum accumulators
|
2012-12-20 19:47:09 -04:00
|
|
|
_payload_counter = 0;
|
2012-12-14 22:47:35 -04:00
|
|
|
} else {
|
2012-12-20 19:47:09 -04:00
|
|
|
_step = 0; // reset and wait for a message of the right class
|
2013-01-01 19:12:55 -04:00
|
|
|
goto restart;
|
2012-12-14 22:47:35 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Receive message data
|
|
|
|
//
|
|
|
|
case 3:
|
|
|
|
_buffer.bytes[_payload_counter++] = data;
|
|
|
|
_ck_b += (_ck_a += data);
|
2013-01-04 04:40:49 -04:00
|
|
|
if (_payload_counter == sizeof(_buffer)) {
|
2012-12-14 22:47:35 -04:00
|
|
|
_step++;
|
2013-01-04 04:40:49 -04:00
|
|
|
}
|
2012-12-14 22:47:35 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Checksum and message processing
|
|
|
|
//
|
|
|
|
case 4:
|
|
|
|
_step++;
|
|
|
|
if (_ck_a != data) {
|
2012-12-20 19:47:09 -04:00
|
|
|
_step = 0;
|
2013-01-04 04:40:49 -04:00
|
|
|
goto restart;
|
2012-12-14 22:47:35 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5:
|
2012-12-20 19:47:09 -04:00
|
|
|
_step = 0;
|
2012-12-14 22:47:35 -04:00
|
|
|
if (_ck_b != data) {
|
2013-01-04 04:40:49 -04:00
|
|
|
goto restart;
|
2012-12-14 22:47:35 -04:00
|
|
|
}
|
|
|
|
|
2013-03-25 04:24:14 -03:00
|
|
|
// parse fix
|
|
|
|
if (_buffer.msg.fix_type == FIX_3D || _buffer.msg.fix_type == FIX_3D_SBAS) {
|
|
|
|
fix = GPS::FIX_3D;
|
|
|
|
}else if (_buffer.msg.fix_type == FIX_2D || _buffer.msg.fix_type == FIX_2D_SBAS) {
|
|
|
|
fix = GPS::FIX_2D;
|
|
|
|
}else{
|
|
|
|
fix = GPS::FIX_NONE;
|
|
|
|
}
|
|
|
|
|
2013-01-01 23:43:33 -04:00
|
|
|
if (_mtk_revision == MTK_GPS_REVISION_V16) {
|
2012-12-20 19:47:09 -04:00
|
|
|
latitude = _buffer.msg.latitude * 10; // V16, V17,V18 doc says *10e7 but device says otherwise
|
|
|
|
longitude = _buffer.msg.longitude * 10; // V16, V17,V18 doc says *10e7 but device says otherwise
|
2013-01-01 23:43:33 -04:00
|
|
|
} else {
|
|
|
|
latitude = _buffer.msg.latitude;
|
|
|
|
longitude = _buffer.msg.longitude;
|
|
|
|
}
|
2012-12-20 19:47:09 -04:00
|
|
|
altitude = _buffer.msg.altitude;
|
|
|
|
ground_speed = _buffer.msg.ground_speed;
|
|
|
|
ground_course = _buffer.msg.ground_course;
|
|
|
|
num_sats = _buffer.msg.satellites;
|
|
|
|
hdop = _buffer.msg.hdop;
|
|
|
|
date = _buffer.msg.utc_date;
|
2012-12-14 22:47:35 -04:00
|
|
|
|
|
|
|
// time from gps is UTC, but convert here to msToD
|
2012-12-20 19:47:09 -04:00
|
|
|
int32_t time_utc = _buffer.msg.utc_time;
|
|
|
|
int32_t temp = (time_utc/10000000);
|
|
|
|
time_utc -= temp*10000000;
|
|
|
|
time = temp * 3600000;
|
|
|
|
temp = (time_utc/100000);
|
|
|
|
time_utc -= temp*100000;
|
|
|
|
time += temp * 60000 + time_utc;
|
|
|
|
parsed = true;
|
2012-12-14 22:47:35 -04:00
|
|
|
|
|
|
|
#ifdef FAKE_GPS_LOCK_TIME
|
|
|
|
if (millis() > FAKE_GPS_LOCK_TIME*1000) {
|
2012-12-20 19:47:09 -04:00
|
|
|
fix = true;
|
|
|
|
latitude = -35000000UL;
|
|
|
|
longitude = 149000000UL;
|
2012-12-14 22:47:35 -04:00
|
|
|
altitude = 584;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-12-21 20:04:02 -04:00
|
|
|
detect a MTK16 or MTK19 GPS
|
2012-12-14 22:47:35 -04:00
|
|
|
*/
|
|
|
|
bool
|
|
|
|
AP_GPS_MTK19::_detect(uint8_t data)
|
|
|
|
{
|
2012-12-20 19:47:09 -04:00
|
|
|
static uint8_t payload_counter;
|
|
|
|
static uint8_t step;
|
|
|
|
static uint8_t ck_a, ck_b;
|
2012-12-14 22:47:35 -04:00
|
|
|
|
2013-01-01 19:12:55 -04:00
|
|
|
restart:
|
2012-12-21 20:04:02 -04:00
|
|
|
switch (step) {
|
2012-12-14 22:47:35 -04:00
|
|
|
case 0:
|
2012-12-20 19:47:09 -04:00
|
|
|
ck_b = ck_a = payload_counter = 0;
|
2013-01-01 23:43:33 -04:00
|
|
|
if (data == PREAMBLE1_V16 || data == PREAMBLE1_V19) {
|
2012-12-20 19:47:09 -04:00
|
|
|
step++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
2013-01-01 23:43:33 -04:00
|
|
|
if (PREAMBLE2 == data) {
|
2012-12-20 19:47:09 -04:00
|
|
|
step++;
|
2013-01-01 19:12:55 -04:00
|
|
|
} else {
|
|
|
|
step = 0;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
break;
|
2012-12-14 22:47:35 -04:00
|
|
|
case 2:
|
|
|
|
if (data == sizeof(struct diyd_mtk_msg)) {
|
|
|
|
step++;
|
2012-12-20 19:47:09 -04:00
|
|
|
ck_b = ck_a = data;
|
2012-12-14 22:47:35 -04:00
|
|
|
} else {
|
2012-12-20 19:47:09 -04:00
|
|
|
step = 0;
|
2013-01-04 04:40:49 -04:00
|
|
|
goto restart;
|
2012-12-14 22:47:35 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
ck_b += (ck_a += data);
|
|
|
|
if (++payload_counter == sizeof(struct diyd_mtk_msg))
|
|
|
|
step++;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
step++;
|
|
|
|
if (ck_a != data) {
|
|
|
|
step = 0;
|
2013-01-04 04:40:49 -04:00
|
|
|
goto restart;
|
2012-12-14 22:47:35 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5:
|
2012-12-20 19:47:09 -04:00
|
|
|
step = 0;
|
2013-01-04 04:40:49 -04:00
|
|
|
if (ck_b != data) {
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
return true;
|
2012-12-14 22:47:35 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|