A toy drone is flown with a hand-held transmitter: two sticks for the throttle, yaw, pitch and roll, sent over a radio to a receiver on the drone, whose flight controller keeps it level and turns the sticks into rotor speeds. This example puts the laptop in the place of the transmitter and flies the quadrotor of The dynamics of a quadrotor in a simulation: from the keyboard in a window, or headless along a scripted flight. It is the first step to flying a real toy drone from the laptop: the commands are those its radio carries.
The scripted flight, at twice the speed it ran: take off, fly 5 m forward, turn round, fly back and land. The red rotor is the front one, the yellow line the track over the floor.
The transmitter sends a command every 20 ms. The receiver passes the latest one to the flight controller at every step of 1 ms; if it has heard nothing for 0.5 s it goes into failsafe and disarms the drone, so that its motors stop, as a toy drone does when it flies out of range.
Arm it, hold W until the throttle passes 0.5 and it lifts off, and let go when it climbs: the status bar at the bottom shows the throttle and the hover throttle. The camera follows behind the drone. --record DIR writes the window to DIR, as in Following a line with infrared sensors and avoiding obstacles; the movie above was made with --render --script --record frames.
The scripted flight, headless. It returns 0 if the drone took off, came back within a metre of the start, landed gently and never tilted beyond the 25 degrees the sticks allow.
A pitch stick of 0.6 tilts the drone by 0.6 x 25 = 15 degrees, nose down to fly forward, nose up to brake. Full yaw for 1.5 s turns it by 182 degrees, a little more than the 180 the sticks ask for, as the rate loop lags. The drone ends 0.29 m from where it took off and touches down at 0.69 m/s. It climbs from 1.6 to 2.4 m along the way: the extra throttle for the tilt is a guess, as it is for a pilot.
Half a second later the receiver disarms the drone. With its motors stopped nothing levels it, and it falls, still tilted, onto the floor at 5 m/s. Cutting the motors is what a toy drone does; a safer failsafe would level the drone and lower it slowly while it still can.
#include "bitsim/bitsim_config.h"
#if defined(BITSIM_CHRONO) && defined(BITSIM_IRRLICHT)
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
#include <thread>
#include <utility>
#include <vector>
{
{
QuadrotorDynamicsConfig config;
config.mass = 0.468;
config.l = 0.225;
config.k_1 = 2.980e-6;
config.k_2 = 1.140e-7;
config.Jx = 4.856e-3;
config.Jy = 4.856e-3;
config.Jz = 8.801e-3;
config.drag = 0.25;
return config;
}
{
std::array<std::pair<std::string, real_t>, 12> values{{{"x", 0.0},
{"y", 0.0},
{"z", 0.0},
{"u", 0.0},
{"v", 0.0},
{"w", 0.0},
{"p", 0.0},
{"q", 0.0},
{"r", 0.0},
{"phi", 0.0},
{"theta", 0.0},
{"psi", 0.0}}};
}
{
public:
controller_(config_)
{
}
SIM_RCLink &
radio() {
return radio_; }
const SIM_RCLink &
radio()
const {
return radio_; }
{
rotors_ = controller_.rotor_speeds(command_, dynamics_);
dynamics_.integrate(rotors_);
stand_on_the_floor_();
}
const QuadrotorDynamics &
dynamics()
const {
return dynamics_; }
const QuadrotorFlightController &
controller()
const {
return controller_; }
const RCCommand &
command()
const {
return command_; }
real_t height()
const {
return -dynamics_.get_position()[2]; }
private:
void stand_on_the_floor_()
{
auto &s = dynamics_.get_state();
if (s.get("z") < 0.0)
{
on_floor_ = false;
return;
}
if (!on_floor_)
{
touchdown_speed_ = std::abs(dynamics_.get_velocity()[2]);
{
++n_hard_landings_;
}
}
on_floor_ = true;
for (const char *name : {"z", "u", "v", "w", "p", "q", "r", "phi", "theta"})
{
s.set(name, 0.0);
}
}
QuadrotorDynamicsConfig config_;
SysState<12> state_;
QuadrotorDynamics dynamics_;
QuadrotorFlightController controller_;
SIM_RCLink radio_{0.5};
RCCommand command_{};
bool on_floor_{true};
int n_hard_landings_{0};
};
{
};
{
const auto at = [](
real_t until,
const std::string &name,
real_t throttle_offset,
real_t yaw,
return Phase{until, name,
RCCommand{0.0, yaw, pitch, roll, armed}, throttle_offset};
};
return {
at(0.5, "on the floor", -0.5, 0.0, 0.0, 0.0, false),
at(1.0, "arm", -0.5, 0.0, 0.0, 0.0),
at(2.5, "take off", 0.06, 0.0, 0.0, 0.0),
at(3.5, "level off", -0.05, 0.0, 0.0, 0.0),
at(4.5, "hover", 0.0, 0.0, 0.0, 0.0),
at(6.5, "forward", 0.02, 0.0, 0.6, 0.0),
at(8.5, "brake", 0.02, 0.0, -0.6, 0.0),
at(10.0, "turn round", 0.0, 1.0, 0.0, 0.0),
at(12.0, "forward", 0.02, 0.0, 0.6, 0.0),
at(14.0, "brake", 0.02, 0.0, -0.6, 0.0),
at(15.5, "hover", 0.0, 0.0, 0.0, 0.0),
at(22.0, "land", -0.02, 0.0, 0.0, 0.0),
at(23.0, "disarm", -0.5, 0.0, 0.0, 0.0, false),
};
}
{
public:
{
const Phase &p =
phase(time);
command.throttle = std::clamp(hover_ + p.throttle_offset, 0.0, 1.0);
}
{
for (const auto &p : phases_)
{
if (time < p.until)
{
return p;
}
}
return phases_.back();
}
private:
std::vector<Phase> phases_;
};
{
};
{
for (int i = 1; i < argc; ++i)
{
const std::string arg = argv[i];
if (arg == "--render")
{
options.render = true;
}
else if (arg == "--script")
{
options.script = true;
}
else if (arg == "--failsafe")
{
options.failsafe = true;
}
else if (arg == "--record" && i + 1 < argc)
{
options.record = argv[++i];
}
else
{
std::cout << "Usage: quadrotor_rc_simulation [--render] [--script] [--failsafe] "
"[--record DIR]\n"
<< " --render fly it from the keyboard in a window\n"
<< " --script with --render, watch the scripted flight instead\n"
<<
" --failsafe stop the transmitter at " <<
FAILSAFE_AT
<< " s of the scripted flight\n"
<< " --record DIR write the window to DIR\n"
<< "Keys: Space arm/disarm, W/S throttle, A/D yaw, arrows pitch and roll, "
"R start again"
<< std::endl;
std::exit(arg == "--help" || arg == "-h" ? 0 : 1);
}
}
if (!options.render)
{
options.script = true;
}
return options;
}
{
const auto &d = drone.dynamics();
const auto &c = drone.command();
std::ostringstream out;
out << std::fixed << std::setprecision(1) << "t " << time << " s "
<< (drone.radio().in_failsafe(time) ? "FAILSAFE" : (c.armed ? "armed" : "disarmed"))
<< " throttle " << std::setprecision(2) << c.throttle << " (hover "
<< drone.controller().hover_throttle() << ") yaw " << c.yaw << " pitch " << c.pitch
<< " roll " << c.roll << " height " << drone.height() << " m x " << p[0] << " y "
<< p[1] <<
" m heading " << std::setprecision(0) << d.psi() /
DEG <<
" deg " << phase;
return out.str();
}
{
auto drone_ptr = std::make_unique<SimulatedQuadrotor>();
const ScriptedPilot pilot(drone_ptr->controller().hover_throttle());
std::optional<bitsim::utils::render::QuadrotorView> display;
bitsim::utils::render::KeyboardRC keyboard;
if (options.render)
{
display.emplace("Flying a quadrotor from the laptop",
std::vector<bitsim::utils::render::QuadrotorView::Style>{{}},
if (!options.script)
{
display->vis().AddUserEventReceiver(&keyboard);
}
}
<<
" kg, arms " <<
quadrotor_config().
l <<
" m, rotors up to " << std::setprecision(0)
<< drone_ptr->controller().max_rotor_speed() << " rad/s, hover throttle "
<< std::setprecision(2) << drone_ptr->controller().hover_throttle() << "\n"
<<
"Radio: a command every " <<
SEND_PERIOD * 1000 <<
" ms, failsafe after "
<< drone_ptr->radio().timeout() << " s without one\n"
<< (options.script ? "Flying the script\n" : "Flying from the keyboard\n")
<< "\n t [s] x [m] y [m] height [m] roll pitch heading [deg] throttle"
" phase\n";
bool flew = false;
const real_t limit = options.script ? pilot.duration() + 1.0 : 1.0e9;
const auto wall_start = std::chrono::steady_clock::now();
while (time < limit)
{
const std::string phase = options.script ? pilot.phase(time).name : "keyboard";
if (time + 1e-9 >= next_send)
{
const bool silent = options.failsafe && time >=
FAILSAFE_AT;
if (!silent)
{
drone_ptr->radio().send(
}
}
drone_ptr->step(time);
const auto &d = drone_ptr->dynamics();
max_height = std::max(max_height, drone_ptr->height());
max_tilt = std::max({max_tilt, std::abs(d.phi()), std::abs(d.theta())});
max_distance = std::max(max_distance, std::hypot(d.get_position()[0], d.get_position()[1]));
flew = flew || drone_ptr->height() > 0.2;
if (time + 1e-9 >= next_print)
{
std::cout << std::setw(9) << std::setprecision(1) << time << std::setprecision(2)
<< std::setw(8) <<
clean(p[0]) << std::setw(8) <<
clean(p[1]) << std::setw(12)
<<
clean(drone_ptr->height()) << std::setprecision(1) << std::setw(6)
<< std::setw(15) <<
clean(d.psi() /
DEG) << std::setprecision(2)
<< std::setw(10) << drone_ptr->command().throttle << " "
<< (drone_ptr->radio().in_failsafe(time) ? "FAILSAFE" : phase) << std::endl;
}
if (display && time + 1e-9 >= next_frame)
{
{
drone_ptr = std::make_unique<SimulatedQuadrotor>();
}
if (!display->show({&drone_ptr->dynamics()}, time,
{
break;
}
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));
}
}
}
const RealColVec3d p = drone_ptr->dynamics().get_position();
const real_t from_start = std::hypot(p[0], p[1]);
std::cout << std::setprecision(2) << "\nHighest " << max_height << " m, furthest "
<< max_distance << " m from the start, most tilt " << std::setprecision(1)
<< max_tilt /
DEG <<
" deg\n"
<< std::setprecision(2) << "Ended "
<< (drone_ptr->on_the_floor() ? "on the floor" : "in the air") << ", " << from_start
<< " m from the start; touched down at " << drone_ptr->last_touchdown_speed()
<< " m/s; hard landings: " << drone_ptr->n_hard_landings() << std::endl;
if (!options.script)
{
return 0;
}
const bool ok = flew && drone_ptr->on_the_floor() && drone_ptr->n_hard_landings() == 0 &&
from_start < 1.0 &&
max_tilt < drone_ptr->controller().parameters().max_tilt + 0.05;
return options.failsafe ? 0 : (ok ? 0 : 1);
}
}
int main(
int argc,
char **argv)
{
try
{
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}
#else
#include <iostream>
{
std::cerr << "quadrotor_rc_simulation needs bitsim configured with Chrono and Irrlicht"
<< std::endl;
return 1;
}
#endif
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
The radio link to a simulated quadrotor: the transmitter end sends commands, and the simulated receiv...
Definition sim_rc_link.h:25
control::RCCommand receive(real_t time) const
What the receiver passes to the flight controller at time [s]: the last command, clamped,...
Definition sim_rc_link.cpp:30
The flight controller of a toy quadrotor in angle mode: it turns the sticks of an RCCommand into roto...
Definition quadrotor_flight_controller.h:52
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
control::RCCommand command(real_t dt)
The command for the keys held now, dt [s] after the last call.
Definition keyboard_rc.cpp:70
bool reset_requested()
True once after R was pressed.
Definition keyboard_rc.cpp:88
void reset()
Put the throttle down and disarm.
Definition keyboard_rc.cpp:95
Definition quadrotor_rc_simulation.cpp:215
const Phase & phase(real_t time) const
Definition quadrotor_rc_simulation.cpp:227
ScriptedPilot(real_t hover_throttle)
Definition quadrotor_rc_simulation.cpp:217
real_t duration() const
Definition quadrotor_rc_simulation.cpp:239
RCCommand command(real_t time) const
Definition quadrotor_rc_simulation.cpp:219
Definition quadrotor_rc_simulation.cpp:106
const QuadrotorDynamics & dynamics() const
Definition quadrotor_rc_simulation.cpp:128
SIM_RCLink & radio()
Definition quadrotor_rc_simulation.cpp:115
real_t last_touchdown_speed() const
Definition quadrotor_rc_simulation.cpp:136
void step(real_t time)
Definition quadrotor_rc_simulation.cpp:120
const RealVec & rotors() const
Definition quadrotor_rc_simulation.cpp:131
SimulatedQuadrotor()
Definition quadrotor_rc_simulation.cpp:108
real_t height() const
Definition quadrotor_rc_simulation.cpp:133
const QuadrotorFlightController & controller() const
Definition quadrotor_rc_simulation.cpp:129
const RCCommand & command() const
Definition quadrotor_rc_simulation.cpp:130
int n_hard_landings() const
Definition quadrotor_rc_simulation.cpp:135
bool on_the_floor() const
Definition quadrotor_rc_simulation.cpp:134
int main()
Definition discounted_returns.cpp:19
Fly a quadrotor from the keyboard of an Irrlicht window, as with the sticks of a transmitter....
const real_t PI
The PI constant.
Definition bitsim_consts.h:63
real_t clean(real_t x, real_t zero)
x, or 0 when zero
Definition print_utils.cpp:15
DynVec< real_t > RealVec
double precision floating point vector
Definition bitsim_types.h:111
double real_t
real_t
Definition bitsim_types.h:20
Eigen::Vector3d RealColVec3d
3D column vector
Definition bitsim_types.h:137
Definition quadrotor_rc_simulation.cpp:44
const real_t HARD_LANDING
Definition quadrotor_rc_simulation.cpp:64
const real_t FAILSAFE_AT
Definition quadrotor_rc_simulation.cpp:65
SysState< 12 > on_the_ground()
Definition quadrotor_rc_simulation.cpp:84
const real_t RECORD_RATE
Definition quadrotor_rc_simulation.cpp:63
int run(const Options &options)
Definition quadrotor_rc_simulation.cpp:323
const real_t STEP_SIZE
Definition quadrotor_rc_simulation.cpp:59
const real_t RENDER_RATE
Definition quadrotor_rc_simulation.cpp:62
const real_t PRINT_PERIOD
Definition quadrotor_rc_simulation.cpp:61
real_t clean(real_t x, real_t zero=5.0e-4)
x, or 0 when zero
Definition print_utils.cpp:15
const real_t PI
Definition quadrotor_rc_simulation.cpp:57
const real_t DEG
Definition quadrotor_rc_simulation.cpp:58
Options parse(int argc, char **argv)
Definition quadrotor_rc_simulation.cpp:264
const real_t SEND_PERIOD
Definition quadrotor_rc_simulation.cpp:60
std::string status_line(const SimulatedQuadrotor &drone, real_t time, const std::string &phase)
Definition quadrotor_rc_simulation.cpp:308
std::vector< Phase > script()
Definition quadrotor_rc_simulation.cpp:191
QuadrotorDynamicsConfig quadrotor_config()
Definition quadrotor_rc_simulation.cpp:69
Print labelled values, one per line, as the examples do: two spaces, the label padded to a width,...
An Irrlicht window that shows quadrotors flying over a tiled floor. Needs bitsim built with Chrono an...
What the sticks of a radio transmitter for a quadrotor say, as a toy drone's transmitter sends them: ...
Definition rc_command.h:20
Definition quadrotor_dynamics.h:13
real_t mass
mass
Definition quadrotor_dynamics.h:27
real_t dt
The time step to use.
Definition quadrotor_dynamics.h:56
real_t l
the arm length
Definition quadrotor_dynamics.h:32
Definition quadrotor_rc_simulation.cpp:257
bool render
Definition quadrotor_rc_simulation.cpp:258
std::string record
Definition quadrotor_rc_simulation.cpp:261
bool failsafe
Definition quadrotor_rc_simulation.cpp:260
bool script
Definition quadrotor_rc_simulation.cpp:259
Definition quadrotor_rc_simulation.cpp:182
real_t throttle_offset
Definition quadrotor_rc_simulation.cpp:186
real_t until
Definition quadrotor_rc_simulation.cpp:183
RCCommand sticks
Definition quadrotor_rc_simulation.cpp:185
std::string name
Definition quadrotor_rc_simulation.cpp:184