Guilherme Aschauer et al. / IFAC-PapersOnLine 48-1 (2015) 067–072

8th Vienna International Conference on Mathematical Modelling February8th Vienna 18 International - 20, 2015. Vienna Conference University on Mathematical of Technology, Modelling Vienna, 8th Vienna International Conference on Mathematical Modelling AustriaFebruary 18 - 20, 2015. Vienna UniversityAvailable of Technology, online at Vienna,www.sciencedirect.com AustriaFebruary 18 - 20, 2015. Vienna University of Technology, Vienna, Austria ScienceDirect

IFAC-PapersOnLine 48-1 (2015) 067–072 Co-Simulation of Matlab and FlightGear Co-Simulation of Matlab and FlightGear for Identification and Control of Aircraft for Identification and Control of Aircraft Guilherme Aschauer ∗ Alexander Schirrer ∗ Martin Kozek ∗ Guilherme Aschauer ∗ Alexander Schirrer ∗ Martin Kozek ∗ Guilherme Aschauer ∗ Alexander Schirrer ∗ Martin Kozek ∗ Inst. of Mechanics & Mechatronics, Vienna University of Technology, ∗ Inst. of Mechanics & Mechatronics, Vienna University of Technology, ∗ Austria ∗ Inst. of Mechanics & Mechatronics,Austria Vienna University of Technology, e-mail: [email protected] e-mail: [email protected] e-mail: [email protected] Abstract: The paper outlines the development of a co-simulation solution of Matlab and Flight- Abstract: The paper outlines the development of a co-simulation solution of Matlab and Flight- Gear in which the communication between these programs is done via UDP without needing GearAbstract: in whichThe the paper communication outlines the development between these of a programs co-simulation is done solution via UDP of Matlab without and needing Flight- further toolsets. The simulation and rendering is done by FlightGear. Flight measurement signals furtherGear in toolsets. which the The communication simulation and between rendering these is done programs by FlightGear. is done Flight via UDP measurement without needing signals are sent to Matlab, which in turn sends back actuator input values computed by a flight control arefurther sent toolsets. to Matlab, The which simulation in turn and sends rendering back actuator is done by input FlightGear. values computed Flight measurement by a flight control signals system. First, gray-box models of the aircraft dynamics are identified for different altitudes for system.are sent First,to Matlab, gray-box which models in turn of sends the aircraft back actuator dynamics input are valuesidentified computed for different by a flight altitudes control for longitudinal and lateral dynamics so that gain-scheduled controllers can be used. The output longitudinalsystem. First, and gray-box lateral models dynamics of the so that aircraft gain-scheduled dynamics are controllers identified can for different be used. altitudes The output for quantities to be controlled are altitude and heading and three state-space controllers (MPC, quantitieslongitudinal to and be controlled lateral dynamics are altitude so that and gain-scheduled heading and controllers three state-space can be controllers used. The (MPC,output LQR and a gain-scheduled LQR) are designed and compared by tracking two reference signals LQRquantities and a to gain-scheduled be controlled LQR) are altitude are designed and heading and compared and three by state-space tracking two controllers reference (MPC,signals including steps and ramps. At the end the robustness of the controllers are verified by flying in includingLQR and steps a gain-scheduled and ramps. LQR)At the are end designed the robustness and compared of the controllers by tracking are two verified reference by flying signals in turbulent weather conditions. turbulentincluding steps weather and conditions. ramps. At the end the robustness of the controllers are verified by flying in turbulent 2015, IFAC weather (International conditions. Federation of Automatic Control) Hosting by Elsevier Ltd. All rights reserved. Keywords:© Aerospace computer control, Automated guided vehicles, Co-Simulation, Parameter Keywords: Aerospace computer control, Automated guided vehicles, Co-Simulation, Parameter estimation, LQR control method, LQG control method, Predictive control estimation,Keywords: Aerospace LQR control computer method, control, LQG control Automated method, guided Predictive vehicles, control Co-Simulation, Parameter estimation, LQR control method, LQG control method, Predictive control 1. INTRODUCTION sufficiently well and still the results have to be taken with 1. INTRODUCTION sufficiently well and still the results have to be taken with caution. 1. INTRODUCTION caution.sufficiently well and still the results have to be taken with Considerable progress has been made in the past few years Thiscaution. study will show how it is possible to run FlightGear Considerable progress has1 been made in the past few years2 This study will show how it is possible to run FlightGear Considerablein affordable (X-Plane progress has1 ) or been freely made available in the (FlightGear past few years2 ) Thisor X-Plane study will in co-simulation show how it iswith possible Matlab to usingrun FlightGear the UDP in affordable (X-Plane 1 ) or freely available (FlightGear 2 ) orThis X-Plane study will in co-simulation show how it iswith possible Matlab to usingrun FlightGear the UDP realisticin affordable flight (X-Plane simulation1 ) software. or freely available Both programs (FlightGear promise2 ) . This approach allows to run both realistic flight simulation software. Both programs promise communicationor X-Plane in co-simulation protocol. This with approach Matlab allows using to the run UDP both realistica largedegree flight simulation of realism software. and are Both even programs used in trainingpromise programs on different platforms as long as they are in a large degree of realism and are even used in training programscommunication on different protocol. platforms This approach as long allows as they to run are both in acockpits large degree for pilots. of realism FlightGear and are offers even the used choice in betweentraining the same local network. In order to obtain high flexibility, cockpits for pilots. FlightGear offers the choice between theprograms same local on different network. platforms In order to as obtain long ashigh they flexibility, are in cockpitsseveral well-established for pilots. FlightGear flight dynamics offers the models choice (FDM) between on and especially the Aerospace Toolbox have not several well-established flight dynamics models (FDM) on Simulinkthe same localand especially network. In the order Aerospace to obtain Toolbox high flexibility, have not severalthe basis well-established of mostly nonlinear flight dynamics equations models of motion (FDM) like on been used since the latter one is so far only working with the basis of mostly nonlinear equations of motion like beenSimulink used and since especially the latter the one Aerospace is so far only Toolbox working have with not theJSBSim basis (Berndt of mostly (2004)) nonlinear or the equations usage of of a user-defined motion like FlightGear and is not a standard package of Matlab. If JSBSim (Berndt (2004)) or the usage of a user-defined FlightGearbeen used since and the is not latter a standard one is so package far only ofworking Matlab. with If JSBSimmodel. This (Berndt is helpful (2004)) where or the highly usage specialized of a user-defined aircraft Matlab is not available, the main functionality of the script model. This is helpful where highly specialized aircraft MatlabFlightGear is not and available, is not thea standard main functionality package of of Matlab. the script If model.like lighter-than-air This is helpful aircraft where are highly used, specialized or a custom aircraft set can easily be implemented in ++ or any other high-level like lighter-than-air aircraft are used, or a custom set canMatlab easily is not be implemented available, the in main C++ functionality or any other of high-levelthe script likeof equations lighter-than-air is available, aircraft and are where used, FlightGear or a custom is just set programming language. of equations is available, and where FlightGear is just programmingcan easily be implemented language. in C++ or any other high-level ofused equations for visualization is available, purposes and and where for FlightGear providing realistic is just used for visualization purposes and for providing realistic Theprogramming flight simulator language. sends flight attitude data and receives usedenvironmental for visualization conditions. purposes The drawback and for providing of this approach realistic The flight simulator sends flight attitude data and receives environmental conditions. The drawback of this approach actuator input values (throttle, elevator, aileron, and rud- environmentalis that the user conditions. has to provide The drawback all the coefficients of this approach used in actuatorThe flight input simulator values sends (throttle, flight elevator, attitude data aileron, and and receives rud- is that the user has to provide all the coefficients used in der). After establishing a working connection between the isthe that FDM, the including user hasa to parametrization provide all the overcoefficients different used flight in der).actuator After input establishing values (throttle, a working elevator, connection aileron, between and rud- the the FDM, including a parametrization over different flight two programs, identification runs are made for longitudinal theparameters FDM, including to increase a parametrization the degree of realism. over different flight twoder). programs, After establishing identification a working runs are connection made for betweenlongitudinal the parameters to increase the degree of realism. and lateral motion independent of each other for different parameters to increase the degree of realism. andtwo programs, lateral motion identification independent runs of are each made other for longitudinalfor different A completely different approach is taken by X-Plane where altitudes with a chirp signal overlaid with low-pass filtered A completely different approach is taken by X-Plane where altitudesand lateral with motion a chirp independent signal overlaid of each with other low-pass for different filtered Ainstead completely of an different FDM the approach blade-element-theory is taken by X-Plane is applied. where white noise as input data to guarantee sufficient excitation instead of an FDM the blade-element-theory is applied. whitealtitudes noise with as inputa chirp data signal to guaranteeoverlaid with sufficient low-pass excitation filtered insteadHere, the of geometryan FDM the of the blade-element-theory aircraft has to be is modeled applied. in the relevant frequency areas. With this input/output Here, the geometry of the aircraft has to be modeled inwhite the noise relevant as input frequency data to areas. guarantee With sufficient this input/output excitation Here,in detail the because geometry the of forces the aircraft and moments has to beare modeled directly data a model estimation process can be performed, using in detail because the forces and moments are directly datain the a relevantmodel estimation frequency process areas.can With be this performed, input/output using incalculated detail because on its elements the forces (e. and g. wings). moments This are offers directly the a black-box model or, preferably, a gray-box model since calculated on its elements (e. g. wings). This offers the adata black-box a model model estimation or, preferably, process can a gray-box be performed, model using since calculatedpossibility toon investigate its elements the (e. approximate g. wings). Thisflying offers behavior the the equations of motion for standard aircraft dynamics are possibility to investigate the approximate flying behavior thea black-box equations model of motion or, preferably, for standard a gray-box aircraft dynamics model since are possibilityof a newly developedto investigate aircraft the approximatewithout having flying to build behavior and available (e. g. Brockhaus (2001)). These models are the of a newly developed aircraft without having to build and availablethe equations (e. g. of Brockhaus motion for(2001)). standard These aircraft models dynamics are theare oftest a newly a prototype. developed Obtaining aircraft without a mathematical having to model build and can starting point for the control design process. In this work, test a prototype. Obtaining a mathematical model can startingavailable point (e. g. for Brockhaus the control (2001)). design These process. models In this are work, the testalso be a prototype. easier with Obtaining this approach a mathematical since one does model not need can three different reference tracking controllers (heading and also be easier with this approach since one does not need threestarting different point for reference the control tracking design controllers process. (heading In this work, and alsospecific be easier knowledge with ofthis the approach implemented/modeled since one does not physical need altitude) have been used: a model predictive controller specific knowledge of the implemented/modeled physical altitude)three different have reference been used: tracking a model controllers predictive (heading controller and specificprocesses knowledge but can rely ofon the the implemented/modeled computations of X-Plane physical and (MPC), an LQR controller, and a scheduled LQR con- processes but can rely on the computations of X-Plane and (MPC),altitude) an have LQR been controller, used: a and model a scheduled predictive LQR controller con- processestry to obtain but can a black- rely on or the gray-box computations model from of X-Plane input and troller. The models and controllers are then validated by try to obtain a black- or gray-box model from input and troller.(MPC), The an LQRmodels controller, and controllers and a are scheduled then validated LQR con- by tryoutput to obtain data. Of a black- course, or the gray-box geometry model has from to be input modeled and flying through turbulent weather conditions with reference output data. Of course, the geometry has to be modeled flyingtroller. through The models turbulent andweather controllers conditions are then with validated reference by output data. Of course, the geometry has to be modeled test signals including steps and ramps. The airplane used testflying signals through including turbulent steps weather and ramps. conditions The with airplane reference used testthroughout signals including this paper steps is a and Concorde ramps. whichThe airplane is theoreti- used 1 throughouttest signals including this paper steps is a and Concorde ramps. whichThe airplane is theoreti- used 1 www.x-plane.com 2 www.x-plane.com throughoutcally capable this of flying paper at is supersonic a Concorde speed. which However, is theoreti- the 1 www.flightgear.org cally capable of flying at supersonic speed. However, the 2 www.flightgear.orgwww.x-plane.com cally capable of flying at supersonic speed. However, the 2 www.flightgear.org Copyright © 2015, IFAC 67 Copyright2405-8963 ©© 2015,2015, IFAC IFAC (International Federation of Automatic Control)67 Hosting by Elsevier Ltd. All rights reserved. CopyrightPeer review © under 2015, responsibility IFAC of International Federation of Automatic67 Control. 10.1016/j.ifacol.2015.05.071 MATHMOD 2015 February68 18 - 20, 2015. Vienna, AustriaGuilherme Aschauer et al. / IFAC-PapersOnLine 48-1 (2015) 067–072

