SIM_Aircraft.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. parent class for aircraft simulators
  15. */
  16. #pragma once
  17. #include <AP_Math/AP_Math.h>
  18. #include "SITL.h"
  19. #include "SITL_Input.h"
  20. #include <AP_Terrain/AP_Terrain.h>
  21. #include "SIM_Sprayer.h"
  22. #include "SIM_Gripper_Servo.h"
  23. #include "SIM_Gripper_EPM.h"
  24. #include "SIM_Parachute.h"
  25. #include "SIM_Precland.h"
  26. #include <Filter/Filter.h>
  27. namespace SITL {
  28. /*
  29. parent class for all simulator types
  30. */
  31. class Aircraft {
  32. public:
  33. Aircraft(const char *frame_str);
  34. // called directly after constructor:
  35. virtual void set_start_location(const Location &start_loc, const float start_yaw);
  36. /*
  37. set simulation speedup
  38. */
  39. void set_speedup(float speedup);
  40. /*
  41. set instance number
  42. */
  43. void set_instance(uint8_t _instance) {
  44. instance = _instance;
  45. }
  46. /*
  47. set directory for additional files such as aircraft models
  48. */
  49. void set_autotest_dir(const char *_autotest_dir) {
  50. autotest_dir = _autotest_dir;
  51. }
  52. /* Create and set in/out socket for extenal simulator */
  53. virtual void set_interface_ports(const char* address, const int port_in, const int port_out) {};
  54. /*
  55. step the FDM by one time step
  56. */
  57. virtual void update(const struct sitl_input &input) = 0;
  58. void update_model(const struct sitl_input &input);
  59. /* fill a sitl_fdm structure from the simulator state */
  60. void fill_fdm(struct sitl_fdm &fdm);
  61. /* smooth sensors to provide kinematic consistancy */
  62. void smooth_sensors(void);
  63. /* return normal distribution random numbers */
  64. static double rand_normal(double mean, double stddev);
  65. // get frame rate of model in Hz
  66. float get_rate_hz(void) const { return rate_hz; }
  67. const Vector3f &get_gyro(void) const {
  68. return gyro;
  69. }
  70. const Vector3f &get_velocity_ef(void) const {
  71. return velocity_ef;
  72. }
  73. const Vector3f &get_velocity_air_ef(void) const {
  74. return velocity_air_ef;
  75. }
  76. const Matrix3f &get_dcm(void) const {
  77. return dcm;
  78. }
  79. const Vector3f &get_mag_field_bf(void) const {
  80. return mag_bf;
  81. }
  82. float gross_mass() const { return mass + external_payload_mass; }
  83. virtual void set_config(const char* config) {
  84. config_ = config;
  85. }
  86. const Location &get_location() const { return location; }
  87. const Vector3f &get_position() const { return position; }
  88. void get_attitude(Quaternion &attitude) const {
  89. attitude.from_rotation_matrix(dcm);
  90. }
  91. const Location &get_home() const { return home; }
  92. float get_home_yaw() const { return home_yaw; }
  93. void set_sprayer(Sprayer *_sprayer) { sprayer = _sprayer; }
  94. void set_parachute(Parachute *_parachute) { parachute = _parachute; }
  95. void set_gripper_servo(Gripper_Servo *_gripper) { gripper = _gripper; }
  96. void set_gripper_epm(Gripper_EPM *_gripper_epm) { gripper_epm = _gripper_epm; }
  97. void set_precland(SIM_Precland *_precland);
  98. protected:
  99. SITL *sitl;
  100. Location home;
  101. bool home_is_set;
  102. Location location;
  103. float ground_level;
  104. float home_yaw;
  105. float frame_height;
  106. Matrix3f dcm; // rotation matrix, APM conventions, from body to earth
  107. Vector3f gyro; // rad/s
  108. Vector3f gyro_prev; // rad/s
  109. Vector3f ang_accel; // rad/s/s
  110. Vector3f velocity_ef; // m/s, earth frame
  111. Vector3f wind_ef; // m/s, earth frame
  112. Vector3f velocity_air_ef; // velocity relative to airmass, earth frame
  113. Vector3f velocity_air_bf; // velocity relative to airmass, body frame
  114. Vector3f position; // meters, NED from origin
  115. float mass; // kg
  116. float external_payload_mass = 0.0f; // kg
  117. Vector3f accel_body; // m/s/s NED, body frame
  118. float airspeed; // m/s, apparent airspeed
  119. float airspeed_pitot; // m/s, apparent airspeed, as seen by fwd pitot tube
  120. float battery_voltage = -1.0f;
  121. float battery_current = 0.0f;
  122. float rpm1 = 0;
  123. float rpm2 = 0;
  124. uint8_t rcin_chan_count = 0;
  125. float rcin[8];
  126. float range = -1.0f; // rangefinder detection in m
  127. struct {
  128. // data from simulated laser scanner, if available
  129. struct vector3f_array points;
  130. struct float_array ranges;
  131. } scanner;
  132. // Wind Turbulence simulated Data
  133. float turbulence_azimuth = 0.0f;
  134. float turbulence_horizontal_speed = 0.0f; // m/s
  135. float turbulence_vertical_speed = 0.0f; // m/s
  136. Vector3f mag_bf; // local earth magnetic field vector in Gauss, earth frame
  137. uint64_t time_now_us;
  138. const float gyro_noise;
  139. const float accel_noise;
  140. float rate_hz;
  141. float achieved_rate_hz;
  142. float target_speedup;
  143. uint64_t frame_time_us;
  144. float scaled_frame_time_us;
  145. uint64_t last_wall_time_us;
  146. uint8_t instance;
  147. const char *autotest_dir;
  148. const char *frame;
  149. bool use_time_sync = true;
  150. float last_speedup = -1.0f;
  151. const char *config_ = "";
  152. // allow for AHRS_ORIENTATION
  153. AP_Int8 *ahrs_orientation;
  154. enum GroundBehaviour {
  155. GROUND_BEHAVIOR_NONE = 0,
  156. GROUND_BEHAVIOR_NO_MOVEMENT,
  157. GROUND_BEHAVIOR_FWD_ONLY,
  158. GROUND_BEHAVIOR_TAILSITTER,
  159. } ground_behavior;
  160. bool use_smoothing;
  161. AP_Terrain *terrain;
  162. float ground_height_difference() const;
  163. virtual bool on_ground() const;
  164. // returns height above ground level in metres
  165. float hagl() const; // metres
  166. /* update location from position */
  167. void update_position(void);
  168. /* update body frame magnetic field */
  169. void update_mag_field_bf(void);
  170. /* advance time by deltat in seconds */
  171. void time_advance();
  172. /* setup the frame step time */
  173. void setup_frame_time(float rate, float speedup);
  174. /* adjust frame_time calculation */
  175. void adjust_frame_time(float rate);
  176. /* try to synchronise simulation time with wall clock time, taking
  177. into account desired speedup */
  178. void sync_frame_time(void);
  179. /* add noise based on throttle level (from 0..1) */
  180. void add_noise(float throttle);
  181. /* return wall clock time in microseconds since 1970 */
  182. uint64_t get_wall_time_us(void) const;
  183. // update attitude and relative position
  184. void update_dynamics(const Vector3f &rot_accel);
  185. // update wind vector
  186. void update_wind(const struct sitl_input &input);
  187. // return filtered servo input as -1 to 1 range
  188. float filtered_idx(float v, uint8_t idx);
  189. float filtered_servo_angle(const struct sitl_input &input, uint8_t idx);
  190. float filtered_servo_range(const struct sitl_input &input, uint8_t idx);
  191. // extrapolate sensors by a given delta time in seconds
  192. void extrapolate_sensors(float delta_time);
  193. // update external payload/sensor dynamic
  194. void update_external_payload(const struct sitl_input &input);
  195. void add_shove_forces(Vector3f &rot_accel, Vector3f &body_accel);
  196. void add_twist_forces(Vector3f &rot_accel);
  197. private:
  198. uint64_t last_time_us = 0;
  199. uint32_t frame_counter = 0;
  200. uint32_t last_ground_contact_ms;
  201. const uint32_t min_sleep_time;
  202. struct {
  203. bool enabled;
  204. Vector3f accel_body;
  205. Vector3f gyro;
  206. Matrix3f rotation_b2e;
  207. Vector3f position;
  208. Vector3f velocity_ef;
  209. uint64_t last_update_us;
  210. Location location;
  211. } smoothing;
  212. LowPassFilterFloat servo_filter[4];
  213. Sprayer *sprayer;
  214. Gripper_Servo *gripper;
  215. Gripper_EPM *gripper_epm;
  216. Parachute *parachute;
  217. SIM_Precland *precland;
  218. };
  219. } // namespace SITL