2016-02-17 21:25:28 -04:00
|
|
|
#pragma once
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include "AP_HAL_SITL.h"
|
2015-05-04 03:15:12 -03:00
|
|
|
#include "AP_HAL_SITL_Namespace.h"
|
|
|
|
#include "HAL_SITL_Class.h"
|
2016-01-03 17:22:02 -04:00
|
|
|
#include "RCInput.h"
|
2012-12-14 21:54:26 -04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2015-08-15 19:51:46 -03:00
|
|
|
#include <AP_Baro/AP_Baro.h>
|
|
|
|
#include <AP_InertialSensor/AP_InertialSensor.h>
|
2016-03-10 20:41:18 -04:00
|
|
|
#include <AP_Compass/AP_Compass.h>
|
2015-08-15 19:51:46 -03:00
|
|
|
#include <AP_OpticalFlow/AP_OpticalFlow.h>
|
|
|
|
#include <AP_Terrain/AP_Terrain.h>
|
|
|
|
#include <SITL/SITL.h>
|
|
|
|
#include <SITL/SIM_Gimbal.h>
|
2015-11-18 21:22:47 -04:00
|
|
|
#include <SITL/SIM_ADSB.h>
|
2016-01-01 02:09:07 -04:00
|
|
|
#include <AP_HAL/utility/Socket.h>
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
class HAL_SITL;
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
class HALSITL::SITL_State {
|
2016-01-10 02:23:32 -04:00
|
|
|
friend class HALSITL::Scheduler;
|
|
|
|
friend class HALSITL::Util;
|
2016-07-21 21:58:02 -03:00
|
|
|
friend class HALSITL::GPIO;
|
2012-12-14 21:54:26 -04:00
|
|
|
public:
|
|
|
|
void init(int argc, char * const argv[]);
|
|
|
|
|
|
|
|
enum vehicle_type {
|
2015-05-04 21:59:07 -03:00
|
|
|
ArduCopter,
|
|
|
|
APMrover2,
|
|
|
|
ArduPlane
|
2012-12-14 21:54:26 -04:00
|
|
|
};
|
|
|
|
|
2012-12-17 22:34:13 -04:00
|
|
|
int gps_pipe(void);
|
2013-12-21 07:26:30 -04:00
|
|
|
int gps2_pipe(void);
|
2012-12-17 22:34:13 -04:00
|
|
|
ssize_t gps_read(int fd, void *buf, size_t count);
|
2015-06-29 19:55:02 -03:00
|
|
|
uint16_t pwm_output[SITL_NUM_CHANNELS];
|
2016-01-03 17:22:02 -04:00
|
|
|
uint16_t pwm_input[SITL_RC_INPUT_CHANNELS];
|
2015-03-21 11:27:25 -03:00
|
|
|
bool new_rc_input;
|
|
|
|
void loop_hook(void);
|
2015-05-04 21:59:07 -03:00
|
|
|
uint16_t base_port(void) const {
|
|
|
|
return _base_port;
|
|
|
|
}
|
2012-12-17 22:34:13 -04:00
|
|
|
|
2016-05-16 03:51:09 -03:00
|
|
|
bool use_rtscts(void) const {
|
|
|
|
return _use_rtscts;
|
|
|
|
}
|
|
|
|
|
2013-11-28 06:43:25 -04:00
|
|
|
// simulated airspeed, sonar and battery monitor
|
2015-03-21 11:27:25 -03:00
|
|
|
uint16_t sonar_pin_value; // pin 0
|
|
|
|
uint16_t airspeed_pin_value; // pin 1
|
|
|
|
uint16_t voltage_pin_value; // pin 13
|
|
|
|
uint16_t current_pin_value; // pin 12
|
2012-12-18 03:42:48 -04:00
|
|
|
|
2015-05-10 02:36:18 -03:00
|
|
|
// return TCP client address for uartC
|
|
|
|
const char *get_client_address(void) const { return _client_address; }
|
|
|
|
|
2015-11-03 16:59:43 -04:00
|
|
|
// paths for UART devices
|
2016-04-19 00:48:18 -03:00
|
|
|
const char *_uart_path[6] {
|
2015-11-03 16:59:43 -04:00
|
|
|
"tcp:0:wait",
|
|
|
|
"GPS1",
|
|
|
|
"tcp:2",
|
|
|
|
"tcp:3",
|
2016-04-19 21:08:35 -03:00
|
|
|
"GPS2",
|
2016-04-19 00:48:18 -03:00
|
|
|
"tcp:4",
|
2015-11-03 16:59:43 -04:00
|
|
|
};
|
|
|
|
|
2012-12-14 21:54:26 -04:00
|
|
|
private:
|
|
|
|
void _parse_command_line(int argc, char * const argv[]);
|
2015-05-04 18:08:42 -03:00
|
|
|
void _set_param_default(const char *parm);
|
2012-12-14 21:54:26 -04:00
|
|
|
void _usage(void);
|
2015-11-18 21:22:47 -04:00
|
|
|
void _sitl_setup(const char *home_str);
|
2012-12-14 21:54:26 -04:00
|
|
|
void _setup_fdm(void);
|
|
|
|
void _setup_timer(void);
|
|
|
|
void _setup_adc(void);
|
|
|
|
|
2015-03-21 11:27:25 -03:00
|
|
|
float height_agl(void);
|
|
|
|
void _update_barometer(float height);
|
|
|
|
void _update_compass(float rollDeg, float pitchDeg, float yawDeg);
|
|
|
|
void _update_flow(void);
|
2013-02-16 05:16:13 -04:00
|
|
|
|
2016-07-13 10:27:56 -03:00
|
|
|
void _set_signal_handlers(void) const;
|
|
|
|
|
2013-02-16 07:00:16 -04:00
|
|
|
struct gps_data {
|
2015-05-04 21:59:07 -03:00
|
|
|
double latitude;
|
|
|
|
double longitude;
|
|
|
|
float altitude;
|
|
|
|
double speedN;
|
|
|
|
double speedE;
|
|
|
|
double speedD;
|
|
|
|
bool have_lock;
|
2013-02-16 07:00:16 -04:00
|
|
|
};
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
#define MAX_GPS_DELAY 100
|
2015-03-21 11:27:25 -03:00
|
|
|
gps_data _gps_data[MAX_GPS_DELAY];
|
|
|
|
|
|
|
|
bool _gps_has_basestation_position;
|
|
|
|
gps_data _gps_basestation_data;
|
|
|
|
void _gps_write(const uint8_t *p, uint16_t size);
|
|
|
|
void _gps_send_ubx(uint8_t msgid, uint8_t *buf, uint16_t size);
|
|
|
|
void _update_gps_ubx(const struct gps_data *d);
|
|
|
|
void _update_gps_mtk(const struct gps_data *d);
|
|
|
|
void _update_gps_mtk16(const struct gps_data *d);
|
|
|
|
void _update_gps_mtk19(const struct gps_data *d);
|
|
|
|
uint16_t _gps_nmea_checksum(const char *s);
|
|
|
|
void _gps_nmea_printf(const char *fmt, ...);
|
|
|
|
void _update_gps_nmea(const struct gps_data *d);
|
|
|
|
void _sbp_send_message(uint16_t msg_type, uint16_t sender_id, uint8_t len, uint8_t *payload);
|
2015-03-19 16:02:39 -03:00
|
|
|
void _update_gps_sbp(const struct gps_data *d);
|
2015-08-30 03:53:29 -03:00
|
|
|
void _update_gps_file(const struct gps_data *d);
|
2016-05-16 10:09:06 -03:00
|
|
|
void _update_gps_nova(const struct gps_data *d);
|
|
|
|
void _nova_send_message(uint8_t *header, uint8_t headerlength, uint8_t *payload, uint8_t payloadlen);
|
|
|
|
uint32_t CRC32Value(uint32_t icrc);
|
|
|
|
uint32_t CalculateBlockCRC32(uint32_t length, uint8_t *buffer, uint32_t crc);
|
2015-03-21 11:27:25 -03:00
|
|
|
|
|
|
|
void _update_gps(double latitude, double longitude, float altitude,
|
2015-05-04 21:59:07 -03:00
|
|
|
double speedN, double speedE, double speedD, bool have_lock);
|
2013-02-16 05:16:13 -04:00
|
|
|
|
2015-03-21 11:27:25 -03:00
|
|
|
void _update_ins(float roll, float pitch, float yaw, // Relative to earth
|
2015-05-04 21:59:07 -03:00
|
|
|
double rollRate, double pitchRate,double yawRate, // Local to plane
|
|
|
|
double xAccel, double yAccel, double zAccel, // Local to plane
|
|
|
|
float airspeed, float altitude);
|
2015-03-21 11:27:25 -03:00
|
|
|
void _fdm_input(void);
|
2015-05-02 07:27:33 -03:00
|
|
|
void _fdm_input_local(void);
|
2016-01-01 02:09:07 -04:00
|
|
|
void _output_to_flightgear(void);
|
2015-10-22 10:04:42 -03:00
|
|
|
void _simulator_servos(SITL::Aircraft::sitl_input &input);
|
2015-03-21 11:27:25 -03:00
|
|
|
void _simulator_output(bool synthetic_clock_mode);
|
|
|
|
uint16_t _airspeed_sensor(float airspeed);
|
|
|
|
uint16_t _ground_sonar();
|
|
|
|
float _rand_float(void);
|
|
|
|
Vector3f _rand_vec3f(void);
|
2015-05-02 08:41:13 -03:00
|
|
|
void _fdm_input_step(void);
|
2015-05-04 21:59:07 -03:00
|
|
|
|
2015-05-02 08:41:13 -03:00
|
|
|
void wait_clock(uint64_t wait_time_usec);
|
2015-03-21 11:27:25 -03:00
|
|
|
|
2012-12-14 21:54:26 -04:00
|
|
|
// internal state
|
2015-03-21 11:27:25 -03:00
|
|
|
enum vehicle_type _vehicle;
|
|
|
|
uint16_t _framerate;
|
2015-05-10 08:02:20 -03:00
|
|
|
uint8_t _instance;
|
2015-03-21 11:27:25 -03:00
|
|
|
uint16_t _base_port;
|
|
|
|
struct sockaddr_in _rcout_addr;
|
|
|
|
pid_t _parent_pid;
|
|
|
|
uint32_t _update_count;
|
|
|
|
|
|
|
|
AP_Baro *_barometer;
|
|
|
|
AP_InertialSensor *_ins;
|
2016-01-10 02:23:32 -04:00
|
|
|
Scheduler *_scheduler;
|
2015-03-21 11:27:25 -03:00
|
|
|
Compass *_compass;
|
|
|
|
OpticalFlow *_optical_flow;
|
2016-06-03 15:07:36 -03:00
|
|
|
#if AP_TERRAIN_AVAILABLE
|
2015-03-21 11:27:25 -03:00
|
|
|
AP_Terrain *_terrain;
|
2016-06-03 15:07:36 -03:00
|
|
|
#endif
|
2015-03-21 11:27:25 -03:00
|
|
|
|
2016-01-03 17:22:02 -04:00
|
|
|
SocketAPM _sitl_rc_in{true};
|
2015-10-22 10:04:42 -03:00
|
|
|
SITL::SITL *_sitl;
|
2015-03-21 11:27:25 -03:00
|
|
|
uint16_t _rcout_port;
|
|
|
|
uint16_t _simin_port;
|
|
|
|
float _current;
|
|
|
|
|
|
|
|
bool _synthetic_clock_mode;
|
2015-04-13 03:16:22 -03:00
|
|
|
|
2016-05-16 03:51:09 -03:00
|
|
|
bool _use_rtscts;
|
|
|
|
|
2015-05-03 19:10:33 -03:00
|
|
|
const char *_fdm_address;
|
|
|
|
|
2015-04-13 03:16:22 -03:00
|
|
|
// delay buffer variables
|
|
|
|
static const uint8_t mag_buffer_length = 250;
|
|
|
|
static const uint8_t wind_buffer_length = 50;
|
|
|
|
static const uint8_t baro_buffer_length = 50;
|
|
|
|
|
|
|
|
// magnetometer delay buffer variables
|
|
|
|
struct readings_mag {
|
|
|
|
uint32_t time;
|
|
|
|
Vector3f data;
|
|
|
|
};
|
|
|
|
uint8_t store_index_mag;
|
|
|
|
uint32_t last_store_time_mag;
|
|
|
|
VectorN<readings_mag,mag_buffer_length> buffer_mag;
|
|
|
|
uint32_t time_delta_mag;
|
|
|
|
uint32_t delayed_time_mag;
|
|
|
|
|
|
|
|
// airspeed sensor delay buffer variables
|
|
|
|
struct readings_wind {
|
|
|
|
uint32_t time;
|
|
|
|
float data;
|
|
|
|
};
|
|
|
|
uint8_t store_index_wind;
|
|
|
|
uint32_t last_store_time_wind;
|
|
|
|
VectorN<readings_wind,wind_buffer_length> buffer_wind;
|
|
|
|
uint32_t time_delta_wind;
|
|
|
|
uint32_t delayed_time_wind;
|
|
|
|
|
|
|
|
// barometer delay buffer variables
|
|
|
|
struct readings_baro {
|
|
|
|
uint32_t time;
|
|
|
|
float data;
|
|
|
|
};
|
|
|
|
uint8_t store_index_baro;
|
|
|
|
uint32_t last_store_time_baro;
|
|
|
|
VectorN<readings_baro,baro_buffer_length> buffer_baro;
|
|
|
|
uint32_t time_delta_baro;
|
|
|
|
uint32_t delayed_time_baro;
|
2015-05-02 07:27:33 -03:00
|
|
|
|
|
|
|
// internal SITL model
|
2015-10-22 10:04:42 -03:00
|
|
|
SITL::Aircraft *sitl_model;
|
2015-05-10 02:36:18 -03:00
|
|
|
|
2015-05-24 23:11:16 -03:00
|
|
|
// simulated gimbal
|
|
|
|
bool enable_gimbal;
|
2015-10-22 10:04:42 -03:00
|
|
|
SITL::Gimbal *gimbal;
|
2015-05-24 23:11:16 -03:00
|
|
|
|
2016-06-15 00:48:43 -03:00
|
|
|
// simulated ADSb
|
2015-11-18 21:22:47 -04:00
|
|
|
SITL::ADSB *adsb;
|
2016-01-01 02:09:07 -04:00
|
|
|
|
|
|
|
// output socket for flightgear viewing
|
|
|
|
SocketAPM fg_socket{true};
|
2015-11-18 21:22:47 -04:00
|
|
|
|
2015-05-10 02:36:18 -03:00
|
|
|
// TCP address to connect uartC to
|
|
|
|
const char *_client_address;
|
2016-01-06 18:09:40 -04:00
|
|
|
|
|
|
|
const char *defaults_path = HAL_PARAM_DEFAULTS_PATH;
|
2016-06-20 22:00:36 -03:00
|
|
|
|
|
|
|
const char *_home_str;
|
2012-12-14 21:54:26 -04:00
|
|
|
};
|
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#endif // CONFIG_HAL_BOARD == HAL_BOARD_SITL
|