SIM_JSBSim.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. simulator connection for JSBSim - https://github.com/JSBSim-Team/jsbsim
  15. */
  16. #pragma once
  17. #include <AP_HAL/utility/Socket.h>
  18. #include "SIM_Aircraft.h"
  19. namespace SITL {
  20. /*
  21. a Jsbsim simulator
  22. */
  23. class JSBSim : public Aircraft {
  24. public:
  25. JSBSim(const char *frame_str);
  26. /* update model by one time step */
  27. void update(const struct sitl_input &input) override;
  28. /* static object creator */
  29. static Aircraft *create(const char *frame_str) {
  30. return new JSBSim(frame_str);
  31. }
  32. private:
  33. // tcp input control socket to JSBSIm
  34. SocketAPM sock_control;
  35. // UDP packets from JSBSim in fgFDM format
  36. SocketAPM sock_fgfdm;
  37. bool initialised;
  38. uint16_t control_port;
  39. uint16_t fdm_port;
  40. char *jsbsim_script;
  41. char *jsbsim_fgout;
  42. int jsbsim_stdout;
  43. // default JSBSim model
  44. const char *jsbsim_model = "Rascal";
  45. bool created_templates;
  46. bool started_jsbsim;
  47. bool opened_control_socket;
  48. bool opened_fdm_socket;
  49. enum {
  50. FRAME_NORMAL,
  51. FRAME_ELEVON,
  52. FRAME_VTAIL
  53. } frame;
  54. bool create_templates(void);
  55. bool start_JSBSim(void);
  56. bool open_control_socket(void);
  57. bool open_fdm_socket(void);
  58. void send_servos(const struct sitl_input &input);
  59. void recv_fdm(const struct sitl_input &input);
  60. void check_stdout(void);
  61. bool expect(const char *str);
  62. void drain_control_socket();
  63. };
  64. /*
  65. FGNetFDM class from JSBSim
  66. */
  67. class FGNetFDM {
  68. public:
  69. enum {
  70. FG_MAX_ENGINES = 4,
  71. FG_MAX_WHEELS = 3,
  72. FG_MAX_TANKS = 4
  73. };
  74. uint32_t version; // increment when data values change
  75. uint32_t padding; // padding
  76. // Positions
  77. double longitude; // geodetic (radians)
  78. double latitude; // geodetic (radians)
  79. double altitude; // above sea level (meters)
  80. float agl; // above ground level (meters)
  81. float phi; // roll (radians)
  82. float theta; // pitch (radians)
  83. float psi; // yaw or true heading (radians)
  84. float alpha; // angle of attack (radians)
  85. float beta; // side slip angle (radians)
  86. // Velocities
  87. float phidot; // roll rate (radians/sec)
  88. float thetadot; // pitch rate (radians/sec)
  89. float psidot; // yaw rate (radians/sec)
  90. float vcas; // calibrated airspeed
  91. float climb_rate; // feet per second
  92. float v_north; // north velocity in local/body frame, fps
  93. float v_east; // east velocity in local/body frame, fps
  94. float v_down; // down/vertical velocity in local/body frame, fps
  95. float v_body_u; // ECEF velocity in body axis
  96. float v_body_v; // ECEF velocity in body axis
  97. float v_body_w; // ECEF velocity in body axis
  98. // Accelerations
  99. float A_X_pilot; // X accel in body frame ft/sec^2
  100. float A_Y_pilot; // Y accel in body frame ft/sec^2
  101. float A_Z_pilot; // Z accel in body frame ft/sec^2
  102. // Stall
  103. float stall_warning; // 0.0 - 1.0 indicating the amount of stall
  104. float slip_deg; // slip ball deflection
  105. // Pressure
  106. // Engine status
  107. uint32_t num_engines; // Number of valid engines
  108. uint32_t eng_state[FG_MAX_ENGINES]; // Engine state (off, cranking, running)
  109. float rpm[FG_MAX_ENGINES]; // Engine RPM rev/min
  110. float fuel_flow[FG_MAX_ENGINES]; // Fuel flow gallons/hr
  111. float fuel_px[FG_MAX_ENGINES]; // Fuel pressure psi
  112. float egt[FG_MAX_ENGINES]; // Exhuast gas temp deg F
  113. float cht[FG_MAX_ENGINES]; // Cylinder head temp deg F
  114. float mp_osi[FG_MAX_ENGINES]; // Manifold pressure
  115. float tit[FG_MAX_ENGINES]; // Turbine Inlet Temperature
  116. float oil_temp[FG_MAX_ENGINES]; // Oil temp deg F
  117. float oil_px[FG_MAX_ENGINES]; // Oil pressure psi
  118. // Consumables
  119. uint32_t num_tanks; // Max number of fuel tanks
  120. float fuel_quantity[FG_MAX_TANKS];
  121. // Gear status
  122. uint32_t num_wheels;
  123. uint32_t wow[FG_MAX_WHEELS];
  124. float gear_pos[FG_MAX_WHEELS];
  125. float gear_steer[FG_MAX_WHEELS];
  126. float gear_compression[FG_MAX_WHEELS];
  127. // Environment
  128. uint32_t cur_time; // current simulation time of JSBSim
  129. int32_t warp; // offset in seconds to unix time
  130. float visibility; // visibility in meters (for env. effects)
  131. // Control surface positions (normalized values)
  132. float elevator;
  133. float elevator_trim_tab;
  134. float left_flap;
  135. float right_flap;
  136. float left_aileron;
  137. float right_aileron;
  138. float rudder;
  139. float nose_wheel;
  140. float speedbrake;
  141. float spoilers;
  142. // nasty hack .... JSBSim sends in little-endian
  143. void ByteSwap(void);
  144. };
  145. } // namespace SITL