bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
The dynamics of a quadrotor

A quadrotor is controlled by the speeds of its four rotors: each pushes up with a thrust \(k\,\omega_i^2\) and turns the body the other way with a drag torque \(\beta\,\omega_i^2\). Equal speeds lift or lower it; differences tilt and turn it, and a tilted thrust moves it sideways. This example simulates QuadrotorDynamics for 2 s with the four rotors at the same speed, so that it only climbs and sinks. It is the model that a controller for a quadrotor, simulated or real, starts from.

What you need

Nothing beyond bitsim itself; Python with matplotlib for the plots. See Installing bitsim.

The model

The state has twelve values: the position \((x, y, z)\) in the world, with \(z\) down as in aircraft, the velocity \((u, v, w)\) and the angular velocity \((p, q, r)\) in the body's axes, and the Euler angles \((\phi, \theta, \psi)\). The rotors are in a plus: 1 in front, 2 on the right, 3 behind and 4 on the left. A faster left rotor rolls the quadrotor right, a faster front rotor pitches its nose up, and rotors 1 and 3 spin the other way to 2 and 4, so speeding them up yaws it. The class documentation has the equations. The parameters are those of the quadrotor in Luukkonen, Modelling and control of quadcopter: a mass of 0.468 kg, arms of 0.225 m, \(k\) = 2.98e-6 and \(\beta\) = 1.14e-7.

Four rotors at 625 rad/s push with \(4k\omega^2\) = 4.66 N, a little more than the weight of 4.59 N. The example raises the speeds to 675 rad/s in the first 0.1 s, lowers them to 550 rad/s by 0.4 s, and brings them back to 625 rad/s at 0.5 s, all four together.

Running the example

./quadrotor_dynamics
python plot.py # plots state.csv

It takes steps of 0.1 ms, prints the height every 0.1 s, and writes the whole state, with the rotor speeds, to state.csv; its height column is \(-z\).

Output

t = 0 s, height 1.39252e-09 m, climbing at 1.39252e-05 m/s, rotors at 625 rad/s
t = 0.1 s, height 0.00341272 m, climbing at 0.0957384 m/s, rotors at 674.994 rad/s
t = 0.2 s, height 0.0196106 m, climbing at 0.20497 m/s, rotors at 633.328 rad/s
t = 0.3 s, height 0.0399304 m, climbing at 0.179784 m/s, rotors at 591.662 rad/s
t = 0.4 s, height 0.0513729 m, climbing at 0.0290241 m/s, rotors at 549.996 rad/s
t = 0.5 s, height 0.0473708 m, climbing at -0.0715539 m/s, rotors at 625 rad/s
...
t = 1 s, height 0.0290038 m, climbing at -0.00192779 m/s, rotors at 625 rad/s
...
t = 2 s, height 0.0967091 m, climbing at 0.137324 m/s, rotors at 625 rad/s

The faster rotors lift it, and it keeps climbing as they slow down until they are below the hover speed, 0.23 s into the run. By 0.4 s it has climbed 5 cm and sinks again. From 0.5 s the rotors are back at 625 rad/s, a little faster than the 620.6 rad/s that carries the weight, and the quadrotor stops sinking at 1 s and climbs ever faster: by 0.139 m/s each second, which is \(4 k \omega^2 / m - g\). It never tilts: with four equal rotors there is no torque.

Things to try

  1. Find the speed at which it hovers, \(\omega = \sqrt{m g / 4k}\), and check it.
  2. Make one pair of opposite rotors faster than the other pair. Which way does it turn, and why?
  3. Write a PID controller for the height, with the rotor speed as its output, using PIDController.

The code

