User:Kwikius/Using FlightGear for software in the loop simulation
Using FlightGear for software in the loop simulation
FlightGear is popular as both a viewer and a flight dynamics engine (FDE) for software in the loop (SITL) simulation. This article describes a project that uses FlightGear to provide a viewer and an FDE for simulating flying an RC aeroplane autonomously using a flight controller (FC). 
The main requirements for a SITL simulation
Interfacing to FlightGear
There are many options to choose from
See Interfacing FlightGear for the main article about this subject.
Sending data to the FDE
I chose to use telnet interface. There is a C++ example in the FlightGear source code , that shows basic useage. Telnet is relatively slow since it sends data as text, but you need to send a surprisingly small amount of information to control the aircraft. Usually you are merely sending information to modify pitch, roll and yaw. Less frequently you are changing throttle ,flaps and so on. By caching data in your app and only sending information if it actually changed, the overhead is quite low. When bandwidth becomes an issue, then there are other protocols available.
Receiving data from the FDE
I chose to use socket interface. Again there is a C++example in the FlightGear source code. Note that the example sends data to FlightGear, but the socket can also be used to read the FGNetFDM structure from FlightGear via a socket, which is what I chose to do. The FGNetFDM structure contains most, if not all the information that is need at a fast rate. Socket UDP interface sends the FGNetFDM structure as a binary chunk, so is fast. Other data can be read back using the telnet interface.
Ideally we would like to receive the FDE data from FlightGear at arbitrarily high rates, but unfortunately this is one area where FlightGear isnt great. I can only get an update rate of 10 Hz. In practise this limitation can be worked with by simulating aircraft where things happen more slowly. The only alternative is to use an external FDE and just use FlightGear as a viewer. 
Visualising the simulation
- The ability to view the simulation graphically. Though not entirely necessary, in practise this is most useful, since visualisation can show up issues that are hidden in data, therfore the examples use 3D graphical simulation where possible using FlightGear as the viewer in most cases.
- The source code for the examples is available on github at https://github.com/kwikius/fg_ext_fdm. The source code is written in reasonably modern C++ and makes use of a C++ library for representing physical quantities called quan, also available on github at https://github.com/kwikius/quan-trunk.
- Lurking in FlightGear, there is a pipe interface which would promise to be very fast, but there is not much information about it. ExternalPipe 
- The video above uses an update rate of 10Hz. The EasyStar has modified parameters.
- This is the approach taken by Ardupilot, by using JSBSim directly.