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