present study only considers subsonic operation. Future x˙ Lat = ALat,4 4xLat + BLat,4 2uLat (2) × × work could consist of an adaptive controller which is not T only dependent on altitude (as used in this work) but also The state vector xLat =[r,β,p,φ] now consists of the on speed, so that a transition from sub- to supersonic yaw rate r, the sideslip angle β, the roll rate p, and the flight would be possible. Since the aerodynamic center of roll angle φ. The introduced output state is the heading Ψ lift of the Concorde changes when this happens, fuel is (often also called yaw) and its derivative is only dependent T transferred inside the plane to different tanks in order on the yaw rate r. The input quantities uLat =[ξ, ζ ] are to shape balance and trim. This of course changes the the aileron ξ and the rudder ζ. These two sets of equations center of gravity and therefore the flight behavior. So the are the linearized equations of motion for a conventional challenging task of designing an efficient controller which is aircraft linearized around level-trimmed flight conditions. capable of flying the plane in sub- and supersonic domains Then the longitudinal and lateral dynamics are mutually was not considered in this paper. decoupled. A co-simulation coupling between X-Plane and Matlab The angle of attack α and the sideslip angle β are called has already been established for example by Ribeiro and aerodynamic angles and describe the direction of the Oliveira (2010), who, however, only investigated the roll relative wind from the airplane-fixed coordinate system dynamics for creating a roll attitude autopilot system. through the body’s center of mass. Roll, pitch, and yaw Nusyirwan (2011) developed a connection between Mat- describe the orientation of the airplane in space with angles lab and FlightGear by using Python. A custom set of about the center of mass and are also referred to as the equations was used for the flight dynamics and a simple aircraft’s principal axes. The angle of climb γ is defined as PID altitude-hold and heading-hold was developed. The the vertical angle between the flight path and the Earth. visualization is done via FlightGear on a multi-monitor One can also interpret it as the ratio between the height setup. Sorton and Hammaker (2005) used FlightGear with change and the distance covered. Therefore, the altitude a custom flight dynamics model in JSBSim and Matlab change is (linearly) only dependent on γ. with a C++ interface for the simulation of an autonomous Both continuous-time models are of the standard form unmanned aerial vehicle and afterwards a Hardware-in- the-Loop testing was done with a self-developed actuator. x˙ (t)=Acx(t)+Bcu(t), (3) y(t)=Cx(t). (4) In this work the flight calculations are done by FlightGear and this program is assumed to be a black box to Matlab, Since the state vectors consist of physical states, the C just as a real plane would be. This approach benefits from matrices just select the output states. The discrete version the high degree of realism provided by FlightGear which of the state equation is written as: already uses well-established, realistic FDMs, which are x(k + 1) = Adx(k)+Bdu(k), (5) based on the nonlinear equations of motion and therefore y(k)=Cx(k). (6) avoids the use of potentially over-simplified flight dynam- ics models. Also, different planes and flying objects can be The computation of the discrete state space matrices is used. Since just a computer with Matlab and FlightGear possible from their continuous counterparts and depends is needed, the developed framework has high potential on the sampling time Ts as for example shown in Ogata in educational use as different controllers can easily be (1995): implemented and tested on potentially highly challenging Ad = expm(AcTs), (7) plants (for example fighter aircrafts in their nonlinear Ts 1 operation area or the already mentioned transition from B = exp (A τ)dτ B = A− (A I) B , (8) d m c c c d − c sub- to supersonic speeds). τ=0  where a zero-order-hold (piecewise constant) input u is 2. METHODOLOGY assumed and expm denotes the matrix exponential. Since the output of most black-box estimation procedures is a The linearized equations of motion are taken from Brock- discrete-time representation of the system dynamics, from haus (2001) and are extended with a fifth state (alti- now on the index d is omitted when referring to the state tude respectively heading) which also functions as the space matrices. controlled output afterwards. The linearized longitudinal dynamics are described by: After the estimation and validation of a model different controllers are designed. The discrete-time LQR (linear x˙ Lon = ALon,4 4xLon + BLon,4 2uLon (1) × × quadratic regulator) minimizes a cost function of the where the specific entries of the matrices A and B depend following form: on the altitude and have to be estimated. The state vector T ∞ T T xLon =[q,α,v,γ] consists of the quantities pitch rate q, J = x(k) Qx(k)+u(k) Ru(k) , (9) the angle of attack α, the velocity v, and the angle of climb k=0    γ. In some books (e. g. Stevens and Lewis (2003)) instead where Q>0 and R>0 are the weighting matrices for of γ the pitch angle θ is used, but since the linearized the state error and input actions. The state vectors are relation γ = θ α holds, both choices are valid. The input − T augmented by the integrated output error to account for quantities uLon =[δF ,η] are the throttle δF and the offset errors, changing operating conditions such as fuel elevator η. A fifth state — the altitude — is introduced usage and wind as well as model mismatch. As shown in and its derivative is only dependent on γ. Ogata (1995) an optimal solution of the form: The lateral dynamics are described by: u(k)= Kx(k), (10) −

