123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- #include "AP_GPS_MTK.h"
- #include "AP_GPS_MTK19.h"
- extern const AP_HAL::HAL& hal;
- AP_GPS_MTK19::AP_GPS_MTK19(AP_GPS &_gps, AP_GPS::GPS_State &_state, AP_HAL::UARTDriver *_port) :
- AP_GPS_Backend(_gps, _state, _port),
- _step(0),
- _payload_counter(0),
- _mtk_revision(0),
- _fix_counter(0)
- {
- AP_GPS_MTK::send_init_blob(_state.instance, _gps);
- }
- 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++) {
-
- data = port->read();
- restart:
- switch(_step) {
-
-
-
-
-
-
-
-
-
- case 0:
- if (data == PREAMBLE1_V16) {
- _mtk_revision = MTK_GPS_REVISION_V16;
- _step++;
- } else if (data == PREAMBLE1_V19) {
- _mtk_revision = MTK_GPS_REVISION_V19;
- _step++;
- }
- break;
- case 1:
- if (data == PREAMBLE2) {
- _step++;
- } else {
- _step = 0;
- goto restart;
- }
- break;
- case 2:
- if (sizeof(_buffer) == data) {
- _step++;
- _ck_b = _ck_a = data;
- _payload_counter = 0;
- } else {
- _step = 0;
- goto restart;
- }
- break;
-
-
- case 3:
- _buffer[_payload_counter++] = data;
- _ck_b += (_ck_a += data);
- if (_payload_counter == sizeof(_buffer)) {
- _step++;
- }
- break;
-
-
- case 4:
- _step++;
- if (_ck_a != data) {
- _step = 0;
- goto restart;
- }
- break;
- case 5:
- _step = 0;
- if (_ck_b != data) {
- goto restart;
- }
-
- if (_buffer.msg.fix_type == FIX_3D || _buffer.msg.fix_type == FIX_3D_SBAS) {
- state.status = AP_GPS::GPS_OK_FIX_3D;
- }else if (_buffer.msg.fix_type == FIX_2D || _buffer.msg.fix_type == FIX_2D_SBAS) {
- state.status = AP_GPS::GPS_OK_FIX_2D;
- }else{
- state.status = AP_GPS::NO_FIX;
- }
- if (_mtk_revision == MTK_GPS_REVISION_V16) {
- state.location.lat = _buffer.msg.latitude * 10;
- state.location.lng = _buffer.msg.longitude * 10;
- } else {
- state.location.lat = _buffer.msg.latitude;
- state.location.lng = _buffer.msg.longitude;
- }
- state.location.alt = _buffer.msg.altitude;
- state.ground_speed = _buffer.msg.ground_speed*0.01f;
- state.ground_course = wrap_360(_buffer.msg.ground_course*0.01f);
- state.num_sats = _buffer.msg.satellites;
- state.hdop = _buffer.msg.hdop;
-
- if (state.status >= AP_GPS::GPS_OK_FIX_2D) {
- if (_fix_counter == 0) {
- uint32_t bcd_time_ms;
- bcd_time_ms = _buffer.msg.utc_time;
- #if 0
- hal.console->printf("utc_date=%lu utc_time=%lu rev=%u\n",
- (unsigned long)_buffer.msg.utc_date,
- (unsigned long)_buffer.msg.utc_time,
- (unsigned)_mtk_revision);
- #endif
- make_gps_time(_buffer.msg.utc_date, bcd_time_ms);
- state.last_gps_time_ms = AP_HAL::millis();
- }
-
-
-
-
- _fix_counter++;
- if (_fix_counter == 50) {
- _fix_counter = 0;
- }
- }
- fill_3d_velocity();
- parsed = true;
- }
- }
- return parsed;
- }
- bool
- AP_GPS_MTK19::_detect(struct MTK19_detect_state &state, uint8_t data)
- {
- restart:
- switch (state.step) {
- case 0:
- state.ck_b = state.ck_a = state.payload_counter = 0;
- if (data == PREAMBLE1_V16 || data == PREAMBLE1_V19) {
- state.step++;
- }
- break;
- case 1:
- if (PREAMBLE2 == data) {
- state.step++;
- } else {
- state.step = 0;
- goto restart;
- }
- break;
- case 2:
- if (data == sizeof(struct diyd_mtk_msg)) {
- state.step++;
- state.ck_b = state.ck_a = data;
- } else {
- state.step = 0;
- goto restart;
- }
- break;
- case 3:
- state.ck_b += (state.ck_a += data);
- if (++state.payload_counter == sizeof(struct diyd_mtk_msg))
- state.step++;
- break;
- case 4:
- state.step++;
- if (state.ck_a != data) {
- state.step = 0;
- goto restart;
- }
- break;
- case 5:
- state.step = 0;
- if (state.ck_b != data) {
- goto restart;
- }
- return true;
- }
- return false;
- }
|