X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FFDM%2FYASim%2FAirplane.cpp;h=166a861b54b1b352792da5b002dcfb2ef86d14f2;hb=c62048d5e26a2931f2a0f5fa7e94b3f7203e4762;hp=1a4e7713aff61c400441520dfdbcf44690d69f92;hpb=fe4e83a10bfa6927a4925215cbce48433a05ec6e;p=flightgear.git diff --git a/src/FDM/YASim/Airplane.cpp b/src/FDM/YASim/Airplane.cpp index 1a4e7713a..166a861b5 100644 --- a/src/FDM/YASim/Airplane.cpp +++ b/src/FDM/YASim/Airplane.cpp @@ -5,8 +5,9 @@ #include "Glue.hpp" #include "RigidBody.hpp" #include "Surface.hpp" +#include "Rotorpart.hpp" +#include "Rotorblade.hpp" #include "Thruster.hpp" - #include "Airplane.hpp" namespace yasim { @@ -15,6 +16,18 @@ namespace yasim { inline float norm(float f) { return f<1 ? 1/f : f; } inline float abs(float f) { return f<0 ? -f : f; } +// Solver threshold. How close to the solution are we trying +// to get? Trying too hard can result in oscillations about +// the correct solution, which is bad. Stick this in as a +// compile time constant for now, and consider making it +// settable per-model. +const float STHRESH = 1; + +// How slowly do we change values in the solver. Too slow, and +// the solution converges very slowly. Too fast, and it can +// oscillate. +const float SOLVE_TWEAK = 0.3226; + Airplane::Airplane() { _emptyWeight = 0; @@ -53,6 +66,8 @@ Airplane::~Airplane() delete (Surface*)_surfs.get(i); for(i=0; i<_contacts.size(); i++) delete[] (float*)_contacts.get(i); + for(i=0; i<_solveWeights.size(); i++) + delete[] (SolveWeight*)_solveWeights.get(i); } void Airplane::iterate(float dt) @@ -61,8 +76,14 @@ void Airplane::iterate(float dt) updateGearState(); _model.iterate(); +} - // FIXME: Consume fuel +void Airplane::calcFuelWeights() +{ + for(int i=0; i<_tanks.size(); i++) { + Tank* t = (Tank*)_tanks.get(i); + _model.getBody()->setMass(t->handle, t->fill); + } } ControlMap* Airplane::getControlMap() @@ -128,27 +149,23 @@ void Airplane::updateGearState() } } -void Airplane::setApproach(float speed, float altitude) -{ - // The zero AoA will become a calculated stall AoA in compile() - setApproach(speed, altitude, 0); -} - -void Airplane::setApproach(float speed, float altitude, float aoa) +void Airplane::setApproach(float speed, float altitude, float aoa, float fuel) { _approachSpeed = speed; _approachP = Atmosphere::getStdPressure(altitude); _approachT = Atmosphere::getStdTemperature(altitude); _approachAoA = aoa; + _approachFuel = fuel; } -void Airplane::setCruise(float speed, float altitude) +void Airplane::setCruise(float speed, float altitude, float fuel) { _cruiseSpeed = speed; _cruiseP = Atmosphere::getStdPressure(altitude); _cruiseT = Atmosphere::getStdTemperature(altitude); _cruiseAoA = 0; _tailIncidence = 0; + _cruiseFuel = fuel; } void Airplane::setElevatorControl(int control) @@ -174,6 +191,15 @@ void Airplane::addCruiseControl(int control, float val) _cruiseControls.add(c); } +void Airplane::addSolutionWeight(bool approach, int idx, float wgt) +{ + SolveWeight* w = new SolveWeight(); + w->approach = approach; + w->idx = idx; + w->wgt = wgt; + _solveWeights.add(w); +} + int Airplane::numTanks() { return _tanks.size(); @@ -184,11 +210,21 @@ float Airplane::getFuel(int tank) return ((Tank*)_tanks.get(tank))->fill; } +float Airplane::setFuel(int tank, float fuel) +{ + ((Tank*)_tanks.get(tank))->fill = fuel; +} + float Airplane::getFuelDensity(int tank) { return ((Tank*)_tanks.get(tank))->density; } +float Airplane::getTankCapacity(int tank) +{ + return ((Tank*)_tanks.get(tank))->cap; +} + void Airplane::setWeight(float weight) { _emptyWeight = weight; @@ -209,6 +245,11 @@ void Airplane::addVStab(Wing* vstab) _vstabs.add(vstab); } +void Airplane::addRotor(Rotor* rotor) +{ + _rotors.add(rotor); +} + void Airplane::addFuselage(float* front, float* back, float width, float taper, float mid) { @@ -298,6 +339,7 @@ void Airplane::setFuelFraction(float frac) int i; for(i=0; i<_tanks.size(); i++) { Tank* t = (Tank*)_tanks.get(i); + t->fill = frac * t->cap; _model.getBody()->setMass(t->handle, t->cap * frac); } } @@ -396,6 +438,43 @@ float Airplane::compileWing(Wing* w) return wgt; } +float Airplane::compileRotor(Rotor* r) +{ + // Todo: add rotor to model!!! + // Todo: calc and add mass!!! + r->compile(); + _model.addRotor(r); + + float wgt = 0; + int i; + for(i=0; inumRotorparts(); i++) { + Rotorpart* s = (Rotorpart*)r->getRotorpart(i); + + _model.addRotorpart(s); + + float mass = s->getWeight(); + mass = mass * Math::sqrt(mass); + float pos[3]; + s->getPosition(pos); + _model.getBody()->addMass(mass, pos); + wgt += mass; + } + + for(i=0; inumRotorblades(); i++) { + Rotorblade* b = (Rotorblade*)r->getRotorblade(i); + + _model.addRotorblade(b); + + float mass = b->getWeight(); + mass = mass * Math::sqrt(mass); + float pos[3]; + b->getPosition(pos); + _model.getBody()->addMass(mass, pos); + wgt += mass; + } + return wgt; +} + float Airplane::compileFuselage(Fuselage* f) { // The front and back are contact points @@ -529,17 +608,19 @@ void Airplane::compile() float aeroWgt = 0; // The Wing objects - aeroWgt += compileWing(_wing); - aeroWgt += compileWing(_tail); + if (_wing) + aeroWgt += compileWing(_wing); + if (_tail) + aeroWgt += compileWing(_tail); int i; - for(i=0; i<_vstabs.size(); i++) { + for(i=0; i<_vstabs.size(); i++) aeroWgt += compileWing((Wing*)_vstabs.get(i)); - } + for(i=0; i<_rotors.size(); i++) + aeroWgt += compileRotor((Rotor*)_rotors.get(i)); // The fuselage(s) - for(i=0; i<_fuselages.size(); i++) { + for(i=0; i<_fuselages.size(); i++) aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i)); - } // Count up the absolute weight we have float nonAeroWgt = _ballast; @@ -581,11 +662,14 @@ void Airplane::compile() // Ground effect float gepos[3]; - float gespan = _wing->getGroundEffect(gepos); + float gespan = 0; + if(_wing) + gespan = _wing->getGroundEffect(gepos); _model.setGroundEffect(gepos, gespan, 0.15f); solveGear(); - solve(); + if(_wing && _tail) solve(); + else solveHelicopter(); // Do this after solveGear, because it creates "gear" objects that // we don't want to affect. @@ -664,6 +748,18 @@ void Airplane::stabilizeThrust() _model.getThruster(i)->stabilize(); } +void Airplane::setupWeights(bool isApproach) +{ + int i; + for(i=0; i<_weights.size(); i++) + setWeight(i, 0); + for(i=0; i<_solveWeights.size(); i++) { + SolveWeight* w = (SolveWeight*)_solveWeights.get(i); + if(w->approach == isApproach) + setWeight(w->idx, w->wgt); + } +} + void Airplane::runCruise() { setupState(_cruiseAoA, _cruiseSpeed, &_cruiseState); @@ -685,8 +781,8 @@ void Airplane::runCruise() Math::mul3(-1, _cruiseState.v, wind); Math::vmul33(_cruiseState.orient, wind, wind); - // Cruise is by convention at 50% tank capacity - setFuelFraction(0.5); + setFuelFraction(_cruiseFuel); + setupWeights(false); // Set up the thruster parameters and iterate until the thrust // stabilizes. @@ -728,8 +824,9 @@ void Airplane::runApproach() Math::mul3(-1, _approachState.v, wind); Math::vmul33(_approachState.orient, wind, wind); - // Approach is by convention at 20% tank capacity - setFuelFraction(0.2f); + setFuelFraction(_approachFuel); + + setupWeights(true); // Run the thrusters until they get to a stable setting. FIXME: // this is lots of wasted work. @@ -752,10 +849,12 @@ void Airplane::runApproach() void Airplane::applyDragFactor(float factor) { - float applied = Math::sqrt(factor); + float applied = Math::pow(factor, SOLVE_TWEAK); _dragFactor *= applied; - _wing->setDragScale(_wing->getDragScale() * applied); - _tail->setDragScale(_tail->getDragScale() * applied); + if(_wing) + _wing->setDragScale(_wing->getDragScale() * applied); + if(_tail) + _tail->setDragScale(_tail->getDragScale() * applied); int i; for(i=0; i<_vstabs.size(); i++) { Wing* w = (Wing*)_vstabs.get(i); @@ -769,10 +868,12 @@ void Airplane::applyDragFactor(float factor) void Airplane::applyLiftRatio(float factor) { - float applied = Math::sqrt(factor); + float applied = Math::pow(factor, SOLVE_TWEAK); _liftRatio *= applied; - _wing->setLiftRatio(_wing->getLiftRatio() * applied); - _tail->setLiftRatio(_tail->getLiftRatio() * applied); + if(_wing) + _wing->setLiftRatio(_wing->getLiftRatio() * applied); + if(_tail) + _tail->setLiftRatio(_tail->getLiftRatio() * applied); int i; for(i=0; i<_vstabs.size(); i++) { Wing* w = (Wing*)_vstabs.get(i); @@ -801,20 +902,11 @@ void Airplane::solve() float tmp[3]; _solutionIterations = 0; _failureMsg = 0; + while(1) { -#if 0 - printf("%d %f %f %f %f %f\n", //DEBUG - _solutionIterations, - 1000*_dragFactor, - _liftRatio, - _cruiseAoA, - _tailIncidence, - _approachElevator.val); -#endif - - if(_solutionIterations++ > 10000) { + if(_solutionIterations++ > 10000) { _failureMsg = "Solution failed to converge after 10000 iterations"; - return; + return; } // Run an iteration at cruise, and extract the needed numbers: @@ -895,30 +987,30 @@ void Airplane::solve() applyLiftRatio(liftFactor); // DON'T do the following until the above are sane - if(normFactor(dragFactor) > 1.0001 - || normFactor(liftFactor) > 1.0001) + if(normFactor(dragFactor) > STHRESH*1.0001 + || normFactor(liftFactor) > STHRESH*1.0001) { continue; } // OK, now we can adjust the minor variables: - _cruiseAoA += 0.5f*aoaDelta; - _tailIncidence += 0.5f*tailDelta; + _cruiseAoA += SOLVE_TWEAK*aoaDelta; + _tailIncidence += SOLVE_TWEAK*tailDelta; _cruiseAoA = clamp(_cruiseAoA, -0.175f, 0.175f); _tailIncidence = clamp(_tailIncidence, -0.175f, 0.175f); - if(abs(xforce/_cruiseWeight) < 0.0001 && - abs(alift/_approachWeight) < 0.0001 && - abs(aoaDelta) < .000017 && - abs(tailDelta) < .000017) + if(abs(xforce/_cruiseWeight) < STHRESH*0.0001 && + abs(alift/_approachWeight) < STHRESH*0.0001 && + abs(aoaDelta) < STHRESH*.000017 && + abs(tailDelta) < STHRESH*.000017) { // If this finaly value is OK, then we're all done - if(abs(elevDelta) < 0.0001) + if(abs(elevDelta) < STHRESH*0.0001) break; // Otherwise, adjust and do the next iteration - _approachElevator.val += 0.8 * elevDelta; + _approachElevator.val += SOLVE_TWEAK * elevDelta; if(abs(_approachElevator.val) > 1) { _failureMsg = "Insufficient elevator to trim for approach"; break; @@ -940,4 +1032,18 @@ void Airplane::solve() return; } } + +void Airplane::solveHelicopter() +{ + _solutionIterations = 0; + _failureMsg = 0; + + applyDragFactor(Math::pow(15.7/1000, 1/SOLVE_TWEAK)); + applyLiftRatio(Math::pow(104, 1/SOLVE_TWEAK)); + setupState(0,0, &_cruiseState); + _model.setState(&_cruiseState); + _controls.reset(); + _model.getBody()->reset(); +} + }; // namespace yasim