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;
61 for(i=0; i<_fuselages.size(); i++)
62 delete (Fuselage*)_fuselages.get(i);
63 for(i=0; i<_tanks.size(); i++)
64 delete (Tank*)_tanks.get(i);
65 for(i=0; i<_thrusters.size(); i++)
66 delete (ThrustRec*)_thrusters.get(i);
67 for(i=0; i<_gears.size(); i++) {
68 GearRec* g = (GearRec*)_gears.get(i);
72 for(i=0; i<_surfs.size(); i++)
73 delete (Surface*)_surfs.get(i);
74 for(i=0; i<_contacts.size(); i++) {
75 ContactRec* c = (ContactRec*)_contacts.get(i);
79 for(i=0; i<_solveWeights.size(); i++)
80 delete (SolveWeight*)_solveWeights.get(i);
81 for(i=0; i<_cruiseControls.size(); i++)
82 delete (Control*)_cruiseControls.get(i);
83 for(i=0; i<_approachControls.size(); i++) {
84 Control* c = (Control*)_approachControls.get(i);
85 if(c != &_approachElevator)
90 for(i=0; i<_vstabs.size(); i++)
91 delete (Wing*)_vstabs.get(i);
92 for(i=0; i<_weights.size(); i++)
93 delete (WeightRec*)_weights.get(i);
96 void Airplane::iterate(float dt)
98 // The gear might have moved. Change their aerodynamics.
104 void Airplane::calcFuelWeights()
106 for(int i=0; i<_tanks.size(); i++) {
107 Tank* t = (Tank*)_tanks.get(i);
108 _model.getBody()->setMass(t->handle, t->fill);
112 ControlMap* Airplane::getControlMap()
117 Model* Airplane::getModel()
122 void Airplane::getPilotAccel(float* out)
124 State* s = _model.getState();
127 Glue::geodUp(s->pos, out);
128 Math::mul3(-9.8f, out, out);
129 Math::vmul33(s->orient, out, out);
132 // The regular acceleration
134 // Convert to aircraft coordinates
135 Math::vmul33(s->orient, s->acc, tmp);
139 Math::add3(tmp, out, out);
141 // FIXME: rotational & centripetal acceleration needed
144 void Airplane::setPilotPos(float* pos)
147 for(i=0; i<3; i++) _pilotPos[i] = pos[i];
150 void Airplane::getPilotPos(float* out)
153 for(i=0; i<3; i++) out[i] = _pilotPos[i];
156 int Airplane::numGear()
158 return _gears.size();
161 Gear* Airplane::getGear(int g)
163 return ((GearRec*)_gears.get(g))->gear;
166 Hook* Airplane::getHook()
168 return _model.getHook();
171 Launchbar* Airplane::getLaunchbar()
173 return _model.getLaunchbar();
176 Rotorgear* Airplane::getRotorgear()
178 return _model.getRotorgear();
181 void Airplane::updateGearState()
183 for(int i=0; i<_gears.size(); i++) {
184 GearRec* gr = (GearRec*)_gears.get(i);
185 float ext = gr->gear->getExtension();
187 gr->surf->setXDrag(ext);
188 gr->surf->setYDrag(ext);
189 gr->surf->setZDrag(ext);
193 void Airplane::setApproach(float speed, float altitude, float aoa, float fuel, float gla)
195 _approachSpeed = speed;
196 _approachP = Atmosphere::getStdPressure(altitude);
197 _approachT = Atmosphere::getStdTemperature(altitude);
199 _approachFuel = fuel;
200 _approachGlideAngle = gla;
203 void Airplane::setCruise(float speed, float altitude, float fuel, float gla)
205 _cruiseSpeed = speed;
206 _cruiseP = Atmosphere::getStdPressure(altitude);
207 _cruiseT = Atmosphere::getStdTemperature(altitude);
211 _cruiseGlideAngle = gla;
214 void Airplane::setElevatorControl(int control)
216 _approachElevator.control = control;
217 _approachElevator.val = 0;
218 _approachControls.add(&_approachElevator);
221 void Airplane::addApproachControl(int control, float val)
223 Control* c = new Control();
224 c->control = control;
226 _approachControls.add(c);
229 void Airplane::addCruiseControl(int control, float val)
231 Control* c = new Control();
232 c->control = control;
234 _cruiseControls.add(c);
237 void Airplane::addSolutionWeight(bool approach, int idx, float wgt)
239 SolveWeight* w = new SolveWeight();
240 w->approach = approach;
243 _solveWeights.add(w);
246 int Airplane::numTanks()
248 return _tanks.size();
251 float Airplane::getFuel(int tank)
253 return ((Tank*)_tanks.get(tank))->fill;
256 float Airplane::setFuel(int tank, float fuel)
258 return ((Tank*)_tanks.get(tank))->fill = fuel;
261 float Airplane::getFuelDensity(int tank)
263 return ((Tank*)_tanks.get(tank))->density;
266 float Airplane::getTankCapacity(int tank)
268 return ((Tank*)_tanks.get(tank))->cap;
271 void Airplane::setWeight(float weight)
273 _emptyWeight = weight;
276 void Airplane::setWing(Wing* wing)
281 void Airplane::setTail(Wing* tail)
286 void Airplane::addVStab(Wing* vstab)
291 void Airplane::addFuselage(float* front, float* back, float width,
292 float taper, float mid,
293 float cx, float cy, float cz, float idrag)
295 Fuselage* f = new Fuselage();
298 f->front[i] = front[i];
299 f->back[i] = back[i];
311 int Airplane::addTank(float* pos, float cap, float density)
313 Tank* t = new Tank();
315 for(i=0; i<3; i++) t->pos[i] = pos[i];
318 t->density = density;
319 t->handle = 0xffffffff;
320 return _tanks.add(t);
323 void Airplane::addGear(Gear* gear)
325 GearRec* g = new GearRec();
331 void Airplane::addHook(Hook* hook)
333 _model.addHook(hook);
336 void Airplane::addHitch(Hitch* hitch)
338 _model.addHitch(hitch);
341 void Airplane::addLaunchbar(Launchbar* launchbar)
343 _model.addLaunchbar(launchbar);
346 void Airplane::addThruster(Thruster* thruster, float mass, float* cg)
348 ThrustRec* t = new ThrustRec();
349 t->thruster = thruster;
352 for(i=0; i<3; i++) t->cg[i] = cg[i];
356 void Airplane::addBallast(float* pos, float mass)
358 _model.getBody()->addMass(mass, pos);
362 int Airplane::addWeight(float* pos, float size)
364 WeightRec* wr = new WeightRec();
365 wr->handle = _model.getBody()->addMass(0, pos);
367 wr->surf = new Surface();
368 wr->surf->setPosition(pos);
369 wr->surf->setTotalDrag(size*size);
370 _model.addSurface(wr->surf);
371 _surfs.add(wr->surf);
373 return _weights.add(wr);
376 void Airplane::setWeight(int handle, float mass)
378 WeightRec* wr = (WeightRec*)_weights.get(handle);
380 _model.getBody()->setMass(wr->handle, mass);
382 // Kill the aerodynamic drag if the mass is exactly zero. This is
383 // how we simulate droppable stores.
385 wr->surf->setXDrag(0);
386 wr->surf->setYDrag(0);
387 wr->surf->setZDrag(0);
389 wr->surf->setXDrag(1);
390 wr->surf->setYDrag(1);
391 wr->surf->setZDrag(1);
395 void Airplane::setFuelFraction(float frac)
398 for(i=0; i<_tanks.size(); i++) {
399 Tank* t = (Tank*)_tanks.get(i);
400 t->fill = frac * t->cap;
401 _model.getBody()->setMass(t->handle, t->cap * frac);
405 float Airplane::getDragCoefficient()
410 float Airplane::getLiftRatio()
415 float Airplane::getCruiseAoA()
420 float Airplane::getTailIncidence()
422 return _tailIncidence;
425 const char* Airplane::getFailureMsg()
430 int Airplane::getSolutionIterations()
432 return _solutionIterations;
435 void Airplane::setupState(float aoa, float speed, float gla, State* s)
437 float cosAoA = Math::cos(aoa);
438 float sinAoA = Math::sin(aoa);
439 s->orient[0] = cosAoA; s->orient[1] = 0; s->orient[2] = sinAoA;
440 s->orient[3] = 0; s->orient[4] = 1; s->orient[5] = 0;
441 s->orient[6] = -sinAoA; s->orient[7] = 0; s->orient[8] = cosAoA;
443 s->v[0] = speed*Math::cos(gla); s->v[1] = -speed*Math::sin(gla); s->v[2] = 0;
447 s->pos[i] = s->rot[i] = s->acc[i] = s->racc[i] = 0;
449 // Put us 1m above the origin, or else the gravity computation in
454 void Airplane::addContactPoint(float* pos)
456 ContactRec* c = new ContactRec;
464 float Airplane::compileWing(Wing* w)
466 // The tip of the wing is a contact point
469 addContactPoint(tip);
470 if(w->isMirrored()) {
472 addContactPoint(tip);
475 // Make sure it's initialized. The surfaces will pop out with
476 // total drag coefficients equal to their areas, which is what we
482 for(i=0; i<w->numSurfaces(); i++) {
483 Surface* s = (Surface*)w->getSurface(i);
485 float td = s->getTotalDrag();
488 _model.addSurface(s);
490 float mass = w->getSurfaceWeight(i);
491 mass = mass * Math::sqrt(mass);
494 _model.getBody()->addMass(mass, pos);
500 void Airplane::compileRotorgear()
502 getRotorgear()->compile();
505 float Airplane::compileFuselage(Fuselage* f)
507 // The front and back are contact points
508 addContactPoint(f->front);
509 addContactPoint(f->back);
513 Math::sub3(f->front, f->back, fwd);
514 float len = Math::mag3(fwd);
516 _failureMsg = "Zero length fuselage";
519 float wid = f->width;
520 int segs = (int)Math::ceil(len/wid);
521 float segWgt = len*wid/segs;
523 for(j=0; j<segs; j++) {
524 float frac = (j+0.5f) / segs;
528 scale = f->taper+(1-f->taper) * (frac / f->mid);
530 scale = f->taper+(1-f->taper) * (frac - f->mid) / (1 - f->mid);
534 Math::mul3(frac, fwd, pos);
535 Math::add3(f->back, pos, pos);
537 // _Mass_ weighting goes as surface area^(3/2)
538 float mass = scale*segWgt * Math::sqrt(scale*segWgt);
539 _model.getBody()->addMass(mass, pos);
542 // Make a Surface too
543 Surface* s = new Surface();
545 float sideDrag = len/wid;
546 s->setYDrag(sideDrag*f->_cy);
547 s->setZDrag(sideDrag*f->_cz);
548 s->setTotalDrag(scale*segWgt*f->_cx);
549 s->setInducedDrag(f->_idrag);
551 // FIXME: fails for fuselages aligned along the Y axis
553 float *x=o, *y=o+3, *z=o+6; // nicknames for the axes
555 y[0] = 0; y[1] = 1; y[2] = 0;
556 Math::cross3(x, y, z);
558 Math::cross3(z, x, y);
559 s->setOrientation(o);
561 _model.addSurface(s);
567 // FIXME: should probably add a mass for the gear, too
568 void Airplane::compileGear(GearRec* gr)
572 // Make a Surface object for the aerodynamic behavior
573 Surface* s = new Surface();
576 // Put the surface at the half-way point on the gear strut, give
577 // it a drag coefficient equal to a square of the same dimension
578 // (gear are really draggy) and make it symmetric. Assume that
579 // the "length" of the gear is 3x the compression distance
580 float pos[3], cmp[3];
581 g->getCompression(cmp);
582 float length = 3 * Math::mag3(cmp);
584 Math::mul3(0.5, cmp, cmp);
585 Math::add3(pos, cmp, pos);
588 s->setTotalDrag(length*length);
591 _model.addSurface(s);
595 void Airplane::compileContactPoints()
597 // Figure it will compress by 20cm
600 comp[0] = 0; comp[1] = 0; comp[2] = DIST;
602 // Give it a spring constant such that at full compression it will
603 // hold up 10 times the planes mass. That's about right. Yeah.
604 float mass = _model.getBody()->getTotalMass();
605 float spring = (1/DIST) * 9.8f * 10.0f * mass;
606 float damp = 2 * Math::sqrt(spring * mass);
609 for(i=0; i<_contacts.size(); i++) {
610 ContactRec* c = (ContactRec*)_contacts.get(i);
612 Gear* g = new Gear();
614 g->setPosition(c->p);
616 g->setCompression(comp);
617 g->setSpring(spring);
622 g->setStaticFriction(0.6f);
623 g->setDynamicFriction(0.5f);
624 g->setContactPoint(1);
630 void Airplane::compile()
632 RigidBody* body = _model.getBody();
633 int firstMass = body->numMasses();
635 // Generate the point masses for the plane. Just use unitless
636 // numbers for a first pass, then go back through and rescale to
637 // make the weight right.
642 aeroWgt += compileWing(_wing);
644 aeroWgt += compileWing(_tail);
646 for(i=0; i<_vstabs.size(); i++)
647 aeroWgt += compileWing((Wing*)_vstabs.get(i));
651 for(i=0; i<_fuselages.size(); i++)
652 aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i));
654 // Count up the absolute weight we have
655 float nonAeroWgt = _ballast;
656 for(i=0; i<_thrusters.size(); i++)
657 nonAeroWgt += ((ThrustRec*)_thrusters.get(i))->mass;
659 // Rescale to the specified empty weight
660 float wscale = (_emptyWeight-nonAeroWgt)/aeroWgt;
661 for(i=firstMass; i<body->numMasses(); i++)
662 body->setMass(i, body->getMass(i)*wscale);
664 // Add the thruster masses
665 for(i=0; i<_thrusters.size(); i++) {
666 ThrustRec* t = (ThrustRec*)_thrusters.get(i);
667 body->addMass(t->mass, t->cg);
670 // Add the tanks, empty for now.
672 for(i=0; i<_tanks.size(); i++) {
673 Tank* t = (Tank*)_tanks.get(i);
674 t->handle = body->addMass(0, t->pos);
677 _cruiseWeight = _emptyWeight + totalFuel*_cruiseFuel;
678 _approachWeight = _emptyWeight + totalFuel*_approachFuel;
682 // Add surfaces for the landing gear.
683 for(i=0; i<_gears.size(); i++)
684 compileGear((GearRec*)_gears.get(i));
686 // The Thruster objects
687 for(i=0; i<_thrusters.size(); i++) {
688 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
689 tr->handle = _model.addThruster(tr->thruster);
696 gespan = _wing->getGroundEffect(gepos);
697 _model.setGroundEffect(gepos, gespan, 0.15f);
700 // solve function below resets failure message
701 // so check if we have any problems and abort here
702 if (_failureMsg) return;
705 if(_wing && _tail) solve();
708 // The rotor(s) mass:
713 // Do this after solveGear, because it creates "gear" objects that
714 // we don't want to affect.
715 compileContactPoints();
718 void Airplane::solveGear()
721 _model.getBody()->getCG(cg);
723 // Calculate spring constant weightings for the gear. Weight by
724 // the inverse of the distance to the c.g. in the XY plane, which
725 // should be correct for most gear arrangements. Add 50cm of
726 // "buffer" to keep things from blowing up with aircraft with a
727 // single gear very near the c.g. (AV-8, for example).
730 for(i=0; i<_gears.size(); i++) {
731 GearRec* gr = (GearRec*)_gears.get(i);
734 Math::sub3(cg, pos, pos);
735 gr->wgt = 1.0f/(0.5f+Math::sqrt(pos[0]*pos[0] + pos[1]*pos[1]));
736 if (!g->getIgnoreWhileSolving())
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)*(1+2*gr->gear->getInitialLoad());
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());
772 void Airplane::initEngines()
774 for(int i=0; i<_thrusters.size(); i++) {
775 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
776 tr->thruster->init();
780 void Airplane::stabilizeThrust()
783 for(i=0; i<_thrusters.size(); i++)
784 _model.getThruster(i)->stabilize();
787 void Airplane::setupWeights(bool isApproach)
790 for(i=0; i<_weights.size(); i++)
792 for(i=0; i<_solveWeights.size(); i++) {
793 SolveWeight* w = (SolveWeight*)_solveWeights.get(i);
794 if(w->approach == isApproach)
795 setWeight(w->idx, w->wgt);
799 void Airplane::runCruise()
801 setupState(_cruiseAoA, _cruiseSpeed,_cruiseGlideAngle, &_cruiseState);
802 _model.setState(&_cruiseState);
803 _model.setAir(_cruiseP, _cruiseT,
804 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
806 // The control configuration
809 for(i=0; i<_cruiseControls.size(); i++) {
810 Control* c = (Control*)_cruiseControls.get(i);
811 _controls.setInput(c->control, c->val);
813 _controls.applyControls(1000000); // Huge dt value
817 Math::mul3(-1, _cruiseState.v, wind);
818 Math::vmul33(_cruiseState.orient, wind, wind);
820 setFuelFraction(_cruiseFuel);
823 // Set up the thruster parameters and iterate until the thrust
825 for(i=0; i<_thrusters.size(); i++) {
826 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
828 t->setAir(_cruiseP, _cruiseT,
829 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
835 // Precompute thrust in the model, and calculate aerodynamic forces
836 _model.getBody()->recalc();
837 _model.getBody()->reset();
838 _model.initIteration();
839 _model.calcForces(&_cruiseState);
842 void Airplane::runApproach()
844 setupState(_approachAoA, _approachSpeed,_approachGlideAngle, &_approachState);
845 _model.setState(&_approachState);
846 _model.setAir(_approachP, _approachT,
847 Atmosphere::calcStdDensity(_approachP, _approachT));
849 // The control configuration
852 for(i=0; i<_approachControls.size(); i++) {
853 Control* c = (Control*)_approachControls.get(i);
854 _controls.setInput(c->control, c->val);
856 _controls.applyControls(1000000);
860 Math::mul3(-1, _approachState.v, wind);
861 Math::vmul33(_approachState.orient, wind, wind);
863 setFuelFraction(_approachFuel);
867 // Run the thrusters until they get to a stable setting. FIXME:
868 // this is lots of wasted work.
869 for(i=0; i<_thrusters.size(); i++) {
870 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
872 t->setAir(_approachP, _approachT,
873 Atmosphere::calcStdDensity(_approachP, _approachT));
879 // Precompute thrust in the model, and calculate aerodynamic forces
880 _model.getBody()->recalc();
881 _model.getBody()->reset();
882 _model.initIteration();
883 _model.calcForces(&_approachState);
886 void Airplane::applyDragFactor(float factor)
888 float applied = Math::pow(factor, SOLVE_TWEAK);
889 _dragFactor *= applied;
891 _wing->setDragScale(_wing->getDragScale() * applied);
893 _tail->setDragScale(_tail->getDragScale() * applied);
895 for(i=0; i<_vstabs.size(); i++) {
896 Wing* w = (Wing*)_vstabs.get(i);
897 w->setDragScale(w->getDragScale() * applied);
899 for(i=0; i<_surfs.size(); i++) {
900 Surface* s = (Surface*)_surfs.get(i);
901 s->setTotalDrag(s->getTotalDrag() * applied);
905 void Airplane::applyLiftRatio(float factor)
907 float applied = Math::pow(factor, SOLVE_TWEAK);
908 _liftRatio *= applied;
910 _wing->setLiftRatio(_wing->getLiftRatio() * applied);
912 _tail->setLiftRatio(_tail->getLiftRatio() * applied);
914 for(i=0; i<_vstabs.size(); i++) {
915 Wing* w = (Wing*)_vstabs.get(i);
916 w->setLiftRatio(w->getLiftRatio() * applied);
920 float Airplane::clamp(float val, float min, float max)
922 if(val < min) return min;
923 if(val > max) return max;
927 float Airplane::normFactor(float f)
934 void Airplane::solve()
936 static const float ARCMIN = 0.0002909f;
939 _solutionIterations = 0;
943 if(_solutionIterations++ > 10000) {
944 _failureMsg = "Solution failed to converge after 10000 iterations";
948 // Run an iteration at cruise, and extract the needed numbers:
951 _model.getThrust(tmp);
952 float thrust = tmp[0] + _cruiseWeight * Math::sin(_cruiseGlideAngle) * 9.81;
954 _model.getBody()->getAccel(tmp);
955 Math::tmul33(_cruiseState.orient, tmp, tmp);
956 float xforce = _cruiseWeight * tmp[0];
957 float clift0 = _cruiseWeight * tmp[2];
959 _model.getBody()->getAngularAccel(tmp);
960 Math::tmul33(_cruiseState.orient, tmp, tmp);
961 float pitch0 = tmp[1];
963 // Run an approach iteration, and do likewise
966 _model.getBody()->getAngularAccel(tmp);
967 Math::tmul33(_approachState.orient, tmp, tmp);
968 double apitch0 = tmp[1];
970 _model.getBody()->getAccel(tmp);
971 Math::tmul33(_approachState.orient, tmp, tmp);
972 float alift = _approachWeight * tmp[2];
974 // Modify the cruise AoA a bit to get a derivative
975 _cruiseAoA += ARCMIN;
977 _cruiseAoA -= ARCMIN;
979 _model.getBody()->getAccel(tmp);
980 Math::tmul33(_cruiseState.orient, tmp, tmp);
981 float clift1 = _cruiseWeight * tmp[2];
983 // Do the same with the tail incidence
984 _tail->setIncidence(_tailIncidence + ARCMIN);
986 _tail->setIncidence(_tailIncidence);
988 _model.getBody()->getAngularAccel(tmp);
989 Math::tmul33(_cruiseState.orient, tmp, tmp);
990 float pitch1 = tmp[1];
993 float awgt = 9.8f * _approachWeight;
995 float dragFactor = thrust / (thrust-xforce);
996 float liftFactor = awgt / (awgt+alift);
997 float aoaDelta = -clift0 * (ARCMIN/(clift1-clift0));
998 float tailDelta = -pitch0 * (ARCMIN/(pitch1-pitch0));
1001 if(dragFactor <= 0 || liftFactor <= 0)
1004 // And the elevator control in the approach. This works just
1005 // like the tail incidence computation (it's solving for the
1006 // same thing -- pitching moment -- by diddling a different
1008 const float ELEVDIDDLE = 0.001f;
1009 _approachElevator.val += ELEVDIDDLE;
1011 _approachElevator.val -= ELEVDIDDLE;
1013 _model.getBody()->getAngularAccel(tmp);
1014 Math::tmul33(_approachState.orient, tmp, tmp);
1015 double apitch1 = tmp[1];
1016 float elevDelta = -apitch0 * (ELEVDIDDLE/(apitch1-apitch0));
1018 // Now apply the values we just computed. Note that the
1019 // "minor" variables are deferred until we get the lift/drag
1020 // numbers in the right ballpark.
1022 applyDragFactor(dragFactor);
1023 applyLiftRatio(liftFactor);
1025 // DON'T do the following until the above are sane
1026 if(normFactor(dragFactor) > STHRESH*1.0001
1027 || normFactor(liftFactor) > STHRESH*1.0001)
1032 // OK, now we can adjust the minor variables:
1033 _cruiseAoA += SOLVE_TWEAK*aoaDelta;
1034 _tailIncidence += SOLVE_TWEAK*tailDelta;
1036 _cruiseAoA = clamp(_cruiseAoA, -0.175f, 0.175f);
1037 _tailIncidence = clamp(_tailIncidence, -0.175f, 0.175f);
1039 if(abs(xforce/_cruiseWeight) < STHRESH*0.0001 &&
1040 abs(alift/_approachWeight) < STHRESH*0.0001 &&
1041 abs(aoaDelta) < STHRESH*.000017 &&
1042 abs(tailDelta) < STHRESH*.000017)
1044 // If this finaly value is OK, then we're all done
1045 if(abs(elevDelta) < STHRESH*0.0001)
1048 // Otherwise, adjust and do the next iteration
1049 _approachElevator.val += SOLVE_TWEAK * elevDelta;
1050 if(abs(_approachElevator.val) > 1) {
1051 _failureMsg = "Insufficient elevator to trim for approach";
1057 if(_dragFactor < 1e-06 || _dragFactor > 1e6) {
1058 _failureMsg = "Drag factor beyond reasonable bounds.";
1060 } else if(_liftRatio < 1e-04 || _liftRatio > 1e4) {
1061 _failureMsg = "Lift ratio beyond reasonable bounds.";
1063 } else if(Math::abs(_cruiseAoA) >= .17453293) {
1064 _failureMsg = "Cruise AoA > 10 degrees";
1066 } else if(Math::abs(_tailIncidence) >= .17453293) {
1067 _failureMsg = "Tail incidence > 10 degrees";
1072 void Airplane::solveHelicopter()
1074 _solutionIterations = 0;
1076 if (getRotorgear()!=0)
1078 Rotorgear* rg = getRotorgear();
1079 applyDragFactor(Math::pow(rg->getYasimDragFactor()/1000,
1081 applyLiftRatio(Math::pow(rg->getYasimLiftFactor(),
1085 //huh, no wing and no rotor? (_rotorgear is constructed,
1086 //if a rotor is defined
1088 applyDragFactor(Math::pow(15.7/1000, 1/SOLVE_TWEAK));
1089 applyLiftRatio(Math::pow(104, 1/SOLVE_TWEAK));
1091 setupState(0,0,0, &_cruiseState);
1092 _model.setState(&_cruiseState);
1095 _model.getBody()->reset();
1096 _model.setAir(_cruiseP, _cruiseT,
1097 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
1101 }; // namespace yasim