1 #include "Atmosphere.hpp"
2 #include "ControlMap.hpp"
6 #include "RigidBody.hpp"
8 #include "Rotorpart.hpp"
9 #include "Thruster.hpp"
10 #include "Airplane.hpp"
15 inline float norm(float f) { return f<1 ? 1/f : f; }
16 inline float abs(float f) { return f<0 ? -f : f; }
18 // Solver threshold. How close to the solution are we trying
19 // to get? Trying too hard can result in oscillations about
20 // the correct solution, which is bad. Stick this in as a
21 // compile time constant for now, and consider making it
22 // settable per-model.
23 const float STHRESH = 1;
25 // How slowly do we change values in the solver. Too slow, and
26 // the solution converges very slowly. Too fast, and it can
28 const float SOLVE_TWEAK = 0.3226;
33 _pilotPos[0] = _pilotPos[1] = _pilotPos[2] = 0;
56 for(i=0; i<_fuselages.size(); i++)
57 delete (Fuselage*)_fuselages.get(i);
58 for(i=0; i<_tanks.size(); i++)
59 delete (Tank*)_tanks.get(i);
60 for(i=0; i<_thrusters.size(); i++)
61 delete (ThrustRec*)_thrusters.get(i);
62 for(i=0; i<_gears.size(); i++) {
63 GearRec* g = (GearRec*)_gears.get(i);
67 for(i=0; i<_surfs.size(); i++)
68 delete (Surface*)_surfs.get(i);
69 for(i=0; i<_contacts.size(); i++) {
70 ContactRec* c = (ContactRec*)_contacts.get(i);
74 for(i=0; i<_solveWeights.size(); i++)
75 delete (SolveWeight*)_solveWeights.get(i);
76 for(i=0; i<_cruiseControls.size(); i++)
77 delete (Control*)_cruiseControls.get(i);
78 for(i=0; i<_approachControls.size(); i++) {
79 Control* c = (Control*)_approachControls.get(i);
80 if(c != &_approachElevator)
85 for(i=0; i<_vstabs.size(); i++)
86 delete (Wing*)_vstabs.get(i);
87 for(i=0; i<_weights.size(); i++)
88 delete (WeightRec*)_weights.get(i);
91 void Airplane::iterate(float dt)
93 // The gear might have moved. Change their aerodynamics.
99 void Airplane::calcFuelWeights()
101 for(int i=0; i<_tanks.size(); i++) {
102 Tank* t = (Tank*)_tanks.get(i);
103 _model.getBody()->setMass(t->handle, t->fill);
107 ControlMap* Airplane::getControlMap()
112 Model* Airplane::getModel()
117 void Airplane::getPilotAccel(float* out)
119 State* s = _model.getState();
122 Glue::geodUp(s->pos, out);
123 Math::mul3(-9.8f, out, out);
125 // The regular acceleration
127 Math::mul3(-1, s->acc, tmp);
128 Math::add3(tmp, out, out);
130 // Convert to aircraft coordinates
131 Math::vmul33(s->orient, out, out);
133 // FIXME: rotational & centripetal acceleration needed
136 void Airplane::setPilotPos(float* pos)
139 for(i=0; i<3; i++) _pilotPos[i] = pos[i];
142 void Airplane::getPilotPos(float* out)
145 for(i=0; i<3; i++) out[i] = _pilotPos[i];
148 int Airplane::numGear()
150 return _gears.size();
153 Gear* Airplane::getGear(int g)
155 return ((GearRec*)_gears.get(g))->gear;
158 Hook* Airplane::getHook()
160 return _model.getHook();
163 Launchbar* Airplane::getLaunchbar()
165 return _model.getLaunchbar();
168 Rotorgear* Airplane::getRotorgear()
170 return _model.getRotorgear();
173 void Airplane::updateGearState()
175 for(int i=0; i<_gears.size(); i++) {
176 GearRec* gr = (GearRec*)_gears.get(i);
177 float ext = gr->gear->getExtension();
179 gr->surf->setXDrag(ext);
180 gr->surf->setYDrag(ext);
181 gr->surf->setZDrag(ext);
185 void Airplane::setApproach(float speed, float altitude, float aoa, float fuel)
187 _approachSpeed = speed;
188 _approachP = Atmosphere::getStdPressure(altitude);
189 _approachT = Atmosphere::getStdTemperature(altitude);
191 _approachFuel = fuel;
194 void Airplane::setCruise(float speed, float altitude, float fuel)
196 _cruiseSpeed = speed;
197 _cruiseP = Atmosphere::getStdPressure(altitude);
198 _cruiseT = Atmosphere::getStdTemperature(altitude);
204 void Airplane::setElevatorControl(int control)
206 _approachElevator.control = control;
207 _approachElevator.val = 0;
208 _approachControls.add(&_approachElevator);
211 void Airplane::addApproachControl(int control, float val)
213 Control* c = new Control();
214 c->control = control;
216 _approachControls.add(c);
219 void Airplane::addCruiseControl(int control, float val)
221 Control* c = new Control();
222 c->control = control;
224 _cruiseControls.add(c);
227 void Airplane::addSolutionWeight(bool approach, int idx, float wgt)
229 SolveWeight* w = new SolveWeight();
230 w->approach = approach;
233 _solveWeights.add(w);
236 int Airplane::numTanks()
238 return _tanks.size();
241 float Airplane::getFuel(int tank)
243 return ((Tank*)_tanks.get(tank))->fill;
246 float Airplane::setFuel(int tank, float fuel)
248 return ((Tank*)_tanks.get(tank))->fill = fuel;
251 float Airplane::getFuelDensity(int tank)
253 return ((Tank*)_tanks.get(tank))->density;
256 float Airplane::getTankCapacity(int tank)
258 return ((Tank*)_tanks.get(tank))->cap;
261 void Airplane::setWeight(float weight)
263 _emptyWeight = weight;
266 void Airplane::setWing(Wing* wing)
271 void Airplane::setTail(Wing* tail)
276 void Airplane::addVStab(Wing* vstab)
281 void Airplane::addFuselage(float* front, float* back, float width,
282 float taper, float mid,
283 float cx, float cy, float cz, float idrag)
285 Fuselage* f = new Fuselage();
288 f->front[i] = front[i];
289 f->back[i] = back[i];
301 int Airplane::addTank(float* pos, float cap, float density)
303 Tank* t = new Tank();
305 for(i=0; i<3; i++) t->pos[i] = pos[i];
308 t->density = density;
309 t->handle = 0xffffffff;
310 return _tanks.add(t);
313 void Airplane::addGear(Gear* gear)
315 GearRec* g = new GearRec();
321 void Airplane::addHook(Hook* hook)
323 _model.addHook(hook);
326 void Airplane::addLaunchbar(Launchbar* launchbar)
328 _model.addLaunchbar(launchbar);
331 void Airplane::addThruster(Thruster* thruster, float mass, float* cg)
333 ThrustRec* t = new ThrustRec();
334 t->thruster = thruster;
337 for(i=0; i<3; i++) t->cg[i] = cg[i];
341 void Airplane::addBallast(float* pos, float mass)
343 _model.getBody()->addMass(mass, pos);
347 int Airplane::addWeight(float* pos, float size)
349 WeightRec* wr = new WeightRec();
350 wr->handle = _model.getBody()->addMass(0, pos);
352 wr->surf = new Surface();
353 wr->surf->setPosition(pos);
354 wr->surf->setTotalDrag(size*size);
355 _model.addSurface(wr->surf);
356 _surfs.add(wr->surf);
358 return _weights.add(wr);
361 void Airplane::setWeight(int handle, float mass)
363 WeightRec* wr = (WeightRec*)_weights.get(handle);
365 _model.getBody()->setMass(wr->handle, mass);
367 // Kill the aerodynamic drag if the mass is exactly zero. This is
368 // how we simulate droppable stores.
370 wr->surf->setXDrag(0);
371 wr->surf->setYDrag(0);
372 wr->surf->setZDrag(0);
374 wr->surf->setXDrag(1);
375 wr->surf->setYDrag(1);
376 wr->surf->setZDrag(1);
380 void Airplane::setFuelFraction(float frac)
383 for(i=0; i<_tanks.size(); i++) {
384 Tank* t = (Tank*)_tanks.get(i);
385 t->fill = frac * t->cap;
386 _model.getBody()->setMass(t->handle, t->cap * frac);
390 float Airplane::getDragCoefficient()
395 float Airplane::getLiftRatio()
400 float Airplane::getCruiseAoA()
405 float Airplane::getTailIncidence()
407 return _tailIncidence;
410 char* Airplane::getFailureMsg()
415 int Airplane::getSolutionIterations()
417 return _solutionIterations;
420 void Airplane::setupState(float aoa, float speed, State* s)
422 float cosAoA = Math::cos(aoa);
423 float sinAoA = Math::sin(aoa);
424 s->orient[0] = cosAoA; s->orient[1] = 0; s->orient[2] = sinAoA;
425 s->orient[3] = 0; s->orient[4] = 1; s->orient[5] = 0;
426 s->orient[6] = -sinAoA; s->orient[7] = 0; s->orient[8] = cosAoA;
428 s->v[0] = speed; s->v[1] = 0; s->v[2] = 0;
432 s->pos[i] = s->rot[i] = s->acc[i] = s->racc[i] = 0;
434 // Put us 1m above the origin, or else the gravity computation in
439 void Airplane::addContactPoint(float* pos)
441 ContactRec* c = new ContactRec;
449 float Airplane::compileWing(Wing* w)
451 // The tip of the wing is a contact point
454 addContactPoint(tip);
455 if(w->isMirrored()) {
457 addContactPoint(tip);
460 // Make sure it's initialized. The surfaces will pop out with
461 // total drag coefficients equal to their areas, which is what we
467 for(i=0; i<w->numSurfaces(); i++) {
468 Surface* s = (Surface*)w->getSurface(i);
470 float td = s->getTotalDrag();
473 _model.addSurface(s);
475 float mass = w->getSurfaceWeight(i);
476 mass = mass * Math::sqrt(mass);
479 _model.getBody()->addMass(mass, pos);
485 float Airplane::compileRotorgear()
487 return getRotorgear()->compile(_model.getBody());
490 float Airplane::compileFuselage(Fuselage* f)
492 // The front and back are contact points
493 addContactPoint(f->front);
494 addContactPoint(f->back);
498 Math::sub3(f->front, f->back, fwd);
499 float len = Math::mag3(fwd);
500 float wid = f->width;
501 int segs = (int)Math::ceil(len/wid);
502 float segWgt = len*wid/segs;
504 for(j=0; j<segs; j++) {
505 float frac = (j+0.5f) / segs;
509 scale = f->taper+(1-f->taper) * (frac / f->mid);
511 scale = f->taper+(1-f->taper) * (frac - f->mid) / (1 - f->mid);
515 Math::mul3(frac, fwd, pos);
516 Math::add3(f->back, pos, pos);
518 // _Mass_ weighting goes as surface area^(3/2)
519 float mass = scale*segWgt * Math::sqrt(scale*segWgt);
520 _model.getBody()->addMass(mass, pos);
523 // Make a Surface too
524 Surface* s = new Surface();
526 float sideDrag = len/wid;
527 s->setYDrag(sideDrag*f->_cy);
528 s->setZDrag(sideDrag*f->_cz);
529 s->setTotalDrag(scale*segWgt*f->_cx);
530 s->setInducedDrag(f->_idrag);
532 // FIXME: fails for fuselages aligned along the Y axis
534 float *x=o, *y=o+3, *z=o+6; // nicknames for the axes
536 y[0] = 0; y[1] = 1; y[2] = 0;
537 Math::cross3(x, y, z);
539 Math::cross3(z, x, y);
540 s->setOrientation(o);
542 _model.addSurface(s);
548 // FIXME: should probably add a mass for the gear, too
549 void Airplane::compileGear(GearRec* gr)
553 // Make a Surface object for the aerodynamic behavior
554 Surface* s = new Surface();
557 // Put the surface at the half-way point on the gear strut, give
558 // it a drag coefficient equal to a square of the same dimension
559 // (gear are really draggy) and make it symmetric. Assume that
560 // the "length" of the gear is 3x the compression distance
561 float pos[3], cmp[3];
562 g->getCompression(cmp);
563 float length = 3 * Math::mag3(cmp);
565 Math::mul3(0.5, cmp, cmp);
566 Math::add3(pos, cmp, pos);
569 s->setTotalDrag(length*length);
572 _model.addSurface(s);
576 void Airplane::compileContactPoints()
578 // Figure it will compress by 20cm
581 comp[0] = 0; comp[1] = 0; comp[2] = DIST;
583 // Give it a spring constant such that at full compression it will
584 // hold up 10 times the planes mass. That's about right. Yeah.
585 float mass = _model.getBody()->getTotalMass();
586 float spring = (1/DIST) * 9.8f * 10.0f * mass;
587 float damp = 2 * Math::sqrt(spring * mass);
590 for(i=0; i<_contacts.size(); i++) {
591 ContactRec* c = (ContactRec*)_contacts.get(i);
593 Gear* g = new Gear();
595 g->setPosition(c->p);
597 g->setCompression(comp);
598 g->setSpring(spring);
603 g->setStaticFriction(0.6f);
604 g->setDynamicFriction(0.5f);
610 void Airplane::compile()
612 RigidBody* body = _model.getBody();
613 int firstMass = body->numMasses();
615 // Generate the point masses for the plane. Just use unitless
616 // numbers for a first pass, then go back through and rescale to
617 // make the weight right.
622 aeroWgt += compileWing(_wing);
624 aeroWgt += compileWing(_tail);
626 for(i=0; i<_vstabs.size(); i++)
627 aeroWgt += compileWing((Wing*)_vstabs.get(i));
630 aeroWgt += compileRotorgear();
633 for(i=0; i<_fuselages.size(); i++)
634 aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i));
636 // Count up the absolute weight we have
637 float nonAeroWgt = _ballast;
638 for(i=0; i<_thrusters.size(); i++)
639 nonAeroWgt += ((ThrustRec*)_thrusters.get(i))->mass;
641 // Rescale to the specified empty weight
642 float wscale = (_emptyWeight-nonAeroWgt)/aeroWgt;
643 for(i=firstMass; i<body->numMasses(); i++)
644 body->setMass(i, body->getMass(i)*wscale);
646 // Add the thruster masses
647 for(i=0; i<_thrusters.size(); i++) {
648 ThrustRec* t = (ThrustRec*)_thrusters.get(i);
649 body->addMass(t->mass, t->cg);
652 // Add the tanks, empty for now.
654 for(i=0; i<_tanks.size(); i++) {
655 Tank* t = (Tank*)_tanks.get(i);
656 t->handle = body->addMass(0, t->pos);
659 _cruiseWeight = _emptyWeight + totalFuel*0.5f;
660 _approachWeight = _emptyWeight + totalFuel*0.2f;
664 // Add surfaces for the landing gear.
665 for(i=0; i<_gears.size(); i++)
666 compileGear((GearRec*)_gears.get(i));
668 // The Thruster objects
669 for(i=0; i<_thrusters.size(); i++) {
670 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
671 tr->handle = _model.addThruster(tr->thruster);
678 gespan = _wing->getGroundEffect(gepos);
679 _model.setGroundEffect(gepos, gespan, 0.15f);
683 if(_wing && _tail) solve();
684 else solveHelicopter();
686 // Do this after solveGear, because it creates "gear" objects that
687 // we don't want to affect.
688 compileContactPoints();
691 void Airplane::solveGear()
694 _model.getBody()->getCG(cg);
696 // Calculate spring constant weightings for the gear. Weight by
697 // the inverse of the distance to the c.g. in the XY plane, which
698 // should be correct for most gear arrangements. Add 50cm of
699 // "buffer" to keep things from blowing up with aircraft with a
700 // single gear very near the c.g. (AV-8, for example).
703 for(i=0; i<_gears.size(); i++) {
704 GearRec* gr = (GearRec*)_gears.get(i);
707 Math::sub3(cg, pos, pos);
708 gr->wgt = 1.0f/(0.5f+Math::sqrt(pos[0]*pos[0] + pos[1]*pos[1]));
712 // Renormalize so they sum to 1
713 for(i=0; i<_gears.size(); i++)
714 ((GearRec*)_gears.get(i))->wgt /= total;
716 // The force at max compression should be sufficient to stop a
717 // plane moving downwards at 2x the approach descent rate. Assume
718 // a 3 degree approach.
719 float descentRate = 2.0f*_approachSpeed/19.1f;
721 // Spread the kinetic energy according to the gear weights. This
722 // will results in an equal compression fraction (not distance) of
724 float energy = 0.5f*_approachWeight*descentRate*descentRate;
726 for(i=0; i<_gears.size(); i++) {
727 GearRec* gr = (GearRec*)_gears.get(i);
728 float e = energy * gr->wgt;
730 gr->gear->getCompression(comp);
731 float len = Math::mag3(comp);
733 // Energy in a spring: e = 0.5 * k * len^2
734 float k = 2 * e / (len*len);
736 gr->gear->setSpring(k * gr->gear->getSpring());
738 // Critically damped (too damped, too!)
739 gr->gear->setDamping(2*Math::sqrt(k*_approachWeight*gr->wgt)
740 * gr->gear->getDamping());
744 void Airplane::initEngines()
746 for(int i=0; i<_thrusters.size(); i++) {
747 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
748 tr->thruster->init();
752 void Airplane::stabilizeThrust()
755 for(i=0; i<_thrusters.size(); i++)
756 _model.getThruster(i)->stabilize();
759 void Airplane::setupWeights(bool isApproach)
762 for(i=0; i<_weights.size(); i++)
764 for(i=0; i<_solveWeights.size(); i++) {
765 SolveWeight* w = (SolveWeight*)_solveWeights.get(i);
766 if(w->approach == isApproach)
767 setWeight(w->idx, w->wgt);
771 void Airplane::runCruise()
773 setupState(_cruiseAoA, _cruiseSpeed, &_cruiseState);
774 _model.setState(&_cruiseState);
775 _model.setAir(_cruiseP, _cruiseT,
776 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
778 // The control configuration
781 for(i=0; i<_cruiseControls.size(); i++) {
782 Control* c = (Control*)_cruiseControls.get(i);
783 _controls.setInput(c->control, c->val);
785 _controls.applyControls(1000000); // Huge dt value
789 Math::mul3(-1, _cruiseState.v, wind);
790 Math::vmul33(_cruiseState.orient, wind, wind);
792 setFuelFraction(_cruiseFuel);
795 // Set up the thruster parameters and iterate until the thrust
797 for(i=0; i<_thrusters.size(); i++) {
798 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
800 t->setAir(_cruiseP, _cruiseT,
801 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
807 // Precompute thrust in the model, and calculate aerodynamic forces
808 _model.getBody()->recalc();
809 _model.getBody()->reset();
810 _model.initIteration();
811 _model.calcForces(&_cruiseState);
814 void Airplane::runApproach()
816 setupState(_approachAoA, _approachSpeed, &_approachState);
817 _model.setState(&_approachState);
818 _model.setAir(_approachP, _approachT,
819 Atmosphere::calcStdDensity(_approachP, _approachT));
821 // The control configuration
824 for(i=0; i<_approachControls.size(); i++) {
825 Control* c = (Control*)_approachControls.get(i);
826 _controls.setInput(c->control, c->val);
828 _controls.applyControls(1000000);
832 Math::mul3(-1, _approachState.v, wind);
833 Math::vmul33(_approachState.orient, wind, wind);
835 setFuelFraction(_approachFuel);
839 // Run the thrusters until they get to a stable setting. FIXME:
840 // this is lots of wasted work.
841 for(i=0; i<_thrusters.size(); i++) {
842 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
844 t->setAir(_approachP, _approachT,
845 Atmosphere::calcStdDensity(_approachP, _approachT));
851 // Precompute thrust in the model, and calculate aerodynamic forces
852 _model.getBody()->recalc();
853 _model.getBody()->reset();
854 _model.initIteration();
855 _model.calcForces(&_approachState);
858 void Airplane::applyDragFactor(float factor)
860 float applied = Math::pow(factor, SOLVE_TWEAK);
861 _dragFactor *= applied;
863 _wing->setDragScale(_wing->getDragScale() * applied);
865 _tail->setDragScale(_tail->getDragScale() * applied);
867 for(i=0; i<_vstabs.size(); i++) {
868 Wing* w = (Wing*)_vstabs.get(i);
869 w->setDragScale(w->getDragScale() * applied);
871 for(i=0; i<_surfs.size(); i++) {
872 Surface* s = (Surface*)_surfs.get(i);
873 s->setTotalDrag(s->getTotalDrag() * applied);
877 void Airplane::applyLiftRatio(float factor)
879 float applied = Math::pow(factor, SOLVE_TWEAK);
880 _liftRatio *= applied;
882 _wing->setLiftRatio(_wing->getLiftRatio() * applied);
884 _tail->setLiftRatio(_tail->getLiftRatio() * applied);
886 for(i=0; i<_vstabs.size(); i++) {
887 Wing* w = (Wing*)_vstabs.get(i);
888 w->setLiftRatio(w->getLiftRatio() * applied);
892 float Airplane::clamp(float val, float min, float max)
894 if(val < min) return min;
895 if(val > max) return max;
899 float Airplane::normFactor(float f)
906 void Airplane::solve()
908 static const float ARCMIN = 0.0002909f;
911 _solutionIterations = 0;
915 if(_solutionIterations++ > 10000) {
916 _failureMsg = "Solution failed to converge after 10000 iterations";
920 // Run an iteration at cruise, and extract the needed numbers:
923 _model.getThrust(tmp);
924 float thrust = tmp[0];
926 _model.getBody()->getAccel(tmp);
927 Math::tmul33(_cruiseState.orient, tmp, tmp);
928 float xforce = _cruiseWeight * tmp[0];
929 float clift0 = _cruiseWeight * tmp[2];
931 _model.getBody()->getAngularAccel(tmp);
932 Math::tmul33(_cruiseState.orient, tmp, tmp);
933 float pitch0 = tmp[1];
935 // Run an approach iteration, and do likewise
938 _model.getBody()->getAngularAccel(tmp);
939 Math::tmul33(_approachState.orient, tmp, tmp);
940 double apitch0 = tmp[1];
942 _model.getBody()->getAccel(tmp);
943 Math::tmul33(_approachState.orient, tmp, tmp);
944 float alift = _approachWeight * tmp[2];
946 // Modify the cruise AoA a bit to get a derivative
947 _cruiseAoA += ARCMIN;
949 _cruiseAoA -= ARCMIN;
951 _model.getBody()->getAccel(tmp);
952 Math::tmul33(_cruiseState.orient, tmp, tmp);
953 float clift1 = _cruiseWeight * tmp[2];
955 // Do the same with the tail incidence
956 _tail->setIncidence(_tailIncidence + ARCMIN);
958 _tail->setIncidence(_tailIncidence);
960 _model.getBody()->getAngularAccel(tmp);
961 Math::tmul33(_cruiseState.orient, tmp, tmp);
962 float pitch1 = tmp[1];
965 float awgt = 9.8f * _approachWeight;
967 float dragFactor = thrust / (thrust-xforce);
968 float liftFactor = awgt / (awgt+alift);
969 float aoaDelta = -clift0 * (ARCMIN/(clift1-clift0));
970 float tailDelta = -pitch0 * (ARCMIN/(pitch1-pitch0));
973 if(dragFactor <= 0 || liftFactor <= 0)
976 // And the elevator control in the approach. This works just
977 // like the tail incidence computation (it's solving for the
978 // same thing -- pitching moment -- by diddling a different
980 const float ELEVDIDDLE = 0.001f;
981 _approachElevator.val += ELEVDIDDLE;
983 _approachElevator.val -= ELEVDIDDLE;
985 _model.getBody()->getAngularAccel(tmp);
986 Math::tmul33(_approachState.orient, tmp, tmp);
987 double apitch1 = tmp[1];
988 float elevDelta = -apitch0 * (ELEVDIDDLE/(apitch1-apitch0));
990 // Now apply the values we just computed. Note that the
991 // "minor" variables are deferred until we get the lift/drag
992 // numbers in the right ballpark.
994 applyDragFactor(dragFactor);
995 applyLiftRatio(liftFactor);
997 // DON'T do the following until the above are sane
998 if(normFactor(dragFactor) > STHRESH*1.0001
999 || normFactor(liftFactor) > STHRESH*1.0001)
1004 // OK, now we can adjust the minor variables:
1005 _cruiseAoA += SOLVE_TWEAK*aoaDelta;
1006 _tailIncidence += SOLVE_TWEAK*tailDelta;
1008 _cruiseAoA = clamp(_cruiseAoA, -0.175f, 0.175f);
1009 _tailIncidence = clamp(_tailIncidence, -0.175f, 0.175f);
1011 if(abs(xforce/_cruiseWeight) < STHRESH*0.0001 &&
1012 abs(alift/_approachWeight) < STHRESH*0.0001 &&
1013 abs(aoaDelta) < STHRESH*.000017 &&
1014 abs(tailDelta) < STHRESH*.000017)
1016 // If this finaly value is OK, then we're all done
1017 if(abs(elevDelta) < STHRESH*0.0001)
1020 // Otherwise, adjust and do the next iteration
1021 _approachElevator.val += SOLVE_TWEAK * elevDelta;
1022 if(abs(_approachElevator.val) > 1) {
1023 _failureMsg = "Insufficient elevator to trim for approach";
1029 if(_dragFactor < 1e-06 || _dragFactor > 1e6) {
1030 _failureMsg = "Drag factor beyond reasonable bounds.";
1032 } else if(_liftRatio < 1e-04 || _liftRatio > 1e4) {
1033 _failureMsg = "Lift ratio beyond reasonable bounds.";
1035 } else if(Math::abs(_cruiseAoA) >= .17453293) {
1036 _failureMsg = "Cruise AoA > 10 degrees";
1038 } else if(Math::abs(_tailIncidence) >= .17453293) {
1039 _failureMsg = "Tail incidence > 10 degrees";
1044 void Airplane::solveHelicopter()
1046 _solutionIterations = 0;
1048 if (getRotorgear()!=0)
1050 Rotorgear* rg = getRotorgear();
1051 applyDragFactor(Math::pow(rg->getYasimDragFactor()/1000,
1053 applyLiftRatio(Math::pow(rg->getYasimLiftFactor(),
1057 //huh, no wing and no rotor? (_rotorgear is constructed,
1058 //if a rotor is defined
1060 applyDragFactor(Math::pow(15.7/1000, 1/SOLVE_TWEAK));
1061 applyLiftRatio(Math::pow(104, 1/SOLVE_TWEAK));
1063 setupState(0,0, &_cruiseState);
1064 _model.setState(&_cruiseState);
1067 _model.getBody()->reset();
1068 _model.setAir(_cruiseP, _cruiseT,
1069 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
1073 }; // namespace yasim