Thomas Umscheid 02/17/15 Dynamics of Flight Code Check #3 Phase 5 acSim #include "stdafx.h" ofstream fout("acdata.txt"); int main() { aircraft ac; double t, tend = 100.0; fout << "t" << "\t" << "Pn" << "\t" << "Pe" << "\t" << "Pd" << "\t" << "u" << "\t" << "v" << "\t" << "w" << "\t" << "phi" << "\t" << "theta" << "\t" << "psi" << "\t" << "p" << "\t" << "q" << "\t" << "r" << "\t" << "as1" << "\t" << "as2" << "\t" << "es1" << "\t" << "es2" << "\t" << "rs1" << "\t" << "rs2" << "\t" << "ts1" << "\t" << "ts2" << endl; for (t = 0.; t < tend;) { ac.control(t); t = ac.rk(t); cout << t << "\t" << h << "\t" << ac.get_state(2) << endl; fout << t; for (int i = 0; i < N; i++) { fout << "\t" << ac.get_state(i); } fout << endl; } return 0; } aircraft.cpp #include "stdafx.h" //Kp, Ki, Kd, Kr, ui_max, uc_max, uc_min, trim Controller La(.01, .001, 0, 0, 4.0, 4.0, -4.0, 0); Controller Gt(10.0, 5.6, .4, 0, .25, .75, -.75, 0); Controller Ro(.3, .4, 0, .1, .3, 1.0, -1.0, 0); Controller Lo(.3, .2, 0, 0, 4.0, 5.0, -5.0, 0); Controller Ve(-.1, -.001, -.025, 0, .3, .7, -.7, 0); Controller Pi(-.5, -.167, 0, -.25, .25, 1.0, -1.0, -.02508); Controller En(.02, .02, .01, 0, 1.0, 1.2, -1.2, .574778); aircraft::aircraft() { mass = 13.5; Jx = 0.8244; Jy = 1.135; Jz = 1.759; Jxz = 0.1204; gamma = Jx*Jz - Jxz*Jxz; gamma1 = Jxz*(Jx - Jy + Jz) / gamma; gamma2 = (Jz*(Jz - Jy) + Jxz*Jxz) / gamma; gamma3 = Jz / gamma; gamma4 = Jxz / gamma; gamma5 = (Jz - Jx) / Jy; gamma6 = Jxz / Jy; gamma7 = (Jx*(Jx - Jy) + Jxz*Jxz) / gamma; gamma8 = Jx / gamma; S = 0.55; b = 2.8956; c = 0.18994; Sprop = 0.2027; Kmotor = 80.; CL0 = 0.28; CD0 = 0.03; Cm0 = -0.02338; CLa = 3.45; CDa = 0.30; Cma = -0.38; CLq = 0.0; CDq = 0.0; Cmq = -3.6; CLde = -0.36; CDde = 0.0; Cmde = -0.5; Cprop = 1.0; CY0 = 0.0; Cl0 = 0.0; Cn0 = 0.0; CYb = -0.98; Clb = -0.12; Cnb = 0.25; CYp = 0.0; Clp = -0.26; Cnp = 0.022; CYr = 0.0; Clr = 0.14; Cnr = -0.35; CYda = 0.0; Clda = 0.08; Cnda = 0.06; CYdr = -0.17; Cldr = 0.105; Cndr = -0.032; // Initialize all parameters init_state(); } aircraft::~aircraft() {} double aircraft::rk(double t) { int i; for (i = 0; i= amax) d[aa] = amax; else if (y[as2] < -amax) d[aa] = -amax; else d[aa] = y[as2]; if (y[es2] >= amax) d[ee] = emax; else if (y[es2] < -amax) d[ee] = -emax; else d[ee] = y[es2]; if (y[as2] >= amax) d[ee] = rmax; else if (y[rs2] < -amax) d[ee] = -rmax; else d[ee] = y[rs2]; if (y[ts2] >= amax) d[tt] = tmax; else if (y[ts2] < -amax) d[tt] = -tmax; else d[tt] = y[ts2]; Flift = .5*roe*Va*Va*S*(CL0 + CLa*alpha + CLq*(c / (2 * Va))*y[q] + CLde*de); Fdrag = .5*roe*Va*Va*S*(CD0 + CDa*alpha + CDq*(c / (2 * Va))*y[q] + CDde*de); Fx = -cos(alpha)*Fdrag + sin(alpha)*Flift - mass*gr*Sth + .5*roe*Sprop*Cprop*((Kmotor*dt)*(Kmotor*dt) - Va*Va); Fy = .5*roe*Va*Va*S*(CY0 + CYb*beta + CYp*(b / (2 * Va))*y[p] + CYr*(b / (2 * Va))*y[r] + CYda*da + CYdr*dr)+mass*gr*Cth*Sphi; Fz = -sin(alpha)*Fdrag - cos(alpha)*Flift+mass*gr*Cth*Cphi; } void aircraft::moment(void) { Va = sqrt(y[u]*y[u] + y[v]*y[v] + y[w]*y[w]); alpha = atan(y[w] / y[u]); beta = asin(y[v] / Va); Mx = .5*roe*Va*Va*S*b*(Cl0 + Clb*beta + Clp*(b / (2 * Va))*y[p] + Clr*(b / (2 * Va))*y[r] + Clda*da + Cldr*dr); My = .5*roe*Va*Va*S*c*(Cm0 + Cma*alpha + Cmq*(c / (2 * Va))*y[q] + Cmde*de); Mz = .5*roe*Va*Va*S*b*(Cn0 + Cnb*beta + Cnp*(b / (2 * Va))*y[p] + Cnr*(b / (2 * Va))*y[r] + Cnda*da + Cndr*dr); } void aircraft::init_state(void) { int i; for (i = 0; i= ui_max) ui = ui_max; else if (ui < -ui_max) ui = -ui_max; ud = Kd*(error-error_previous)/h; ur = Kr*rate_error; error_previous = error; uc = up+ui+ud+ur+trim; if (uc >= uc_max) uc=uc_max; else if (uc < uc_min) uc = uc_min; Output = uc; return Output; } controller.h #include "stdafx.h" class Controller { private: double Kp, Ki, Kd, Kr; // Proportional, Integral, Derivative and Rate Controller Gains double error, up, ui, ud, ur, error_previous; double ui_max, uc_max, uc_min, uc, rate_error, trim; public: // Variables passed to the Constructor: Proportional Gain (Kp), Integral Gain (Ki), // Derivative Gain (Kd), Rate Gain (Kr), Integral Windeup limit (ui_max), // Output Limiter Max (uc_max), Output Limiter Min (uc_min), Output Trim value (trim). Controller(double, double, double, double, double, double, double, double); // constructor ~Controller(); // destructor // Variables passed to the pidr function: Input, Command, Measured, // Rate_Command, Rate_Measured. double pidr(double, double, double, double, double); }; stdafx.h #pragma once #define gr 9.80665 // gravity (m/s^2) #define roe 1.2682 // air density (kg/m^3) #define pi 3.1416 #define d2r pi/180. #define N 20 // # states #define h 0.01 // numerical integration step-size (s) #include #include #include #include #include #include "aircraft.h" using namespace std;