1 #include "Atmosphere.hpp"
2 #include "ControlMap.hpp"
6 #include "RigidBody.hpp"
8 #include "Rotorpart.hpp"
9 #include "Thruster.hpp"
11 #include "Airplane.hpp"
16 inline float norm(float f) { return f<1 ? 1/f : f; }
17 inline float abs(float f) { return f<0 ? -f : f; }
19 // Solver threshold. How close to the solution are we trying
20 // to get? Trying too hard can result in oscillations about
21 // the correct solution, which is bad. Stick this in as a
22 // compile time constant for now, and consider making it
23 // settable per-model.
24 const float STHRESH = 1;
26 // How slowly do we change values in the solver. Too slow, and
27 // the solution converges very slowly. Too fast, and it can
29 const float SOLVE_TWEAK = 0.3226;
34 _pilotPos[0] = _pilotPos[1] = _pilotPos[2] = 0;
42 _cruiseGlideAngle = 0;
48 _approachGlideAngle = 0;
59 for(i=0; i<_fuselages.size(); i++)
60 delete (Fuselage*)_fuselages.get(i);
61 for(i=0; i<_tanks.size(); i++)
62 delete (Tank*)_tanks.get(i);
63 for(i=0; i<_thrusters.size(); i++)
64 delete (ThrustRec*)_thrusters.get(i);
65 for(i=0; i<_gears.size(); i++) {
66 GearRec* g = (GearRec*)_gears.get(i);
70 for(i=0; i<_surfs.size(); i++)
71 delete (Surface*)_surfs.get(i);
72 for(i=0; i<_contacts.size(); i++) {
73 ContactRec* c = (ContactRec*)_contacts.get(i);
77 for(i=0; i<_solveWeights.size(); i++)
78 delete (SolveWeight*)_solveWeights.get(i);
79 for(i=0; i<_cruiseControls.size(); i++)
80 delete (Control*)_cruiseControls.get(i);
81 for(i=0; i<_approachControls.size(); i++) {
82 Control* c = (Control*)_approachControls.get(i);
83 if(c != &_approachElevator)
88 for(i=0; i<_vstabs.size(); i++)
89 delete (Wing*)_vstabs.get(i);
90 for(i=0; i<_weights.size(); i++)
91 delete (WeightRec*)_weights.get(i);
94 void Airplane::iterate(float dt)
96 // The gear might have moved. Change their aerodynamics.
102 void Airplane::calcFuelWeights()
104 for(int i=0; i<_tanks.size(); i++) {
105 Tank* t = (Tank*)_tanks.get(i);
106 _model.getBody()->setMass(t->handle, t->fill);
110 ControlMap* Airplane::getControlMap()
115 Model* Airplane::getModel()
120 void Airplane::getPilotAccel(float* out)
122 State* s = _model.getState();
125 Glue::geodUp(s->pos, out);
126 Math::mul3(-9.8f, out, out);
128 // The regular acceleration
130 Math::mul3(-1, s->acc, tmp);
131 Math::add3(tmp, out, out);
133 // Convert to aircraft coordinates
134 Math::vmul33(s->orient, out, out);
136 // FIXME: rotational & centripetal acceleration needed
139 void Airplane::setPilotPos(float* pos)
142 for(i=0; i<3; i++) _pilotPos[i] = pos[i];
145 void Airplane::getPilotPos(float* out)
148 for(i=0; i<3; i++) out[i] = _pilotPos[i];
151 int Airplane::numGear()
153 return _gears.size();
156 Gear* Airplane::getGear(int g)
158 return ((GearRec*)_gears.get(g))->gear;
161 Hook* Airplane::getHook()
163 return _model.getHook();
166 Launchbar* Airplane::getLaunchbar()
168 return _model.getLaunchbar();
171 Rotorgear* Airplane::getRotorgear()
173 return _model.getRotorgear();
176 void Airplane::updateGearState()
178 for(int i=0; i<_gears.size(); i++) {
179 GearRec* gr = (GearRec*)_gears.get(i);
180 float ext = gr->gear->getExtension();
182 gr->surf->setXDrag(ext);
183 gr->surf->setYDrag(ext);
184 gr->surf->setZDrag(ext);
188 void Airplane::setApproach(float speed, float altitude, float aoa, float fuel, float gla)
190 _approachSpeed = speed;
191 _approachP = Atmosphere::getStdPressure(altitude);
192 _approachT = Atmosphere::getStdTemperature(altitude);
194 _approachFuel = fuel;
195 _approachGlideAngle = gla;
198 void Airplane::setCruise(float speed, float altitude, float fuel, float gla)
200 _cruiseSpeed = speed;
201 _cruiseP = Atmosphere::getStdPressure(altitude);
202 _cruiseT = Atmosphere::getStdTemperature(altitude);
206 _cruiseGlideAngle = gla;
209 void Airplane::setElevatorControl(int control)
211 _approachElevator.control = control;
212 _approachElevator.val = 0;
213 _approachControls.add(&_approachElevator);
216 void Airplane::addApproachControl(int control, float val)
218 Control* c = new Control();
219 c->control = control;
221 _approachControls.add(c);
224 void Airplane::addCruiseControl(int control, float val)
226 Control* c = new Control();
227 c->control = control;
229 _cruiseControls.add(c);
232 void Airplane::addSolutionWeight(bool approach, int idx, float wgt)
234 SolveWeight* w = new SolveWeight();
235 w->approach = approach;
238 _solveWeights.add(w);
241 int Airplane::numTanks()
243 return _tanks.size();
246 float Airplane::getFuel(int tank)
248 return ((Tank*)_tanks.get(tank))->fill;
251 float Airplane::setFuel(int tank, float fuel)
253 return ((Tank*)_tanks.get(tank))->fill = fuel;
256 float Airplane::getFuelDensity(int tank)
258 return ((Tank*)_tanks.get(tank))->density;
261 float Airplane::getTankCapacity(int tank)
263 return ((Tank*)_tanks.get(tank))->cap;
266 void Airplane::setWeight(float weight)
268 _emptyWeight = weight;
271 void Airplane::setWing(Wing* wing)
276 void Airplane::setTail(Wing* tail)
281 void Airplane::addVStab(Wing* vstab)
286 void Airplane::addFuselage(float* front, float* back, float width,
287 float taper, float mid,
288 float cx, float cy, float cz, float idrag)
290 Fuselage* f = new Fuselage();
293 f->front[i] = front[i];
294 f->back[i] = back[i];
306 int Airplane::addTank(float* pos, float cap, float density)
308 Tank* t = new Tank();
310 for(i=0; i<3; i++) t->pos[i] = pos[i];
313 t->density = density;
314 t->handle = 0xffffffff;
315 return _tanks.add(t);
318 void Airplane::addGear(Gear* gear)
320 GearRec* g = new GearRec();
326 void Airplane::addHook(Hook* hook)
328 _model.addHook(hook);
331 void Airplane::addHitch(Hitch* hitch)
333 _model.addHitch(hitch);
336 void Airplane::addLaunchbar(Launchbar* launchbar)
338 _model.addLaunchbar(launchbar);
341 void Airplane::addThruster(Thruster* thruster, float mass, float* cg)
343 ThrustRec* t = new ThrustRec();
344 t->thruster = thruster;
347 for(i=0; i<3; i++) t->cg[i] = cg[i];
351 void Airplane::addBallast(float* pos, float mass)
353 _model.getBody()->addMass(mass, pos);
357 int Airplane::addWeight(float* pos, float size)
359 WeightRec* wr = new WeightRec();
360 wr->handle = _model.getBody()->addMass(0, pos);
362 wr->surf = new Surface();
363 wr->surf->setPosition(pos);
364 wr->surf->setTotalDrag(size*size);
365 _model.addSurface(wr->surf);
366 _surfs.add(wr->surf);
368 return _weights.add(wr);
371 void Airplane::setWeight(int handle, float mass)
373 WeightRec* wr = (WeightRec*)_weights.get(handle);
375 _model.getBody()->setMass(wr->handle, mass);
377 // Kill the aerodynamic drag if the mass is exactly zero. This is
378 // how we simulate droppable stores.
380 wr->surf->setXDrag(0);
381 wr->surf->setYDrag(0);
382 wr->surf->setZDrag(0);
384 wr->surf->setXDrag(1);
385 wr->surf->setYDrag(1);
386 wr->surf->setZDrag(1);
390 void Airplane::setFuelFraction(float frac)
393 for(i=0; i<_tanks.size(); i++) {
394 Tank* t = (Tank*)_tanks.get(i);
395 t->fill = frac * t->cap;
396 _model.getBody()->setMass(t->handle, t->cap * frac);
400 float Airplane::getDragCoefficient()
405 float Airplane::getLiftRatio()
410 float Airplane::getCruiseAoA()
415 float Airplane::getTailIncidence()
417 return _tailIncidence;
420 const char* Airplane::getFailureMsg()
425 int Airplane::getSolutionIterations()
427 return _solutionIterations;
430 void Airplane::setupState(float aoa, float speed, float gla, State* s)
432 float cosAoA = Math::cos(aoa);
433 float sinAoA = Math::sin(aoa);
434 s->orient[0] = cosAoA; s->orient[1] = 0; s->orient[2] = sinAoA;
435 s->orient[3] = 0; s->orient[4] = 1; s->orient[5] = 0;
436 s->orient[6] = -sinAoA; s->orient[7] = 0; s->orient[8] = cosAoA;
438 s->v[0] = speed*Math::cos(gla); s->v[1] = -speed*Math::sin(gla); s->v[2] = 0;
442 s->pos[i] = s->rot[i] = s->acc[i] = s->racc[i] = 0;
444 // Put us 1m above the origin, or else the gravity computation in
449 void Airplane::addContactPoint(float* pos)
451 ContactRec* c = new ContactRec;
459 float Airplane::compileWing(Wing* w)
461 // The tip of the wing is a contact point
464 addContactPoint(tip);
465 if(w->isMirrored()) {
467 addContactPoint(tip);
470 // Make sure it's initialized. The surfaces will pop out with
471 // total drag coefficients equal to their areas, which is what we
477 for(i=0; i<w->numSurfaces(); i++) {
478 Surface* s = (Surface*)w->getSurface(i);
480 float td = s->getTotalDrag();
483 _model.addSurface(s);
485 float mass = w->getSurfaceWeight(i);
486 mass = mass * Math::sqrt(mass);
489 _model.getBody()->addMass(mass, pos);
495 void Airplane::compileRotorgear()
497 getRotorgear()->compile();
500 float Airplane::compileFuselage(Fuselage* f)
502 // The front and back are contact points
503 addContactPoint(f->front);
504 addContactPoint(f->back);
508 Math::sub3(f->front, f->back, fwd);
509 float len = Math::mag3(fwd);
510 float wid = f->width;
511 int segs = (int)Math::ceil(len/wid);
512 float segWgt = len*wid/segs;
514 for(j=0; j<segs; j++) {
515 float frac = (j+0.5f) / segs;
519 scale = f->taper+(1-f->taper) * (frac / f->mid);
521 scale = f->taper+(1-f->taper) * (frac - f->mid) / (1 - f->mid);
525 Math::mul3(frac, fwd, pos);
526 Math::add3(f->back, pos, pos);
528 // _Mass_ weighting goes as surface area^(3/2)
529 float mass = scale*segWgt * Math::sqrt(scale*segWgt);
530 _model.getBody()->addMass(mass, pos);
533 // Make a Surface too
534 Surface* s = new Surface();
536 float sideDrag = len/wid;
537 s->setYDrag(sideDrag*f->_cy);
538 s->setZDrag(sideDrag*f->_cz);
539 s->setTotalDrag(scale*segWgt*f->_cx);
540 s->setInducedDrag(f->_idrag);
542 // FIXME: fails for fuselages aligned along the Y axis
544 float *x=o, *y=o+3, *z=o+6; // nicknames for the axes
546 y[0] = 0; y[1] = 1; y[2] = 0;
547 Math::cross3(x, y, z);
549 Math::cross3(z, x, y);
550 s->setOrientation(o);
552 _model.addSurface(s);
558 // FIXME: should probably add a mass for the gear, too
559 void Airplane::compileGear(GearRec* gr)
563 // Make a Surface object for the aerodynamic behavior
564 Surface* s = new Surface();
567 // Put the surface at the half-way point on the gear strut, give
568 // it a drag coefficient equal to a square of the same dimension
569 // (gear are really draggy) and make it symmetric. Assume that
570 // the "length" of the gear is 3x the compression distance
571 float pos[3], cmp[3];
572 g->getCompression(cmp);
573 float length = 3 * Math::mag3(cmp);
575 Math::mul3(0.5, cmp, cmp);
576 Math::add3(pos, cmp, pos);
579 s->setTotalDrag(length*length);
582 _model.addSurface(s);
586 void Airplane::compileContactPoints()
588 // Figure it will compress by 20cm
591 comp[0] = 0; comp[1] = 0; comp[2] = DIST;
593 // Give it a spring constant such that at full compression it will
594 // hold up 10 times the planes mass. That's about right. Yeah.
595 float mass = _model.getBody()->getTotalMass();
596 float spring = (1/DIST) * 9.8f * 10.0f * mass;
597 float damp = 2 * Math::sqrt(spring * mass);
600 for(i=0; i<_contacts.size(); i++) {
601 ContactRec* c = (ContactRec*)_contacts.get(i);
603 Gear* g = new Gear();
605 g->setPosition(c->p);
607 g->setCompression(comp);
608 g->setSpring(spring);
613 g->setStaticFriction(0.6f);
614 g->setDynamicFriction(0.5f);
615 g->setContactPoint(1);
621 void Airplane::compile()
623 RigidBody* body = _model.getBody();
624 int firstMass = body->numMasses();
626 // Generate the point masses for the plane. Just use unitless
627 // numbers for a first pass, then go back through and rescale to
628 // make the weight right.
633 aeroWgt += compileWing(_wing);
635 aeroWgt += compileWing(_tail);
637 for(i=0; i<_vstabs.size(); i++)
638 aeroWgt += compileWing((Wing*)_vstabs.get(i));
642 for(i=0; i<_fuselages.size(); i++)
643 aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i));
645 // Count up the absolute weight we have
646 float nonAeroWgt = _ballast;
647 for(i=0; i<_thrusters.size(); i++)
648 nonAeroWgt += ((ThrustRec*)_thrusters.get(i))->mass;
650 // Rescale to the specified empty weight
651 float wscale = (_emptyWeight-nonAeroWgt)/aeroWgt;
652 for(i=firstMass; i<body->numMasses(); i++)
653 body->setMass(i, body->getMass(i)*wscale);
655 // Add the thruster masses
656 for(i=0; i<_thrusters.size(); i++) {
657 ThrustRec* t = (ThrustRec*)_thrusters.get(i);
658 body->addMass(t->mass, t->cg);
661 // Add the tanks, empty for now.
663 for(i=0; i<_tanks.size(); i++) {
664 Tank* t = (Tank*)_tanks.get(i);
665 t->handle = body->addMass(0, t->pos);
668 _cruiseWeight = _emptyWeight + totalFuel*0.5f;
669 _approachWeight = _emptyWeight + totalFuel*0.2f;
673 // Add surfaces for the landing gear.
674 for(i=0; i<_gears.size(); i++)
675 compileGear((GearRec*)_gears.get(i));
677 // The Thruster objects
678 for(i=0; i<_thrusters.size(); i++) {
679 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
680 tr->handle = _model.addThruster(tr->thruster);
687 gespan = _wing->getGroundEffect(gepos);
688 _model.setGroundEffect(gepos, gespan, 0.15f);
692 if(_wing && _tail) solve();
695 // The rotor(s) mass:
700 // Do this after solveGear, because it creates "gear" objects that
701 // we don't want to affect.
702 compileContactPoints();
705 void Airplane::solveGear()
708 _model.getBody()->getCG(cg);
710 // Calculate spring constant weightings for the gear. Weight by
711 // the inverse of the distance to the c.g. in the XY plane, which
712 // should be correct for most gear arrangements. Add 50cm of
713 // "buffer" to keep things from blowing up with aircraft with a
714 // single gear very near the c.g. (AV-8, for example).
717 for(i=0; i<_gears.size(); i++) {
718 GearRec* gr = (GearRec*)_gears.get(i);
721 Math::sub3(cg, pos, pos);
722 gr->wgt = 1.0f/(0.5f+Math::sqrt(pos[0]*pos[0] + pos[1]*pos[1]));
723 if (!g->getIgnoreWhileSolving())
727 // Renormalize so they sum to 1
728 for(i=0; i<_gears.size(); i++)
729 ((GearRec*)_gears.get(i))->wgt /= total;
731 // The force at max compression should be sufficient to stop a
732 // plane moving downwards at 2x the approach descent rate. Assume
733 // a 3 degree approach.
734 float descentRate = 2.0f*_approachSpeed/19.1f;
736 // Spread the kinetic energy according to the gear weights. This
737 // will results in an equal compression fraction (not distance) of
739 float energy = 0.5f*_approachWeight*descentRate*descentRate;
741 for(i=0; i<_gears.size(); i++) {
742 GearRec* gr = (GearRec*)_gears.get(i);
743 float e = energy * gr->wgt;
745 gr->gear->getCompression(comp);
746 float len = Math::mag3(comp)*(1+2*gr->gear->getInitialLoad());
748 // Energy in a spring: e = 0.5 * k * len^2
749 float k = 2 * e / (len*len);
751 gr->gear->setSpring(k * gr->gear->getSpring());
753 // Critically damped (too damped, too!)
754 gr->gear->setDamping(2*Math::sqrt(k*_approachWeight*gr->wgt)
755 * gr->gear->getDamping());
759 void Airplane::initEngines()
761 for(int i=0; i<_thrusters.size(); i++) {
762 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
763 tr->thruster->init();
767 void Airplane::stabilizeThrust()
770 for(i=0; i<_thrusters.size(); i++)
771 _model.getThruster(i)->stabilize();
774 void Airplane::setupWeights(bool isApproach)
777 for(i=0; i<_weights.size(); i++)
779 for(i=0; i<_solveWeights.size(); i++) {
780 SolveWeight* w = (SolveWeight*)_solveWeights.get(i);
781 if(w->approach == isApproach)
782 setWeight(w->idx, w->wgt);
786 void Airplane::runCruise()
788 setupState(_cruiseAoA, _cruiseSpeed,_approachGlideAngle, &_cruiseState);
789 _model.setState(&_cruiseState);
790 _model.setAir(_cruiseP, _cruiseT,
791 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
793 // The control configuration
796 for(i=0; i<_cruiseControls.size(); i++) {
797 Control* c = (Control*)_cruiseControls.get(i);
798 _controls.setInput(c->control, c->val);
800 _controls.applyControls(1000000); // Huge dt value
804 Math::mul3(-1, _cruiseState.v, wind);
805 Math::vmul33(_cruiseState.orient, wind, wind);
807 setFuelFraction(_cruiseFuel);
810 // Set up the thruster parameters and iterate until the thrust
812 for(i=0; i<_thrusters.size(); i++) {
813 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
815 t->setAir(_cruiseP, _cruiseT,
816 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
822 // Precompute thrust in the model, and calculate aerodynamic forces
823 _model.getBody()->recalc();
824 _model.getBody()->reset();
825 _model.initIteration();
826 _model.calcForces(&_cruiseState);
829 void Airplane::runApproach()
831 setupState(_approachAoA, _approachSpeed,_approachGlideAngle, &_approachState);
832 _model.setState(&_approachState);
833 _model.setAir(_approachP, _approachT,
834 Atmosphere::calcStdDensity(_approachP, _approachT));
836 // The control configuration
839 for(i=0; i<_approachControls.size(); i++) {
840 Control* c = (Control*)_approachControls.get(i);
841 _controls.setInput(c->control, c->val);
843 _controls.applyControls(1000000);
847 Math::mul3(-1, _approachState.v, wind);
848 Math::vmul33(_approachState.orient, wind, wind);
850 setFuelFraction(_approachFuel);
854 // Run the thrusters until they get to a stable setting. FIXME:
855 // this is lots of wasted work.
856 for(i=0; i<_thrusters.size(); i++) {
857 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
859 t->setAir(_approachP, _approachT,
860 Atmosphere::calcStdDensity(_approachP, _approachT));
866 // Precompute thrust in the model, and calculate aerodynamic forces
867 _model.getBody()->recalc();
868 _model.getBody()->reset();
869 _model.initIteration();
870 _model.calcForces(&_approachState);
873 void Airplane::applyDragFactor(float factor)
875 float applied = Math::pow(factor, SOLVE_TWEAK);
876 _dragFactor *= applied;
878 _wing->setDragScale(_wing->getDragScale() * applied);
880 _tail->setDragScale(_tail->getDragScale() * applied);
882 for(i=0; i<_vstabs.size(); i++) {
883 Wing* w = (Wing*)_vstabs.get(i);
884 w->setDragScale(w->getDragScale() * applied);
886 for(i=0; i<_surfs.size(); i++) {
887 Surface* s = (Surface*)_surfs.get(i);
888 s->setTotalDrag(s->getTotalDrag() * applied);
892 void Airplane::applyLiftRatio(float factor)
894 float applied = Math::pow(factor, SOLVE_TWEAK);
895 _liftRatio *= applied;
897 _wing->setLiftRatio(_wing->getLiftRatio() * applied);
899 _tail->setLiftRatio(_tail->getLiftRatio() * applied);
901 for(i=0; i<_vstabs.size(); i++) {
902 Wing* w = (Wing*)_vstabs.get(i);
903 w->setLiftRatio(w->getLiftRatio() * applied);
907 float Airplane::clamp(float val, float min, float max)
909 if(val < min) return min;
910 if(val > max) return max;
914 float Airplane::normFactor(float f)
921 void Airplane::solve()
923 static const float ARCMIN = 0.0002909f;
926 _solutionIterations = 0;
930 if(_solutionIterations++ > 10000) {
931 _failureMsg = "Solution failed to converge after 10000 iterations";
935 // Run an iteration at cruise, and extract the needed numbers:
938 _model.getThrust(tmp);
939 float thrust = tmp[0] + _cruiseWeight * Math::sin(_cruiseGlideAngle) * 9.81;
941 _model.getBody()->getAccel(tmp);
942 Math::tmul33(_cruiseState.orient, tmp, tmp);
943 float xforce = _cruiseWeight * tmp[0];
944 float clift0 = _cruiseWeight * tmp[2];
946 _model.getBody()->getAngularAccel(tmp);
947 Math::tmul33(_cruiseState.orient, tmp, tmp);
948 float pitch0 = tmp[1];
950 // Run an approach iteration, and do likewise
953 _model.getBody()->getAngularAccel(tmp);
954 Math::tmul33(_approachState.orient, tmp, tmp);
955 double apitch0 = tmp[1];
957 _model.getBody()->getAccel(tmp);
958 Math::tmul33(_approachState.orient, tmp, tmp);
959 float alift = _approachWeight * tmp[2];
961 // Modify the cruise AoA a bit to get a derivative
962 _cruiseAoA += ARCMIN;
964 _cruiseAoA -= ARCMIN;
966 _model.getBody()->getAccel(tmp);
967 Math::tmul33(_cruiseState.orient, tmp, tmp);
968 float clift1 = _cruiseWeight * tmp[2];
970 // Do the same with the tail incidence
971 _tail->setIncidence(_tailIncidence + ARCMIN);
973 _tail->setIncidence(_tailIncidence);
975 _model.getBody()->getAngularAccel(tmp);
976 Math::tmul33(_cruiseState.orient, tmp, tmp);
977 float pitch1 = tmp[1];
980 float awgt = 9.8f * _approachWeight;
982 float dragFactor = thrust / (thrust-xforce);
983 float liftFactor = awgt / (awgt+alift);
984 float aoaDelta = -clift0 * (ARCMIN/(clift1-clift0));
985 float tailDelta = -pitch0 * (ARCMIN/(pitch1-pitch0));
988 if(dragFactor <= 0 || liftFactor <= 0)
991 // And the elevator control in the approach. This works just
992 // like the tail incidence computation (it's solving for the
993 // same thing -- pitching moment -- by diddling a different
995 const float ELEVDIDDLE = 0.001f;
996 _approachElevator.val += ELEVDIDDLE;
998 _approachElevator.val -= ELEVDIDDLE;
1000 _model.getBody()->getAngularAccel(tmp);
1001 Math::tmul33(_approachState.orient, tmp, tmp);
1002 double apitch1 = tmp[1];
1003 float elevDelta = -apitch0 * (ELEVDIDDLE/(apitch1-apitch0));
1005 // Now apply the values we just computed. Note that the
1006 // "minor" variables are deferred until we get the lift/drag
1007 // numbers in the right ballpark.
1009 applyDragFactor(dragFactor);
1010 applyLiftRatio(liftFactor);
1012 // DON'T do the following until the above are sane
1013 if(normFactor(dragFactor) > STHRESH*1.0001
1014 || normFactor(liftFactor) > STHRESH*1.0001)
1019 // OK, now we can adjust the minor variables:
1020 _cruiseAoA += SOLVE_TWEAK*aoaDelta;
1021 _tailIncidence += SOLVE_TWEAK*tailDelta;
1023 _cruiseAoA = clamp(_cruiseAoA, -0.175f, 0.175f);
1024 _tailIncidence = clamp(_tailIncidence, -0.175f, 0.175f);
1026 if(abs(xforce/_cruiseWeight) < STHRESH*0.0001 &&
1027 abs(alift/_approachWeight) < STHRESH*0.0001 &&
1028 abs(aoaDelta) < STHRESH*.000017 &&
1029 abs(tailDelta) < STHRESH*.000017)
1031 // If this finaly value is OK, then we're all done
1032 if(abs(elevDelta) < STHRESH*0.0001)
1035 // Otherwise, adjust and do the next iteration
1036 _approachElevator.val += SOLVE_TWEAK * elevDelta;
1037 if(abs(_approachElevator.val) > 1) {
1038 _failureMsg = "Insufficient elevator to trim for approach";
1044 if(_dragFactor < 1e-06 || _dragFactor > 1e6) {
1045 _failureMsg = "Drag factor beyond reasonable bounds.";
1047 } else if(_liftRatio < 1e-04 || _liftRatio > 1e4) {
1048 _failureMsg = "Lift ratio beyond reasonable bounds.";
1050 } else if(Math::abs(_cruiseAoA) >= .17453293) {
1051 _failureMsg = "Cruise AoA > 10 degrees";
1053 } else if(Math::abs(_tailIncidence) >= .17453293) {
1054 _failureMsg = "Tail incidence > 10 degrees";
1059 void Airplane::solveHelicopter()
1061 _solutionIterations = 0;
1063 if (getRotorgear()!=0)
1065 Rotorgear* rg = getRotorgear();
1066 applyDragFactor(Math::pow(rg->getYasimDragFactor()/1000,
1068 applyLiftRatio(Math::pow(rg->getYasimLiftFactor(),
1072 //huh, no wing and no rotor? (_rotorgear is constructed,
1073 //if a rotor is defined
1075 applyDragFactor(Math::pow(15.7/1000, 1/SOLVE_TWEAK));
1076 applyLiftRatio(Math::pow(104, 1/SOLVE_TWEAK));
1078 setupState(0,0,0, &_cruiseState);
1079 _model.setState(&_cruiseState);
1082 _model.getBody()->reset();
1083 _model.setAir(_cruiseP, _cruiseT,
1084 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
1088 }; // namespace yasim