68 MATHMOD 2015 February 18 - 20, 2015. Vienna, AustriaGuilherme Aschauer et al. / IFAC-PapersOnLine 48-1 (2015) 067–072 69 exists, with K obtained from the solution of the associated Riccati equation, FlightGear T 1 T K = R + B PB − B P A, actuator flight T T 1 T input attitude P = Q + A P  PB R + B PB − B P A, − data data which minimizes the cost function (9). The integrator in discrete time is realized by the Forward-Euler formula: Matlab

xi(k + 1) = xi(k)+Tse(k), where e(k) is the control error between the reference r(k) Fig. 1. UDP communication block diagram and the output y(k): can be chosen freely and was set to 0.1 s. Care must be e(k)=r(k) y(k). − taken that Matlab does not take longer than that amount In the gain-scheduled version of the LQR controller, the of time to compute and send the actuator input values, gain matrix K is computed for different altitudes and because otherwise the packages are being buffered and then linearly interpolated between two resulting gains Matlab receives out-dated flight attitude data which may depending on the actual altitude. lead to unintended flight behavior due to wrong actuator inputs calculations by the controller. A simplified outline The second controller used is an MPC (model predictive of the communication is shown in Figure 1. A sample file controller), also called receding horizon controller. In every that describes the communication protocol used between iteration, a (constrained) optimization problem is solved Matlab and FlightGear is shown in Listing 1. and the first step of the resulting control strategy is applied. A prediction horizon np is specified as the number Listing 1. Example communication protocol file of time instants (samples) for which the predicted output y is calculated when control action is applied for n p c samples. Often, nc is chosen much shorter than np to reduce computation time since the optimization problem true size has to be solved faster than the sampling time Ts which was set to 0.1 s in this work. The implementation of the predictive controller follows the approach in Wang (2009) in which the differences of the control and state /controls/engines/engine/throttle vectors are used: throttle float ∆x(k)=x(k) x(k 1), − − %f ∆u(k)=u(k) u(k 1). − − Then the augmented state vector is defined as /controls/flight/elevator T T T elevator xaug(k)= ∆x(k) , y(k) float to finally obtain an augmented state space model on which %f   the optimization process is based: ∆x(k + 1) A 0 ∆x(k) B = + ∆u(k), y(k + 1) CA I y(k) CB        /position/altitude -ft ∆x(k) altitude -ft y(k)= 0 I float y(k)   %f With this approach, integrators are implicitly added for the outputs. Then a constrained optimization problem is formulated that is solved by quadratic programming. Limitations on the absolute values as well as on the rate of change of the actuator inputs and plant outputs can In this example FlightGear receives the throttle and ele- directly be cast into linear inequality constraints. vator input values and sends the current altitude. This is done on UDP ports that have to be specified via parame- 3. FRAMEWORK ters when FlightGear is started, as well as the rate at which packets are sent. More values can easily be accessed by The main part of the communication between Matlab and adding them in chunk blocks inside the protocol definition. FlightGear is done via UDP (user datagram protocol), an IP-based network protocol with a simple transmission Additionally, FlightGear can start a local HTTP server to model and very little overhead. There is no handshake manipulate a large number of variables that can be read dialogue before a communication and therefore no guar- out and written to in a normal web browser as well as by antee of successful delivery and ordering. Also, packages opening URLs from inside Matlab. Opening the URL can be received out of order, a missing package cannot be detected easily, and there is no error checking provided http://localhost:5505/controls/gear/ within the protocol itself. In real-time systems with time- brake-parking?value=1&submit=update, sensitive applications, UDP is often used when package for example enables the parking brake if the server has loss is preferable to waiting for resent packages. The sam- been started on port 5505. If the variable should only be pling time at which FlightGear sends and receives data read then the part of the URL after the question mark has

