bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
A fixed wing UAV: trim, stability, autopilot, takeoff and landing

Flying a simulated quadrotor from the laptop flew a quadrotor, which hovers on its rotors. A fixed wing aircraft flies on its wing: it has to move through the air to stay up, it turns by banking, and it needs a runway. This example simulates the Aerosonde, the small UAV of Beard and McLain, Small Unmanned Aircraft: Theory and Practice: a 2.9 m wing, 11 kg, a propeller on an electric motor, and here a tricycle landing gear. It finds how the aircraft flies level, works out how it moves about that, designs an autopilot from it, and flies a whole mission: it takes off, flies a circuit of waypoints and lands back on the runway.

The takeoff and the landing, at twice the speed they ran

The takeoff and the landing, below 25 m, at twice the speed they ran. The yellow line is the path flown, the cyan one the line the autopilot follows: the climb out along the runway, and the glide slope down to the aim point.

What you need

Nothing for the flight itself; bitsim built with Chrono and Irrlicht to watch it, see Installing bitsim. Chrono only draws: the flight is FixedWingDynamics.

The aircraft

FixedWingDynamics is a rigid body in 6 degrees of freedom. Its state is the position in the world (north, east, down), the velocity in the body (u, v, w), the roll, pitch and yaw, and the body rates (p, q, r). The forces on it are:

  • the air, from the velocity through the air, which gives the airspeed \(V_a\), the angle of attack \(\alpha\) and the sideslip \(\beta\). With \(\bar q = \frac{1}{2}\rho V_a^2\), the lift is \(\bar q S C_L\) with \(C_L = C_{L0} + C_{L\alpha}\alpha\) up to the stall, after which it falls to that of a flat plate. The drag adds the induced drag, \(C_L^2 / (\pi e A)\). The elevator, the pitch rate, the sideslip, the ailerons and the rudder each add their coefficient times their value: these are the aircraft's stability derivatives, in FixedWingParameters;
  • the propeller, driven by a DC motor. For a throttle, the motor turns where its torque equals the propeller's, and the thrust follows from how far the propeller moves forward each turn. At low throttle and high airspeed the air drives the propeller, and it is a drag;
  • gravity;
  • the wheels, each a spring and damper while it is below the ground, that rolls along its heading and grips sideways. The nose wheel steers with the rudder, the main wheels brake.

It integrates with fourth order Runge-Kutta, in steps of 2 ms on the stiff gear.

Control Positive Limit
elevator trailing edge down: the nose pitches down 0.5 rad
aileron rolls right 0.5 rad
rudder yaws the nose left, and steers the nose wheel left 0.5 rad
throttle the motor's voltage, a share of the battery's 0 to 1
brake the main wheels 0 to 1

1. Trim

An aircraft is trimmed when nothing changes: every force and moment balances. trim_level_flight finds, for straight and level flight at an airspeed, the angle of attack, the sideslip, and the four controls that make the six accelerations zero. It searches with Levenberg-Marquardt steps from the angle of attack whose lift carries the weight.

Slower, the wing needs a larger angle of attack, and the elevator more up to hold it. Below 16 m/s the elevator cannot, and above 32 m/s the motor cannot pull the aircraft fast enough: between them is the speed range of the aircraft.

2. Stability

linearise works out how the aircraft moves near a trim, \(\dot{\delta x} = A\,\delta x + B\,\delta u\), and flight_modes splits the eigenvalues of \(A\) into the modes that every aircraft has:

Mode What it is
short period the nose bobs up and down at almost the same speed and height: fast and damped
phugoid a slow swap of height and speed: the aircraft climbs, slows, dives and speeds up
roll the roll rate settles after an aileron input: very fast
Dutch roll the nose wags from side to side as the aircraft rocks
spiral a slow wander in bank and heading, often unstable, as here

The spiral of the Aerosonde grows by \(e\) in 16 s: left alone, it would slowly tip into a turn. The autopilot is what keeps its wings level.

3. The autopilot

FixedWingAutopilot holds an airspeed, an altitude and a course with loops one inside the other, each about ten times slower than the loop inside it (Beard and McLain, chapter 6):

  • the course sets a bank, and the bank the ailerons; the rudder holds the sideslip at 0, so the turns are coordinated;
  • the altitude sets a pitch, the pitch the elevator, and the throttle holds the airspeed. More than 15 m below the commanded altitude it climbs at full throttle and the pitch holds the airspeed; more than 15 m above, it descends at idle.

Every gain comes from the linear model at 25 m/s: the roll loop, for example, has \(\dot p = -a_1 p + a_2 \delta_a\) with \(a_1\) and \(a_2\) read from \(A\) and \(B\), and its gains give it a chosen damping when the aileron reaches its limit at a 20 degree error.

4. The mission

