| 1 | // AISim.cxx -- interface to the AI Sim |
| 2 | // |
| 3 | // Written by Erik Hofman, started November 2016 |
| 4 | // |
| 5 | // Copyright (C) 2016-2020 by Erik Hofman <[email protected]> |
| 6 | // |
| 7 | // |
| 8 | // This program is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU General Public License as |
| 10 | // published by the Free Software Foundation; either version 2 of the |
| 11 | // License, or (at your option) any later version. |
| 12 | // |
| 13 | // This program is distributed in the hope that it will be useful, but |
| 14 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this program; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | // |
| 22 | |
| 23 | |
| 24 | #ifndef _FGAISim_HXX |
| 25 | #define _FGAISim_HXX |
| 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | # include <config.h> |
| 29 | #endif |
| 30 | |
| 31 | #include <string> |
| 32 | #include <cmath> |
| 33 | #include <map> |
| 34 | |
| 35 | #ifdef ENABLE_SP_FDM |
| 36 | # include <simgear/math/simd.hxx> |
| 37 | # include <simgear/constants.h> |
| 38 | # include <FDM/flight.hxx> |
| 39 | #else |
| 40 | # include "simd.hxx" |
| 41 | # include "simd4x4.hxx" |
| 42 | # define SG_METER_TO_FEET 3.2808399 |
| 43 | # define SG_FEET_TO_METER (1/SG_METER_TO_FEET) |
| 44 | # define SGD_DEGREES_TO_RADIANS 0.0174532925 |
| 45 | # define SGD_RADIANS_TO_DEGREES (1/SGD_DEGREES_TO_RADIANS) |
| 46 | # define SGD_PI 3.1415926536 |
| 47 | #endif |
| 48 | |
| 49 | // #define SG_DEGREES_TO_RADIANS 0.0174532925f |
| 50 | |
| 51 | // max. no. gears, max. no. engines |
| 52 | #define AISIM_MAX 4 |
| 53 | #define AISIM_G 32.174f |
| 54 | |
| 55 | #ifndef _MINMAX |
| 56 | # define _MINMAX(a,b,c) (((a)>(c)) ? (c) : (((a)<(b)) ? (b) : (a))) |
| 57 | #endif |
| 58 | |
| 59 | class FGAISim |
| 60 | #ifdef ENABLE_SP_FDM |
| 61 | : public FGInterface |
| 62 | #endif |
| 63 | { |
| 64 | private: |
| 65 | enum { X=0, Y=1, Z=2 }; |
| 66 | enum { XX=0, YY=1, ZZ=2, XZ=3 }; |
| 67 | enum { LATITUDE=0, LONGITUDE=1, ALTITUDE=2 }; |
| 68 | enum { NORTH=0, EAST=1, DOWN=2 }; |
| 69 | enum { LEFT=0, RIGHT=1, UP=2 }; |
| 70 | enum { MAX=0, VELOCITY=1, PROPULSION=2 }; |
| 71 | enum { FLAPS=2, RUDDER=2, MIN=3, AILERON=3, ELEVATOR=3 }; |
| 72 | enum { DRAG=0, SIDE=1, LIFT=2 }; |
| 73 | enum { ROLL=0, PITCH=1, YAW=2, THRUST=3 }; |
| 74 | enum { PHI=0, THETA, PSI }; |
| 75 | enum { ALPHA=0, BETA=1 }; |
| 76 | enum { P=0, Q=1, R=2 }; |
| 77 | enum { U=0, V=1, W=2 }; |
| 78 | |
| 79 | |
| 80 | using aiVec2 = simd4_t<float,2>; |
| 81 | using aiVec3d = simd4_t<double,3>; |
| 82 | using aiVec3 = simd4_t<float,3>; |
| 83 | using aiVec4 = simd4_t<float,4>; |
| 84 | using aiMtx4 = simd4x4_t<float,4>; |
| 85 | |
| 86 | using jsonMap = std::map<std::string,float>; |
| 87 | |
| 88 | public: |
| 89 | FGAISim(double dt); |
| 90 | ~FGAISim(); |
| 91 | |
| 92 | // Subsystem API. |
| 93 | void init() override; |
| 94 | void update(double dt) override; |
| 95 | |
| 96 | // Subsystem identification. |
| 97 | static const char* staticSubsystemClassId() { return "aisim"; } |
| 98 | |
| 99 | bool load(std::string path); |
| 100 | |
| 101 | #ifdef ENABLE_SP_FDM |
| 102 | // copy FDM state to AISim structures |
| 103 | bool copy_to_AISim(); |
| 104 | |
| 105 | // copy AISim structures to FDM state |
| 106 | bool copy_from_AISim(); |
| 107 | #endif |
| 108 | |
| 109 | /* controls */ |
| 110 | inline void set_rudder_norm(float f) { |
| 111 | xCDYLT.ptr()[RUDDER][SIDE] = CYdr_n*f; |
| 112 | xClmnT.ptr()[RUDDER][ROLL] = Cldr_n*f; |
| 113 | xClmnT.ptr()[RUDDER][YAW] = -Cndr_n*f; |
| 114 | } |
| 115 | inline void set_elevator_norm(float f) { |
| 116 | xClmnT.ptr()[ELEVATOR][PITCH] = Cmde_n*f; |
| 117 | } |
| 118 | inline void set_aileron_norm(float f) { |
| 119 | xClmnT.ptr()[AILERON][ROLL] = Clda_n*f; |
| 120 | xClmnT.ptr()[AILERON][YAW] = Cnda_n*f; |
| 121 | } |
| 122 | inline void set_flaps_norm(float f) { |
| 123 | xCDYLT.ptr()[FLAPS][LIFT] = -CLdf_n*f; |
| 124 | xCDYLT.ptr()[FLAPS][DRAG] = -CDdf_n*std::abs(f); |
| 125 | xClmnT.ptr()[FLAPS][PITCH] = Cmdf_n*f; |
| 126 | } |
| 127 | inline void set_throttle_norm(float f) { throttle = f; } |
| 128 | inline void set_brake_norm(float f) { mu_body[0] = -0.02f-0.7f*f; } |
| 129 | |
| 130 | /* (initial) state, local frame */ |
| 131 | inline void set_location_geod(aiVec3d& p) { |
| 132 | location_geod = p; |
| 133 | } |
| 134 | inline void set_location_geod(double lat, double lon, double alt) { |
| 135 | location_geod = aiVec3d(lat, lon, alt); |
| 136 | } |
| 137 | inline void set_altitude_asl_ft(float f) { location_geod[ALTITUDE] = f; }; |
| 138 | inline void set_altitude_agl_ft(float f) { cg_agl = f; } |
| 139 | |
| 140 | inline void set_euler_angles_rad(const aiVec3& e) { |
| 141 | euler = e; |
| 142 | } |
| 143 | inline void set_euler_angles_rad(float phi, float theta, float psi) { |
| 144 | euler = aiVec3(phi, theta, psi); |
| 145 | } |
| 146 | inline void set_roll_rad(float f) { euler[PHI] = f; } |
| 147 | inline void set_pitch_rad(float f) { euler[THETA] = f; } |
| 148 | inline void set_heading_rad(float f) { euler[PSI] = f; } |
| 149 | |
| 150 | void set_velocity_fps(const aiVec3& v) { vUVW = v; } |
| 151 | void set_velocity_fps(float u, float v, float w) { |
| 152 | vUVW = aiVec3(u, v, w); |
| 153 | } |
| 154 | void set_velocity_fps(float u) { |
| 155 | vUVW = aiVec3(u, 0.0f, 0.0f); |
| 156 | } |
| 157 | |
| 158 | inline void set_wind_ned_fps(const aiVec3& w) { wind_ned = w; } |
| 159 | inline void set_wind_ned_fps(float n, float e, float d) { |
| 160 | wind_ned = aiVec3(n, e, d); |
| 161 | } |
| 162 | |
| 163 | inline void set_alpha_rad(float f) { |
| 164 | f = _MINMAX(f, -0.25f, 0.25f); // -14 to 14 degrees |
| 165 | xCDYLT.ptr()[ALPHA][LIFT] = -CLa*f; |
| 166 | xCDYLT.ptr()[ALPHA][DRAG] = -CDa*std::abs(f); |
| 167 | xClmnT.ptr()[ALPHA][PITCH] = Cma*f; |
| 168 | AOA[ALPHA] = f; |
| 169 | } |
| 170 | inline void set_beta_rad(float f) { |
| 171 | f = _MINMAX(f, -0.30f, 0.30f); // -17 to 17 degrees |
| 172 | xCDYLT.ptr()[BETA][DRAG] = -CDb*std::abs(f); |
| 173 | xCDYLT.ptr()[BETA][SIDE] = CYb*f; |
| 174 | xClmnT.ptr()[BETA][ROLL] = Clb*f; |
| 175 | xClmnT.ptr()[BETA][YAW] = Cnb*f; |
| 176 | AOA[BETA] = f; |
| 177 | } |
| 178 | inline float get_alpha_rad() { |
| 179 | return AOA[ALPHA]; |
| 180 | } |
| 181 | inline float get_beta_rad() { |
| 182 | return AOA[BETA]; |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | std::map<std::string,float> jsonParse(const char *str); |
| 187 | void update_velocity(float v); |
| 188 | aiMtx4 matrix_inverse(aiMtx4 mtx); |
| 189 | aiMtx4 invert_inertia(aiMtx4 mtx); |
| 190 | void struct_to_body(aiVec3 &pos); |
| 191 | |
| 192 | /* aircraft normalized controls */ |
| 193 | float throttle = 0.0f; /* throttle command */ |
| 194 | |
| 195 | /* aircraft state */ |
| 196 | aiVec3d location_geod = 0.0; /* lat, lon, altitude */ |
| 197 | aiVec3 XYZdot = 0.0f; /* local body accelrations */ |
| 198 | aiVec3 vNED = 0.0f; /* North, East, Down velocity */ |
| 199 | aiVec3 vUVW = 0.0f; /* fwd, side, down velocity */ |
| 200 | aiVec3 vUVWdot = 0.0f; /* fwd, side, down acceleration */ |
| 201 | aiVec3 vPQR = 0.0f; /* roll, pitch, yaw rate */ |
| 202 | aiVec3 vPQRdot = 0.0f; /* roll, pitch, yaw acceleration */ |
| 203 | aiVec3 AOA = 0.0f; /* alpha, beta */ |
| 204 | aiVec3 AOAdot = 0.0f; /* adot, bdot */ |
| 205 | aiVec3 euler = 0.0f; /* phi, theta, psi */ |
| 206 | aiVec3 euler_dot = 0.0f; /* change in phi, theta, psi */ |
| 207 | aiVec3 wind_ned = 0.0f; /* wind north, east, down */ |
| 208 | aiVec3 mu_body = { -0.02f, -0.8f, 0.0f }; |
| 209 | |
| 210 | /* ---------------------------------------------------------------- */ |
| 211 | /* This should reduce the time spent in update() since controls */ |
| 212 | /* change less often than the update function runs which might */ |
| 213 | /* run 20 to 60 times (or more) per second */ |
| 214 | |
| 215 | /* cache */ |
| 216 | aiVec3 vUVWaero = 0.0f; /* airmass relative to the body */ |
| 217 | aiVec3 FT[AISIM_MAX]; /* thrust force */ |
| 218 | aiVec3 MT[AISIM_MAX]; /* thrust moment */ |
| 219 | float n2[AISIM_MAX]; |
| 220 | aiVec3 b_2U = 0.0f; |
| 221 | aiVec3 cbar_2U = 0.0f; |
| 222 | aiVec3 inv_mass; |
| 223 | float cg_agl = 0.0f; |
| 224 | float alpha = 0.0f; |
| 225 | float beta = 0.0f; |
| 226 | float velocity = 0.0f; |
| 227 | float mach = 0.0f; |
| 228 | double sl_radius = 0.0; |
| 229 | bool WoW = true; |
| 230 | |
| 231 | /* dynamic coefficients (already multiplied with their value) */ |
| 232 | aiVec3 xCq = 0.0f; |
| 233 | aiVec3 xCadot = 0.0f; |
| 234 | aiVec3 xCp = 0.0f; |
| 235 | aiVec3 xCr = 0.0f; |
| 236 | aiMtx4 xCDYLT; |
| 237 | aiMtx4 xClmnT; |
| 238 | aiVec4 Coef2Force; |
| 239 | aiVec4 Coef2Moment; |
| 240 | |
| 241 | /* ---------------------------------------------------------------- */ |
| 242 | /* aircraft static data */ |
| 243 | size_t no_engines = 0; |
| 244 | size_t no_contacts = 0; |
| 245 | aiMtx4 mJ, mJinv; /* inertia matrix */ |
| 246 | aiVec3 contact_pos[AISIM_MAX+1]; /* pos in structural frame */ |
| 247 | aiVec3 mass = 0.0f; /* mass */ |
| 248 | aiVec3 cg = 0.0f; /* center of gravity */ |
| 249 | aiVec4 I = 0.0f; /* inertia */ |
| 250 | float Sw = 0.0f; /* wing area */ |
| 251 | float cbar = 0.0f; /* mean average chord */ |
| 252 | float span = 0.0f; /* wing span */ |
| 253 | |
| 254 | /* static coefficients, *_n is for normalized surface deflection */ |
| 255 | float contact_spring[AISIM_MAX]; /* contact spring coeffients */ |
| 256 | float contact_damp[AISIM_MAX]; /* contact damping coefficients */ |
| 257 | float CLmin, CLa, CLadot, CLq, CLdf_n; |
| 258 | float CDmin, CDa, CDb, CDi, CDdf_n; |
| 259 | float CYb, CYp, CYr, CYdr_n; |
| 260 | float Clb, Clp, Clr, Clda_n, Cldr_n; |
| 261 | float Cma, Cmadot, Cmq, Cmde_n, Cmdf_n; |
| 262 | float Cnb, Cnp, Cnr, Cnda_n, Cndr_n; |
| 263 | |
| 264 | /* environment data */ |
| 265 | static float density[101], vsound[101]; |
| 266 | aiVec3 gravity_ned = { 0.0f, 0.0f, AISIM_G }; |
| 267 | //size_t alt_idx = -1; |
| 268 | float rho = 0.0f; |
| 269 | float qbar = 0.0f; |
| 270 | float sigma = 0.0f; |
| 271 | }; |
| 272 | |
| 273 | #endif // _FGAISim_HXX |
| 274 |