From fb4e68f0f04f8fa1b1eb8688f08bfa2aadf59fd8 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 16 Feb 2013 22:00:16 +1100 Subject: [PATCH] SITL: added SIM_GPS_BYTELOSS option this allows testing of protocol recovery after losing bytes on the GPS serial link --- libraries/AP_HAL_AVR_SITL/SITL_State.h | 19 +++++++++ libraries/AP_HAL_AVR_SITL/sitl_gps.cpp | 57 +++++++++++++++----------- libraries/SITL/SITL.cpp | 1 + libraries/SITL/SITL.h | 1 + 4 files changed, 53 insertions(+), 25 deletions(-) diff --git a/libraries/AP_HAL_AVR_SITL/SITL_State.h b/libraries/AP_HAL_AVR_SITL/SITL_State.h index 9727a4c132..1b3cd438e9 100644 --- a/libraries/AP_HAL_AVR_SITL/SITL_State.h +++ b/libraries/AP_HAL_AVR_SITL/SITL_State.h @@ -56,6 +56,25 @@ private: static void _update_barometer(float height); static void _update_compass(float roll, float pitch, float yaw); + struct gps_data { + double latitude; + double longitude; + float altitude; + double speedN; + double speedE; + bool have_lock; + }; + + #define MAX_GPS_DELAY 100 + static gps_data _gps_data[MAX_GPS_DELAY]; + + static void _gps_write(uint8_t *p, uint16_t size); + static void _gps_send_ubx(uint8_t msgid, uint8_t *buf, uint16_t size); + static void _update_gps_ubx(const struct gps_data *d); + static void _update_gps_mtk(const struct gps_data *d); + static void _update_gps_mtk16(const struct gps_data *d); + static void _update_gps_mtk19(const struct gps_data *d); + static void _update_gps(double latitude, double longitude, float altitude, double speedN, double speedE, bool have_lock); diff --git a/libraries/AP_HAL_AVR_SITL/sitl_gps.cpp b/libraries/AP_HAL_AVR_SITL/sitl_gps.cpp index 3484935493..6d13e48a4b 100644 --- a/libraries/AP_HAL_AVR_SITL/sitl_gps.cpp +++ b/libraries/AP_HAL_AVR_SITL/sitl_gps.cpp @@ -29,19 +29,9 @@ using namespace AVR_SITL; extern const AP_HAL::HAL& hal; -#define MAX_GPS_DELAY 100 - -struct gps_data { - double latitude; - double longitude; - float altitude; - double speedN; - double speedE; - bool have_lock; -} gps_data[MAX_GPS_DELAY]; - static uint8_t next_gps_index; static uint8_t gps_delay; +SITL_State::gps_data SITL_State::_gps_data[MAX_GPS_DELAY]; // state of GPS emulation static struct { @@ -87,11 +77,28 @@ int SITL_State::gps_pipe(void) return gps_state.client_fd; } +/* + write some bytes from the simulated GPS + */ +void SITL_State::_gps_write(uint8_t *p, uint16_t size) +{ + while (size--) { + if (_sitl->gps_byteloss > 0.0) { + float r = ((((unsigned)random()) % 1000000)) / 1.0e4; + if (r < _sitl->gps_byteloss) { + // lose the byte + p++; + continue; + } + } + write(gps_state.gps_fd, p++, 1); + } +} /* send a UBLOX GPS message */ -static void _gps_send_ubx(uint8_t msgid, uint8_t *buf, uint16_t size) +void SITL_State::_gps_send_ubx(uint8_t msgid, uint8_t *buf, uint16_t size) { const uint8_t PREAMBLE1 = 0xb5; const uint8_t PREAMBLE2 = 0x62; @@ -110,9 +117,9 @@ static void _gps_send_ubx(uint8_t msgid, uint8_t *buf, uint16_t size) for (uint8_t i=0; i= gps_delay) { next_gps_index = 0; } - d = gps_data[next_gps_index]; + d = _gps_data[next_gps_index]; if (_sitl->gps_delay != gps_delay) { // cope with updates to the delay control gps_delay = _sitl->gps_delay; for (uint8_t i=0; i