6 #include <simgear/props/props.hxx>
7 #include <simgear/xml/easyxml.hxx>
10 #include "Atmosphere.hpp"
11 #include "Airplane.hpp"
13 using namespace yasim;
15 // Stubs. Not needed by a batch program, but required to link.
16 bool fgSetFloat (const char * name, float val) { return false; }
17 bool fgSetBool(char const * name, bool val) { return false; }
18 bool fgGetBool(char const * name, bool def) { return false; }
19 bool fgSetString(char const * name, char const * str) { return false; }
20 SGPropertyNode* fgGetNode (const char * path, bool create) { return 0; }
21 SGPropertyNode* fgGetNode (const char * path, int i, bool create) { return 0; }
22 float fgGetFloat (const char * name, float defaultValue) { return 0; }
23 double fgGetDouble (const char * name, double defaultValue = 0.0) { return 0; }
24 bool fgSetDouble (const char * name, double defaultValue = 0.0) { return 0; }
26 static const float RAD2DEG = 57.2957795131;
27 static const float DEG2RAD = 0.0174532925199;
28 static const float KTS2MPS = 0.514444444444;
31 // Generate a graph of lift, drag and L/D against AoA at the specified
32 // speed and altitude. The result is a space-separated file of
33 // numbers: "aoa lift drag LD" (aoa in degrees, lift and drag in
34 // G's). You can use this in gnuplot like so (assuming the output is
35 // in a file named "dat":
37 // plot "dat" using 1:2 with lines title 'lift', \
38 // "dat" using 1:3 with lines title 'drag', \
39 // "dat" using 1:4 with lines title 'LD'
41 void yasim_graph(Airplane* a, float alt, float kts)
43 Model* m = a->getModel();
46 m->setAir(Atmosphere::getStdPressure(alt),
47 Atmosphere::getStdTemperature(alt),
48 Atmosphere::getStdDensity(alt));
49 m->getBody()->recalc();
51 for(int deg=-179; deg<=179; deg++) {
52 float aoa = deg * DEG2RAD;
53 Airplane::setupState(aoa, kts * KTS2MPS, 0 ,&s);
54 m->getBody()->reset();
59 m->getBody()->getAccel(acc);
60 Math::tmul33(s.orient, acc, acc);
62 float drag = acc[0] * (-1/9.8);
63 float lift = 1 + acc[2] * (1/9.8);
65 printf("%d %g %g %g\n", deg, lift, drag, lift/drag);
71 fprintf(stderr, "Usage: yasim <ac.xml> [-g [-a alt] [-s kts]]\n");
75 int main(int argc, char** argv)
77 FGFDM* fdm = new FGFDM();
78 Airplane* a = fdm->getAirplane();
80 if(argc < 2) return usage();
84 string file = argv[1];
86 } catch (const sg_exception &e) {
87 printf("XML parse error: %s (%s)\n",
88 e.getFormattedMessage().c_str(), e.getOrigin());
93 if(a->getFailureMsg())
94 printf("SOLUTION FAILURE: %s\n", a->getFailureMsg());
96 if(!a->getFailureMsg() && argc > 2 && strcmp(argv[2], "-g") == 0) {
97 float alt = 5000, kts = 100;
98 for(int i=3; i<argc; i++) {
99 if (std::strcmp(argv[i], "-a") == 0) alt = std::atof(argv[++i]);
100 else if(std::strcmp(argv[i], "-s") == 0) kts = std::atof(argv[++i]);
103 yasim_graph(a, alt, kts);
105 float aoa = a->getCruiseAoA() * RAD2DEG;
106 float tail = -1 * a->getTailIncidence() * RAD2DEG;
107 float drag = 1000 * a->getDragCoefficient();
109 a->getModel()->getBody()->getCG(cg);
110 a->getModel()->getBody()->recalc();
113 a->getModel()->getBody()->getInertiaMatrix(SI_inertia);
115 printf("Solution results:");
116 printf(" Iterations: %d\n", a->getSolutionIterations());
117 printf(" Drag Coefficient: %f\n", drag);
118 printf(" Lift Ratio: %f\n", a->getLiftRatio());
119 printf(" Cruise AoA: %f\n", aoa);
120 printf(" Tail Incidence: %f\n", tail);
121 printf("Approach Elevator: %f\n", a->getApproachElevator());
122 printf(" CG: x:%.3f, y:%.3f, z:%.3f\n\n", cg[0], cg[1], cg[2]);
123 printf(" Inertia tensor : %.3f, %.3f, %.3f\n", SI_inertia[0], SI_inertia[1], SI_inertia[2]);
124 printf(" [kg*m^2] %.3f, %.3f, %.3f\n", SI_inertia[3], SI_inertia[4], SI_inertia[5]);
125 printf(" Origo at CG %.3f, %.3f, %.3f\n", SI_inertia[6], SI_inertia[7], SI_inertia[8]);