69 MATHMOD 2015 70February 18 - 20, 2015. Vienna, AustriaGuilherme Aschauer et al. / IFAC-PapersOnLine 48-1 (2015) 067–072

to be omitted. Thus all FlightGear variables can either be 1.1 0.1 accessed via UDP or HTTP. The latter option has the advantage of manipulating variables only when there is a 0 1 need to do so. It is therefore not necessary to send data at 0.1 every sample interval in UDP packages. On the other hand, − 0.9 0.2 if called repeatedly, the HTTP operations are too slow for − real-time usage. So this option is chosen to control the , Throttle Elevator 0.8 0.3 autopilot, the flaps during take-off, and the parking brake. − 0 50 100 150 200 250 300 Inside Matlab, the communication is done via two UDP time (s) objects (one for reading and one for writing) which have to match the ports specified in FlightGear. Then these 20 0 objects can be treated like normal files by calling fread 20 − and fwrite. URLs are called by the command url and no 40 − further initialization has to be done. 60 −

power spectral 80 A sample command line execution of FlightGear with density (dB/Hz) − 0 0.5 1 1.5 2 2.5 3 providing some options is: frequency (Hz) fgfs --turbulence=0 --wind=0@0 --aircraft=Concorde --timeofday=noon --httpd=5505 --enable-clock-freeze --generic=socket,out,10,localhost,5501,udp,simple Fig. 2. Throttle (gray) and elevator (black) input for --generic=socket,in,10,,5502,udp,simple obtaining identification data and their power spectra This starts FlightGear with the Concorde aircraft in r e xi u x y paused mode (clock freeze) standing on the standard air- K FlightGear C − port with mild weather conditions (no wind and turbu- lence). A local webserver is started on port 5505 to access the property tree for getting access to all internal variables via a web browser. On port 5501 FlightGear sends UDP Fig. 3. Integrating LQR controller with full state feeback packages at a rate of 10 Hz in the form described in the file simple.xml and on port 5502 it waits for UDP packages. Figure 2 shows that there are no relevant information above 0.5 Hz and 1.5 Hz for the throttle and elevator inputs, respectively. The aileron and rudder characteristics 4. MODELLING are similar to the elevator since their time constants are on a comparable scale. Higher frequency inputs not Highly accurate nonlinear sets of equations exist to de- violating the Nyquist criterion are possible, but show no scribe the standard flight motion of an aircraft (see for ex- relevant dynamical effects on the aircraft for the modeling ample, Stevens and Lewis (2003) and Brockhaus (2001)). goals. The dynamics of the throttle-actuated thrust are As long as the plane is flying in conditions usually occuring even slower than those of the other actuators and so during scheduled flights, a linearized version of these equa- there is no need for high-frequency inputs. At this point tions are sufficient for long-distance maneuvers like head- it also has to be mentioned that at the moment the ing and altitude changes. Brockhaus (2001) lists the state model of the Concorde in FlightGear does not seem matrices for lateral and longitudinal dynamics for different to represent realistic actuator dynamics except for the flight conditions for six different aircraft. Throughout this throttle and rudder inputs, which also discourages the use paper, the Concorde was used because an initial model of high-frequency inputs. for the estimation process could be obtained from the Excitation runs are then made for different altitudes, each literature and the model of the airplane in FlightGear 2000 m, starting at 2000 m until 16 000 m. At high alti- is sufficiently accurate. Furthermore, more advanced con- tudes the excitation runs had to be repeated occasionally, trollers capable of supersonic flight could be developed in because the estimation process sometimes failed. One pos- the future. sible reason for this is the trans-sonic speed regime reached In order to obtain input/output data for the estimation even without using the afterburner. process the plane’s built-in autopilot is used to reach 5. CONTROLLER AND OBSERVER DESIGN steady state conditions on a specified altitude. After turn- ing off the autopilot, the actuators for the longitudinal The first tested controller type are LQR controllers with dynamics (throttle and elevator) are excited with a chirp integration of the output error and full state feedback. The signal overlaid with low-pass filtered white noise, and the structure is shown in Figure 3. input and output (the whole state vector) are logged via Matlab and stored in a matrix. Then the procedure with The curled magenta lines indicate UDP communication reaching steady state with the autopilot and excitation while the rest of the block diagram is processed in Matlab. is repeated for the lateral dynamics (aileron and rudder). The reference value r is either the heading Ψr for the Care has to be taken that there are no relevant signal lateral or the reference altitude Hr for the longitudinal contents above the Nyquist frequency which is half the dynamics. Since both dynamics are linearly decoupled, two sampling frequency. An example of a throttle and elevator separate controllers are designed. The integrated output input action as well as their power spectra are depicted in error is appended to the state vector. The new state vector Figure 2. is multiplied by the controller gain matrix K obtained

