APM:Libraries
SIM_JSBSim.h
Go to the documentation of this file.
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 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 /*
16  simulator connection for ardupilot version of JSBSim
17 */
18 
19 #pragma once
20 
21 #include <AP_HAL/utility/Socket.h>
22 
23 #include "SIM_Aircraft.h"
24 
25 namespace SITL {
26 
27 /*
28  a Jsbsim simulator
29  */
30 class JSBSim : public Aircraft {
31 public:
32  JSBSim(const char *home_str, const char *frame_str);
33 
34  /* update model by one time step */
35  void update(const struct sitl_input &input);
36 
37  /* static object creator */
38  static Aircraft *create(const char *home_str, const char *frame_str) {
39  return new JSBSim(home_str, frame_str);
40  }
41 
42 private:
43  // tcp input control socket to JSBSIm
44  SocketAPM sock_control;
45 
46  // UDP packets from JSBSim in fgFDM format
47  SocketAPM sock_fgfdm;
48 
50 
51  uint16_t control_port;
52  uint16_t fdm_port;
54  char *jsbsim_fgout;
56 
57  // default JSBSim model
58  const char *jsbsim_model = "Rascal";
59 
64 
65  enum {
69  } frame;
70 
71  bool create_templates(void);
72  bool start_JSBSim(void);
73  bool open_control_socket(void);
74  bool open_fdm_socket(void);
75  void send_servos(const struct sitl_input &input);
76  void recv_fdm(const struct sitl_input &input);
77  void check_stdout(void);
78  bool expect(const char *str);
79 
80  void drain_control_socket();
81 };
82 
83 /*
84  FGNetFDM class from JSBSim
85  */
86 class FGNetFDM {
87 public:
88 
89  enum {
90  FG_MAX_ENGINES = 4,
91  FG_MAX_WHEELS = 3,
92  FG_MAX_TANKS = 4
93  };
94 
95  uint32_t version; // increment when data values change
96  uint32_t padding; // padding
97 
98  // Positions
99  double longitude; // geodetic (radians)
100  double latitude; // geodetic (radians)
101  double altitude; // above sea level (meters)
102  float agl; // above ground level (meters)
103  float phi; // roll (radians)
104  float theta; // pitch (radians)
105  float psi; // yaw or true heading (radians)
106  float alpha; // angle of attack (radians)
107  float beta; // side slip angle (radians)
108 
109  // Velocities
110  float phidot; // roll rate (radians/sec)
111  float thetadot; // pitch rate (radians/sec)
112  float psidot; // yaw rate (radians/sec)
113  float vcas; // calibrated airspeed
114  float climb_rate; // feet per second
115  float v_north; // north velocity in local/body frame, fps
116  float v_east; // east velocity in local/body frame, fps
117  float v_down; // down/vertical velocity in local/body frame, fps
118  float v_wind_body_north; // north velocity in local/body frame
119  // relative to local airmass, fps
120  float v_wind_body_east; // east velocity in local/body frame
121  // relative to local airmass, fps
122  float v_wind_body_down; // down/vertical velocity in local/body
123  // frame relative to local airmass, fps
124 
125  // Accelerations
126  float A_X_pilot; // X accel in body frame ft/sec^2
127  float A_Y_pilot; // Y accel in body frame ft/sec^2
128  float A_Z_pilot; // Z accel in body frame ft/sec^2
129 
130  // Stall
131  float stall_warning; // 0.0 - 1.0 indicating the amount of stall
132  float slip_deg; // slip ball deflection
133 
134  // Pressure
135 
136  // Engine status
137  uint32_t num_engines; // Number of valid engines
138  uint32_t eng_state[FG_MAX_ENGINES];// Engine state (off, cranking, running)
139  float rpm[FG_MAX_ENGINES]; // Engine RPM rev/min
140  float fuel_flow[FG_MAX_ENGINES]; // Fuel flow gallons/hr
141  float fuel_px[FG_MAX_ENGINES]; // Fuel pressure psi
142  float egt[FG_MAX_ENGINES]; // Exhuast gas temp deg F
143  float cht[FG_MAX_ENGINES]; // Cylinder head temp deg F
144  float mp_osi[FG_MAX_ENGINES]; // Manifold pressure
145  float tit[FG_MAX_ENGINES]; // Turbine Inlet Temperature
146  float oil_temp[FG_MAX_ENGINES]; // Oil temp deg F
147  float oil_px[FG_MAX_ENGINES]; // Oil pressure psi
148 
149  // Consumables
150  uint32_t num_tanks; // Max number of fuel tanks
151  float fuel_quantity[FG_MAX_TANKS];
152 
153  // Gear status
154  uint32_t num_wheels;
155  uint32_t wow[FG_MAX_WHEELS];
156  float gear_pos[FG_MAX_WHEELS];
157  float gear_steer[FG_MAX_WHEELS];
158  float gear_compression[FG_MAX_WHEELS];
159 
160  // Environment
161  uint32_t cur_time; // current unix time
162  // FIXME: make this uint64_t before 2038
163  int32_t warp; // offset in seconds to unix time
164  float visibility; // visibility in meters (for env. effects)
165 
166  // Control surface positions (normalized values)
167  float elevator;
169  float left_flap;
170  float right_flap;
173  float rudder;
174  float nose_wheel;
175  float speedbrake;
176  float spoilers;
177 
178  void ByteSwap(void);
179 };
180 
181 } // namespace SITL
bool create_templates(void)
Definition: SIM_JSBSim.cpp:68
uint32_t version
Definition: SIM_JSBSim.h:95
float v_wind_body_down
Definition: SIM_JSBSim.h:122
int jsbsim_stdout
Definition: SIM_JSBSim.h:55
uint32_t num_tanks
Definition: SIM_JSBSim.h:150
bool opened_control_socket
Definition: SIM_JSBSim.h:62
SocketAPM sock_fgfdm
Definition: SIM_JSBSim.h:47
static Aircraft * create(const char *home_str, const char *frame_str)
Definition: SIM_JSBSim.h:38
float v_wind_body_east
Definition: SIM_JSBSim.h:120
void recv_fdm(const struct sitl_input &input)
Definition: SIM_JSBSim.cpp:399
bool created_templates
Definition: SIM_JSBSim.h:60
float left_aileron
Definition: SIM_JSBSim.h:171
double latitude
Definition: SIM_JSBSim.h:100
float speedbrake
Definition: SIM_JSBSim.h:175
float stall_warning
Definition: SIM_JSBSim.h:131
float climb_rate
Definition: SIM_JSBSim.h:114
bool start_JSBSim(void)
Definition: SIM_JSBSim.cpp:166
double longitude
Definition: SIM_JSBSim.h:99
uint16_t fdm_port
Definition: SIM_JSBSim.h:52
uint32_t padding
Definition: SIM_JSBSim.h:96
char * jsbsim_script
Definition: SIM_JSBSim.h:53
void send_servos(const struct sitl_input &input)
Definition: SIM_JSBSim.cpp:322
void update(const struct sitl_input &input)
Definition: SIM_JSBSim.cpp:455
void check_stdout(void)
Definition: SIM_JSBSim.cpp:242
bool opened_fdm_socket
Definition: SIM_JSBSim.h:63
uint32_t num_wheels
Definition: SIM_JSBSim.h:154
bool open_fdm_socket(void)
Definition: SIM_JSBSim.cpp:303
int32_t warp
Definition: SIM_JSBSim.h:163
float nose_wheel
Definition: SIM_JSBSim.h:174
float right_flap
Definition: SIM_JSBSim.h:170
double altitude
Definition: SIM_JSBSim.h:101
float visibility
Definition: SIM_JSBSim.h:164
char * jsbsim_fgout
Definition: SIM_JSBSim.h:54
void drain_control_socket()
Definition: SIM_JSBSim.cpp:435
const char * jsbsim_model
Definition: SIM_JSBSim.h:58
JSBSim(const char *home_str, const char *frame_str)
Definition: SIM_JSBSim.cpp:39
bool expect(const char *str)
Definition: SIM_JSBSim.cpp:256
float right_aileron
Definition: SIM_JSBSim.h:172
uint32_t cur_time
Definition: SIM_JSBSim.h:161
float elevator_trim_tab
Definition: SIM_JSBSim.h:168
uint32_t num_engines
Definition: SIM_JSBSim.h:137
bool open_control_socket(void)
Definition: SIM_JSBSim.cpp:279
bool started_jsbsim
Definition: SIM_JSBSim.h:61
float v_wind_body_north
Definition: SIM_JSBSim.h:118
SocketAPM sock_control
Definition: SIM_JSBSim.h:44
uint16_t control_port
Definition: SIM_JSBSim.h:51
bool initialised
Definition: SIM_JSBSim.h:49
enum SITL::JSBSim::@206 frame