/*
* In this example we simulate a quadrotor.
* The quadrotor data is taken from
* https://sal.aalto.fi/publications/pdf-files/eluu11_public.pdf
*/
#include <array>
#include <iostream>
#include <string>
#include <vector>
{
using namespace bitsim::dynamics;
{
RealVec w(4);
if (t <= 0.1)
{
w[0] = 500 * t + 625.0;
w[1] = 500 * t + 625.0;
w[2] = 500 * t + 625.0;
w[3] = 500 * t + 625.0;
return w;
}
else if (t > 0.1 && t <= 0.4)
{
w[0] = -416.66 * t + 716.66;
w[1] = -416.66 * t + 716.66;
w[2] = -416.66 * t + 716.66;
w[3] = -416.66 * t + 716.66;
return w;
}
else if (t > 0.4 && t <= 0.5)
{
w[0] = 750.0 * t + 250.0;
w[1] = 750.0 * t + 250.0;
w[2] = 750.0 * t + 250.0;
w[3] = 750.0 * t + 250.0;
return w;
}
else
{
w[0] = 625.0;
w[1] = 625.0;
w[2] = 625.0;
w[3] = 625.0;
return w;
}
}
} // namespace quadrotor_dynamics
int main()
{
using namespace quadrotor_dynamics;
config.dt = 0.0001;
config.mass = 0.468;
config.l = 0.225;
config.k_1 = 2.980e-6; // this is the k coeff
config.k_2 = 1.140e-7; // this is the beta coeff
config.Jx = 4.856e-3;
config.Jy = 4.856e-3;
config.Jz = 8.801e-3;
// initialize the state variables
std::array<std::pair<std::string, real_t>, 12> values;
// initial position in NED
values[0] = std::make_pair("x", 0.0);
values[1] = std::make_pair("y", 0.0);
values[2] = std::make_pair("z", 0.0);
// initial translation velocities body coords
values[3] = std::make_pair("u", 0.0);
values[4] = std::make_pair("v", 0.0);
values[5] = std::make_pair("w", 0.0);
// initial rotational velocities
values[6] = std::make_pair("p", 0.0);
values[7] = std::make_pair("q", 0.0);
values[8] = std::make_pair("r", 0.0);
// Euler angles
values[9] = std::make_pair("phi", 0.0);
values[10] = std::make_pair("theta", 0.0);
values[11] = std::make_pair("psi", 0.0);
SysState<12> state(std::move(values));
QuadrotorDynamics dynamics(config, state);
// the motor velocities rad/sec
RealVec omegas = RealVec::Zero(4);
omegas[0] = omegas[1] = omegas[2] = omegas[3] = 625.0;
CSVWriter csv_writer("state");
csv_writer.open();
std::vector<std::string> names = {"t", "x", "y", "height", "u", "v", "w", "p", "q",
"r", "phi", "theta", "psi", "w0", "w1", "w2", "w3"};
csv_writer.write_column_names(names);
const real_t T = 2.0;
real_t time = 0.0;
long step = 0;
std::vector<real_t> row(17, 0.0);
while (time < T)
{
auto omega_motor = compute_motor_speed(time);
dynamics.integrate(omega_motor);
auto p = dynamics.get_position();
auto v = dynamics.get_velocity();
auto omega = dynamics.get_angular_velocity();
auto euler = dynamics.get_euler_angles();
row[0] = time;
row[1] = p[0];
row[2] = p[1];
row[3] = -p[2]; // z is down in NED: the height is -z
row[4] = v[0];
row[5] = v[1];
row[6] = v[2];
row[7] = omega[0];
row[8] = omega[1];
row[9] = omega[2];
row[13] = omega_motor[0];
row[14] = omega_motor[1];
row[15] = omega_motor[2];
row[16] = omega_motor[3];
csv_writer.write_row(row);
if (step % 1000 == 0)
{
std::cout << "t = " << time << " s, height " << -p[2] << " m, climbing at " << -v[2]
<< " m/s, rotors at " << omega_motor[0] << " rad/s" << std::endl;
}
++step;
time += config.dt;
}
csv_writer.close();
return 0;
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
The QuadrotorDynamics class. Implements quadrotor dynamics The implementation of this class follows t...
Definition quadrotor_dynamics.h:134
SysState utility class describing the state of a system.
Definition system_state.h:25
The CSVWriter class. Handles writing into CSV file format.
Definition csv_file_writer.h:22
int main()
Definition discounted_returns.cpp:19
Models of how things move: wheeled robots, a bicycle, a quadrotor and a fixed wing aircraft,...
Definition bicycle_vehicle_model_dynamics.cpp:10
real_t rad_to_degrees(real_t rad)
Definition unit_converter.h:36
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
DynVec< real_t > RealVec
double precision floating point vector
Definition bitsim_types.h:111
double real_t
real_t
Definition bitsim_types.h:20
state
Definition play.py:39
list values
Definition plot_losses.py:13
Definition quadrotor_dynamics.cpp:18
RealVec compute_motor_speed(real_t t)
Definition quadrotor_dynamics.cpp:26
Vector step(const QuadrotorDynamicsConfig &config, const Vector &x, const Vector &u, real_t max_rotor_speed)
Definition quadrotor_lqr.cpp:114
Definition quadrotor_dynamics.h:13
real_t mass
mass
Definition quadrotor_dynamics.h:27
real_t k_2
Proportionality constant to scale the morot velocity used when calculating torques.
Definition quadrotor_dynamics.h:45
real_t Jy
Definition quadrotor_dynamics.h:66
real_t Jz
Definition quadrotor_dynamics.h:71
real_t k_1
Proportionality constant to scale the morot velocity.
Definition quadrotor_dynamics.h:38
real_t dt
The time step to use.
Definition quadrotor_dynamics.h:56
real_t l
the arm length
Definition quadrotor_dynamics.h:32
real_t Jx
Definition quadrotor_dynamics.h:61
Various unit conversion utilities.