| 1 | #ifndef _AIRPLANE_HPP |
| 2 | #define _AIRPLANE_HPP |
| 3 | |
| 4 | #include "ControlMap.hpp" |
| 5 | #include "Model.hpp" |
| 6 | #include "Wing.hpp" |
| 7 | #include "Rotor.hpp" |
| 8 | #include "Vector.hpp" |
| 9 | #include "Version.hpp" |
| 10 | #include <simgear/props/props.hxx> |
| 11 | |
| 12 | namespace yasim { |
| 13 | |
| 14 | class Gear; |
| 15 | class Hook; |
| 16 | class Launchbar; |
| 17 | class Thruster; |
| 18 | class Hitch; |
| 19 | |
| 20 | /// The Airplane class ties together the different components |
| 21 | class Airplane : public Version { |
| 22 | SGPropertyNode_ptr _wingsN; |
| 23 | public: |
| 24 | Airplane(); |
| 25 | ~Airplane(); |
| 26 | |
| 27 | enum Configuration { |
| 28 | NONE, |
| 29 | APPROACH, |
| 30 | CRUISE, |
| 31 | TAKEOFF, // for testing |
| 32 | TEST, // for testing |
| 33 | }; |
| 34 | |
| 35 | void iterate(float dt); |
| 36 | void calcFuelWeights(); |
| 37 | |
| 38 | ControlMap* getControlMap() { return &_controlMap; } |
| 39 | Model* getModel() { return &_model; } |
| 40 | |
| 41 | void setPilotPos(const float* pos) { Math::set3(pos, _pilotPos); } |
| 42 | void getPilotPos(float* out) { Math::set3(_pilotPos, out); } |
| 43 | void getPilotAccel(float* out); |
| 44 | |
| 45 | void setEmptyWeight(float weight) { _emptyWeight = weight; } |
| 46 | float getEmptyWeight() const { return _emptyWeight; } |
| 47 | |
| 48 | Wing* getWing(); |
| 49 | bool hasWing() const { return (_wing != nullptr); } |
| 50 | Wing* getTail(); |
| 51 | void addVStab(Wing* vstab) { _vstabs.add(vstab); } |
| 52 | |
| 53 | void addFuselage(const float* front, const float* back, float width, |
| 54 | float taper=1, float mid=0.5f, |
| 55 | float cx=1, float cy=1, float cz=1, float idrag=1); |
| 56 | int addTank(const float* pos, float cap, float fuelDensity); |
| 57 | void addGear(Gear* g); |
| 58 | void addHook(Hook* h) { _model.addHook(h); } |
| 59 | void addLaunchbar(Launchbar* l) { _model.addLaunchbar(l); } |
| 60 | void addThruster(Thruster* thruster, float mass, const float* cg); |
| 61 | void addBallast(const float* pos, float mass); |
| 62 | void addHitch(Hitch* h) { _model.addHitch(h); } |
| 63 | |
| 64 | int addWeight(const float* pos, float size); |
| 65 | void setWeight(int handle, float mass); |
| 66 | |
| 67 | void setConfig(Configuration cfg, float speed, float altitude, float fuel, |
| 68 | float gla = 0, float aoa = 0); |
| 69 | |
| 70 | /// add (fixed) control setting to approach/cruise config (for solver) |
| 71 | void addControlSetting(Configuration cfg, const char* prop, float val); |
| 72 | /// add a control input mapping for runtime |
| 73 | void addControlInput(const char* propName, ControlMap::ControlType type, void* obj, int subobj, int opt, float src0, float src1, float dst0, float dst1); |
| 74 | void addSolutionWeight(Configuration cfg, int idx, float wgt); |
| 75 | |
| 76 | int numGear() const { return _gears.size(); } |
| 77 | Gear* getGear(int g) { return ((GearRec*)_gears.get(g))->gear; } |
| 78 | Hook* getHook() const { return _model.getHook(); } |
| 79 | int numHitches() const { return _hitches.size(); } |
| 80 | Hitch* getHitch(int h); |
| 81 | Rotorgear* getRotorgear() { return _model.getRotorgear(); } |
| 82 | Launchbar* getLaunchbar() const { return _model.getLaunchbar(); } |
| 83 | |
| 84 | int numThrusters() const { return _thrusters.size(); } |
| 85 | Thruster* getThruster(int n) { |
| 86 | return ((ThrustRec*)_thrusters.get(n))->thruster; } |
| 87 | |
| 88 | int numTanks() const { return _tanks.size(); } |
| 89 | void setFuelFraction(float frac); // 0-1, total amount of fuel |
| 90 | /// get fuel in kg |
| 91 | float getFuel(int tank) const { return ((Tank*)_tanks.get(tank))->fill; } |
| 92 | /// set fuel in kg |
| 93 | float setFuel(int tank, float fuel) { return ((Tank*)_tanks.get(tank))->fill = fuel; } |
| 94 | /// get fuel density in kg/m^3 |
| 95 | float getFuelDensity(int tank) const { return ((Tank*)_tanks.get(tank))->density; } |
| 96 | float getTankCapacity(int tank) const { return ((Tank*)_tanks.get(tank))->cap; } |
| 97 | |
| 98 | void compile(bool verbose = false); // generate point masses & such, then solve |
| 99 | void initEngines(); |
| 100 | void stabilizeThrust(); |
| 101 | |
| 102 | // Solution output values |
| 103 | int getSolutionIterations() const { return _solutionIterations; } |
| 104 | float getDragCoefficient() const { return _dragFactor; } |
| 105 | float getLiftRatio() const { return _liftRatio; } |
| 106 | float getCruiseAoA() const { return _config[CRUISE].aoa; } |
| 107 | float getTailIncidence()const; |
| 108 | float getApproachElevator() const; |
| 109 | const char* getFailureMsg() const { return _failureMsg; } |
| 110 | float getMass() const { return _model.getMass(); }; |
| 111 | |
| 112 | // next two are used only in yasim CLI tool |
| 113 | void setApproachControls() { setControlValues(_config[APPROACH].controls); } |
| 114 | void setCruiseControls() { setControlValues(_config[CRUISE].controls); } |
| 115 | |
| 116 | float getCGHardLimitXMin() const { return _cgMin; } // get min x-coordinate for c.g (from main gear) |
| 117 | float getCGHardLimitXMax() const { return _cgMax; } // get max x-coordinate for c.g (from nose gear) |
| 118 | float getCGMAC(); // return c.g. x as fraction of MAC |
| 119 | /// set desired range for C.G. in % of MAC, 0% = leading edge, 100% trailing edge |
| 120 | void setDesiredCGRangeInPercentOfMAC(float MACPercentMin, float MACPercentMax) { _cgDesiredMin = MACPercentMin; _cgDesiredMax = MACPercentMax; } |
| 121 | float getCGSoftLimitXMin() const { return _cgDesiredAft; } // get x-coordinate limit calculated from MAC and setCGRange values |
| 122 | float getCGSoftLimitXMax() const { return _cgDesiredFront; } // get x-coordinate limit calculated from MAC and setCGRange values |
| 123 | |
| 124 | void setMTOW(float mtow) { _mtow = mtow; } |
| 125 | float getMTOW() const { return _mtow; } |
| 126 | float getWingSpan() const; |
| 127 | float getWingArea() const; |
| 128 | float getWingLoadEmpty() const { return _getWingLoad(_emptyWeight); }; |
| 129 | float getWingLoadMTOW() const { return _getWingLoad(_mtow); }; |
| 130 | /// get x-distance between CG and 25% MAC of wing |
| 131 | float getWingLever() const { return _getWingLever(_wing); }; |
| 132 | /// get x-distance between CG and 25% MAC of tail |
| 133 | float getTailLever() const { return _getWingLever(_tail); }; |
| 134 | float getMaxThrust(); |
| 135 | float getThrust2WeightEmpty() { return getMaxThrust()/(_emptyWeight * KG2N); }; |
| 136 | float getThrust2WeightMTOW() { return getMaxThrust()/(_mtow*KG2N); }; |
| 137 | void setSolverTweak(float f) { _solverDelta = f; }; |
| 138 | void setSolverThreshold(float threshold) { _solverThreshold = threshold; }; |
| 139 | void setSolverMaxIterations(int i) { _solverMaxIterations = i; }; |
| 140 | void setSolverMode(int i) { _solverMode = i; }; |
| 141 | |
| 142 | private: |
| 143 | struct Tank { |
| 144 | float pos[3] {0,0,0}; |
| 145 | float cap {0}, fill {0}, density {0}; |
| 146 | int handle {-1}; |
| 147 | }; |
| 148 | struct Fuselage { |
| 149 | float front[3] {0,0,0}, back[3] {0,0,0}; |
| 150 | float width, taper, mid, _cx, _cy, _cz, _idrag; |
| 151 | Vector surfs; |
| 152 | }; |
| 153 | struct GearRec { |
| 154 | Gear* gear; |
| 155 | Surface* surf; |
| 156 | float wgt {0}; |
| 157 | }; |
| 158 | struct ThrustRec { |
| 159 | int handle {-1}; |
| 160 | Thruster* thruster {nullptr}; |
| 161 | float cg[3] {0,0,0}; |
| 162 | float mass {0}; |
| 163 | }; |
| 164 | struct ControlSetting { |
| 165 | int propHandle {-1}; |
| 166 | float val {0}; |
| 167 | }; |
| 168 | struct WeightRec { |
| 169 | int handle {-1}; |
| 170 | Surface* surf {nullptr}; |
| 171 | }; |
| 172 | struct SolveWeight { |
| 173 | int id {-1}; |
| 174 | Configuration cfg {APPROACH}; |
| 175 | float wgt {0}; |
| 176 | }; |
| 177 | struct ContactRec { |
| 178 | Gear* gear {nullptr}; |
| 179 | float p[3] {0,0,0}; |
| 180 | }; |
| 181 | struct Config { |
| 182 | Configuration id; |
| 183 | float speed {0}; |
| 184 | float fuel {0}; |
| 185 | float glideAngle {0}; |
| 186 | float aoa {0}; |
| 187 | float altitude {0}; |
| 188 | float weight {0}; |
| 189 | State state; |
| 190 | Vector controls; |
| 191 | }; |
| 192 | Config _config[Configuration::TEST]; |
| 193 | |
| 194 | /// load values for controls as defined in cruise/approach configuration |
| 195 | void setControlValues(const Vector& controls); |
| 196 | /// Helper for solve() |
| 197 | void runConfig(Config &cfg); |
| 198 | void solveGear(); |
| 199 | float _getPitch(Config &cfg); |
| 200 | float _getLiftForce(Config &cfg); |
| 201 | float _getDragForce(Config &cfg); |
| 202 | float _checkConvergence(float prev, float current); |
| 203 | void solveAirplane(bool verbose = false); |
| 204 | void solveHelicopter(bool verbose = false); |
| 205 | float compileWing(Wing* w); |
| 206 | void compileRotorgear(); |
| 207 | float compileFuselage(Fuselage* f); |
| 208 | void compileGear(GearRec* gr); |
| 209 | void applyDragFactor(float factor); |
| 210 | void applyLiftRatio(float factor); |
| 211 | void addContactPoint(const float* pos); |
| 212 | void compileContactPoints(); |
| 213 | float normFactor(float f); |
| 214 | void updateGearState(); |
| 215 | void setupWeights(Configuration cfg); |
| 216 | void calculateCGHardLimits(); |
| 217 | ///calculate mass divided by area of main wing |
| 218 | float _getWingLoad(float mass) const; |
| 219 | ///calculate distance between CGx and AC of wing w |
| 220 | float _getWingLever(const Wing* w) const; |
| 221 | ControlSetting* _addControlSetting(Configuration cfg, const char* prop, float val); |
| 222 | ///set name of property controlling the elevator |
| 223 | void setElevatorControl(const char* propName); |
| 224 | /// set property name controling tail trim (incidence) |
| 225 | void setHstabTrimControl(const char* propName); |
| 226 | |
| 227 | int _solverMode {1}; |
| 228 | float _solverDelta {0.3226f}; |
| 229 | // How close to the solution are we trying get? |
| 230 | // Trying too hard can result in oscillations (no convergence). |
| 231 | float _solverThreshold {1}; |
| 232 | int _solverMaxIterations {10000}; |
| 233 | Model _model; |
| 234 | ControlMap _controlMap; |
| 235 | |
| 236 | float _emptyWeight {0}; |
| 237 | ///max take of weight |
| 238 | float _mtow {0}; |
| 239 | float _pilotPos[3] {0, 0, 0}; |
| 240 | |
| 241 | Wing* _wing {nullptr}; |
| 242 | Wing* _tail {nullptr}; |
| 243 | |
| 244 | Vector _fuselages; |
| 245 | Vector _vstabs; |
| 246 | Vector _tanks; |
| 247 | Vector _thrusters; |
| 248 | float _ballast {0}; |
| 249 | |
| 250 | Vector _gears; |
| 251 | Vector _contacts; // non-gear ground contact points |
| 252 | Vector _weights; |
| 253 | Vector _surfs; // NON-wing Surfaces |
| 254 | Vector _hitches; //for airtow and winch |
| 255 | |
| 256 | Vector _solveWeights; |
| 257 | |
| 258 | int _solutionIterations {0}; |
| 259 | float _dragFactor {1}; |
| 260 | float _liftRatio {1}; |
| 261 | ControlSetting* _tailIncidence {nullptr}; // added to approach config so solver can change it |
| 262 | // Copy of _tailIncidence added to cruise config. See setHstabTrimControl() for explanation. |
| 263 | ControlSetting* _tailIncidenceCopy {nullptr}; |
| 264 | ControlSetting* _approachElevator {nullptr}; |
| 265 | const char* _failureMsg {0}; |
| 266 | /// hard limits for cg from gear position |
| 267 | float _cgMax {-1e6}; |
| 268 | /// hard limits for cg from gear position |
| 269 | float _cgMin {1e6}; |
| 270 | /// desired cg max in %MAC from config |
| 271 | float _cgDesiredMax {0.3f}; |
| 272 | /// desired cg min in %MAC from config |
| 273 | float _cgDesiredMin {0.25f}; |
| 274 | /// calculated desired cg x max |
| 275 | float _cgDesiredFront {0}; |
| 276 | /// calculated desired cg x min |
| 277 | float _cgDesiredAft {0}; |
| 278 | }; |
| 279 | |
| 280 | }; // namespace yasim |
| 281 | #endif // _AIRPLANE_HPP |