2015-05-02 05:09:30 -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/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
parent class for aircraft simulators
|
|
|
|
*/
|
|
|
|
|
2015-10-22 11:04:23 -03:00
|
|
|
#pragma once
|
2015-05-02 07:28:57 -03:00
|
|
|
|
2015-08-11 03:28:46 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2015-05-02 05:09:30 -03:00
|
|
|
|
2015-10-22 10:58:33 -03:00
|
|
|
#include "SITL.h"
|
2018-07-31 09:33:23 -03:00
|
|
|
#include "SITL_Input.h"
|
2016-07-19 01:42:31 -03:00
|
|
|
#include <AP_Terrain/AP_Terrain.h>
|
2018-07-31 09:33:23 -03:00
|
|
|
#include "SIM_Sprayer.h"
|
|
|
|
#include "SIM_Gripper_Servo.h"
|
|
|
|
#include "SIM_Gripper_EPM.h"
|
2019-01-08 21:39:44 -04:00
|
|
|
#include "SIM_Parachute.h"
|
2018-08-03 07:52:20 -03:00
|
|
|
#include "SIM_Precland.h"
|
2019-09-28 08:32:57 -03:00
|
|
|
#include "SIM_RichenPower.h"
|
2020-08-03 00:24:27 -03:00
|
|
|
#include "SIM_I2C.h"
|
2019-10-07 04:30:22 -03:00
|
|
|
#include "SIM_Buzzer.h"
|
2019-06-26 23:37:34 -03:00
|
|
|
#include <Filter/Filter.h>
|
2016-09-21 17:46:12 -03:00
|
|
|
|
2015-10-22 10:04:42 -03:00
|
|
|
namespace SITL {
|
|
|
|
|
2015-05-02 05:09:30 -03:00
|
|
|
/*
|
|
|
|
parent class for all simulator types
|
|
|
|
*/
|
2015-10-22 10:15:34 -03:00
|
|
|
class Aircraft {
|
2015-05-02 05:09:30 -03:00
|
|
|
public:
|
2019-08-15 01:01:24 -03:00
|
|
|
Aircraft(const char *frame_str);
|
|
|
|
|
|
|
|
// called directly after constructor:
|
2019-08-15 03:53:35 -03:00
|
|
|
virtual void set_start_location(const Location &start_loc, const float start_yaw);
|
2015-05-02 05:09:30 -03:00
|
|
|
|
2015-05-02 08:41:33 -03:00
|
|
|
/*
|
|
|
|
set simulation speedup
|
|
|
|
*/
|
|
|
|
void set_speedup(float speedup);
|
2020-04-07 00:56:22 -03:00
|
|
|
float get_speedup() { return target_speedup; }
|
2015-05-02 08:41:33 -03:00
|
|
|
|
2015-05-10 08:01:49 -03:00
|
|
|
/*
|
|
|
|
set instance number
|
|
|
|
*/
|
|
|
|
void set_instance(uint8_t _instance) {
|
|
|
|
instance = _instance;
|
|
|
|
}
|
|
|
|
|
2015-05-25 00:04:46 -03:00
|
|
|
/*
|
|
|
|
set directory for additional files such as aircraft models
|
|
|
|
*/
|
|
|
|
void set_autotest_dir(const char *_autotest_dir) {
|
|
|
|
autotest_dir = _autotest_dir;
|
|
|
|
}
|
|
|
|
|
2016-12-16 12:18:04 -04:00
|
|
|
/* Create and set in/out socket for extenal simulator */
|
|
|
|
virtual void set_interface_ports(const char* address, const int port_in, const int port_out) {};
|
2016-09-21 17:46:12 -03:00
|
|
|
|
2015-05-02 05:09:30 -03:00
|
|
|
/*
|
|
|
|
step the FDM by one time step
|
|
|
|
*/
|
|
|
|
virtual void update(const struct sitl_input &input) = 0;
|
|
|
|
|
2019-08-15 03:53:35 -03:00
|
|
|
void update_model(const struct sitl_input &input);
|
|
|
|
|
2015-05-02 05:09:30 -03:00
|
|
|
/* fill a sitl_fdm structure from the simulator state */
|
2016-07-19 08:38:16 -03:00
|
|
|
void fill_fdm(struct sitl_fdm &fdm);
|
2015-05-02 05:09:30 -03:00
|
|
|
|
2016-07-19 08:38:16 -03:00
|
|
|
/* smooth sensors to provide kinematic consistancy */
|
|
|
|
void smooth_sensors(void);
|
2016-12-19 05:48:27 -04:00
|
|
|
|
2015-11-18 21:20:01 -04:00
|
|
|
/* return normal distribution random numbers */
|
|
|
|
static double rand_normal(double mean, double stddev);
|
|
|
|
|
2015-11-16 00:10:29 -04:00
|
|
|
// get frame rate of model in Hz
|
2016-12-19 05:48:27 -04:00
|
|
|
float get_rate_hz(void) const { return rate_hz; }
|
2015-12-31 23:15:06 -04:00
|
|
|
|
2020-01-17 17:19:40 -04:00
|
|
|
// get number of motors for model
|
|
|
|
uint16_t get_num_motors() const {
|
|
|
|
return num_motors;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get motor offset for model
|
|
|
|
virtual uint16_t get_motors_offset() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-31 23:15:06 -04:00
|
|
|
const Vector3f &get_gyro(void) const {
|
|
|
|
return gyro;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vector3f &get_velocity_ef(void) const {
|
|
|
|
return velocity_ef;
|
|
|
|
}
|
|
|
|
|
2016-04-19 22:48:37 -03:00
|
|
|
const Vector3f &get_velocity_air_ef(void) const {
|
|
|
|
return velocity_air_ef;
|
|
|
|
}
|
2016-12-19 05:48:27 -04:00
|
|
|
|
2015-12-31 23:15:06 -04:00
|
|
|
const Matrix3f &get_dcm(void) const {
|
|
|
|
return dcm;
|
|
|
|
}
|
2016-06-17 00:46:12 -03:00
|
|
|
|
|
|
|
const Vector3f &get_mag_field_bf(void) const {
|
|
|
|
return mag_bf;
|
|
|
|
}
|
2016-10-27 03:12:09 -03:00
|
|
|
|
2018-07-31 09:33:23 -03:00
|
|
|
float gross_mass() const { return mass + external_payload_mass; }
|
2016-10-27 03:12:09 -03:00
|
|
|
|
2019-07-18 14:36:52 -03:00
|
|
|
virtual void set_config(const char* config) {
|
|
|
|
config_ = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-01 04:10:23 -03:00
|
|
|
const Location &get_location() const { return location; }
|
|
|
|
|
2018-03-07 21:25:44 -04:00
|
|
|
const Vector3f &get_position() const { return position; }
|
2020-08-04 00:47:24 -03:00
|
|
|
|
|
|
|
// distance the rangefinder is perceiving
|
|
|
|
float rangefinder_range() const;
|
2018-03-07 21:25:44 -04:00
|
|
|
|
|
|
|
void get_attitude(Quaternion &attitude) const {
|
|
|
|
attitude.from_rotation_matrix(dcm);
|
|
|
|
}
|
|
|
|
|
2018-08-03 07:52:20 -03:00
|
|
|
const Location &get_home() const { return home; }
|
|
|
|
float get_home_yaw() const { return home_yaw; }
|
|
|
|
|
2019-10-07 04:30:22 -03:00
|
|
|
void set_buzzer(Buzzer *_buzzer) { buzzer = _buzzer; }
|
2018-07-31 09:33:23 -03:00
|
|
|
void set_sprayer(Sprayer *_sprayer) { sprayer = _sprayer; }
|
2019-01-08 21:39:44 -04:00
|
|
|
void set_parachute(Parachute *_parachute) { parachute = _parachute; }
|
2019-09-28 08:32:57 -03:00
|
|
|
void set_richenpower(RichenPower *_richenpower) { richenpower = _richenpower; }
|
2018-07-31 09:33:23 -03:00
|
|
|
void set_gripper_servo(Gripper_Servo *_gripper) { gripper = _gripper; }
|
|
|
|
void set_gripper_epm(Gripper_EPM *_gripper_epm) { gripper_epm = _gripper_epm; }
|
2018-08-03 07:52:20 -03:00
|
|
|
void set_precland(SIM_Precland *_precland);
|
2020-08-03 00:24:27 -03:00
|
|
|
void set_i2c(class I2C *_i2c) { i2c = _i2c; }
|
2018-07-31 09:33:23 -03:00
|
|
|
|
2015-05-02 05:09:30 -03:00
|
|
|
protected:
|
2016-06-16 19:35:08 -03:00
|
|
|
SITL *sitl;
|
2015-05-02 05:09:30 -03:00
|
|
|
Location home;
|
2019-08-15 03:53:35 -03:00
|
|
|
bool home_is_set;
|
2015-05-02 05:09:30 -03:00
|
|
|
Location location;
|
|
|
|
|
|
|
|
float ground_level;
|
2015-11-23 18:14:47 -04:00
|
|
|
float home_yaw;
|
2015-05-02 05:09:30 -03:00
|
|
|
float frame_height;
|
2018-07-31 09:33:23 -03:00
|
|
|
Matrix3f dcm; // rotation matrix, APM conventions, from body to earth
|
|
|
|
Vector3f gyro; // rad/s
|
|
|
|
Vector3f velocity_ef; // m/s, earth frame
|
|
|
|
Vector3f wind_ef; // m/s, earth frame
|
|
|
|
Vector3f velocity_air_ef; // velocity relative to airmass, earth frame
|
|
|
|
Vector3f velocity_air_bf; // velocity relative to airmass, body frame
|
|
|
|
Vector3f position; // meters, NED from origin
|
|
|
|
float mass; // kg
|
2020-09-22 19:44:18 -03:00
|
|
|
float external_payload_mass; // kg
|
|
|
|
Vector3f accel_body{0.0f, 0.0f, -GRAVITY_MSS}; // m/s/s NED, body frame
|
2018-07-31 09:33:23 -03:00
|
|
|
float airspeed; // m/s, apparent airspeed
|
|
|
|
float airspeed_pitot; // m/s, apparent airspeed, as seen by fwd pitot tube
|
2017-03-01 11:29:53 -04:00
|
|
|
float battery_voltage = -1.0f;
|
2020-09-22 19:44:18 -03:00
|
|
|
float battery_current;
|
2019-09-27 16:56:15 -03:00
|
|
|
uint8_t num_motors = 1;
|
|
|
|
float rpm[12];
|
2020-09-22 19:44:18 -03:00
|
|
|
uint8_t rcin_chan_count;
|
2020-07-25 13:04:49 -03:00
|
|
|
float rcin[12];
|
2020-08-04 00:47:24 -03:00
|
|
|
float range = -1.0f; // externally supplied rangefinder value, assumed to have been corrected for vehicle attitude
|
2015-05-02 05:09:30 -03:00
|
|
|
|
2018-12-04 01:53:15 -04:00
|
|
|
struct {
|
|
|
|
// data from simulated laser scanner, if available
|
|
|
|
struct vector3f_array points;
|
|
|
|
struct float_array ranges;
|
|
|
|
} scanner;
|
2020-07-17 20:37:34 -03:00
|
|
|
|
|
|
|
// Rangefinder
|
|
|
|
float rangefinder_m[RANGEFINDER_MAX_INSTANCES];
|
|
|
|
|
2020-08-15 12:35:50 -03:00
|
|
|
// Windvane apparent wind
|
|
|
|
struct {
|
|
|
|
float speed;
|
|
|
|
float direction;
|
|
|
|
} wind_vane_apparent;
|
|
|
|
|
2016-12-19 05:48:27 -04:00
|
|
|
// Wind Turbulence simulated Data
|
2020-09-22 19:44:18 -03:00
|
|
|
float turbulence_azimuth;
|
|
|
|
float turbulence_horizontal_speed; // m/s
|
|
|
|
float turbulence_vertical_speed; // m/s
|
2016-11-13 20:41:06 -04:00
|
|
|
|
2016-12-19 05:48:27 -04:00
|
|
|
Vector3f mag_bf; // local earth magnetic field vector in Gauss, earth frame
|
2016-06-17 00:46:12 -03:00
|
|
|
|
2015-05-02 05:09:30 -03:00
|
|
|
uint64_t time_now_us;
|
|
|
|
|
2020-09-22 19:44:18 -03:00
|
|
|
const float gyro_noise = radians(0.1f);
|
|
|
|
const float accel_noise = 0.3f;
|
|
|
|
float rate_hz = 1200.0f;
|
2015-05-02 05:09:30 -03:00
|
|
|
float target_speedup;
|
2015-05-02 09:13:02 -03:00
|
|
|
uint64_t frame_time_us;
|
2015-05-02 05:09:30 -03:00
|
|
|
uint64_t last_wall_time_us;
|
2020-06-04 05:07:26 -03:00
|
|
|
uint32_t last_fps_report_ms;
|
2020-06-04 06:00:40 -03:00
|
|
|
int64_t sleep_debt_us;
|
|
|
|
uint32_t last_frame_count;
|
2015-05-10 08:01:49 -03:00
|
|
|
uint8_t instance;
|
2015-05-25 00:04:46 -03:00
|
|
|
const char *autotest_dir;
|
2015-06-01 20:08:55 -03:00
|
|
|
const char *frame;
|
2015-11-19 02:11:13 -04:00
|
|
|
bool use_time_sync = true;
|
2017-03-01 11:29:53 -04:00
|
|
|
float last_speedup = -1.0f;
|
2019-07-18 14:36:52 -03:00
|
|
|
const char *config_ = "";
|
2015-05-02 05:09:30 -03:00
|
|
|
|
2017-11-22 23:26:01 -04:00
|
|
|
// allow for AHRS_ORIENTATION
|
|
|
|
AP_Int8 *ahrs_orientation;
|
2019-12-11 20:11:03 -04:00
|
|
|
enum Rotation last_imu_rotation;
|
2020-01-13 13:47:52 -04:00
|
|
|
AP_Float* custom_roll;
|
|
|
|
AP_Float* custom_pitch;
|
|
|
|
AP_Float* custom_yaw;
|
2018-07-31 09:33:23 -03:00
|
|
|
|
2019-04-10 22:55:04 -03:00
|
|
|
enum GroundBehaviour {
|
2016-12-19 05:48:27 -04:00
|
|
|
GROUND_BEHAVIOR_NONE = 0,
|
2016-07-19 01:42:31 -03:00
|
|
|
GROUND_BEHAVIOR_NO_MOVEMENT,
|
|
|
|
GROUND_BEHAVIOR_FWD_ONLY,
|
2017-02-11 07:30:42 -04:00
|
|
|
GROUND_BEHAVIOR_TAILSITTER,
|
2016-07-19 01:42:31 -03:00
|
|
|
} ground_behavior;
|
2016-07-19 08:38:16 -03:00
|
|
|
|
|
|
|
bool use_smoothing;
|
2016-12-19 05:48:27 -04:00
|
|
|
|
2016-07-19 01:42:31 -03:00
|
|
|
AP_Terrain *terrain;
|
2016-11-22 20:09:33 -04:00
|
|
|
float ground_height_difference() const;
|
2016-07-19 01:42:31 -03:00
|
|
|
|
2017-03-06 13:52:47 -04:00
|
|
|
virtual bool on_ground() const;
|
2015-05-02 05:09:30 -03:00
|
|
|
|
2016-11-22 20:19:02 -04:00
|
|
|
// returns height above ground level in metres
|
2016-12-19 05:48:27 -04:00
|
|
|
float hagl() const; // metres
|
2016-11-22 20:19:02 -04:00
|
|
|
|
2015-05-02 05:09:30 -03:00
|
|
|
/* update location from position */
|
|
|
|
void update_position(void);
|
|
|
|
|
2016-06-17 00:46:12 -03:00
|
|
|
/* update body frame magnetic field */
|
|
|
|
void update_mag_field_bf(void);
|
|
|
|
|
2015-05-02 05:09:30 -03:00
|
|
|
/* advance time by deltat in seconds */
|
2017-03-03 06:23:40 -04:00
|
|
|
void time_advance();
|
2015-05-02 05:09:30 -03:00
|
|
|
|
|
|
|
/* setup the frame step time */
|
|
|
|
void setup_frame_time(float rate, float speedup);
|
|
|
|
|
|
|
|
/* adjust frame_time calculation */
|
|
|
|
void adjust_frame_time(float rate);
|
|
|
|
|
|
|
|
/* try to synchronise simulation time with wall clock time, taking
|
|
|
|
into account desired speedup */
|
|
|
|
void sync_frame_time(void);
|
|
|
|
|
|
|
|
/* add noise based on throttle level (from 0..1) */
|
|
|
|
void add_noise(float throttle);
|
|
|
|
|
2020-02-18 09:42:25 -04:00
|
|
|
/* return a monotonic wall clock time in microseconds */
|
2015-05-02 05:09:30 -03:00
|
|
|
uint64_t get_wall_time_us(void) const;
|
|
|
|
|
2016-01-01 00:11:55 -04:00
|
|
|
// update attitude and relative position
|
|
|
|
void update_dynamics(const Vector3f &rot_accel);
|
|
|
|
|
2016-04-19 22:48:37 -03:00
|
|
|
// update wind vector
|
|
|
|
void update_wind(const struct sitl_input &input);
|
2016-06-17 02:25:23 -03:00
|
|
|
|
2016-10-25 05:54:56 -03:00
|
|
|
// return filtered servo input as -1 to 1 range
|
|
|
|
float filtered_idx(float v, uint8_t idx);
|
|
|
|
float filtered_servo_angle(const struct sitl_input &input, uint8_t idx);
|
|
|
|
float filtered_servo_range(const struct sitl_input &input, uint8_t idx);
|
2016-12-19 05:48:27 -04:00
|
|
|
|
2017-10-20 21:57:59 -03:00
|
|
|
// extrapolate sensors by a given delta time in seconds
|
|
|
|
void extrapolate_sensors(float delta_time);
|
2018-07-31 09:33:23 -03:00
|
|
|
|
|
|
|
// update external payload/sensor dynamic
|
|
|
|
void update_external_payload(const struct sitl_input &input);
|
|
|
|
|
2017-11-27 06:45:23 -04:00
|
|
|
void add_shove_forces(Vector3f &rot_accel, Vector3f &body_accel);
|
2019-04-10 22:55:04 -03:00
|
|
|
void add_twist_forces(Vector3f &rot_accel);
|
2017-11-27 06:45:23 -04:00
|
|
|
|
2019-05-26 06:56:10 -03:00
|
|
|
// get local thermal updraft
|
|
|
|
float get_local_updraft(Vector3f currentPos);
|
|
|
|
|
2015-05-05 08:28:38 -03:00
|
|
|
private:
|
2020-09-22 19:44:18 -03:00
|
|
|
uint64_t last_time_us;
|
|
|
|
uint32_t frame_counter;
|
2016-01-04 16:20:05 -04:00
|
|
|
uint32_t last_ground_contact_ms;
|
2020-09-22 19:44:18 -03:00
|
|
|
#if defined(__CYGWIN__) || defined(__CYGWIN64__)
|
|
|
|
const uint32_t min_sleep_time{20000};
|
|
|
|
#else
|
|
|
|
const uint32_t min_sleep_time{5000};
|
|
|
|
#endif
|
2016-06-17 02:25:23 -03:00
|
|
|
|
2016-07-19 08:38:16 -03:00
|
|
|
struct {
|
|
|
|
Vector3f accel_body;
|
|
|
|
Vector3f gyro;
|
|
|
|
Matrix3f rotation_b2e;
|
|
|
|
Vector3f position;
|
|
|
|
Vector3f velocity_ef;
|
|
|
|
uint64_t last_update_us;
|
|
|
|
Location location;
|
|
|
|
} smoothing;
|
2016-10-25 05:54:56 -03:00
|
|
|
|
|
|
|
LowPassFilterFloat servo_filter[4];
|
2018-07-31 09:33:23 -03:00
|
|
|
|
2019-10-07 04:30:22 -03:00
|
|
|
Buzzer *buzzer;
|
2018-07-31 09:33:23 -03:00
|
|
|
Sprayer *sprayer;
|
|
|
|
Gripper_Servo *gripper;
|
|
|
|
Gripper_EPM *gripper_epm;
|
2019-01-08 21:39:44 -04:00
|
|
|
Parachute *parachute;
|
2019-09-28 08:32:57 -03:00
|
|
|
RichenPower *richenpower;
|
2018-08-03 07:52:20 -03:00
|
|
|
SIM_Precland *precland;
|
2020-08-03 00:24:27 -03:00
|
|
|
class I2C *i2c;
|
2015-05-02 05:09:30 -03:00
|
|
|
};
|
2015-05-02 07:28:57 -03:00
|
|
|
|
2015-10-22 10:04:42 -03:00
|
|
|
} // namespace SITL
|