1 #include "Atmosphere.hpp"
2 #include "ControlMap.hpp"
6 #include "RigidBody.hpp"
8 #include "Rotorpart.hpp"
9 #include "Rotorblade.hpp"
10 #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;
57 for(i=0; i<_fuselages.size(); i++)
58 delete (Fuselage*)_fuselages.get(i);
59 for(i=0; i<_tanks.size(); i++)
60 delete (Tank*)_tanks.get(i);
61 for(i=0; i<_thrusters.size(); i++)
62 delete (ThrustRec*)_thrusters.get(i);
63 for(i=0; i<_gears.size(); i++) {
64 GearRec* g = (GearRec*)_gears.get(i);
68 for(i=0; i<_surfs.size(); i++)
69 delete (Surface*)_surfs.get(i);
70 for(i=0; i<_contacts.size(); i++) {
71 ContactRec* c = (ContactRec*)_contacts.get(i);
75 for(i=0; i<_solveWeights.size(); i++)
76 delete (SolveWeight*)_solveWeights.get(i);
77 for(i=0; i<_cruiseControls.size(); i++)
78 delete (Control*)_cruiseControls.get(i);
79 for(i=0; i<_approachControls.size(); i++) {
80 Control* c = (Control*)_approachControls.get(i);
81 if(c != &_approachElevator)
86 for(i=0; i<_vstabs.size(); i++)
87 delete (Wing*)_vstabs.get(i);
88 for(i=0; i<_weights.size(); i++)
89 delete (WeightRec*)_weights.get(i);
90 for(i=0; i<_rotors.size(); i++)
91 delete (Rotor*)_rotors.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 void Airplane::updateGearState()
173 for(int i=0; i<_gears.size(); i++) {
174 GearRec* gr = (GearRec*)_gears.get(i);
175 float ext = gr->gear->getExtension();
177 gr->surf->setXDrag(ext);
178 gr->surf->setYDrag(ext);
179 gr->surf->setZDrag(ext);
183 void Airplane::setApproach(float speed, float altitude, float aoa, float fuel)
185 _approachSpeed = speed;
186 _approachP = Atmosphere::getStdPressure(altitude);
187 _approachT = Atmosphere::getStdTemperature(altitude);
189 _approachFuel = fuel;
192 void Airplane::setCruise(float speed, float altitude, float fuel)
194 _cruiseSpeed = speed;
195 _cruiseP = Atmosphere::getStdPressure(altitude);
196 _cruiseT = Atmosphere::getStdTemperature(altitude);
202 void Airplane::setElevatorControl(int control)
204 _approachElevator.control = control;
205 _approachElevator.val = 0;
206 _approachControls.add(&_approachElevator);
209 void Airplane::addApproachControl(int control, float val)
211 Control* c = new Control();
212 c->control = control;
214 _approachControls.add(c);
217 void Airplane::addCruiseControl(int control, float val)
219 Control* c = new Control();
220 c->control = control;
222 _cruiseControls.add(c);
225 void Airplane::addSolutionWeight(bool approach, int idx, float wgt)
227 SolveWeight* w = new SolveWeight();
228 w->approach = approach;
231 _solveWeights.add(w);
234 int Airplane::numTanks()
236 return _tanks.size();
239 float Airplane::getFuel(int tank)
241 return ((Tank*)_tanks.get(tank))->fill;
244 float Airplane::setFuel(int tank, float fuel)
246 return ((Tank*)_tanks.get(tank))->fill = fuel;
249 float Airplane::getFuelDensity(int tank)
251 return ((Tank*)_tanks.get(tank))->density;
254 float Airplane::getTankCapacity(int tank)
256 return ((Tank*)_tanks.get(tank))->cap;
259 void Airplane::setWeight(float weight)
261 _emptyWeight = weight;
264 void Airplane::setWing(Wing* wing)
269 void Airplane::setTail(Wing* tail)
274 void Airplane::addVStab(Wing* vstab)
279 void Airplane::addRotor(Rotor* rotor)
284 void Airplane::addFuselage(float* front, float* back, float width,
285 float taper, float mid)
287 Fuselage* f = new Fuselage();
290 f->front[i] = front[i];
291 f->back[i] = back[i];
299 int Airplane::addTank(float* pos, float cap, float density)
301 Tank* t = new Tank();
303 for(i=0; i<3; i++) t->pos[i] = pos[i];
306 t->density = density;
307 t->handle = 0xffffffff;
308 return _tanks.add(t);
311 void Airplane::addGear(Gear* gear)
313 GearRec* g = new GearRec();
319 void Airplane::addHook(Hook* hook)
321 _model.addHook(hook);
324 void Airplane::addLaunchbar(Launchbar* launchbar)
326 _model.addLaunchbar(launchbar);
329 void Airplane::addThruster(Thruster* thruster, float mass, float* cg)
331 ThrustRec* t = new ThrustRec();
332 t->thruster = thruster;
335 for(i=0; i<3; i++) t->cg[i] = cg[i];
339 void Airplane::addBallast(float* pos, float mass)
341 _model.getBody()->addMass(mass, pos);
345 int Airplane::addWeight(float* pos, float size)
347 WeightRec* wr = new WeightRec();
348 wr->handle = _model.getBody()->addMass(0, pos);
350 wr->surf = new Surface();
351 wr->surf->setPosition(pos);
352 wr->surf->setTotalDrag(size*size);
353 _model.addSurface(wr->surf);
354 _surfs.add(wr->surf);
356 return _weights.add(wr);
359 void Airplane::setWeight(int handle, float mass)
361 WeightRec* wr = (WeightRec*)_weights.get(handle);
363 _model.getBody()->setMass(wr->handle, mass);
365 // Kill the aerodynamic drag if the mass is exactly zero. This is
366 // how we simulate droppable stores.
368 wr->surf->setXDrag(0);
369 wr->surf->setYDrag(0);
370 wr->surf->setZDrag(0);
372 wr->surf->setXDrag(1);
373 wr->surf->setYDrag(1);
374 wr->surf->setZDrag(1);
378 void Airplane::setFuelFraction(float frac)
381 for(i=0; i<_tanks.size(); i++) {
382 Tank* t = (Tank*)_tanks.get(i);
383 t->fill = frac * t->cap;
384 _model.getBody()->setMass(t->handle, t->cap * frac);
388 float Airplane::getDragCoefficient()
393 float Airplane::getLiftRatio()
398 float Airplane::getCruiseAoA()
403 float Airplane::getTailIncidence()
405 return _tailIncidence;
408 char* Airplane::getFailureMsg()
413 int Airplane::getSolutionIterations()
415 return _solutionIterations;
418 void Airplane::setupState(float aoa, float speed, State* s)
420 float cosAoA = Math::cos(aoa);
421 float sinAoA = Math::sin(aoa);
422 s->orient[0] = cosAoA; s->orient[1] = 0; s->orient[2] = sinAoA;
423 s->orient[3] = 0; s->orient[4] = 1; s->orient[5] = 0;
424 s->orient[6] = -sinAoA; s->orient[7] = 0; s->orient[8] = cosAoA;
426 s->v[0] = speed; s->v[1] = 0; s->v[2] = 0;
430 s->pos[i] = s->rot[i] = s->acc[i] = s->racc[i] = 0;
432 // Put us 1m above the origin, or else the gravity computation in
437 void Airplane::addContactPoint(float* pos)
439 ContactRec* c = new ContactRec;
447 float Airplane::compileWing(Wing* w)
449 // The tip of the wing is a contact point
452 addContactPoint(tip);
453 if(w->isMirrored()) {
455 addContactPoint(tip);
458 // Make sure it's initialized. The surfaces will pop out with
459 // total drag coefficients equal to their areas, which is what we
465 for(i=0; i<w->numSurfaces(); i++) {
466 Surface* s = (Surface*)w->getSurface(i);
468 float td = s->getTotalDrag();
471 _model.addSurface(s);
473 float mass = w->getSurfaceWeight(i);
474 mass = mass * Math::sqrt(mass);
477 _model.getBody()->addMass(mass, pos);
483 float Airplane::compileRotor(Rotor* r)
485 // Todo: add rotor to model!!!
486 // Todo: calc and add mass!!!
492 for(i=0; i<r->numRotorparts(); i++) {
493 Rotorpart* s = (Rotorpart*)r->getRotorpart(i);
495 _model.addRotorpart(s);
497 float mass = s->getWeight();
498 mass = mass * Math::sqrt(mass);
501 _model.getBody()->addMass(mass, pos);
505 for(i=0; i<r->numRotorblades(); i++) {
506 Rotorblade* b = (Rotorblade*)r->getRotorblade(i);
508 _model.addRotorblade(b);
510 float mass = b->getWeight();
511 mass = mass * Math::sqrt(mass);
514 _model.getBody()->addMass(mass, pos);
520 float Airplane::compileFuselage(Fuselage* f)
522 // The front and back are contact points
523 addContactPoint(f->front);
524 addContactPoint(f->back);
528 Math::sub3(f->front, f->back, fwd);
529 float len = Math::mag3(fwd);
530 float wid = f->width;
531 int segs = (int)Math::ceil(len/wid);
532 float segWgt = len*wid/segs;
534 for(j=0; j<segs; j++) {
535 float frac = (j+0.5f) / segs;
539 scale = f->taper+(1-f->taper) * (frac / f->mid);
541 scale = f->taper+(1-f->taper) * (frac - f->mid) / (1 - f->mid);
545 Math::mul3(frac, fwd, pos);
546 Math::add3(f->back, pos, pos);
548 // _Mass_ weighting goes as surface area^(3/2)
549 float mass = scale*segWgt * Math::sqrt(scale*segWgt);
550 _model.getBody()->addMass(mass, pos);
553 // Make a Surface too
554 Surface* s = new Surface();
556 float sideDrag = len/wid;
557 s->setYDrag(sideDrag);
558 s->setZDrag(sideDrag);
559 s->setTotalDrag(scale*segWgt);
561 // FIXME: fails for fuselages aligned along the Y axis
563 float *x=o, *y=o+3, *z=o+6; // nicknames for the axes
565 y[0] = 0; y[1] = 1; y[2] = 0;
566 Math::cross3(x, y, z);
568 Math::cross3(z, x, y);
569 s->setOrientation(o);
571 _model.addSurface(s);
577 // FIXME: should probably add a mass for the gear, too
578 void Airplane::compileGear(GearRec* gr)
582 // Make a Surface object for the aerodynamic behavior
583 Surface* s = new Surface();
586 // Put the surface at the half-way point on the gear strut, give
587 // it a drag coefficient equal to a square of the same dimension
588 // (gear are really draggy) and make it symmetric. Assume that
589 // the "length" of the gear is 3x the compression distance
590 float pos[3], cmp[3];
591 g->getCompression(cmp);
592 float length = 3 * Math::mag3(cmp);
594 Math::mul3(0.5, cmp, cmp);
595 Math::add3(pos, cmp, pos);
598 s->setTotalDrag(length*length);
601 _model.addSurface(s);
605 void Airplane::compileContactPoints()
607 // Figure it will compress by 20cm
610 comp[0] = 0; comp[1] = 0; comp[2] = DIST;
612 // Give it a spring constant such that at full compression it will
613 // hold up 10 times the planes mass. That's about right. Yeah.
614 float mass = _model.getBody()->getTotalMass();
615 float spring = (1/DIST) * 9.8f * 10.0f * mass;
616 float damp = 2 * Math::sqrt(spring * mass);
619 for(i=0; i<_contacts.size(); i++) {
620 ContactRec* c = (ContactRec*)_contacts.get(i);
622 Gear* g = new Gear();
624 g->setPosition(c->p);
626 g->setCompression(comp);
627 g->setSpring(spring);
632 g->setStaticFriction(0.6f);
633 g->setDynamicFriction(0.5f);
639 void Airplane::compile()
641 RigidBody* body = _model.getBody();
642 int firstMass = body->numMasses();
644 // Generate the point masses for the plane. Just use unitless
645 // numbers for a first pass, then go back through and rescale to
646 // make the weight right.
651 aeroWgt += compileWing(_wing);
653 aeroWgt += compileWing(_tail);
655 for(i=0; i<_vstabs.size(); i++)
656 aeroWgt += compileWing((Wing*)_vstabs.get(i));
657 for(i=0; i<_rotors.size(); i++)
658 aeroWgt += compileRotor((Rotor*)_rotors.get(i));
661 for(i=0; i<_fuselages.size(); i++)
662 aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i));
664 // Count up the absolute weight we have
665 float nonAeroWgt = _ballast;
666 for(i=0; i<_thrusters.size(); i++)
667 nonAeroWgt += ((ThrustRec*)_thrusters.get(i))->mass;
669 // Rescale to the specified empty weight
670 float wscale = (_emptyWeight-nonAeroWgt)/aeroWgt;
671 for(i=firstMass; i<body->numMasses(); i++)
672 body->setMass(i, body->getMass(i)*wscale);
674 // Add the thruster masses
675 for(i=0; i<_thrusters.size(); i++) {
676 ThrustRec* t = (ThrustRec*)_thrusters.get(i);
677 body->addMass(t->mass, t->cg);
680 // Add the tanks, empty for now.
682 for(i=0; i<_tanks.size(); i++) {
683 Tank* t = (Tank*)_tanks.get(i);
684 t->handle = body->addMass(0, t->pos);
687 _cruiseWeight = _emptyWeight + totalFuel*0.5f;
688 _approachWeight = _emptyWeight + totalFuel*0.2f;
692 // Add surfaces for the landing gear.
693 for(i=0; i<_gears.size(); i++)
694 compileGear((GearRec*)_gears.get(i));
696 // The Thruster objects
697 for(i=0; i<_thrusters.size(); i++) {
698 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
699 tr->handle = _model.addThruster(tr->thruster);
706 gespan = _wing->getGroundEffect(gepos);
707 _model.setGroundEffect(gepos, gespan, 0.15f);
711 if(_wing && _tail) solve();
712 else solveHelicopter();
714 // Do this after solveGear, because it creates "gear" objects that
715 // we don't want to affect.
716 compileContactPoints();
719 void Airplane::solveGear()
722 _model.getBody()->getCG(cg);
724 // Calculate spring constant weightings for the gear. Weight by
725 // the inverse of the distance to the c.g. in the XY plane, which
726 // should be correct for most gear arrangements. Add 50cm of
727 // "buffer" to keep things from blowing up with aircraft with a
728 // single gear very near the c.g. (AV-8, for example).
731 for(i=0; i<_gears.size(); i++) {
732 GearRec* gr = (GearRec*)_gears.get(i);
735 Math::sub3(cg, pos, pos);
736 gr->wgt = 1.0f/(0.5f+Math::sqrt(pos[0]*pos[0] + pos[1]*pos[1]));
740 // Renormalize so they sum to 1
741 for(i=0; i<_gears.size(); i++)
742 ((GearRec*)_gears.get(i))->wgt /= total;
744 // The force at max compression should be sufficient to stop a
745 // plane moving downwards at 2x the approach descent rate. Assume
746 // a 3 degree approach.
747 float descentRate = 2.0f*_approachSpeed/19.1f;
749 // Spread the kinetic energy according to the gear weights. This
750 // will results in an equal compression fraction (not distance) of
752 float energy = 0.5f*_approachWeight*descentRate*descentRate;
754 for(i=0; i<_gears.size(); i++) {
755 GearRec* gr = (GearRec*)_gears.get(i);
756 float e = energy * gr->wgt;
758 gr->gear->getCompression(comp);
759 float len = Math::mag3(comp);
761 // Energy in a spring: e = 0.5 * k * len^2
762 float k = 2 * e / (len*len);
764 gr->gear->setSpring(k * gr->gear->getSpring());
766 // Critically damped (too damped, too!)
767 gr->gear->setDamping(2*Math::sqrt(k*_approachWeight*gr->wgt)
768 * gr->gear->getDamping());
770 // These are pretty generic
771 gr->gear->setStaticFriction(0.8f);
772 gr->gear->setDynamicFriction(0.7f);
776 void Airplane::initEngines()
778 for(int i=0; i<_thrusters.size(); i++) {
779 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
780 tr->thruster->init();
784 void Airplane::stabilizeThrust()
787 for(i=0; i<_thrusters.size(); i++)
788 _model.getThruster(i)->stabilize();
791 void Airplane::setupWeights(bool isApproach)
794 for(i=0; i<_weights.size(); i++)
796 for(i=0; i<_solveWeights.size(); i++) {
797 SolveWeight* w = (SolveWeight*)_solveWeights.get(i);
798 if(w->approach == isApproach)
799 setWeight(w->idx, w->wgt);
803 void Airplane::runCruise()
805 setupState(_cruiseAoA, _cruiseSpeed, &_cruiseState);
806 _model.setState(&_cruiseState);
807 _model.setAir(_cruiseP, _cruiseT,
808 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
810 // The control configuration
813 for(i=0; i<_cruiseControls.size(); i++) {
814 Control* c = (Control*)_cruiseControls.get(i);
815 _controls.setInput(c->control, c->val);
817 _controls.applyControls(1000000); // Huge dt value
821 Math::mul3(-1, _cruiseState.v, wind);
822 Math::vmul33(_cruiseState.orient, wind, wind);
824 setFuelFraction(_cruiseFuel);
827 // Set up the thruster parameters and iterate until the thrust
829 for(i=0; i<_thrusters.size(); i++) {
830 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
832 t->setAir(_cruiseP, _cruiseT,
833 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
839 // Precompute thrust in the model, and calculate aerodynamic forces
840 _model.getBody()->recalc();
841 _model.getBody()->reset();
842 _model.initIteration();
843 _model.calcForces(&_cruiseState);
846 void Airplane::runApproach()
848 setupState(_approachAoA, _approachSpeed, &_approachState);
849 _model.setState(&_approachState);
850 _model.setAir(_approachP, _approachT,
851 Atmosphere::calcStdDensity(_approachP, _approachT));
853 // The control configuration
856 for(i=0; i<_approachControls.size(); i++) {
857 Control* c = (Control*)_approachControls.get(i);
858 _controls.setInput(c->control, c->val);
860 _controls.applyControls(1000000);
864 Math::mul3(-1, _approachState.v, wind);
865 Math::vmul33(_approachState.orient, wind, wind);
867 setFuelFraction(_approachFuel);
871 // Run the thrusters until they get to a stable setting. FIXME:
872 // this is lots of wasted work.
873 for(i=0; i<_thrusters.size(); i++) {
874 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
876 t->setAir(_approachP, _approachT,
877 Atmosphere::calcStdDensity(_approachP, _approachT));
883 // Precompute thrust in the model, and calculate aerodynamic forces
884 _model.getBody()->recalc();
885 _model.getBody()->reset();
886 _model.initIteration();
887 _model.calcForces(&_approachState);
890 void Airplane::applyDragFactor(float factor)
892 float applied = Math::pow(factor, SOLVE_TWEAK);
893 _dragFactor *= applied;
895 _wing->setDragScale(_wing->getDragScale() * applied);
897 _tail->setDragScale(_tail->getDragScale() * applied);
899 for(i=0; i<_vstabs.size(); i++) {
900 Wing* w = (Wing*)_vstabs.get(i);
901 w->setDragScale(w->getDragScale() * applied);
903 for(i=0; i<_surfs.size(); i++) {
904 Surface* s = (Surface*)_surfs.get(i);
905 s->setTotalDrag(s->getTotalDrag() * applied);
909 void Airplane::applyLiftRatio(float factor)
911 float applied = Math::pow(factor, SOLVE_TWEAK);
912 _liftRatio *= applied;
914 _wing->setLiftRatio(_wing->getLiftRatio() * applied);
916 _tail->setLiftRatio(_tail->getLiftRatio() * applied);
918 for(i=0; i<_vstabs.size(); i++) {
919 Wing* w = (Wing*)_vstabs.get(i);
920 w->setLiftRatio(w->getLiftRatio() * applied);
924 float Airplane::clamp(float val, float min, float max)
926 if(val < min) return min;
927 if(val > max) return max;
931 float Airplane::normFactor(float f)
938 void Airplane::solve()
940 static const float ARCMIN = 0.0002909f;
943 _solutionIterations = 0;
947 if(_solutionIterations++ > 10000) {
948 _failureMsg = "Solution failed to converge after 10000 iterations";
952 // Run an iteration at cruise, and extract the needed numbers:
955 _model.getThrust(tmp);
956 float thrust = tmp[0];
958 _model.getBody()->getAccel(tmp);
959 Math::tmul33(_cruiseState.orient, tmp, tmp);
960 float xforce = _cruiseWeight * tmp[0];
961 float clift0 = _cruiseWeight * tmp[2];
963 _model.getBody()->getAngularAccel(tmp);
964 Math::tmul33(_cruiseState.orient, tmp, tmp);
965 float pitch0 = tmp[1];
967 // Run an approach iteration, and do likewise
970 _model.getBody()->getAngularAccel(tmp);
971 Math::tmul33(_approachState.orient, tmp, tmp);
972 double apitch0 = tmp[1];
974 _model.getBody()->getAccel(tmp);
975 Math::tmul33(_approachState.orient, tmp, tmp);
976 float alift = _approachWeight * tmp[2];
978 // Modify the cruise AoA a bit to get a derivative
979 _cruiseAoA += ARCMIN;
981 _cruiseAoA -= ARCMIN;
983 _model.getBody()->getAccel(tmp);
984 Math::tmul33(_cruiseState.orient, tmp, tmp);
985 float clift1 = _cruiseWeight * tmp[2];
987 // Do the same with the tail incidence
988 _tail->setIncidence(_tailIncidence + ARCMIN);
990 _tail->setIncidence(_tailIncidence);
992 _model.getBody()->getAngularAccel(tmp);
993 Math::tmul33(_cruiseState.orient, tmp, tmp);
994 float pitch1 = tmp[1];
997 float awgt = 9.8f * _approachWeight;
999 float dragFactor = thrust / (thrust-xforce);
1000 float liftFactor = awgt / (awgt+alift);
1001 float aoaDelta = -clift0 * (ARCMIN/(clift1-clift0));
1002 float tailDelta = -pitch0 * (ARCMIN/(pitch1-pitch0));
1005 if(dragFactor <= 0 || liftFactor <= 0)
1008 // And the elevator control in the approach. This works just
1009 // like the tail incidence computation (it's solving for the
1010 // same thing -- pitching moment -- by diddling a different
1012 const float ELEVDIDDLE = 0.001f;
1013 _approachElevator.val += ELEVDIDDLE;
1015 _approachElevator.val -= ELEVDIDDLE;
1017 _model.getBody()->getAngularAccel(tmp);
1018 Math::tmul33(_approachState.orient, tmp, tmp);
1019 double apitch1 = tmp[1];
1020 float elevDelta = -apitch0 * (ELEVDIDDLE/(apitch1-apitch0));
1022 // Now apply the values we just computed. Note that the
1023 // "minor" variables are deferred until we get the lift/drag
1024 // numbers in the right ballpark.
1026 applyDragFactor(dragFactor);
1027 applyLiftRatio(liftFactor);
1029 // DON'T do the following until the above are sane
1030 if(normFactor(dragFactor) > STHRESH*1.0001
1031 || normFactor(liftFactor) > STHRESH*1.0001)
1036 // OK, now we can adjust the minor variables:
1037 _cruiseAoA += SOLVE_TWEAK*aoaDelta;
1038 _tailIncidence += SOLVE_TWEAK*tailDelta;
1040 _cruiseAoA = clamp(_cruiseAoA, -0.175f, 0.175f);
1041 _tailIncidence = clamp(_tailIncidence, -0.175f, 0.175f);
1043 if(abs(xforce/_cruiseWeight) < STHRESH*0.0001 &&
1044 abs(alift/_approachWeight) < STHRESH*0.0001 &&
1045 abs(aoaDelta) < STHRESH*.000017 &&
1046 abs(tailDelta) < STHRESH*.000017)
1048 // If this finaly value is OK, then we're all done
1049 if(abs(elevDelta) < STHRESH*0.0001)
1052 // Otherwise, adjust and do the next iteration
1053 _approachElevator.val += SOLVE_TWEAK * elevDelta;
1054 if(abs(_approachElevator.val) > 1) {
1055 _failureMsg = "Insufficient elevator to trim for approach";
1061 if(_dragFactor < 1e-06 || _dragFactor > 1e6) {
1062 _failureMsg = "Drag factor beyond reasonable bounds.";
1064 } else if(_liftRatio < 1e-04 || _liftRatio > 1e4) {
1065 _failureMsg = "Lift ratio beyond reasonable bounds.";
1067 } else if(Math::abs(_cruiseAoA) >= .17453293) {
1068 _failureMsg = "Cruise AoA > 10 degrees";
1070 } else if(Math::abs(_tailIncidence) >= .17453293) {
1071 _failureMsg = "Tail incidence > 10 degrees";
1076 void Airplane::solveHelicopter()
1078 _solutionIterations = 0;
1081 applyDragFactor(Math::pow(15.7/1000, 1/SOLVE_TWEAK));
1082 applyLiftRatio(Math::pow(104, 1/SOLVE_TWEAK));
1083 setupState(0,0, &_cruiseState);
1084 _model.setState(&_cruiseState);
1086 _model.getBody()->reset();
1089 }; // namespace yasim