70 MATHMOD 2015 February 18 - 20, 2015. Vienna, AustriaGuilherme Aschauer et al. / IFAC-PapersOnLine 48-1 (2015) 067–072 71

10 2 Disturbance · − Noise 5 xi 0 r e u y ym 5

K FlightGear pitch rate − − 0 50 100 150 200 250 300 2 10− yˆ OBSERVER 4 · + 2 H 0

− alpha 2 −4 + − xˆk+1 xˆ yˆ 0 50 100 150 200 250 300 1 k B z C 1 + + − 0.9 0.8

xˆ speed 0.7 A 0.6 0 50 100 150 200 250 300 10 2 · − 5 Fig. 4. Integrating LQR controller with state observer 0 5

gamma − 10 using Matlab’s lqi command. In case that not the entire − 0 50 100 150 200 250 300 state vectors are measurable, an observer is utilized to re- 0.55 construct the states. Here, a standard Luenberger-observer 0.5 0.45 is designed for demonstration purposes. The uncertainty altitude of the process model and appearance of the measurement 0 50 100 150 200 250 300 noise is just considered with one parameter during the time (s) design process. This parameter describes to which ex- tent the system relies on the measurements in favor of Fig. 5. Comparison between the (normalized) input/out- its own process model. Since the measurement noise has put data (gray) and the the output of the estimated artificially been added in Matlab its standard deviation model (black). is perfectly known and the process noise’s deviation can be estimated depending on the weather conditions (in the pitch rate 82.6 % yaw rate 89.9 % Matlab model, no wind and weather data were taken into alpha 78.3 % sideslip angle 96.6 % account). These two variables were used to additionally speed 89.7 % roll rate 88.7 % build a Kalman filter to calculate the optimal observer gain gamma 85.6 % roll angle 93.2 % matrix H. The full configuration is shown in Figure 4 but altitude 88.5 % heading 86.0 % . the outer feedback loop of the estimated outputy ˆ to the reference was omitted for clarity and space reasons. Again, The estimation results for different altitudes and also for the curled magenta lines indicate UDP communication be- the horizontal dynamics are of similar, high quality. After tween Matlab and FlightGear. The controller and observer designing the controllers, a flight path with changing ref- gains for MPC and LQR have been chosen to achieve erence altitude and heading values was created to be used acceptable performance for small- to medium-sized steps by all three controller types. For the predictive controller (maximum of 40 degrees change in heading direction and the control horizon was set to 15 samples (1.5 s) and the 4000 m in altitude). Larger reference steps would require prediction horizon was set to 300 samples. With these a feed-forward command shaping, lowering the feedback values it was just possible to run the co-simulation on a gains, or implementing error saturations and anti-reset standard notebook in simulated real-time. At first, mild windup logics in Matlab. When full state feedback was weather conditions were assumed for the validation of the used, all five states were considered measurable without controllers. The results are plotted in Figure 6 for the noise and directly used to calculate the next input action. scheduled LQR case with full state feedback. No further When observers were used, only the heading and altitude parameter tuning was done after the controllers showed were measured and additionally overlaid by noise. This satisfying behaviour (following a reference trajectory in means that four states had to be estimated from one noisy turbulent weather conditions). Especially after about 850 s measurement. in Figure 6 the mutual influence of the lateral and longitu- denal dynamics on each other can be seen. During a turn maneuver (ramp for changing the heading) the aircraft’s 6. RESULTS motion was disturbed by a large step in the reference altitude which led to large elevator action and the plane leaving the linear operation range where both dynamics The identification process results are highly satisfying, are decoupled. although sometimes the excitation process has to be re- peated for higher altitudes, presumably because the plane Then, the same runs were repeated with enabled turbu- is too close to Mach 1. Most of the time, though, and espe- lences (they were set to 0.75 in a range from zero to cially for altitudes below 14 000 m, the estimation results one). With these severe disturbances even FlightGear’s are in good agreement with the measurement data as can built-in autopilot had sometimes trouble with reaching be seen in Figure 5 where the measurements are compared respectively holding the desired heading. Figure 7 shows with the output of the estimated model for the vertical a part of the same reference flight path as Figure 6, but dynamics at 10 000 m. The root-mean-squared-error-based in turbulent weather conditions using a model predictive fit values for these examples are as follows: controller. Only the altitude and heading were measurable

