2021-10-16 00:10:40 -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/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
simulate GPS sensors
|
|
|
|
|
|
|
|
Usage example:
|
|
|
|
param set SERIAL5_PROTOCOL 5
|
|
|
|
|
2023-12-11 13:13:24 -04:00
|
|
|
sim_vehicle.py -D --console --map -A --serial5=sim:gps:2
|
2021-10-16 00:10:40 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-11-28 06:24:44 -04:00
|
|
|
#include "SIM_config.h"
|
2021-10-16 00:10:40 -03:00
|
|
|
|
|
|
|
#if HAL_SIM_GPS_ENABLED
|
|
|
|
|
2024-08-12 19:25:51 -03:00
|
|
|
#include <sys/time.h>
|
2021-10-16 00:10:40 -03:00
|
|
|
#include "SIM_SerialDevice.h"
|
|
|
|
|
|
|
|
namespace SITL {
|
|
|
|
|
2023-08-09 01:36:44 -03:00
|
|
|
// for delay simulation:
|
|
|
|
struct GPS_Data {
|
|
|
|
uint32_t timestamp_ms;
|
|
|
|
double latitude;
|
|
|
|
double longitude;
|
|
|
|
float altitude;
|
|
|
|
double speedN;
|
|
|
|
double speedE;
|
|
|
|
double speedD;
|
|
|
|
double yaw_deg;
|
|
|
|
double roll_deg;
|
|
|
|
double pitch_deg;
|
|
|
|
bool have_lock;
|
2023-12-01 03:29:39 -04:00
|
|
|
float horizontal_acc;
|
|
|
|
float vertical_acc;
|
|
|
|
float speed_acc;
|
|
|
|
uint8_t num_sats;
|
2023-08-09 01:36:44 -03:00
|
|
|
|
2024-05-12 00:02:44 -03:00
|
|
|
// Get course over ground [rad], where 0 = North in WGS-84 coordinate system.
|
|
|
|
// Calculated from 2D velocity.
|
|
|
|
float ground_track_rad() const WARN_IF_UNUSED;
|
2023-08-09 01:36:44 -03:00
|
|
|
|
|
|
|
// Get 2D speed [m/s] in WGS-84 coordinate system
|
|
|
|
float speed_2d() const WARN_IF_UNUSED;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class GPS_Backend {
|
|
|
|
public:
|
|
|
|
CLASS_NO_COPY(GPS_Backend);
|
|
|
|
|
|
|
|
GPS_Backend(class GPS &front, uint8_t _instance);
|
|
|
|
virtual ~GPS_Backend() {}
|
|
|
|
|
|
|
|
// 0 baud means "unset" i.e. baud-rate checks should not apply
|
|
|
|
virtual uint32_t device_baud() const { return 0; }
|
|
|
|
|
|
|
|
ssize_t write_to_autopilot(const char *p, size_t size) const;
|
|
|
|
ssize_t read_from_autopilot(char *buffer, size_t size) const;
|
|
|
|
|
2023-11-16 03:04:09 -04:00
|
|
|
// read and process config from autopilot (e.g.)
|
|
|
|
virtual void update_read();
|
|
|
|
// writing fix information to autopilot (e.g.)
|
|
|
|
virtual void publish(const GPS_Data *d) = 0;
|
2023-08-09 01:36:44 -03:00
|
|
|
|
2023-11-28 06:24:44 -04:00
|
|
|
struct GPS_TOW {
|
|
|
|
// Number of weeks since midnight 5-6 January 1980
|
|
|
|
uint16_t week;
|
|
|
|
// Time since start of the GPS week [mS]
|
|
|
|
uint32_t ms;
|
2023-11-14 04:11:39 -04:00
|
|
|
};
|
|
|
|
|
2023-11-28 06:24:44 -04:00
|
|
|
static GPS_TOW gps_time();
|
2023-12-01 00:57:21 -04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
uint8_t instance;
|
|
|
|
GPS &front;
|
|
|
|
|
|
|
|
class SIM *_sitl;
|
|
|
|
|
2023-11-28 06:24:44 -04:00
|
|
|
static void simulation_timeval(struct timeval *tv);
|
2023-08-09 01:36:44 -03:00
|
|
|
};
|
|
|
|
|
2021-10-16 00:10:40 -03:00
|
|
|
class GPS : public SerialDevice {
|
|
|
|
public:
|
|
|
|
|
2021-11-11 17:30:30 -04:00
|
|
|
CLASS_NO_COPY(GPS);
|
|
|
|
|
2021-10-16 00:10:40 -03:00
|
|
|
enum Type {
|
|
|
|
NONE = 0,
|
2023-11-28 06:24:44 -04:00
|
|
|
#if AP_SIM_GPS_UBLOX_ENABLED
|
2021-10-16 00:10:40 -03:00
|
|
|
UBLOX = 1,
|
2023-11-28 06:24:44 -04:00
|
|
|
#endif
|
|
|
|
#if AP_SIM_GPS_NMEA_ENABLED
|
2021-10-16 00:10:40 -03:00
|
|
|
NMEA = 5,
|
2023-11-28 06:24:44 -04:00
|
|
|
#endif
|
|
|
|
#if AP_SIM_GPS_SBP_ENABLED
|
2021-10-16 00:10:40 -03:00
|
|
|
SBP = 6,
|
2023-11-28 06:24:44 -04:00
|
|
|
#endif
|
2021-11-01 01:56:39 -03:00
|
|
|
#if AP_SIM_GPS_FILE_ENABLED
|
2021-10-16 00:10:40 -03:00
|
|
|
FILE = 7,
|
2021-11-01 01:56:39 -03:00
|
|
|
#endif
|
2023-11-28 06:24:44 -04:00
|
|
|
#if AP_SIM_GPS_NOVA_ENABLED
|
2021-10-16 00:10:40 -03:00
|
|
|
NOVA = 8,
|
2023-11-28 06:24:44 -04:00
|
|
|
#endif
|
|
|
|
#if AP_SIM_GPS_SBP2_ENABLED
|
2021-10-16 00:10:40 -03:00
|
|
|
SBP2 = 9,
|
2023-11-28 06:24:44 -04:00
|
|
|
#endif
|
2023-11-28 16:19:24 -04:00
|
|
|
#if AP_SIM_GPS_TRIMBLE_ENABLED
|
2024-03-15 01:54:39 -03:00
|
|
|
TRIMBLE = 11, // matches GPS1_TYPE
|
2023-11-28 06:24:44 -04:00
|
|
|
#endif
|
|
|
|
#if AP_SIM_GPS_MSP_ENABLED
|
2023-05-19 02:09:44 -03:00
|
|
|
MSP = 19,
|
2023-11-28 06:24:44 -04:00
|
|
|
#endif
|
2021-10-16 00:10:40 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
GPS(uint8_t _instance);
|
|
|
|
|
|
|
|
// update state
|
|
|
|
void update();
|
|
|
|
|
|
|
|
ssize_t write_to_autopilot(const char *p, size_t size) const override;
|
|
|
|
|
2022-09-28 07:54:52 -03:00
|
|
|
uint32_t device_baud() const override; // 0 meaning unset
|
|
|
|
|
2021-10-16 00:10:40 -03:00
|
|
|
private:
|
|
|
|
|
|
|
|
uint8_t instance;
|
|
|
|
|
2023-11-14 04:11:39 -04:00
|
|
|
// The last time GPS data was written [mS]
|
|
|
|
uint32_t last_write_update_ms;
|
2021-10-16 00:10:40 -03:00
|
|
|
|
2022-01-05 20:32:55 -04:00
|
|
|
// last 20 samples, allowing for up to 20 samples of delay
|
2023-08-09 01:36:44 -03:00
|
|
|
GPS_Data _gps_history[20];
|
2021-10-16 00:10:40 -03:00
|
|
|
|
2023-12-01 03:29:39 -04:00
|
|
|
// state of jamming simulation
|
|
|
|
struct {
|
|
|
|
uint32_t last_jam_ms;
|
|
|
|
uint32_t jam_start_ms;
|
|
|
|
uint32_t last_sats_change_ms;
|
|
|
|
uint32_t last_vz_change_ms;
|
|
|
|
uint32_t last_vel_change_ms;
|
|
|
|
uint32_t last_pos_change_ms;
|
|
|
|
uint32_t last_acc_change_ms;
|
|
|
|
double latitude;
|
|
|
|
double longitude;
|
|
|
|
} jamming[2];
|
|
|
|
|
2021-10-16 00:10:40 -03:00
|
|
|
bool _gps_has_basestation_position;
|
2023-08-09 01:36:44 -03:00
|
|
|
GPS_Data _gps_basestation_data;
|
2023-05-19 02:09:44 -03:00
|
|
|
|
2023-12-01 03:29:39 -04:00
|
|
|
void simulate_jamming(GPS_Data &d);
|
|
|
|
|
2022-01-05 20:32:55 -04:00
|
|
|
// get delayed data
|
2023-08-09 01:36:44 -03:00
|
|
|
GPS_Data interpolate_data(const GPS_Data &d, uint32_t delay_ms);
|
|
|
|
|
|
|
|
uint8_t allocated_type;
|
|
|
|
GPS_Backend *backend;
|
|
|
|
void check_backend_allocation();
|
2021-10-16 00:10:40 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAL_SIM_GPS_ENABLED
|