FixedWingMission flies the aircraft from the runway and back:

  1. takeoff roll: full throttle, wings level, the nose wheel steering along the centre line;
  2. rotate at 17 m/s: pitch up to 10 degrees, until the wheels are 8 m up;
  3. enroute: follow straight lines from the runway to each waypoint in turn, and on to the final approach fix. On a line, the course commanded is \(\chi_q - \chi_\infty \frac{2}{\pi}\arctan(k e)\), with \(\chi_q\) the line's direction and \(e\) the distance right of it: across it far away, along it close to it. The next line starts when the aircraft crosses the half plane that bisects the corner;
  4. approach: down a 4 degree glide slope at 18 m/s, to an aim point 60 m past the threshold;
  5. flare: below 3 m the pitch holds a sink rate that falls with the height, to 0.3 m/s at the ground, and the throttle closes;
  6. rollout: on its wheels it steers along the centre line, and brakes below 12 m/s until it stops.

Running the example

./fixed_wing_flight # headless, as fast as it can
./fixed_wing_flight --wind 0 -4 # with a wind of 4 m/s towards the west: a crosswind
./fixed_wing_flight --render # in a window, in real time
./fixed_wing_flight --record DIR # and write the takeoff and the landing to DIR

It returns 0 if the aircraft stopped on the runway after a touchdown at less than 1.5 m/s.

Output

1. Trim for straight and level flight
Va [m/s] alpha [deg] elevator [deg] throttle trim
14.0 14.4 -39.2 0.48 no: needs more elevator than it has
18.0 7.8 -20.8 0.57 yes
22.0 4.4 -11.4 0.68 yes
26.0 2.4 -6.0 0.80 yes
30.0 1.2 -2.6 0.92 yes
34.0 0.4 -0.3 1.05 no: needs more than full throttle
2. The modes of its motion at 25 m/s
mode eigenvalue wn [rad/s] damping period [s] time constant [s]
short period -4.895 +/- 9.870i 11.017 0.444 0.64 0.20
phugoid -0.143 +/- 0.478i 0.499 0.286 13.14 7.02
Dutch roll -1.551 +/- 5.935i 6.134 0.253 1.06 0.64
roll -22.017 22.017 1.000 - 0.05
spiral 0.062 0.062 -1.000 - -16.04 it grows
3. Autopilot gains, from the linear model at 25 m/s
roll: kp 1.429, kd 0.012
course: kp 4.646, ki 2.118
sideslip: kp 2.941, ki 3.049
pitch: kp -2.941, kd -0.569, DC gain 0.516
altitude: kp 0.100, ki 0.040
airspeed: kp 0.097, ki 0.038 with the throttle; kp -0.512, ki -0.407 with the pitch
4. Takeoff, circuit and landing
Runway 400 m long, heading 0 deg. The route, north, east and altitude [m]:
700 0 60
700 500 80
-450 500 80
-700 250 50
-490 0 39 final approach fix
60 0 0 aim point
Wind towards the north 0.0 m/s and the east 0.0 m/s
t [s] phase leg north [m] east [m] alt [m] Va [m/s] bank [deg] off the line [m]
1.0 takeoff roll 0 5.0 -0.0 0.3 0.1 -0.0 0.0
3.8 rotate 0 31.1 -0.0 0.3 17.1 -0.1 -0.0 <- rotate
8.2 enroute 0 132.0 -0.9 8.4 26.6 -0.3 -0.0 <- enroute
10.0 enroute 0 178.6 -0.3 20.8 25.8 -1.7 -0.3
20.0 enroute 0 402.9 0.0 60.2 22.3 -0.1 0.0
40.0 enroute 1 785.5 93.4 80.1 22.4 30.5 -85.5
60.0 enroute 2 699.7 515.3 79.8 22.1 32.8 -15.1
...
150.3 approach 5 -490.8 -2.5 38.6 22.0 -1.3 -2.3 <- approach
170.0 approach 5 -144.3 -0.3 14.6 18.0 -0.1 -0.3
179.0 flare 5 17.5 -0.0 3.3 18.0 -0.0 -0.0 <- flare
181.6 rollout 5 61.4 0.0 0.3 16.1 -0.0 0.0 <- rollout
188.4 stopped 5 130.6 0.0 0.3 0.1 0.0 0.0 <- stopped
Lift-off after a roll of 36.9 m, at 19.6 m/s
On the last 300 m of the approach: at most 3.25 m off the centre line and 0.10 m off the glide slope
Touchdown at t = 181.5 s, 61.3 m past the threshold, 0.02 m off the centre line, sinking at 0.77 m/s, at 16.1 m/s
Stopped at t = 188.4 s, 130.6 m past the threshold, 0.01 m off the centre line
  • The takeoff needs 37 m of runway: the aircraft rotates at 17 m/s and leaves the ground at 19.6 m/s.
  • The turns overshoot each line by up to 85 m: the aircraft turns at its 30 degree bank limit, a circle of about 85 m radius at 22 m/s, and the half plane switches only at the corner.
  • The approach starts 2.3 m off the centre line after the last turn, and is within 3.3 m of it and 0.1 m of the glide slope over its last 300 m.
  • The touchdown is 1.3 m past the aim point, on the centre line, sinking at 0.77 m/s: a firm but normal landing for a small UAV. The aircraft stops 69 m later.

In a wind, the autopilot flies the course over the ground, so it crabs into a crosswind; the rudder keeps the sideslip at 0 and the wheels straighten the aircraft at the touchdown:

Wind towards (north, east) [m/s] Lift-off roll [m] Touchdown past the threshold [m] Off the centre line [m] Sinking [m/s] Stopped past the threshold [m]
(0, 0) 36.9 61.3 0.02 0.77 130.6
(-5, 0), a headwind 21.5 50.2 0.03 0.74 88.5
(0, -4), from the east 34.2 60.0 0.06 0.83 124.9
(0, 4), from the west 34.4 60.2 0.11 0.77 125.2

A headwind shortens the takeoff and the rollout, as it should: the aircraft reaches its airspeed at a lower speed over the ground.

The library

Class What it does
FixedWingDynamics the 6-DOF aircraft, its propeller and its landing gear
FixedWingParameters its mass, geometry, coefficients and gear; aerosonde()
trim_level_flight the trim for level flight at an airspeed
linearise the linear model about a trim
flight_modes the modes of the linear model
FixedWingAutopilot airspeed, altitude and course hold, with gains from the linear model
FixedWingMission takeoff, waypoints, approach, flare and rollout

Things to try

  1. Make Cn_beta 0, the aircraft's weathervane stability. What happens to the Dutch roll, and can the autopilot still fly the mission?
  2. Make the glide slope 6 degrees. How hard does it land, and why?
  3. Raise max_bank in the autopilot's design to 45 degrees. How much less do the turns overshoot?
  4. Add a gust: change the wind for 2 s during the approach. Where does the aircraft touch down?
  5. Design the autopilot at 18 m/s instead of 25. Does the approach get better, and the cruise worse?

The code