71 MATHMOD 2015 72February 18 - 20, 2015. Vienna, AustriaGuilherme Aschauer et al. / IFAC-PapersOnLine 48-1 (2015) 067–072

14 12 10 8 6 4 altitude (km)

) 30 ◦ 20 10 0

heading ( 10 −0.3 0.03 0.2 0.02 0.1 0.01 0 0 0.1 0.01 − −

actuators 0.2 0.02 − elevator aileron − 0.3 0.03 − − 0 100 200 300 400 500 600 700 800 900 1000 1100 1200 time (s) Fig. 6. Reference trajectory (dashed line) and real altitude and heading by an integrating scheduled LQR controller with full state feedback (all states measurable without noise) and mild weather conditions. The third plot shows the normalized actuator input values (between 1 and 1) of the elevator (black) and aileron (red). − 12 10 types of aircraft. The realistic, open-source flight simula- 8 tor FlightGear offers great flexibility in accessing internal 6 variables via external interfaces and it was shown that 4

altitude (km) 2 an aircraft can be controlled with Matlab. The whole ) 30 ◦ process of starting FlightGear, selecting a specific aircraft, 20 establishing a UDP connection, take-off, identification, 10 controller design and following a reference trajectory is 0 heading ( all fully automated by a Matlab script, which offers the 0.2 possibility to easily design scheduled controllers. Different 0 types of controllers and state observers have been imple- 0.2 − elevator mented and their performance tested on a well visualized, 400 600 800 1000 1150 complex dynamic system. The design task can be made more challenging by changing weather conditions, failure time (s) of aircraft parts, leaving the linear operating range or Fig. 7. Reference trajectory (dashed line) and real altitude guaranteeing passenger comfort, and thus lends itself to and heading by a model predictive controller with use cases in control education. a state estimator (only altitude and heading were measurable and corrupted by noise). REFERENCES but corrupted with white noise, and the other states had to Berndt, J.S. (2004). JSBSim: An Open Source Flight be estimated. Depending on how aggressive the controller Dynamics Model in C++. In AIAA Modeling and Sim- is tuned the actuators seem to be excited with highly ulation Technologies Conference and Exhibit. Citeseer, dynamic inputs. However, further investigations showed Providence, Rhode Island. nd that the maximum rate of change of the elevator was Brockhaus, R. (2001). Flugregelung. Springer, 2 edition. less than 10◦ per second, and for the aileron and rudder Nusyirwan, I.F. (2011). Engineering flight simulator using commands even less demanding inputs were observed. Matlab, Python and Flightgear. In Simtect. Melbourne, Australia. The lateral dynamics are much more influenced by severe Ogata, K. (1995). Discrete-Time Control Systems. Pren- turbulence and appear noisy while the error dynamics of tice Hall, 2nd edition. the longitudinal dynamics are less affected by gusts. A Ribeiro, L. and Oliveira, N.M.F. (2010). UAV autopilot cause for the strong weather influence on the heading is controllers test platform using Matlab/Simulink and also the larger penalty imposed on the aileron and rudder X-Plane. In Frontiers in Education Conference (FIE), inputs in the optimization problem when designing the 2010 IEEE, S2H–1–S2H–6. controllers. They were chosen to avoid large roll angles Sorton, E. and Hammaker, S. (2005). Simulated flight which give rise to nonlinear dynamics and sometimes led testing of an autonomous unmanned aerial vehicle using to crashes, especially when combined with low speeds due Flightgear. In Infotech@Aerospace, 2005 AIAA. to climbing maneuvers. Stevens, B.L. and Lewis, F.L. (2003). Aircraft Control and Simulation. Wiley-Interscience. 7. CONCLUSION Wang, L. (2009). Model Predictive Control System Design and Implementation Using Matlab. Springer, 1st edition. The developed framework is able to establish a connection between FlightGear and Matlab and works with different

72