5 #include "Atmosphere.hpp"
6 #include "ControlMap.hpp"
10 #include "RigidBody.hpp"
11 #include "Surface.hpp"
12 #include "Rotorpart.hpp"
13 #include "Thruster.hpp"
15 #include "Airplane.hpp"
20 inline float norm(float f) { return f<1 ? 1/f : f; }
21 inline float abs(float f) { return f<0 ? -f : f; }
23 // Solver threshold. How close to the solution are we trying
24 // to get? Trying too hard can result in oscillations about
25 // the correct solution, which is bad. Stick this in as a
26 // compile time constant for now, and consider making it
27 // settable per-model.
28 const float STHRESH = 1;
30 // How slowly do we change values in the solver. Too slow, and
31 // the solution converges very slowly. Too fast, and it can
33 const float SOLVE_TWEAK = 0.3226;
38 _pilotPos[0] = _pilotPos[1] = _pilotPos[2] = 0;
46 _cruiseGlideAngle = 0;
52 _approachGlideAngle = 0;
65 for(i=0; i<_fuselages.size(); i++)
66 delete (Fuselage*)_fuselages.get(i);
67 for(i=0; i<_tanks.size(); i++)
68 delete (Tank*)_tanks.get(i);
69 for(i=0; i<_thrusters.size(); i++)
70 delete (ThrustRec*)_thrusters.get(i);
71 for(i=0; i<_gears.size(); i++) {
72 GearRec* g = (GearRec*)_gears.get(i);
76 for(i=0; i<_surfs.size(); i++)
77 delete (Surface*)_surfs.get(i);
78 for(i=0; i<_contacts.size(); i++) {
79 ContactRec* c = (ContactRec*)_contacts.get(i);
83 for(i=0; i<_solveWeights.size(); i++)
84 delete (SolveWeight*)_solveWeights.get(i);
85 for(i=0; i<_cruiseControls.size(); i++)
86 delete (Control*)_cruiseControls.get(i);
87 for(i=0; i<_approachControls.size(); i++) {
88 Control* c = (Control*)_approachControls.get(i);
89 if(c != &_approachElevator)
94 for(i=0; i<_vstabs.size(); i++)
95 delete (Wing*)_vstabs.get(i);
96 for(i=0; i<_weights.size(); i++)
97 delete (WeightRec*)_weights.get(i);
100 void Airplane::iterate(float dt)
102 // The gear might have moved. Change their aerodynamics.
108 void Airplane::calcFuelWeights()
110 for(int i=0; i<_tanks.size(); i++) {
111 Tank* t = (Tank*)_tanks.get(i);
112 _model.getBody()->setMass(t->handle, t->fill);
116 ControlMap* Airplane::getControlMap()
121 Model* Airplane::getModel()
126 void Airplane::getPilotAccel(float* out)
128 State* s = _model.getState();
131 Glue::geodUp(s->pos, out);
132 Math::mul3(-9.8f, out, out);
133 Math::vmul33(s->orient, out, out);
136 // The regular acceleration
138 // Convert to aircraft coordinates
139 Math::vmul33(s->orient, s->acc, tmp);
143 Math::add3(tmp, out, out);
145 // FIXME: rotational & centripetal acceleration needed
148 void Airplane::setPilotPos(float* pos)
151 for(i=0; i<3; i++) _pilotPos[i] = pos[i];
154 void Airplane::getPilotPos(float* out)
157 for(i=0; i<3; i++) out[i] = _pilotPos[i];
160 int Airplane::numGear()
162 return _gears.size();
165 Gear* Airplane::getGear(int g)
167 return ((GearRec*)_gears.get(g))->gear;
170 Hook* Airplane::getHook()
172 return _model.getHook();
175 Launchbar* Airplane::getLaunchbar()
177 return _model.getLaunchbar();
180 Rotorgear* Airplane::getRotorgear()
182 return _model.getRotorgear();
185 void Airplane::updateGearState()
187 for(int i=0; i<_gears.size(); i++) {
188 GearRec* gr = (GearRec*)_gears.get(i);
189 float ext = gr->gear->getExtension();
191 gr->surf->setXDrag(ext);
192 gr->surf->setYDrag(ext);
193 gr->surf->setZDrag(ext);
197 void Airplane::setApproach(float speed, float altitude, float aoa, float fuel, float gla)
199 _approachSpeed = speed;
200 _approachP = Atmosphere::getStdPressure(altitude);
201 _approachT = Atmosphere::getStdTemperature(altitude);
203 _approachFuel = fuel;
204 _approachGlideAngle = gla;
207 void Airplane::setCruise(float speed, float altitude, float fuel, float gla)
209 _cruiseSpeed = speed;
210 _cruiseP = Atmosphere::getStdPressure(altitude);
211 _cruiseT = Atmosphere::getStdTemperature(altitude);
215 _cruiseGlideAngle = gla;
218 void Airplane::setElevatorControl(int control)
220 _approachElevator.control = control;
221 _approachElevator.val = 0;
222 _approachControls.add(&_approachElevator);
225 void Airplane::addApproachControl(int control, float val)
227 Control* c = new Control();
228 c->control = control;
230 _approachControls.add(c);
233 void Airplane::addCruiseControl(int control, float val)
235 Control* c = new Control();
236 c->control = control;
238 _cruiseControls.add(c);
241 void Airplane::addSolutionWeight(bool approach, int idx, float wgt)
243 SolveWeight* w = new SolveWeight();
244 w->approach = approach;
247 _solveWeights.add(w);
250 int Airplane::numTanks()
252 return _tanks.size();
255 float Airplane::getFuel(int tank)
257 return ((Tank*)_tanks.get(tank))->fill;
260 float Airplane::setFuel(int tank, float fuel)
262 return ((Tank*)_tanks.get(tank))->fill = fuel;
265 float Airplane::getFuelDensity(int tank)
267 return ((Tank*)_tanks.get(tank))->density;
270 float Airplane::getTankCapacity(int tank)
272 return ((Tank*)_tanks.get(tank))->cap;
275 void Airplane::setWeight(float weight)
277 _emptyWeight = weight;
280 void Airplane::setWing(Wing* wing)
285 void Airplane::setTail(Wing* tail)
290 void Airplane::addVStab(Wing* vstab)
295 void Airplane::addFuselage(float* front, float* back, float width,
296 float taper, float mid,
297 float cx, float cy, float cz, float idrag)
299 Fuselage* f = new Fuselage();
302 f->front[i] = front[i];
303 f->back[i] = back[i];
315 int Airplane::addTank(float* pos, float cap, float density)
317 Tank* t = new Tank();
319 for(i=0; i<3; i++) t->pos[i] = pos[i];
322 t->density = density;
323 t->handle = 0xffffffff;
324 return _tanks.add(t);
327 void Airplane::addGear(Gear* gear)
329 GearRec* g = new GearRec();
335 void Airplane::addHook(Hook* hook)
337 _model.addHook(hook);
340 void Airplane::addHitch(Hitch* hitch)
342 _model.addHitch(hitch);
345 void Airplane::addLaunchbar(Launchbar* launchbar)
347 _model.addLaunchbar(launchbar);
350 void Airplane::addThruster(Thruster* thruster, float mass, float* cg)
352 ThrustRec* t = new ThrustRec();
353 t->thruster = thruster;
356 for(i=0; i<3; i++) t->cg[i] = cg[i];
360 void Airplane::addBallast(float* pos, float mass)
362 _model.getBody()->addMass(mass, pos);
366 int Airplane::addWeight(float* pos, float size)
368 WeightRec* wr = new WeightRec();
369 wr->handle = _model.getBody()->addMass(0, pos);
371 wr->surf = new Surface();
372 wr->surf->setPosition(pos);
373 wr->surf->setTotalDrag(size*size);
374 _model.addSurface(wr->surf);
375 _surfs.add(wr->surf);
377 return _weights.add(wr);
380 void Airplane::setWeight(int handle, float mass)
382 WeightRec* wr = (WeightRec*)_weights.get(handle);
384 _model.getBody()->setMass(wr->handle, mass);
386 // Kill the aerodynamic drag if the mass is exactly zero. This is
387 // how we simulate droppable stores.
389 wr->surf->setXDrag(0);
390 wr->surf->setYDrag(0);
391 wr->surf->setZDrag(0);
393 wr->surf->setXDrag(1);
394 wr->surf->setYDrag(1);
395 wr->surf->setZDrag(1);
399 void Airplane::setFuelFraction(float frac)
402 for(i=0; i<_tanks.size(); i++) {
403 Tank* t = (Tank*)_tanks.get(i);
404 t->fill = frac * t->cap;
405 _model.getBody()->setMass(t->handle, t->cap * frac);
409 float Airplane::getDragCoefficient()
414 float Airplane::getLiftRatio()
419 float Airplane::getCruiseAoA()
424 float Airplane::getTailIncidence()
426 return _tailIncidence;
429 const char* Airplane::getFailureMsg()
434 int Airplane::getSolutionIterations()
436 return _solutionIterations;
439 void Airplane::setupState(float aoa, float speed, float gla, State* s)
441 float cosAoA = Math::cos(aoa);
442 float sinAoA = Math::sin(aoa);
443 s->orient[0] = cosAoA; s->orient[1] = 0; s->orient[2] = sinAoA;
444 s->orient[3] = 0; s->orient[4] = 1; s->orient[5] = 0;
445 s->orient[6] = -sinAoA; s->orient[7] = 0; s->orient[8] = cosAoA;
447 s->v[0] = speed*Math::cos(gla); s->v[1] = -speed*Math::sin(gla); s->v[2] = 0;
451 s->pos[i] = s->rot[i] = s->acc[i] = s->racc[i] = 0;
453 // Put us 1m above the origin, or else the gravity computation in
458 void Airplane::addContactPoint(float* pos)
460 ContactRec* c = new ContactRec;
468 float Airplane::compileWing(Wing* w)
470 // The tip of the wing is a contact point
473 addContactPoint(tip);
474 if(w->isMirrored()) {
476 addContactPoint(tip);
479 // Make sure it's initialized. The surfaces will pop out with
480 // total drag coefficients equal to their areas, which is what we
486 for(i=0; i<w->numSurfaces(); i++) {
487 Surface* s = (Surface*)w->getSurface(i);
489 float td = s->getTotalDrag();
492 _model.addSurface(s);
494 float mass = w->getSurfaceWeight(i);
495 mass = mass * Math::sqrt(mass);
498 _model.getBody()->addMass(mass, pos);
504 void Airplane::compileRotorgear()
506 getRotorgear()->compile();
509 float Airplane::compileFuselage(Fuselage* f)
511 // The front and back are contact points
512 addContactPoint(f->front);
513 addContactPoint(f->back);
517 Math::sub3(f->front, f->back, fwd);
518 float len = Math::mag3(fwd);
520 _failureMsg = "Zero length fuselage";
523 float wid = f->width;
524 int segs = (int)Math::ceil(len/wid);
525 float segWgt = len*wid/segs;
527 for(j=0; j<segs; j++) {
528 float frac = (j+0.5f) / segs;
532 scale = f->taper+(1-f->taper) * (frac / f->mid);
534 scale = f->taper+(1-f->taper) * (frac - f->mid) / (1 - f->mid);
538 Math::mul3(frac, fwd, pos);
539 Math::add3(f->back, pos, pos);
541 // _Mass_ weighting goes as surface area^(3/2)
542 float mass = scale*segWgt * Math::sqrt(scale*segWgt);
543 _model.getBody()->addMass(mass, pos);
546 // Make a Surface too
547 Surface* s = new Surface();
549 float sideDrag = len/wid;
550 s->setYDrag(sideDrag*f->_cy);
551 s->setZDrag(sideDrag*f->_cz);
552 s->setTotalDrag(scale*segWgt*f->_cx);
553 s->setInducedDrag(f->_idrag);
555 // FIXME: fails for fuselages aligned along the Y axis
557 float *x=o, *y=o+3, *z=o+6; // nicknames for the axes
559 y[0] = 0; y[1] = 1; y[2] = 0;
560 Math::cross3(x, y, z);
562 Math::cross3(z, x, y);
563 s->setOrientation(o);
565 _model.addSurface(s);
571 // FIXME: should probably add a mass for the gear, too
572 void Airplane::compileGear(GearRec* gr)
576 // Make a Surface object for the aerodynamic behavior
577 Surface* s = new Surface();
580 // Put the surface at the half-way point on the gear strut, give
581 // it a drag coefficient equal to a square of the same dimension
582 // (gear are really draggy) and make it symmetric. Assume that
583 // the "length" of the gear is 3x the compression distance
584 float pos[3], cmp[3];
585 g->getCompression(cmp);
586 float length = 3 * Math::mag3(cmp);
588 Math::mul3(0.5, cmp, cmp);
589 Math::add3(pos, cmp, pos);
592 s->setTotalDrag(length*length);
595 _model.addSurface(s);
599 void Airplane::compileContactPoints()
601 // Figure it will compress by 20cm
604 comp[0] = 0; comp[1] = 0; comp[2] = DIST;
606 // Give it a spring constant such that at full compression it will
607 // hold up 10 times the planes mass. That's about right. Yeah.
608 float mass = _model.getBody()->getTotalMass();
609 float spring = (1/DIST) * 9.8f * 10.0f * mass;
610 float damp = 2 * Math::sqrt(spring * mass);
613 for(i=0; i<_contacts.size(); i++) {
614 ContactRec* c = (ContactRec*)_contacts.get(i);
616 Gear* g = new Gear();
618 g->setPosition(c->p);
620 g->setCompression(comp);
621 g->setSpring(spring);
626 g->setStaticFriction(0.6f);
627 g->setDynamicFriction(0.5f);
628 g->setContactPoint(1);
634 void Airplane::compile()
636 RigidBody* body = _model.getBody();
637 int firstMass = body->numMasses();
639 // Generate the point masses for the plane. Just use unitless
640 // numbers for a first pass, then go back through and rescale to
641 // make the weight right.
646 aeroWgt += compileWing(_wing);
648 aeroWgt += compileWing(_tail);
650 for(i=0; i<_vstabs.size(); i++)
651 aeroWgt += compileWing((Wing*)_vstabs.get(i));
655 for(i=0; i<_fuselages.size(); i++)
656 aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i));
658 // Count up the absolute weight we have
659 float nonAeroWgt = _ballast;
660 for(i=0; i<_thrusters.size(); i++)
661 nonAeroWgt += ((ThrustRec*)_thrusters.get(i))->mass;
663 // Rescale to the specified empty weight
664 float wscale = (_emptyWeight-nonAeroWgt)/aeroWgt;
665 for(i=firstMass; i<body->numMasses(); i++)
666 body->setMass(i, body->getMass(i)*wscale);
668 // Add the thruster masses
669 for(i=0; i<_thrusters.size(); i++) {
670 ThrustRec* t = (ThrustRec*)_thrusters.get(i);
671 body->addMass(t->mass, t->cg);
674 // Add the tanks, empty for now.
676 for(i=0; i<_tanks.size(); i++) {
677 Tank* t = (Tank*)_tanks.get(i);
678 t->handle = body->addMass(0, t->pos);
681 _cruiseWeight = _emptyWeight + totalFuel*_cruiseFuel;
682 _approachWeight = _emptyWeight + totalFuel*_approachFuel;
686 // Add surfaces for the landing gear.
687 for(i=0; i<_gears.size(); i++)
688 compileGear((GearRec*)_gears.get(i));
690 // The Thruster objects
691 for(i=0; i<_thrusters.size(); i++) {
692 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
693 tr->handle = _model.addThruster(tr->thruster);
700 gespan = _wing->getGroundEffect(gepos);
701 _model.setGroundEffect(gepos, gespan, 0.15f);
704 // solve function below resets failure message
705 // so check if we have any problems and abort here
706 if (_failureMsg) return;
709 if(_wing && _tail) solve();
712 // The rotor(s) mass:
717 // Do this after solveGear, because it creates "gear" objects that
718 // we don't want to affect.
719 compileContactPoints();
722 void Airplane::solveGear()
725 _model.getBody()->getCG(cg);
727 // Calculate spring constant weightings for the gear. Weight by
728 // the inverse of the distance to the c.g. in the XY plane, which
729 // should be correct for most gear arrangements. Add 50cm of
730 // "buffer" to keep things from blowing up with aircraft with a
731 // single gear very near the c.g. (AV-8, for example).
734 for(i=0; i<_gears.size(); i++) {
735 GearRec* gr = (GearRec*)_gears.get(i);
738 Math::sub3(cg, pos, pos);
739 gr->wgt = 1.0f/(0.5f+Math::sqrt(pos[0]*pos[0] + pos[1]*pos[1]));
740 if (!g->getIgnoreWhileSolving())
744 // Renormalize so they sum to 1
745 for(i=0; i<_gears.size(); i++)
746 ((GearRec*)_gears.get(i))->wgt /= total;
748 // The force at max compression should be sufficient to stop a
749 // plane moving downwards at 2x the approach descent rate. Assume
750 // a 3 degree approach.
751 float descentRate = 2.0f*_approachSpeed/19.1f;
753 // Spread the kinetic energy according to the gear weights. This
754 // will results in an equal compression fraction (not distance) of
756 float energy = 0.5f*_approachWeight*descentRate*descentRate;
758 for(i=0; i<_gears.size(); i++) {
759 GearRec* gr = (GearRec*)_gears.get(i);
760 float e = energy * gr->wgt;
762 gr->gear->getCompression(comp);
763 float len = Math::mag3(comp)*(1+2*gr->gear->getInitialLoad());
765 // Energy in a spring: e = 0.5 * k * len^2
766 float k = 2 * e / (len*len);
768 gr->gear->setSpring(k * gr->gear->getSpring());
770 // Critically damped (too damped, too!)
771 gr->gear->setDamping(2*Math::sqrt(k*_approachWeight*gr->wgt)
772 * gr->gear->getDamping());
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,_cruiseGlideAngle, &_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,_approachGlideAngle, &_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] + _cruiseWeight * Math::sin(_cruiseGlideAngle) * 9.81;
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;
1080 if (getRotorgear()!=0)
1082 Rotorgear* rg = getRotorgear();
1083 applyDragFactor(Math::pow(rg->getYasimDragFactor()/1000,
1085 applyLiftRatio(Math::pow(rg->getYasimLiftFactor(),
1089 //huh, no wing and no rotor? (_rotorgear is constructed,
1090 //if a rotor is defined
1092 applyDragFactor(Math::pow(15.7/1000, 1/SOLVE_TWEAK));
1093 applyLiftRatio(Math::pow(104, 1/SOLVE_TWEAK));
1095 setupState(0,0,0, &_cruiseState);
1096 _model.setState(&_cruiseState);
1099 _model.getBody()->reset();
1100 _model.setAir(_cruiseP, _cruiseT,
1101 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
1105 }; // namespace yasim