// A fixed wing UAV, the Aerosonde of Beard and McLain, takes off from a runway, flies a circuit of
// waypoints with an autopilot, and lands back on the runway. First the example finds the
// aircraft's trim for level flight at a range of airspeeds and the modes of its motion about one
// of them; the autopilot's gains come from that linear model.
//
// Usage: fixed_wing_flight [--wind N E] [--render] [--record DIR]
//
// --wind N E blows a steady wind towards the north and the east [m/s]. --render shows the flight
// in a window, in real time. --record DIR also writes the window to DIR, ten pictures a second,
// during the takeoff and the landing, below 25 m.
#include "bitsim/bitsim_config.h"
#if defined(BITSIM_CHRONO) && defined(BITSIM_IRRLICHT)
#include <chrono/assets/ChVisualShapeBox.h>
#include <chrono/physics/ChBody.h>
#include <chrono/physics/ChBodyEasy.h>
#include <chrono/physics/ChSystemNSC.h>
#include <chrono_irrlicht/ChVisualSystemIrrlicht.h>
#define FIXED_WING_RENDER
#endif
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
{
using namespace bitsim::dynamics;
using namespace bitsim::control;
const real_t DEG = PI / 180.0;
const real_t STEP = 0.01; // [s]
const real_t PRINT_PERIOD = 10.0; // [s]
const real_t TIME_LIMIT = 400.0; // [s]
const real_t RENDER_RATE = 30.0; // [frames/s]
const real_t RECORD_RATE = 10.0; // [pictures/s]
const real_t DESIGN_AIRSPEED = 25.0; // [m/s]
// A runway 400 m long running north, and a circuit around it: north, east, back south past the
// runway, and onto the final approach from the south west
const Runway RUNWAY{0.0, 0.0, 0.0, 400.0, 20.0};
const std::vector<MissionWaypoint> ROUTE{
{700.0, 0.0, 60.0}, {700.0, 500.0, 80.0}, {-450.0, 500.0, 80.0}, {-700.0, 250.0, 50.0}};
struct Options
{
bool render{false};
std::string record;
};
// ---------------------------------------------------------------------------
// Trim and modes
// ---------------------------------------------------------------------------
{
print_title("1. Trim for straight and level flight");
std::cout << " Va [m/s] alpha [deg] elevator [deg] throttle trim\n";
for (real_t Va = 14.0; Va <= 34.0 + 1e-9; Va += 4.0)
{
const auto t = trim_level_flight(p, Va);
std::string why = "yes";
if (!t.converged)
{
why = t.controls.throttle > 1.0 ? "no: needs more than full throttle"
: std::abs(t.controls.elevator) > p.max_elevator
? "no: needs more elevator than it has"
: "no";
}
std::cout << std::fixed << std::setprecision(1) << std::setw(10) << Va << std::setw(13)
<< t.alpha / DEG << std::setw(16) << t.controls.elevator / DEG
<< std::setprecision(2) << std::setw(10) << t.controls.throttle << " " << why
<< "\n";
}
}
{
print_title("2. The modes of its motion at 25 m/s");
const auto trim = trim_level_flight(p, DESIGN_AIRSPEED);
const auto modes = flight_modes(linearise(p, trim));
std::cout << " mode eigenvalue wn [rad/s] damping period [s] "
"time constant [s]\n";
for (const auto &m : modes)
{
std::ostringstream eig;
eig << std::fixed << std::setprecision(3) << m.eigenvalue.real();
if (std::abs(m.eigenvalue.imag()) > 1e-9)
{
eig << " +/- " << std::abs(m.eigenvalue.imag()) << "i";
}
std::ostringstream period;
if (m.period > 0.0)
{
period << std::fixed << std::setprecision(2) << m.period;
}
else
{
period << "-";
}
std::cout << " " << std::left << std::setw(14) << m.name << std::setw(20) << eig.str()
<< std::right << std::fixed << std::setprecision(3) << std::setw(10)
<< m.natural_frequency << std::setw(9) << m.damping << std::setw(12)
<< period.str() << std::setprecision(2) << std::setw(15) << m.time_constant
<< (m.time_constant < 0 ? " it grows" : "") << "\n";
}
}
void show_gains(const FixedWingAutopilot &autopilot)
{
print_title("3. Autopilot gains, from the linear model at 25 m/s");
const auto &g = autopilot.gains();
std::cout << std::fixed << std::setprecision(3) << " roll: kp " << g.roll_kp << ", kd "
<< g.roll_kd << "\n"
<< " course: kp " << g.course_kp << ", ki " << g.course_ki << "\n"
<< " sideslip: kp " << g.sideslip_kp << ", ki " << g.sideslip_ki << "\n"
<< " pitch: kp " << g.pitch_kp << ", kd " << g.pitch_kd << ", DC gain "
<< g.pitch_dc_gain << "\n"
<< " altitude: kp " << g.altitude_kp << ", ki " << g.altitude_ki << "\n"
<< " airspeed: kp " << g.throttle_kp << ", ki " << g.throttle_ki
<< " with the throttle; kp " << g.airspeed_pitch_kp << ", ki " << g.airspeed_pitch_ki
<< " with the pitch\n";
}
// ---------------------------------------------------------------------------
// The window
// ---------------------------------------------------------------------------
#ifdef FIXED_WING_RENDER
using chrono::ChColor;
using chrono::ChVector3d;
// NED to Chrono's z up, a half turn about x: (n, e, d) -> (n, -e, -d)
const chrono::ChMatrix33d NED_TO_Z_UP(ChVector3d(1, 0, 0), ChVector3d(0, -1, 0),
ChVector3d(0, 0, -1));
ChVector3d to_z_up(real_t n, real_t e, real_t d) { return {n, -e, -d}; }
class Display
{
public:
Display(const FixedWingMission &mission, const FixedWingParameters &p,
const std::string &record)
: recorder_(record, RECORD_RATE)
{
system_.SetGravitationalAcceleration(ChVector3d(0, 0, 0));
add_box_({0, 0, -0.05}, {4000, 4000, 0.1}, ChColor(0.42f, 0.55f, 0.36f));
// The runway, its centre line and the threshold's bars
const real_t L = RUNWAY.length;
add_box_({L / 2, 0, 0.005}, {L, RUNWAY.width, 0.01}, ChColor(0.28f, 0.28f, 0.30f));
for (real_t x = 20.0; x < L - 10.0; x += 25.0)
{
add_box_({x, 0, 0.012}, {12, 0.4, 0.01}, ChColor(0.92f, 0.92f, 0.92f));
}
for (real_t y = -7.5; y <= 7.5; y += 2.5)
{
add_box_({5, y, 0.012}, {8, 1.0, 0.01}, ChColor(0.92f, 0.92f, 0.92f));
}
// A pole under each point of the route, as high as it
for (std::size_t i = 1; i + 1 < mission.points().size(); ++i)
{
const auto &w = mission.points()[i];
add_box_(to_z_up(w.north, w.east, -0.5 * w.altitude), {1.5, 1.5, w.altitude},
ChColor(0.9f, 0.55f, 0.15f));
}
aircraft_ = make_aircraft_(p);
system_.Add(aircraft_);
vis_.AttachSystem(&system_);
vis_.SetWindowSize(1280, 720);
vis_.SetWindowTitle("A fixed wing UAV: takeoff, circuit and landing");
vis_.SetCameraVertical(chrono::CameraVerticalDir::Z);
vis_.Initialize();
vis_.AddCamera({-12, 4, 4}, {0, 0, 0});
vis_.GetActiveCamera()->setFarValue(5000.0f);
// A soft light: the typical lights wash the scene out
vis_.AddLightDirectional(60.0, 60.0, ChColor(0.5f, 0.5f, 0.5f),
ChColor(0.05f, 0.05f, 0.05f), ChColor(0.55f, 0.55f, 0.55f));
}
// Returns false once the window is closed
bool show(const FixedWingDynamics &aircraft, const FixedWingMission &mission,
const std::string &status, bool record)
{
if (!vis_.Run())
{
return false;
}
const auto &x = aircraft.state();
const ChVector3d position = to_z_up(x[fw::PN], x[fw::PE], x[fw::PD]);
chrono::ChMatrix33d Rc;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
Rc(i, j) = R(i, j);
}
}
aircraft_->SetPos(position);
aircraft_->SetRot(chrono::ChMatrix33d(NED_TO_Z_UP * Rc * NED_TO_Z_UP));
trail_.add(position);
// The camera follows behind, to the right and above, along the course. In the drawing's
// axes north is x and east is -y
const real_t chi = aircraft.course();
const ChVector3d ahead(std::cos(chi), -std::sin(chi), 0.0);
const ChVector3d right(-std::sin(chi), -std::cos(chi), 0.0);
vis_.UpdateCamera(position - ahead * 7.0 + right * 4.0 + ChVector3d(0, 0, 2.0), position);
vis_.BeginScene();
vis_.Render();
// The route, and the line the aircraft follows now
const auto &pts = mission.points();
for (std::size_t i = 1; i < pts.size(); ++i)
{
const bool now = i == mission.leg() + 1;
vis_, to_z_up(pts[i - 1].north, pts[i - 1].east, -pts[i - 1].altitude),
to_z_up(pts[i].north, pts[i].east, -pts[i].altitude),
now ? ChColor(0.2f, 0.9f, 1.0f) : ChColor(0.6f, 0.75f, 0.8f), now ? 2.0 : 1.0);
}
trail_.draw(vis_, ChColor(1.0f, 0.85f, 0.2f), 2.0);
ChVector3d(position.x(), position.y(), 0.0),
ChColor(0.2f, 0.2f, 0.2f), 1.0);
vis_.EndScene();
if (record)
{
recorder_.record(vis_, aircraft.time());
}
return true;
}
private:
void add_box_(const ChVector3d &at, const ChVector3d &size, const ChColor &colour)
{
auto box = chrono_types::make_shared<chrono::ChBodyEasyBox>(size.x(), size.y(), size.z(),
100.0, true, false);
box->SetPos(at);
box->SetFixed(true);
box->GetVisualShape(0)->SetColor(colour);
system_.Add(box);
}
// A fuselage, a wing, a tail and the wheels, in the drawing's axes: x ahead, y left, z up
std::shared_ptr<chrono::ChBody> make_aircraft_(const FixedWingParameters &p)
{
auto body = chrono_types::make_shared<chrono::ChBody>();
body->SetFixed(true);
const auto add = [&](const ChVector3d &size, const ChVector3d &at, const ChColor &colour)
{
auto shape =
chrono_types::make_shared<chrono::ChVisualShapeBox>(size.x(), size.y(), size.z());
shape->SetColor(colour);
body->AddVisualShape(shape, chrono::ChFrame<>(at));
};
const ChColor white(0.92f, 0.92f, 0.95f);
const ChColor red(0.85f, 0.15f, 0.12f);
add({1.7, 0.18, 0.18}, {0.05, 0, 0}, white); // fuselage
add({0.30, p.b, 0.03}, {0.0, 0, 0.06}, white); // wing
add({0.22, 0.95, 0.02}, {-0.85, 0, 0.04}, white); // horizontal tail
add({0.26, 0.02, 0.34}, {-0.88, 0, 0.21}, red); // fin
add({0.04, 0.05, 0.05}, {0.05, p.b / 2, 0.06}, red); // left wing tip
add({0.04, 0.05, 0.05}, {0.05, -p.b / 2, 0.06}, ChColor(0.1f, 0.7f, 0.2f)); // right
add({0.02, 0.55, 0.05}, {0.92, 0, 0}, ChColor(0.2f, 0.2f, 0.2f)); // propeller
// The wheels, whose bottoms are where they touch the ground
for (const auto &w : p.gear)
{
add({0.1, 0.04, 0.1}, {w.position[0], -w.position[1], -w.position[2] + 0.05},
ChColor(0.1f, 0.1f, 0.1f));
}
return body;
}
chrono::ChSystemNSC system_;
std::shared_ptr<chrono::ChBody> aircraft_;
chrono::irrlicht::ChVisualSystemIrrlicht vis_;
bitsim::utils::render::FrameRecorder recorder_;
bitsim::utils::render::Trail trail_{1.0, 0.0};
};
#endif
// ---------------------------------------------------------------------------
// The flight
// ---------------------------------------------------------------------------
int fly(const FixedWingParameters &p, FixedWingAutopilot &autopilot, const Options &options)
{
print_title("4. Takeoff, circuit and landing");
FixedWingMission mission(autopilot, RUNWAY, ROUTE);
std::cout << std::fixed << std::setprecision(0) << " Runway " << RUNWAY.length
<< " m long, heading " << RUNWAY.heading / DEG
<< " deg. The route, north, east and altitude [m]:\n";
for (std::size_t i = 1; i < mission.points().size(); ++i)
{
const auto &w = mission.points()[i];
const char *what = i + 2 == mission.points().size() ? " final approach fix"
: i + 1 == mission.points().size() ? " aim point"
: "";
std::cout << std::fixed << std::setprecision(0) << " " << std::setw(6) << w.north
<< std::setw(7) << w.east << std::setw(5) << w.altitude << what << "\n";
}
std::cout << std::setprecision(1) << " Wind towards the north " << options.wind_north
<< " m/s and the east " << options.wind_east << " m/s\n\n";
// On its wheels at the start of the runway
FixedWingState x = FixedWingState::Zero();
x[fw::PN] = RUNWAY.north + 5.0;
x[fw::PE] = RUNWAY.east;
x[fw::PD] = -p.gear.front().position[2];
x[fw::PSI] = RUNWAY.heading;
FixedWingDynamics aircraft(p, x);
aircraft.set_wind({options.wind_north, options.wind_east, 0.0});
for (int k = 0; k < 100; ++k)
{
aircraft.step({}, STEP);
}
#ifdef FIXED_WING_RENDER
std::optional<Display> display;
if (options.render)
{
display.emplace(mission, p, options.record);
}
#else
if (options.render)
{
std::cerr << "--render and --record need bitsim built with Chrono and Irrlicht\n";
return 1;
}
#endif
std::cout << " t [s] phase leg north [m] east [m] alt [m] Va [m/s] "
"bank [deg] off the line [m]\n";
const auto print = [&](const std::string &note)
{
const auto &s = aircraft.state();
std::cout << std::fixed << std::setprecision(1) << std::setw(9) << aircraft.time() << " "
<< std::left << std::setw(12) << to_string(mission.phase()) << std::right
<< std::setw(5) << mission.leg() << std::setw(11) << s[fw::PN] << std::setw(10)
<< s[fw::PE] << std::setw(9) << aircraft.altitude() << std::setw(10)
<< aircraft.airspeed() << std::setw(12) << s[fw::PHI] / DEG << std::setw(18)
<< mission.cross_track() << note << "\n";
};
MissionPhase phase = mission.phase();
std::optional<real_t> liftoff_distance;
real_t liftoff_airspeed = 0.0;
real_t max_approach_off = 0.0;
real_t max_glide_off = 0.0;
real_t next_print = 0.0;
real_t next_frame = 0.0;
const real_t start_north = aircraft.state()[fw::PN];
const auto wall_start = std::chrono::steady_clock::now();
while (aircraft.time() < TIME_LIMIT && mission.phase() != MissionPhase::STOPPED)
{
const auto u = mission.update(aircraft, STEP);
aircraft.step(u, STEP);
if (!liftoff_distance && mission.phase() == MissionPhase::ROTATE && !aircraft.on_ground())
{
liftoff_distance = aircraft.state()[fw::PN] - start_north;
liftoff_airspeed = aircraft.airspeed();
}
if (mission.phase() == MissionPhase::APPROACH)
{
const auto rc =
mission.runway_coordinates(aircraft.state()[fw::PN], aircraft.state()[fw::PE]);
// The last 300 m before the threshold, once lined up
if (rc[0] > -300.0)
{
max_approach_off = std::max(max_approach_off, std::abs(mission.cross_track()));
max_glide_off = std::max(
max_glide_off, std::abs(aircraft.altitude() - mission.commands().altitude));
}
}
if (mission.phase() != phase)
{
phase = mission.phase();
print(" <- " + to_string(phase));
}
else if (aircraft.time() + 1e-9 >= next_print)
{
print("");
}
if (aircraft.time() + 1e-9 >= next_print)
{
next_print += PRINT_PERIOD;
}
#ifdef FIXED_WING_RENDER
if (display && aircraft.time() + 1e-9 >= next_frame)
{
next_frame += 1.0 / RENDER_RATE;
std::ostringstream status;
status << std::fixed << std::setprecision(1) << "t = " << aircraft.time() << " s "
<< to_string(mission.phase()) << " altitude " << aircraft.altitude()
<< " m airspeed " << aircraft.airspeed() << " m/s bank "
<< aircraft.state()[fw::PHI] / DEG << " deg throttle " << std::setprecision(2)
<< u.throttle << " leg " << mission.leg() << " yellow: flown, cyan: the "
<< "line it follows";
// Record the takeoff to 25 m, and the landing from 25 m
const bool climbing_out = mission.phase() == MissionPhase::ENROUTE &&
mission.leg() == 0 && aircraft.altitude() < 25.0;
const bool landing =
(mission.phase() == MissionPhase::APPROACH && aircraft.altitude() < 25.0) ||
mission.phase() == MissionPhase::FLARE || mission.phase() == MissionPhase::ROLLOUT;
const bool record = mission.phase() == MissionPhase::TAKEOFF_ROLL ||
mission.phase() == MissionPhase::ROTATE || climbing_out || landing;
if (!display->show(aircraft, mission, status.str(), record))
{
return 0;
}
const real_t ahead =
aircraft.time() -
std::chrono::duration<real_t>(std::chrono::steady_clock::now() - wall_start)
.count();
if (ahead > 0.0)
{
std::this_thread::sleep_for(std::chrono::duration<real_t>(ahead));
}
}
#else
(void)next_frame;
#endif
}
const auto &s = aircraft.state();
const auto end = mission.runway_coordinates(s[fw::PN], s[fw::PE]);
std::cout << std::fixed << std::setprecision(1) << "\n";
if (liftoff_distance)
{
std::cout << " Lift-off after a roll of " << *liftoff_distance << " m, at "
<< liftoff_airspeed << " m/s\n";
}
std::cout << std::setprecision(2) << " On the last 300 m of the approach: at most "
<< max_approach_off << " m off the centre line and " << max_glide_off
<< " m off the glide slope\n";
const auto &td = mission.touchdown();
if (td)
{
std::cout << std::setprecision(1) << " Touchdown at t = " << td->time << " s, "
<< td->along << " m past the threshold, " << std::setprecision(2) << td->across
<< " m off the centre line, sinking at " << td->sink_rate << " m/s, at "
<< std::setprecision(1) << td->airspeed << " m/s\n";
}
std::cout << " " << (mission.phase() == MissionPhase::STOPPED ? "Stopped" : "Still going")
<< " at t = " << aircraft.time() << " s, " << end[0] << " m past the threshold, "
<< std::setprecision(2) << end[1] << " m off the centre line\n";
// Landed and stopped on the runway, and not too hard
const bool on_runway =
end[0] >= 0.0 && end[0] <= RUNWAY.length && std::abs(end[1]) < 0.5 * RUNWAY.width;
return mission.phase() == MissionPhase::STOPPED && on_runway && td && td->sink_rate < 1.5 ? 0
: 1;
}
} // namespace fixed_wing_flight
int main(int argc, char **argv)
{
using namespace fixed_wing_flight;
Options options;
for (int i = 1; i < argc; ++i)
{
const std::string arg = argv[i];
if (arg == "--wind" && i + 2 < argc)
{
options.wind_north = std::atof(argv[++i]);
options.wind_east = std::atof(argv[++i]);
}
else if (arg == "--render")
{
options.render = true;
}
else if (arg == "--record" && i + 1 < argc)
{
options.render = true;
options.record = argv[++i];
}
else
{
std::cerr << "Usage: fixed_wing_flight [--wind N E] [--render] [--record DIR]\n";
return 1;
}
}
FixedWingAutopilot autopilot(p, design);
show_gains(autopilot);
return fly(p, autopilot, options);
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
Holds airspeed, altitude and course with loops closed one inside the other, as in Beard and McLain,...
Definition fixed_wing_autopilot.h:78
const FixedWingAutopilotGains & gains() const noexcept
Definition fixed_wing_autopilot.h:103
Takes off from a runway, flies a route of waypoints and lands back on the runway.
Definition fixed_wing_mission.h:101
std::size_t leg() const noexcept
The line being followed: from points()[leg()] to points()[leg() + 1].
Definition fixed_wing_mission.h:116
real_t cross_track() const noexcept
Definition fixed_wing_mission.h:118
const std::vector< MissionWaypoint > & points() const noexcept
Definition fixed_wing_mission.h:117
MissionPhase phase() const noexcept
Definition fixed_wing_mission.h:112
dynamics::FixedWingControls update(const dynamics::FixedWingDynamics &aircraft, real_t dt)
The controls for the aircraft now, after dt [s].
Definition fixed_wing_mission.cpp:134
const std::optional< Touchdown > & touchdown() const noexcept
Definition fixed_wing_mission.h:120
const FixedWingCommands & commands() const noexcept
Definition fixed_wing_mission.h:119
std::array< real_t, 2 > runway_coordinates(real_t north, real_t east) const
Definition fixed_wing_mission.cpp:76
The 6-DOF flight dynamics of a fixed wing aircraft, with its landing gear on a flat ground at down = ...
Definition fixed_wing_dynamics.h:176
bool on_ground() const
whether a wheel touches the ground
Definition fixed_wing_dynamics.cpp:270
real_t course() const
the direction of the velocity over the ground [rad]
Definition fixed_wing_dynamics.cpp:256
void step(const FixedWingControls &controls, real_t dt)
Advance by dt [s] with the given controls, which are first limited.
Definition fixed_wing_dynamics.cpp:223
const FixedWingState & state() const noexcept
Definition fixed_wing_dynamics.h:206
real_t altitude() const
Definition fixed_wing_dynamics.h:215
real_t time() const noexcept
Definition fixed_wing_dynamics.h:209
void set_wind(const RealColVec3d &wind_ned)
The wind, in the world [m/s]; the direction it blows towards.
Definition fixed_wing_dynamics.h:203
static RealMat3d rotation(real_t phi, real_t theta, real_t psi)
Definition fixed_wing_dynamics.cpp:55
real_t airspeed() const
Definition fixed_wing_dynamics.cpp:245
Definition fixed_wing_flight.cpp:167
bool show(const FixedWingDynamics &aircraft, const FixedWingMission &mission, const std::string &status, bool record)
Definition fixed_wing_flight.cpp:209
Display(const FixedWingMission &mission, const FixedWingParameters &p, const std::string &record)
Definition fixed_wing_flight.cpp:169
int main()
Definition discounted_returns.cpp:19
The trim of a fixed wing aircraft for level flight, its linear model about a trim,...
Pieces of an Irrlicht window for a robot simulation: a texture on the floor, a path drawn behind a ro...
Vec & add(Vec &v1, const Vec &v2)
Definition vector_math.h:248
const real_t PI
The PI constant.
Definition bitsim_consts.h:63
Controllers: PID controllers, line following, a quadrotor flight controller, a fixed wing autopilot,...
Definition control_module.h:18
std::string to_string(MissionPhase phase)
Definition fixed_wing_mission.cpp:31
MissionPhase
Definition fixed_wing_mission.h:59
@ THETA
Definition fixed_wing_dynamics.h:35
@ PE
Definition fixed_wing_dynamics.h:29
@ PD
Definition fixed_wing_dynamics.h:30
@ PN
Definition fixed_wing_dynamics.h:28
@ PSI
Definition fixed_wing_dynamics.h:36
@ PHI
Definition fixed_wing_dynamics.h:34
@ R
Definition fixed_wing_dynamics.h:39
Models of how things move: wheeled robots, a bicycle, a quadrotor and a fixed wing aircraft,...
Definition bicycle_vehicle_model_dynamics.cpp:10
FixedWingTrim trim_level_flight(const FixedWingParameters &parameters, real_t airspeed, real_t altitude, real_t heading)
Find the controls for straight and level flight at the given airspeed, with no wind....
Definition fixed_wing_trim.cpp:108
UnalignedMat< real_t, 12, 1 > FixedWingState
The 12 values of the state of a fixed wing aircraft, in this order: the position (north,...
Definition fixed_wing_dynamics.h:19
FixedWingLinearModel linearise(const FixedWingParameters &parameters, const FixedWingTrim &trim)
Linearise the dynamics about a trim, by central differences.
Definition fixed_wing_trim.cpp:194
std::vector< FlightMode > flight_modes(const FixedWingLinearModel &model)
The classical modes: short period and phugoid from the longitudinal states (u, w, q,...
Definition fixed_wing_trim.cpp:218
void print_title(const std::string &title, std::ostream &out)
An empty line and then the title.
Definition print_utils.cpp:17
void irr_draw_line(chrono::irrlicht::ChVisualSystemIrrlicht &vis, const chrono::ChVector3d &start, const chrono::ChVector3d &end, const chrono::ChColor &color, real_t thickness)
Definition irrlicht_utils.cpp:19
void draw_status_bar(chrono::irrlicht::ChVisualSystemIrrlicht &vis, const std::string &text)
Write text on a light bar along the bottom of the window. Call this between Render() and EndScene().
Definition irrlicht_scene.cpp:151
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Eigen::Matrix3< real_t > RealMat3d
3×3 matrix of type double.
Definition bitsim_types.h:86
double real_t
real_t
Definition bitsim_types.h:20
void print(const Run &run)
Definition cart_pole_lqr_mpc.cpp:101
void record(const std::vector< Run > &runs, const std::string &directory)
Definition cart_pole_lqr_mpc.cpp:194
Definition fixed_wing_flight.cpp:46
int fly(const FixedWingParameters &p, FixedWingAutopilot &autopilot, const Options &options)
Definition fixed_wing_flight.cpp:316
const real_t TIME_LIMIT
Definition fixed_wing_flight.cpp:60
const real_t STEP
Definition fixed_wing_flight.cpp:58
const chrono::ChMatrix33d NED_TO_Z_UP(ChVector3d(1, 0, 0), ChVector3d(0, -1, 0), ChVector3d(0, 0, -1))
void show_modes(const FixedWingParameters &p)
Definition fixed_wing_flight.cpp:105
void show_trims(const FixedWingParameters &p)
Definition fixed_wing_flight.cpp:83
const real_t PI
Definition fixed_wing_flight.cpp:55
const real_t DEG
Definition fixed_wing_flight.cpp:56
void show_gains(const FixedWingAutopilot &autopilot)
Definition fixed_wing_flight.cpp:137
const real_t RECORD_RATE
Definition fixed_wing_flight.cpp:62
const Runway RUNWAY
Definition fixed_wing_flight.cpp:67
ChVector3d to_z_up(real_t n, real_t e, real_t d)
Definition fixed_wing_flight.cpp:164
const real_t DESIGN_AIRSPEED
Definition fixed_wing_flight.cpp:63
const real_t RENDER_RATE
Definition fixed_wing_flight.cpp:61
void print_title(const std::string &title, std::ostream &out)
An empty line and then the title.
Definition print_utils.cpp:17
const std::vector< MissionWaypoint > ROUTE
Definition fixed_wing_flight.cpp:68
const real_t PRINT_PERIOD
Definition fixed_wing_flight.cpp:59
Print labelled values, one per line, as the examples do: two spaces, the label padded to a width,...
How the loops are designed, and the limits of what they command.
Definition fixed_wing_autopilot.h:39
real_t airspeed
the trim the gains are designed at [m/s]
Definition fixed_wing_autopilot.h:40
real_t roll_kp
Definition fixed_wing_autopilot.h:26
real_t altitude
[m]
Definition fixed_wing_autopilot.h:17
A runway on the ground: it starts at its threshold and runs along its heading.
Definition fixed_wing_mission.h:19
real_t length
[m]
Definition fixed_wing_mission.h:23
real_t width
[m]
Definition fixed_wing_mission.h:24
The aircraft: its mass, its geometry, its aerodynamic coefficients, its propeller and motor,...
Definition fixed_wing_dynamics.h:73
std::vector< LandingGearWheel > gear
Definition fixed_wing_dynamics.h:116
static FixedWingParameters aerosonde()
The Aerosonde of Beard and McLain, Small Unmanned Aircraft, 2nd edition, with a tricycle landing gear...
Definition fixed_wing_dynamics.h:130
real_t max_elevator
Definition fixed_wing_dynamics.h:110
real_t b
the wing span [m]
Definition fixed_wing_dynamics.h:83
Definition fixed_wing_flight.cpp:72
std::string record
Definition fixed_wing_flight.cpp:76
bool render
Definition fixed_wing_flight.cpp:75
real_t wind_east
Definition fixed_wing_flight.cpp:74
real_t wind_north
Definition fixed_wing_flight.cpp:73