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"
|
2016-07-19 01:42:31 -03:00
|
|
|
#include <AP_Terrain/AP_Terrain.h>
|
2015-10-22 10:58:33 -03:00
|
|
|
|
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 {
|
2016-11-22 20:50:01 -04:00
|
|
|
friend class Gripper_Servo;
|
|
|
|
|
2015-05-02 05:09:30 -03:00
|
|
|
public:
|
2015-05-03 04:47:58 -03:00
|
|
|
Aircraft(const char *home_str, const char *frame_str);
|
2015-05-02 05:09:30 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
structure passed in giving servo positions as PWM values in
|
|
|
|
microseconds
|
|
|
|
*/
|
|
|
|
struct sitl_input {
|
|
|
|
uint16_t servos[16];
|
2015-05-22 22:03:57 -03:00
|
|
|
struct {
|
|
|
|
float speed; // m/s
|
|
|
|
float direction; // degrees 0..360
|
2015-06-28 22:25:59 -03:00
|
|
|
float turbulence;
|
2018-01-30 12:38:36 -04:00
|
|
|
float dir_z; //degrees -90..90
|
2015-05-22 22:03:57 -03:00
|
|
|
} wind;
|
2015-05-02 05:09:30 -03:00
|
|
|
};
|
|
|
|
|
2015-05-02 08:41:33 -03:00
|
|
|
/*
|
|
|
|
set simulation speedup
|
|
|
|
*/
|
|
|
|
void set_speedup(float speedup);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* parse a home location string */
|
|
|
|
static bool parse_home(const char *home_str, Location &loc, float &yaw_degrees);
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
virtual float gross_mass() const { return mass; }
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
void get_attitude(Quaternion &attitude) const {
|
|
|
|
attitude.from_rotation_matrix(dcm);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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;
|
2017-03-01 11:29:53 -04:00
|
|
|
Matrix3f dcm; // rotation matrix, APM conventions, from body to earth
|
|
|
|
Vector3f gyro; // rad/s
|
|
|
|
Vector3f gyro_prev; // rad/s
|
|
|
|
Vector3f ang_accel; // rad/s/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
|
|
|
|
Vector3f accel_body; // m/s/s NED, body frame
|
|
|
|
float airspeed; // m/s, apparent airspeed
|
|
|
|
float airspeed_pitot; // m/s, apparent airspeed, as seen by fwd pitot tube
|
|
|
|
float battery_voltage = -1.0f;
|
|
|
|
float battery_current = 0.0f;
|
2015-11-22 22:46:01 -04:00
|
|
|
float rpm1 = 0;
|
|
|
|
float rpm2 = 0;
|
2016-05-03 23:49:42 -03:00
|
|
|
uint8_t rcin_chan_count = 0;
|
|
|
|
float rcin[8];
|
2017-03-01 11:29:53 -04:00
|
|
|
float range = -1.0f; // rangefinder detection in m
|
2015-05-02 05:09:30 -03:00
|
|
|
|
2016-12-19 05:48:27 -04:00
|
|
|
// Wind Turbulence simulated Data
|
2017-03-01 11:29:53 -04:00
|
|
|
float turbulence_azimuth = 0.0f;
|
|
|
|
float turbulence_horizontal_speed = 0.0f; // m/s
|
|
|
|
float turbulence_vertical_speed = 0.0f; // 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;
|
|
|
|
|
|
|
|
const float gyro_noise;
|
|
|
|
const float accel_noise;
|
|
|
|
float rate_hz;
|
|
|
|
float achieved_rate_hz;
|
|
|
|
float target_speedup;
|
2015-05-02 09:13:02 -03:00
|
|
|
uint64_t frame_time_us;
|
2015-05-02 05:09:30 -03:00
|
|
|
float scaled_frame_time_us;
|
|
|
|
uint64_t last_wall_time_us;
|
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;
|
2015-05-02 05:09:30 -03:00
|
|
|
|
2017-11-22 23:26:01 -04:00
|
|
|
// allow for AHRS_ORIENTATION
|
|
|
|
AP_Int8 *ahrs_orientation;
|
|
|
|
|
2016-07-19 01:42:31 -03:00
|
|
|
enum {
|
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
|
|
|
|
2016-06-17 09:03:00 -03:00
|
|
|
const float FEET_TO_METERS = 0.3048f;
|
|
|
|
const float KNOTS_TO_METERS_PER_SECOND = 0.51444f;
|
2016-11-22 20:09:33 -04: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);
|
|
|
|
|
|
|
|
/* return wall clock time in microseconds since 1970 */
|
|
|
|
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);
|
|
|
|
|
2015-05-05 08:28:38 -03:00
|
|
|
private:
|
2015-07-05 20:21:49 -03:00
|
|
|
uint64_t last_time_us = 0;
|
|
|
|
uint32_t frame_counter = 0;
|
2016-01-04 16:20:05 -04:00
|
|
|
uint32_t last_ground_contact_ms;
|
2015-05-25 03:48:13 -03:00
|
|
|
const uint32_t min_sleep_time;
|
2016-06-17 02:25:23 -03:00
|
|
|
|
2016-07-19 08:38:16 -03:00
|
|
|
struct {
|
|
|
|
bool enabled;
|
|
|
|
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];
|
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
|