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)
284 Fuselage* f = new Fuselage();
287 f->front[i] = front[i];
288 f->back[i] = back[i];
296 int Airplane::addTank(float* pos, float cap, float density)
298 Tank* t = new Tank();
300 for(i=0; i<3; i++) t->pos[i] = pos[i];
303 t->density = density;
304 t->handle = 0xffffffff;
305 return _tanks.add(t);
308 void Airplane::addGear(Gear* gear)
310 GearRec* g = new GearRec();
316 void Airplane::addHook(Hook* hook)
318 _model.addHook(hook);
321 void Airplane::addLaunchbar(Launchbar* launchbar)
323 _model.addLaunchbar(launchbar);
326 void Airplane::addThruster(Thruster* thruster, float mass, float* cg)
328 ThrustRec* t = new ThrustRec();
329 t->thruster = thruster;
332 for(i=0; i<3; i++) t->cg[i] = cg[i];
336 void Airplane::addBallast(float* pos, float mass)
338 _model.getBody()->addMass(mass, pos);
342 int Airplane::addWeight(float* pos, float size)
344 WeightRec* wr = new WeightRec();
345 wr->handle = _model.getBody()->addMass(0, pos);
347 wr->surf = new Surface();
348 wr->surf->setPosition(pos);
349 wr->surf->setTotalDrag(size*size);
350 _model.addSurface(wr->surf);
351 _surfs.add(wr->surf);
353 return _weights.add(wr);
356 void Airplane::setWeight(int handle, float mass)
358 WeightRec* wr = (WeightRec*)_weights.get(handle);
360 _model.getBody()->setMass(wr->handle, mass);
362 // Kill the aerodynamic drag if the mass is exactly zero. This is
363 // how we simulate droppable stores.
365 wr->surf->setXDrag(0);
366 wr->surf->setYDrag(0);
367 wr->surf->setZDrag(0);
369 wr->surf->setXDrag(1);
370 wr->surf->setYDrag(1);
371 wr->surf->setZDrag(1);
375 void Airplane::setFuelFraction(float frac)
378 for(i=0; i<_tanks.size(); i++) {
379 Tank* t = (Tank*)_tanks.get(i);
380 t->fill = frac * t->cap;
381 _model.getBody()->setMass(t->handle, t->cap * frac);
385 float Airplane::getDragCoefficient()
390 float Airplane::getLiftRatio()
395 float Airplane::getCruiseAoA()
400 float Airplane::getTailIncidence()
402 return _tailIncidence;
405 char* Airplane::getFailureMsg()
410 int Airplane::getSolutionIterations()
412 return _solutionIterations;
415 void Airplane::setupState(float aoa, float speed, State* s)
417 float cosAoA = Math::cos(aoa);
418 float sinAoA = Math::sin(aoa);
419 s->orient[0] = cosAoA; s->orient[1] = 0; s->orient[2] = sinAoA;
420 s->orient[3] = 0; s->orient[4] = 1; s->orient[5] = 0;
421 s->orient[6] = -sinAoA; s->orient[7] = 0; s->orient[8] = cosAoA;
423 s->v[0] = speed; s->v[1] = 0; s->v[2] = 0;
427 s->pos[i] = s->rot[i] = s->acc[i] = s->racc[i] = 0;
429 // Put us 1m above the origin, or else the gravity computation in
434 void Airplane::addContactPoint(float* pos)
436 ContactRec* c = new ContactRec;
444 float Airplane::compileWing(Wing* w)
446 // The tip of the wing is a contact point
449 addContactPoint(tip);
450 if(w->isMirrored()) {
452 addContactPoint(tip);
455 // Make sure it's initialized. The surfaces will pop out with
456 // total drag coefficients equal to their areas, which is what we
462 for(i=0; i<w->numSurfaces(); i++) {
463 Surface* s = (Surface*)w->getSurface(i);
465 float td = s->getTotalDrag();
468 _model.addSurface(s);
470 float mass = w->getSurfaceWeight(i);
471 mass = mass * Math::sqrt(mass);
474 _model.getBody()->addMass(mass, pos);
480 float Airplane::compileRotorgear()
482 return getRotorgear()->compile(_model.getBody());
485 float Airplane::compileFuselage(Fuselage* f)
487 // The front and back are contact points
488 addContactPoint(f->front);
489 addContactPoint(f->back);
493 Math::sub3(f->front, f->back, fwd);
494 float len = Math::mag3(fwd);
495 float wid = f->width;
496 int segs = (int)Math::ceil(len/wid);
497 float segWgt = len*wid/segs;
499 for(j=0; j<segs; j++) {
500 float frac = (j+0.5f) / segs;
504 scale = f->taper+(1-f->taper) * (frac / f->mid);
506 scale = f->taper+(1-f->taper) * (frac - f->mid) / (1 - f->mid);
510 Math::mul3(frac, fwd, pos);
511 Math::add3(f->back, pos, pos);
513 // _Mass_ weighting goes as surface area^(3/2)
514 float mass = scale*segWgt * Math::sqrt(scale*segWgt);
515 _model.getBody()->addMass(mass, pos);
518 // Make a Surface too
519 Surface* s = new Surface();
521 float sideDrag = len/wid;
522 s->setYDrag(sideDrag);
523 s->setZDrag(sideDrag);
524 s->setTotalDrag(scale*segWgt);
526 // FIXME: fails for fuselages aligned along the Y axis
528 float *x=o, *y=o+3, *z=o+6; // nicknames for the axes
530 y[0] = 0; y[1] = 1; y[2] = 0;
531 Math::cross3(x, y, z);
533 Math::cross3(z, x, y);
534 s->setOrientation(o);
536 _model.addSurface(s);
542 // FIXME: should probably add a mass for the gear, too
543 void Airplane::compileGear(GearRec* gr)
547 // Make a Surface object for the aerodynamic behavior
548 Surface* s = new Surface();
551 // Put the surface at the half-way point on the gear strut, give
552 // it a drag coefficient equal to a square of the same dimension
553 // (gear are really draggy) and make it symmetric. Assume that
554 // the "length" of the gear is 3x the compression distance
555 float pos[3], cmp[3];
556 g->getCompression(cmp);
557 float length = 3 * Math::mag3(cmp);
559 Math::mul3(0.5, cmp, cmp);
560 Math::add3(pos, cmp, pos);
563 s->setTotalDrag(length*length);
566 _model.addSurface(s);
570 void Airplane::compileContactPoints()
572 // Figure it will compress by 20cm
575 comp[0] = 0; comp[1] = 0; comp[2] = DIST;
577 // Give it a spring constant such that at full compression it will
578 // hold up 10 times the planes mass. That's about right. Yeah.
579 float mass = _model.getBody()->getTotalMass();
580 float spring = (1/DIST) * 9.8f * 10.0f * mass;
581 float damp = 2 * Math::sqrt(spring * mass);
584 for(i=0; i<_contacts.size(); i++) {
585 ContactRec* c = (ContactRec*)_contacts.get(i);
587 Gear* g = new Gear();
589 g->setPosition(c->p);
591 g->setCompression(comp);
592 g->setSpring(spring);
597 g->setStaticFriction(0.6f);
598 g->setDynamicFriction(0.5f);
604 void Airplane::compile()
606 RigidBody* body = _model.getBody();
607 int firstMass = body->numMasses();
609 // Generate the point masses for the plane. Just use unitless
610 // numbers for a first pass, then go back through and rescale to
611 // make the weight right.
616 aeroWgt += compileWing(_wing);
618 aeroWgt += compileWing(_tail);
620 for(i=0; i<_vstabs.size(); i++)
621 aeroWgt += compileWing((Wing*)_vstabs.get(i));
624 aeroWgt += compileRotorgear();
627 for(i=0; i<_fuselages.size(); i++)
628 aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i));
630 // Count up the absolute weight we have
631 float nonAeroWgt = _ballast;
632 for(i=0; i<_thrusters.size(); i++)
633 nonAeroWgt += ((ThrustRec*)_thrusters.get(i))->mass;
635 // Rescale to the specified empty weight
636 float wscale = (_emptyWeight-nonAeroWgt)/aeroWgt;
637 for(i=firstMass; i<body->numMasses(); i++)
638 body->setMass(i, body->getMass(i)*wscale);
640 // Add the thruster masses
641 for(i=0; i<_thrusters.size(); i++) {
642 ThrustRec* t = (ThrustRec*)_thrusters.get(i);
643 body->addMass(t->mass, t->cg);
646 // Add the tanks, empty for now.
648 for(i=0; i<_tanks.size(); i++) {
649 Tank* t = (Tank*)_tanks.get(i);
650 t->handle = body->addMass(0, t->pos);
653 _cruiseWeight = _emptyWeight + totalFuel*0.5f;
654 _approachWeight = _emptyWeight + totalFuel*0.2f;
658 // Add surfaces for the landing gear.
659 for(i=0; i<_gears.size(); i++)
660 compileGear((GearRec*)_gears.get(i));
662 // The Thruster objects
663 for(i=0; i<_thrusters.size(); i++) {
664 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
665 tr->handle = _model.addThruster(tr->thruster);
672 gespan = _wing->getGroundEffect(gepos);
673 _model.setGroundEffect(gepos, gespan, 0.15f);
677 if(_wing && _tail) solve();
678 else solveHelicopter();
680 // Do this after solveGear, because it creates "gear" objects that
681 // we don't want to affect.
682 compileContactPoints();
685 void Airplane::solveGear()
688 _model.getBody()->getCG(cg);
690 // Calculate spring constant weightings for the gear. Weight by
691 // the inverse of the distance to the c.g. in the XY plane, which
692 // should be correct for most gear arrangements. Add 50cm of
693 // "buffer" to keep things from blowing up with aircraft with a
694 // single gear very near the c.g. (AV-8, for example).
697 for(i=0; i<_gears.size(); i++) {
698 GearRec* gr = (GearRec*)_gears.get(i);
701 Math::sub3(cg, pos, pos);
702 gr->wgt = 1.0f/(0.5f+Math::sqrt(pos[0]*pos[0] + pos[1]*pos[1]));
706 // Renormalize so they sum to 1
707 for(i=0; i<_gears.size(); i++)
708 ((GearRec*)_gears.get(i))->wgt /= total;
710 // The force at max compression should be sufficient to stop a
711 // plane moving downwards at 2x the approach descent rate. Assume
712 // a 3 degree approach.
713 float descentRate = 2.0f*_approachSpeed/19.1f;
715 // Spread the kinetic energy according to the gear weights. This
716 // will results in an equal compression fraction (not distance) of
718 float energy = 0.5f*_approachWeight*descentRate*descentRate;
720 for(i=0; i<_gears.size(); i++) {
721 GearRec* gr = (GearRec*)_gears.get(i);
722 float e = energy * gr->wgt;
724 gr->gear->getCompression(comp);
725 float len = Math::mag3(comp);
727 // Energy in a spring: e = 0.5 * k * len^2
728 float k = 2 * e / (len*len);
730 gr->gear->setSpring(k * gr->gear->getSpring());
732 // Critically damped (too damped, too!)
733 gr->gear->setDamping(2*Math::sqrt(k*_approachWeight*gr->wgt)
734 * gr->gear->getDamping());
736 // These are pretty generic
737 gr->gear->setStaticFriction(0.8f);
738 gr->gear->setDynamicFriction(0.7f);
742 void Airplane::initEngines()
744 for(int i=0; i<_thrusters.size(); i++) {
745 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
746 tr->thruster->init();
750 void Airplane::stabilizeThrust()
753 for(i=0; i<_thrusters.size(); i++)
754 _model.getThruster(i)->stabilize();
757 void Airplane::setupWeights(bool isApproach)
760 for(i=0; i<_weights.size(); i++)
762 for(i=0; i<_solveWeights.size(); i++) {
763 SolveWeight* w = (SolveWeight*)_solveWeights.get(i);
764 if(w->approach == isApproach)
765 setWeight(w->idx, w->wgt);
769 void Airplane::runCruise()
771 setupState(_cruiseAoA, _cruiseSpeed, &_cruiseState);
772 _model.setState(&_cruiseState);
773 _model.setAir(_cruiseP, _cruiseT,
774 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
776 // The control configuration
779 for(i=0; i<_cruiseControls.size(); i++) {
780 Control* c = (Control*)_cruiseControls.get(i);
781 _controls.setInput(c->control, c->val);
783 _controls.applyControls(1000000); // Huge dt value
787 Math::mul3(-1, _cruiseState.v, wind);
788 Math::vmul33(_cruiseState.orient, wind, wind);
790 setFuelFraction(_cruiseFuel);
793 // Set up the thruster parameters and iterate until the thrust
795 for(i=0; i<_thrusters.size(); i++) {
796 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
798 t->setAir(_cruiseP, _cruiseT,
799 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
805 // Precompute thrust in the model, and calculate aerodynamic forces
806 _model.getBody()->recalc();
807 _model.getBody()->reset();
808 _model.initIteration();
809 _model.calcForces(&_cruiseState);
812 void Airplane::runApproach()
814 setupState(_approachAoA, _approachSpeed, &_approachState);
815 _model.setState(&_approachState);
816 _model.setAir(_approachP, _approachT,
817 Atmosphere::calcStdDensity(_approachP, _approachT));
819 // The control configuration
822 for(i=0; i<_approachControls.size(); i++) {
823 Control* c = (Control*)_approachControls.get(i);
824 _controls.setInput(c->control, c->val);
826 _controls.applyControls(1000000);
830 Math::mul3(-1, _approachState.v, wind);
831 Math::vmul33(_approachState.orient, wind, wind);
833 setFuelFraction(_approachFuel);
837 // Run the thrusters until they get to a stable setting. FIXME:
838 // this is lots of wasted work.
839 for(i=0; i<_thrusters.size(); i++) {
840 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
842 t->setAir(_approachP, _approachT,
843 Atmosphere::calcStdDensity(_approachP, _approachT));
849 // Precompute thrust in the model, and calculate aerodynamic forces
850 _model.getBody()->recalc();
851 _model.getBody()->reset();
852 _model.initIteration();
853 _model.calcForces(&_approachState);
856 void Airplane::applyDragFactor(float factor)
858 float applied = Math::pow(factor, SOLVE_TWEAK);
859 _dragFactor *= applied;
861 _wing->setDragScale(_wing->getDragScale() * applied);
863 _tail->setDragScale(_tail->getDragScale() * applied);
865 for(i=0; i<_vstabs.size(); i++) {
866 Wing* w = (Wing*)_vstabs.get(i);
867 w->setDragScale(w->getDragScale() * applied);
869 for(i=0; i<_surfs.size(); i++) {
870 Surface* s = (Surface*)_surfs.get(i);
871 s->setTotalDrag(s->getTotalDrag() * applied);
875 void Airplane::applyLiftRatio(float factor)
877 float applied = Math::pow(factor, SOLVE_TWEAK);
878 _liftRatio *= applied;
880 _wing->setLiftRatio(_wing->getLiftRatio() * applied);
882 _tail->setLiftRatio(_tail->getLiftRatio() * applied);
884 for(i=0; i<_vstabs.size(); i++) {
885 Wing* w = (Wing*)_vstabs.get(i);
886 w->setLiftRatio(w->getLiftRatio() * applied);
890 float Airplane::clamp(float val, float min, float max)
892 if(val < min) return min;
893 if(val > max) return max;
897 float Airplane::normFactor(float f)
904 void Airplane::solve()
906 static const float ARCMIN = 0.0002909f;
909 _solutionIterations = 0;
913 if(_solutionIterations++ > 10000) {
914 _failureMsg = "Solution failed to converge after 10000 iterations";
918 // Run an iteration at cruise, and extract the needed numbers:
921 _model.getThrust(tmp);
922 float thrust = tmp[0];
924 _model.getBody()->getAccel(tmp);
925 Math::tmul33(_cruiseState.orient, tmp, tmp);
926 float xforce = _cruiseWeight * tmp[0];
927 float clift0 = _cruiseWeight * tmp[2];
929 _model.getBody()->getAngularAccel(tmp);
930 Math::tmul33(_cruiseState.orient, tmp, tmp);
931 float pitch0 = tmp[1];
933 // Run an approach iteration, and do likewise
936 _model.getBody()->getAngularAccel(tmp);
937 Math::tmul33(_approachState.orient, tmp, tmp);
938 double apitch0 = tmp[1];
940 _model.getBody()->getAccel(tmp);
941 Math::tmul33(_approachState.orient, tmp, tmp);
942 float alift = _approachWeight * tmp[2];
944 // Modify the cruise AoA a bit to get a derivative
945 _cruiseAoA += ARCMIN;
947 _cruiseAoA -= ARCMIN;
949 _model.getBody()->getAccel(tmp);
950 Math::tmul33(_cruiseState.orient, tmp, tmp);
951 float clift1 = _cruiseWeight * tmp[2];
953 // Do the same with the tail incidence
954 _tail->setIncidence(_tailIncidence + ARCMIN);
956 _tail->setIncidence(_tailIncidence);
958 _model.getBody()->getAngularAccel(tmp);
959 Math::tmul33(_cruiseState.orient, tmp, tmp);
960 float pitch1 = tmp[1];
963 float awgt = 9.8f * _approachWeight;
965 float dragFactor = thrust / (thrust-xforce);
966 float liftFactor = awgt / (awgt+alift);
967 float aoaDelta = -clift0 * (ARCMIN/(clift1-clift0));
968 float tailDelta = -pitch0 * (ARCMIN/(pitch1-pitch0));
971 if(dragFactor <= 0 || liftFactor <= 0)
974 // And the elevator control in the approach. This works just
975 // like the tail incidence computation (it's solving for the
976 // same thing -- pitching moment -- by diddling a different
978 const float ELEVDIDDLE = 0.001f;
979 _approachElevator.val += ELEVDIDDLE;
981 _approachElevator.val -= ELEVDIDDLE;
983 _model.getBody()->getAngularAccel(tmp);
984 Math::tmul33(_approachState.orient, tmp, tmp);
985 double apitch1 = tmp[1];
986 float elevDelta = -apitch0 * (ELEVDIDDLE/(apitch1-apitch0));
988 // Now apply the values we just computed. Note that the
989 // "minor" variables are deferred until we get the lift/drag
990 // numbers in the right ballpark.
992 applyDragFactor(dragFactor);
993 applyLiftRatio(liftFactor);
995 // DON'T do the following until the above are sane
996 if(normFactor(dragFactor) > STHRESH*1.0001
997 || normFactor(liftFactor) > STHRESH*1.0001)
1002 // OK, now we can adjust the minor variables:
1003 _cruiseAoA += SOLVE_TWEAK*aoaDelta;
1004 _tailIncidence += SOLVE_TWEAK*tailDelta;
1006 _cruiseAoA = clamp(_cruiseAoA, -0.175f, 0.175f);
1007 _tailIncidence = clamp(_tailIncidence, -0.175f, 0.175f);
1009 if(abs(xforce/_cruiseWeight) < STHRESH*0.0001 &&
1010 abs(alift/_approachWeight) < STHRESH*0.0001 &&
1011 abs(aoaDelta) < STHRESH*.000017 &&
1012 abs(tailDelta) < STHRESH*.000017)
1014 // If this finaly value is OK, then we're all done
1015 if(abs(elevDelta) < STHRESH*0.0001)
1018 // Otherwise, adjust and do the next iteration
1019 _approachElevator.val += SOLVE_TWEAK * elevDelta;
1020 if(abs(_approachElevator.val) > 1) {
1021 _failureMsg = "Insufficient elevator to trim for approach";
1027 if(_dragFactor < 1e-06 || _dragFactor > 1e6) {
1028 _failureMsg = "Drag factor beyond reasonable bounds.";
1030 } else if(_liftRatio < 1e-04 || _liftRatio > 1e4) {
1031 _failureMsg = "Lift ratio beyond reasonable bounds.";
1033 } else if(Math::abs(_cruiseAoA) >= .17453293) {
1034 _failureMsg = "Cruise AoA > 10 degrees";
1036 } else if(Math::abs(_tailIncidence) >= .17453293) {
1037 _failureMsg = "Tail incidence > 10 degrees";
1042 void Airplane::solveHelicopter()
1044 _solutionIterations = 0;
1046 if (getRotorgear()!=0)
1048 Rotorgear* rg = getRotorgear();
1049 applyDragFactor(Math::pow(rg->getYasimDragFactor()/1000,
1051 applyLiftRatio(Math::pow(rg->getYasimLiftFactor(),
1055 //huh, no wing and no rotor? (_rotorgear is constructed,
1056 //if a rotor is defined
1058 applyDragFactor(Math::pow(15.7/1000, 1/SOLVE_TWEAK));
1059 applyLiftRatio(Math::pow(104, 1/SOLVE_TWEAK));
1061 setupState(0,0, &_cruiseState);
1062 _model.setState(&_cruiseState);
1065 _model.getBody()->reset();
1066 _model.setAir(_cruiseP, _cruiseT,
1067 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
1071 }; // namespace yasim