2014-04-04 20:05:54 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
2015-04-22 20:10:46 -03:00
|
|
|
// Swift Navigation SBP GPS driver for ArduPilot.
|
|
|
|
// Code by Niels Joubert
|
|
|
|
//
|
|
|
|
// Swift Binary Protocol format: http://docs.swift-nav.com/
|
2014-04-04 20:05:54 -03:00
|
|
|
//
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include "AP_GPS.h"
|
2014-04-04 20:05:54 -03:00
|
|
|
#include "AP_GPS_SBP.h"
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <DataFlash/DataFlash.h>
|
2014-04-04 20:05:54 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
#define SBP_DEBUGGING 1
|
|
|
|
#define SBP_HW_LOGGING 1
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
#define SBP_TIMEOUT_HEATBEAT 4000
|
|
|
|
#define SBP_TIMEOUT_PVT 500
|
2014-04-04 20:05:54 -03:00
|
|
|
|
|
|
|
#if SBP_DEBUGGING
|
2015-04-22 20:10:46 -03:00
|
|
|
# define Debug(fmt, args ...) \
|
|
|
|
do { \
|
|
|
|
hal.console->printf("%s:%d: " fmt "\n", \
|
|
|
|
__FUNCTION__, __LINE__, \
|
|
|
|
## args); \
|
|
|
|
hal.scheduler->delay(1); \
|
|
|
|
} while(0)
|
2014-04-04 20:05:54 -03:00
|
|
|
#else
|
|
|
|
# define Debug(fmt, args ...)
|
|
|
|
#endif
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
AP_GPS_SBP::AP_GPS_SBP(AP_GPS &_gps, AP_GPS::GPS_State &_state,
|
|
|
|
AP_HAL::UARTDriver *_port) :
|
|
|
|
AP_GPS_Backend(_gps, _state, _port),
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2015-05-30 09:51:00 -03:00
|
|
|
last_injected_data_ms(0),
|
|
|
|
last_iar_num_hypotheses(0),
|
2015-04-22 20:10:46 -03:00
|
|
|
last_full_update_tow(0),
|
|
|
|
last_full_update_cpu_ms(0),
|
2015-05-30 09:51:00 -03:00
|
|
|
crc_error_counter(0)
|
2014-04-04 20:05:54 -03:00
|
|
|
{
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
Debug("SBP Driver Initialized");
|
|
|
|
|
2014-04-04 20:05:54 -03:00
|
|
|
parser_state.state = sbp_parser_state_t::WAITING;
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
//Externally visible state
|
2014-04-04 20:05:54 -03:00
|
|
|
state.status = AP_GPS::NO_FIX;
|
2015-11-19 23:10:22 -04:00
|
|
|
state.last_gps_time_ms = last_heatbeat_received_ms = AP_HAL::millis();
|
2014-04-04 20:05:54 -03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
// Process all bytes available from the stream
|
|
|
|
//
|
|
|
|
bool
|
2014-04-04 20:05:54 -03:00
|
|
|
AP_GPS_SBP::read(void)
|
|
|
|
{
|
2015-04-22 20:10:46 -03:00
|
|
|
|
2014-06-06 18:58:11 -03:00
|
|
|
//Invariant: Calling this function processes *all* data current in the UART buffer.
|
|
|
|
//
|
|
|
|
//IMPORTANT NOTICE: This function is NOT CALLED for several seconds
|
|
|
|
// during arming. That should not cause the driver to die. Process *all* waiting messages
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
_sbp_process();
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
return _attempt_state_update();
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2014-04-04 20:05:54 -03:00
|
|
|
}
|
|
|
|
|
2017-03-27 22:13:31 -03:00
|
|
|
void
|
2016-10-04 04:37:52 -03:00
|
|
|
AP_GPS_SBP::inject_data(const uint8_t *data, uint16_t len)
|
2014-04-04 20:05:54 -03:00
|
|
|
{
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
if (port->txspace() > len) {
|
2015-11-19 23:10:22 -04:00
|
|
|
last_injected_data_ms = AP_HAL::millis();
|
2015-04-22 20:10:46 -03:00
|
|
|
port->write(data, len);
|
|
|
|
} else {
|
|
|
|
Debug("PIKSI: Not enough TXSPACE");
|
2014-06-06 18:58:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
//This attempts to reads all SBP messages from the incoming port.
|
2014-06-06 18:58:11 -03:00
|
|
|
//Returns true if a new message was read, false if we failed to read a message.
|
2015-04-22 20:10:46 -03:00
|
|
|
void
|
2017-03-27 22:13:31 -03:00
|
|
|
AP_GPS_SBP::_sbp_process()
|
2014-06-06 18:58:11 -03:00
|
|
|
{
|
2015-04-22 20:10:46 -03:00
|
|
|
|
2014-06-06 18:58:11 -03:00
|
|
|
while (port->available() > 0) {
|
2014-04-04 20:05:54 -03:00
|
|
|
uint8_t temp = port->read();
|
|
|
|
uint16_t crc;
|
|
|
|
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2014-04-04 20:05:54 -03:00
|
|
|
//This switch reads one character at a time,
|
|
|
|
//parsing it into buffers until a full message is dispatched
|
2017-02-02 10:52:41 -04:00
|
|
|
switch (parser_state.state) {
|
2014-04-04 20:05:54 -03:00
|
|
|
case sbp_parser_state_t::WAITING:
|
|
|
|
if (temp == SBP_PREAMBLE) {
|
|
|
|
parser_state.n_read = 0;
|
|
|
|
parser_state.state = sbp_parser_state_t::GET_TYPE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sbp_parser_state_t::GET_TYPE:
|
|
|
|
*((uint8_t*)&(parser_state.msg_type) + parser_state.n_read) = temp;
|
|
|
|
parser_state.n_read += 1;
|
|
|
|
if (parser_state.n_read >= 2) {
|
|
|
|
parser_state.n_read = 0;
|
|
|
|
parser_state.state = sbp_parser_state_t::GET_SENDER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sbp_parser_state_t::GET_SENDER:
|
|
|
|
*((uint8_t*)&(parser_state.sender_id) + parser_state.n_read) = temp;
|
|
|
|
parser_state.n_read += 1;
|
|
|
|
if (parser_state.n_read >= 2) {
|
|
|
|
parser_state.n_read = 0;
|
|
|
|
parser_state.state = sbp_parser_state_t::GET_LEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sbp_parser_state_t::GET_LEN:
|
|
|
|
parser_state.msg_len = temp;
|
|
|
|
parser_state.n_read = 0;
|
|
|
|
parser_state.state = sbp_parser_state_t::GET_MSG;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sbp_parser_state_t::GET_MSG:
|
|
|
|
*((uint8_t*)&(parser_state.msg_buff) + parser_state.n_read) = temp;
|
|
|
|
parser_state.n_read += 1;
|
|
|
|
if (parser_state.n_read >= parser_state.msg_len) {
|
|
|
|
parser_state.n_read = 0;
|
|
|
|
parser_state.state = sbp_parser_state_t::GET_CRC;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sbp_parser_state_t::GET_CRC:
|
|
|
|
*((uint8_t*)&(parser_state.crc) + parser_state.n_read) = temp;
|
|
|
|
parser_state.n_read += 1;
|
|
|
|
if (parser_state.n_read >= 2) {
|
|
|
|
parser_state.state = sbp_parser_state_t::WAITING;
|
|
|
|
|
|
|
|
crc = crc16_ccitt((uint8_t*)&(parser_state.msg_type), 2, 0);
|
|
|
|
crc = crc16_ccitt((uint8_t*)&(parser_state.sender_id), 2, crc);
|
|
|
|
crc = crc16_ccitt(&(parser_state.msg_len), 1, crc);
|
|
|
|
crc = crc16_ccitt(parser_state.msg_buff, parser_state.msg_len, crc);
|
|
|
|
if (parser_state.crc == crc) {
|
2015-04-22 20:10:46 -03:00
|
|
|
_sbp_process_message();
|
2014-04-04 20:05:54 -03:00
|
|
|
} else {
|
2014-06-06 18:58:11 -03:00
|
|
|
Debug("CRC Error Occurred!");
|
2014-04-04 20:05:54 -03:00
|
|
|
crc_error_counter += 1;
|
|
|
|
}
|
|
|
|
|
2017-02-02 10:52:41 -04:00
|
|
|
parser_state.state = sbp_parser_state_t::WAITING;
|
2014-04-04 20:05:54 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
parser_state.state = sbp_parser_state_t::WAITING;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-06 18:58:11 -03:00
|
|
|
}
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
|
|
|
|
//INVARIANT: A fully received message with correct CRC is currently in parser_state
|
2014-06-06 18:58:11 -03:00
|
|
|
void
|
2015-04-22 20:10:46 -03:00
|
|
|
AP_GPS_SBP::_sbp_process_message() {
|
2017-02-02 10:52:41 -04:00
|
|
|
switch (parser_state.msg_type) {
|
2015-04-22 20:10:46 -03:00
|
|
|
case SBP_HEARTBEAT_MSGTYPE:
|
2015-11-19 23:10:22 -04:00
|
|
|
last_heatbeat_received_ms = AP_HAL::millis();
|
2015-04-22 20:10:46 -03:00
|
|
|
break;
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
case SBP_GPS_TIME_MSGTYPE:
|
2015-05-04 04:25:19 -03:00
|
|
|
memcpy(&last_gps_time, parser_state.msg_buff, sizeof(last_gps_time));
|
2015-04-22 20:10:46 -03:00
|
|
|
break;
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
case SBP_VEL_NED_MSGTYPE:
|
2015-05-04 04:25:19 -03:00
|
|
|
memcpy(&last_vel_ned, parser_state.msg_buff, sizeof(last_vel_ned));
|
2015-04-22 20:10:46 -03:00
|
|
|
break;
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
case SBP_POS_LLH_MSGTYPE: {
|
|
|
|
struct sbp_pos_llh_t *pos_llh = (struct sbp_pos_llh_t*)parser_state.msg_buff;
|
|
|
|
// Check if this is a single point or RTK solution
|
|
|
|
// flags = 0 -> single point
|
|
|
|
if (pos_llh->flags == 0) {
|
|
|
|
last_pos_llh_spp = *pos_llh;
|
|
|
|
} else if (pos_llh->flags == 1 || pos_llh->flags == 2) {
|
|
|
|
last_pos_llh_rtk = *pos_llh;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
case SBP_DOPS_MSGTYPE:
|
2015-05-04 04:25:19 -03:00
|
|
|
memcpy(&last_dops, parser_state.msg_buff, sizeof(last_dops));
|
2015-04-22 20:10:46 -03:00
|
|
|
break;
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
case SBP_TRACKING_STATE_MSGTYPE:
|
|
|
|
//INTENTIONALLY BLANK
|
2016-05-12 14:02:29 -03:00
|
|
|
//Currently unhandled, but logged after switch statement.
|
2015-04-22 20:10:46 -03:00
|
|
|
break;
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
case SBP_IAR_STATE_MSGTYPE: {
|
|
|
|
sbp_iar_state_t *iar = (struct sbp_iar_state_t*)parser_state.msg_buff;
|
|
|
|
last_iar_num_hypotheses = iar->num_hypotheses;
|
|
|
|
break;
|
|
|
|
}
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
default:
|
2017-03-27 22:13:31 -03:00
|
|
|
// log anyway if it's an unsupported message.
|
2015-07-02 14:32:19 -03:00
|
|
|
// The log mask will be used to adjust or suppress logging
|
2017-03-27 22:13:31 -03:00
|
|
|
break;
|
2015-04-22 20:10:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
logging_log_raw_sbp(parser_state.msg_type, parser_state.sender_id, parser_state.msg_len, parser_state.msg_buff);
|
2014-04-04 20:05:54 -03:00
|
|
|
}
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
bool
|
|
|
|
AP_GPS_SBP::_attempt_state_update()
|
2014-04-04 20:05:54 -03:00
|
|
|
{
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
// If we currently have heartbeats
|
|
|
|
// - NO FIX
|
|
|
|
//
|
|
|
|
// If we have a full update available, save it
|
|
|
|
//
|
2015-11-19 23:10:22 -04:00
|
|
|
uint32_t now = AP_HAL::millis();
|
2015-04-22 20:10:46 -03:00
|
|
|
bool ret = false;
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
if (now - last_heatbeat_received_ms > SBP_TIMEOUT_HEATBEAT) {
|
2017-02-02 10:52:41 -04:00
|
|
|
|
2017-03-27 22:13:31 -03:00
|
|
|
state.status = AP_GPS::NO_FIX;
|
2015-04-22 20:10:46 -03:00
|
|
|
Debug("No Heartbeats from Piksi! Driver Ready to Die!");
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
} else if (last_pos_llh_rtk.tow == last_vel_ned.tow
|
|
|
|
&& abs((int32_t) (last_gps_time.tow - last_vel_ned.tow)) < 10000
|
|
|
|
&& abs((int32_t) (last_dops.tow - last_vel_ned.tow)) < 60000
|
|
|
|
&& last_vel_ned.tow > last_full_update_tow) {
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
// Use the RTK position
|
|
|
|
sbp_pos_llh_t *pos_llh = &last_pos_llh_rtk;
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
// Update time state
|
|
|
|
state.time_week = last_gps_time.wn;
|
|
|
|
state.time_week_ms = last_vel_ned.tow;
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
state.hdop = last_dops.hdop;
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
// Update velocity state
|
2017-03-27 22:13:31 -03:00
|
|
|
state.velocity[0] = (float)(last_vel_ned.n * 1.0e-3);
|
|
|
|
state.velocity[1] = (float)(last_vel_ned.e * 1.0e-3);
|
|
|
|
state.velocity[2] = (float)(last_vel_ned.d * 1.0e-3);
|
|
|
|
state.have_vertical_velocity = true;
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
float ground_vector_sq = state.velocity[0]*state.velocity[0] + state.velocity[1]*state.velocity[1];
|
|
|
|
state.ground_speed = safe_sqrt(ground_vector_sq);
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2016-05-04 22:28:35 -03:00
|
|
|
state.ground_course = wrap_360(degrees(atan2f(state.velocity[1], state.velocity[0])));
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
// Update position state
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2017-01-31 13:19:09 -04:00
|
|
|
state.location.lat = (int32_t) (pos_llh->lat * (double)1e7);
|
|
|
|
state.location.lng = (int32_t) (pos_llh->lon * (double)1e7);
|
|
|
|
state.location.alt = (int32_t) (pos_llh->height * 100);
|
2015-04-22 20:10:46 -03:00
|
|
|
state.num_sats = pos_llh->n_sats;
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2017-02-02 10:52:41 -04:00
|
|
|
if (pos_llh->flags == 0) {
|
2015-04-22 20:10:46 -03:00
|
|
|
state.status = AP_GPS::GPS_OK_FIX_3D;
|
2017-02-02 10:52:41 -04:00
|
|
|
} else if (pos_llh->flags == 2) {
|
2017-01-28 13:43:53 -04:00
|
|
|
state.status = AP_GPS::GPS_OK_FIX_3D_RTK_FLOAT;
|
2017-02-02 10:52:41 -04:00
|
|
|
} else if (pos_llh->flags == 1) {
|
2017-01-28 13:29:38 -04:00
|
|
|
state.status = AP_GPS::GPS_OK_FIX_3D_RTK_FIXED;
|
2017-02-02 10:52:41 -04:00
|
|
|
}
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
last_full_update_tow = last_vel_ned.tow;
|
|
|
|
last_full_update_cpu_ms = now;
|
2017-07-05 13:45:44 -03:00
|
|
|
state.rtk_iar_num_hypotheses = last_iar_num_hypotheses;
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
logging_log_full_update();
|
|
|
|
ret = true;
|
|
|
|
|
|
|
|
} else if (now - last_full_update_cpu_ms > SBP_TIMEOUT_PVT) {
|
|
|
|
|
|
|
|
//INVARIANT: If we currently have a fix, ONLY return true after a full update.
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
state.status = AP_GPS::NO_FIX;
|
|
|
|
ret = true;
|
|
|
|
|
|
|
|
} else {
|
2017-02-02 10:52:41 -04:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
//No timeouts yet, no data yet, nothing has happened.
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2017-02-02 10:52:41 -04:00
|
|
|
}
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
return ret;
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2014-04-04 20:05:54 -03:00
|
|
|
}
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
|
|
|
|
|
2014-04-04 20:05:54 -03:00
|
|
|
bool
|
2015-04-22 20:10:46 -03:00
|
|
|
AP_GPS_SBP::_detect(struct SBP_detect_state &state, uint8_t data)
|
2014-04-04 20:05:54 -03:00
|
|
|
{
|
2015-04-22 20:10:46 -03:00
|
|
|
// This switch reads one character at a time, if we find something that
|
|
|
|
// looks like our preamble we'll try to read the full message length,
|
|
|
|
// calculating the CRC. If the CRC matches, we have an SBP GPS!
|
|
|
|
|
2017-02-02 10:52:41 -04:00
|
|
|
switch (state.state) {
|
2014-04-04 20:05:54 -03:00
|
|
|
case SBP_detect_state::WAITING:
|
|
|
|
if (data == SBP_PREAMBLE) {
|
|
|
|
state.n_read = 0;
|
|
|
|
state.crc_so_far = 0;
|
|
|
|
state.state = SBP_detect_state::GET_TYPE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SBP_detect_state::GET_TYPE:
|
2017-03-27 22:13:31 -03:00
|
|
|
*((uint8_t*)&(state.msg_type) + state.n_read) = data;
|
2014-04-04 20:05:54 -03:00
|
|
|
state.crc_so_far = crc16_ccitt(&data, 1, state.crc_so_far);
|
|
|
|
state.n_read += 1;
|
|
|
|
if (state.n_read >= 2) {
|
|
|
|
state.n_read = 0;
|
|
|
|
state.state = SBP_detect_state::GET_SENDER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SBP_detect_state::GET_SENDER:
|
|
|
|
state.crc_so_far = crc16_ccitt(&data, 1, state.crc_so_far);
|
|
|
|
state.n_read += 1;
|
|
|
|
if (state.n_read >= 2) {
|
|
|
|
state.n_read = 0;
|
|
|
|
state.state = SBP_detect_state::GET_LEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SBP_detect_state::GET_LEN:
|
|
|
|
state.crc_so_far = crc16_ccitt(&data, 1, state.crc_so_far);
|
|
|
|
state.msg_len = data;
|
|
|
|
state.n_read = 0;
|
|
|
|
state.state = SBP_detect_state::GET_MSG;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SBP_detect_state::GET_MSG:
|
2017-03-27 22:13:31 -03:00
|
|
|
if (state.msg_type == SBP_HEARTBEAT_MSGTYPE && state.n_read < 4) {
|
|
|
|
*((uint8_t*)&(state.heartbeat_buff) + state.n_read) = data;
|
|
|
|
}
|
2014-04-04 20:05:54 -03:00
|
|
|
state.crc_so_far = crc16_ccitt(&data, 1, state.crc_so_far);
|
|
|
|
state.n_read += 1;
|
|
|
|
if (state.n_read >= state.msg_len) {
|
|
|
|
state.n_read = 0;
|
|
|
|
state.state = SBP_detect_state::GET_CRC;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SBP_detect_state::GET_CRC:
|
|
|
|
*((uint8_t*)&(state.crc) + state.n_read) = data;
|
|
|
|
state.n_read += 1;
|
|
|
|
if (state.n_read >= 2) {
|
|
|
|
state.state = SBP_detect_state::WAITING;
|
2017-03-27 22:13:31 -03:00
|
|
|
if (state.crc == state.crc_so_far
|
|
|
|
&& state.msg_type == SBP_HEARTBEAT_MSGTYPE) {
|
|
|
|
struct sbp_heartbeat_t* heartbeat = ((struct sbp_heartbeat_t*)state.heartbeat_buff);
|
|
|
|
return heartbeat->protocol_major == 0;
|
|
|
|
}
|
|
|
|
return false;
|
2014-04-04 20:05:54 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
state.state = SBP_detect_state::WAITING;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SBP_HW_LOGGING
|
|
|
|
|
2017-03-27 22:13:31 -03:00
|
|
|
void
|
2015-04-22 20:10:46 -03:00
|
|
|
AP_GPS_SBP::logging_log_full_update()
|
2014-04-04 20:05:54 -03:00
|
|
|
{
|
|
|
|
|
2017-06-27 05:12:45 -03:00
|
|
|
if (!should_df_log()) {
|
|
|
|
return;
|
2014-04-04 20:05:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct log_SbpHealth pkt = {
|
|
|
|
LOG_PACKET_HEADER_INIT(LOG_MSG_SBPHEALTH),
|
2015-11-19 23:10:22 -04:00
|
|
|
time_us : AP_HAL::micros64(),
|
2014-06-06 18:58:11 -03:00
|
|
|
crc_error_counter : crc_error_counter,
|
2015-04-22 20:10:46 -03:00
|
|
|
last_injected_data_ms : last_injected_data_ms,
|
|
|
|
last_iar_num_hypotheses : last_iar_num_hypotheses,
|
2014-06-06 18:58:11 -03:00
|
|
|
};
|
|
|
|
|
2017-06-27 05:12:45 -03:00
|
|
|
DataFlash_Class::instance()->WriteBlock(&pkt, sizeof(pkt));
|
2014-06-06 18:58:11 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2017-03-27 22:13:31 -03:00
|
|
|
AP_GPS_SBP::logging_log_raw_sbp(uint16_t msg_type,
|
|
|
|
uint16_t sender_id,
|
|
|
|
uint8_t msg_len,
|
2015-04-22 20:10:46 -03:00
|
|
|
uint8_t *msg_buff) {
|
2017-06-27 05:12:45 -03:00
|
|
|
if (!should_df_log()) {
|
|
|
|
return;
|
2014-06-06 18:58:11 -03:00
|
|
|
}
|
|
|
|
|
2015-04-22 20:10:46 -03:00
|
|
|
//MASK OUT MESSAGES WE DON'T WANT TO LOG
|
|
|
|
if (( ((uint16_t) gps._sbp_logmask) & msg_type) == 0) {
|
|
|
|
return;
|
2014-04-04 20:05:54 -03:00
|
|
|
}
|
|
|
|
|
2015-11-19 23:10:22 -04:00
|
|
|
uint64_t time_us = AP_HAL::micros64();
|
2017-06-26 12:41:24 -03:00
|
|
|
uint8_t pages = 1;
|
|
|
|
|
|
|
|
if (msg_len > 48) {
|
|
|
|
pages += (msg_len - 48) / 104 + 1;
|
|
|
|
}
|
2014-06-06 18:58:11 -03:00
|
|
|
|
2017-06-26 12:41:24 -03:00
|
|
|
struct log_SbpRAWH pkt = {
|
|
|
|
LOG_PACKET_HEADER_INIT(LOG_MSG_SBPRAWH),
|
2015-04-30 00:49:03 -03:00
|
|
|
time_us : time_us,
|
2015-04-22 20:10:46 -03:00
|
|
|
msg_type : msg_type,
|
|
|
|
sender_id : sender_id,
|
2017-06-26 12:41:24 -03:00
|
|
|
index : 1,
|
|
|
|
pages : pages,
|
2015-04-22 20:10:46 -03:00
|
|
|
msg_len : msg_len,
|
2014-04-04 20:05:54 -03:00
|
|
|
};
|
2017-06-26 12:41:24 -03:00
|
|
|
memcpy(pkt.data, msg_buff, MIN(msg_len, 48));
|
2017-06-27 05:12:45 -03:00
|
|
|
DataFlash_Class::instance()->WriteBlock(&pkt, sizeof(pkt));
|
2014-04-04 20:05:54 -03:00
|
|
|
|
2017-06-26 12:41:24 -03:00
|
|
|
for (uint8_t i = 0; i < pages - 1; i++) {
|
|
|
|
struct log_SbpRAWM pkt2 = {
|
|
|
|
LOG_PACKET_HEADER_INIT(LOG_MSG_SBPRAWM),
|
2015-04-30 00:49:03 -03:00
|
|
|
time_us : time_us,
|
2015-04-22 20:10:46 -03:00
|
|
|
msg_type : msg_type,
|
2017-06-26 12:41:24 -03:00
|
|
|
sender_id : sender_id,
|
|
|
|
index : uint8_t(i + 2),
|
|
|
|
pages : pages,
|
|
|
|
msg_len : msg_len,
|
2015-04-22 20:10:46 -03:00
|
|
|
};
|
2017-06-26 12:41:24 -03:00
|
|
|
memcpy(pkt2.data, &msg_buff[48 + i * 104], MIN(msg_len - (48 + i * 104), 104));
|
2017-06-27 05:12:45 -03:00
|
|
|
DataFlash_Class::instance()->WriteBlock(&pkt2, sizeof(pkt2));
|
2015-04-22 20:10:46 -03:00
|
|
|
}
|
2014-06-06 18:58:11 -03:00
|
|
|
};
|
2014-04-04 20:05:54 -03:00
|
|
|
|
|
|
|
#endif // SBP_HW_LOGGING
|