Documentation
Documentation
Swarm of Drones
Credits to:
Author: Prof. Randal W. Beard
Enrica Soria Prof. Timothy W. McLain
Contact: Victor Delafontaine
enrica.soria@epfl.ch Anthony De Bortoli
Prof. Dario Floreano
2 Simulator structure 2
3 Documentation 4
3.1 Drone and Swarm class . . . . . . . . . . . . . . . . . . . . . . 4
3.2 Autopilot versions . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Guidance model . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.4 Parameter files . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.5 Different main functions . . . . . . . . . . . . . . . . . . . . . 10
3.5.1 main controller . . . . . . . . . . . . . . . . . . . . . . 11
3.5.2 main path follower . . . . . . . . . . . . . . . . . . . . 13
3.5.3 main path manager . . . . . . . . . . . . . . . . . . . . 14
3.5.4 main path planner . . . . . . . . . . . . . . . . . . . . 15
3.5.5 main swarming . . . . . . . . . . . . . . . . . . . . . . 17
3.5.6 main GUI . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.6 Simulation modes . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.7 Graphical User Interface . . . . . . . . . . . . . . . . . . . . . 21
3.8 Wind model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
i
Drone simulator - June 25, 2019 Enrica Soria
Abstract
This Matlab simulator was created with versatility in mind. Its
goal is to be able to simulate multiple scenarios depending on the user
requirements: either a single drone or a swarm of hundreds of drones.
This project extends the work provided by Beard and McLain in [1],
focused on reproduction of the flight of a single fiexed-wing drone.
Building on it, this simulator provides a useful tool to test drone and
swarm dynamics. An example is shown in figure 1 where three drones
fly together after 46 seconds of simulation.
ii
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
https://github.com/randybeard/mavsim˙template˙files
The first chapter is on the drone’s basic coordinate frames. In this section,
the authors describe the Euler angle representation, the NED (north-east-
down) reference frame as well as the airspeed model. These basics allow to
know how to represent the drone in the three dimensional space.
The second chapter describes the drone kinematics and dynamics. The
drone has a total of six degrees of freedom. Here, the drone state is repre-
sented with four main triplets. This same state will be used throughout this
simulator. It is represented as follows:
x = [pn pe pd u v w φ θ ψ p q r]
The four triplets are position in NED frame, velocity in body frame, attitude
(roll, pitch and yaw) and attitude rate, in the order.
The kinematic equations are essential to get the evolution of the drone state
as well as the forces and moments applied on the drone are also introduced.
The external forces are the gravity and drag, and the drone creates control
moments from the control surfaces (elevator, rudder and ailerons) and pro-
peller thrust.
1
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
The authors then describe a sensor and state estimation model. The sen-
sor used are GPS, accelerometers, gyros and pressure sensors. The Kalman
filtering is used for state estimation. While templates for this are provided
at the link before, it is not incorporated into our simulation.
The following chapters are on the guidance model. Once again, this is
mainly focused on fixed-wings and varies for a quadcopter. The guidance
model is staged onto three layers. The first is a straight-line and orbit fol-
lower. Once the drone is able to follow a given path, the path manager is
added to create the path based on given waypoints. The final layer is the
creation of the waypoints by the path manager. These will be explained in
more details later in this project in section 3.3.
2 Simulator structure
The simulator structure follows the waterfall structure described in Beard and
McLain[1]. It is shown in figure 2 below. This structure is valid for a single
drone. For multiple drones, the guidance model (path planner, manager and
follower) will be replaced by a swarm version. The input commands for the
drones will come from the swarming model based on the drones’ positions
and velocities and on the obstacles.
The book was used as a base to build our simulator. Some of the func-
tions’ structure presented in the Simulink simulator were taken directly from
the open-source templates suggested by the authors. In figure 2 these are
shown with the relative chapter next to their names.
The waterfall structure enabled the possibility to build the simulator from
the ground up, adding functions as the project progressed. Before staring this
project, the templates were implemented for a fixed-wing drone. The first
step was to convert the simulator up to the autopilot to Object Oriented Pro-
gramming (OOP). Then, equivalent blocks for the quadcopter were added.
Finally, the swarm object was created to allow the simultaneous simulation
of multiple drones.
2
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
goal
Path planner (chap 12)
map
status waypoints
Autopilot (chap 6)
actuator commands
wind x̂
UAV model (chap 2)
3
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
3 Documentation
This section explains the functioning of the simulator and how to use it. The
main objects of interest are the followings:
1. the Drone and Swarm classes: what they contain and how to use them;
7. the graphical user interface (GUI): how to use it to run the simulation
and change parameters;
4
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
– path: (vector 33x1) contains the current path returned by the path
manager: path = [f lag V ad r q c ρ λ state f lag need new wpts].
r and q are the inertial position and direction of the path, used
for straight lines. c is the center of a circle of radius ρ in direction
λ (+1 for CW, -1 for CCW) for an orbit follow.
– nb waypoints: the number of waypoints computed by the path
planner. The path manager can demand new waypoints by setting
the f lag need new wpts to one
– waypoints: (vector 5*Tx1, with T contained inside structure P as
P.size waypoint array) contains the waypoints, each defined by 5
variables: [pn pe pd χ V ad ], stored as a line vector (need to re-
shape before using). The ”empty” waypoints with an index above
nb waypoints defined above are set to −[9999 9999 9999 9999 9999]
• Miscellaneous variables:
5
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
• equivalent drone: the equivalent drone for the swarm, namely a virtual
drone with position at the barycenter of the swarm. It is used in the
guidance model
The creation of a Drone or Swarm object can be done with the lines
shown in respectively listing 1 and 2.
1 % Drone i n i t i a l i z a t i o n
2 drone = Drone (DRONE TYPE, AUTOPILOT VERSION, REALISTIC SIM , P , B
, map) ;
Listing 1: Drone object creation
6
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
1 % Swarm i n i t i a l i z a t i o n
2 swarm = Swarm (SWARMING ALGO) ;
3 f o r i = 1 : S . nb agents
4 swarm . add drone ( Drone (DRONE TYPE, AUTOPILOT VERSION,
REALISTIC SIM , P , B, map) ) ;
5 end
Listing 2: Swarm object creation
In order to put the drones of a swarm in random positions inside the
map, you can run the lines shown below in listing 3. This sets all drones in
a cube of size map.width (default as 200). The sixth line can be used to fix
the altitude as a negative value between 0 and 120 for visibility in the plots.
1 % S e t swarm p o s i t i o n
2 s e e d = 5 ; % f i x e d s e e d t o a v o i d randomness
3 rng ( s e e d ) ;
4 pos0 = repmat (map . width , 1 , S . n b a g e n t s ) . ∗ rand ( 3 , S . n b a g e n t s ) ;
5 swarm . s e t P o s ( pos0 ) ;
6 %pos0 ( 3 , : ) = −pos0 ( 3 , : ) ∗ 0 . 6 ;
Listing 3: Swarm position initialization
7
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
Each of these functions will call ”hold” sub-functions that uses Matlab’s
pid loop function to obtain the actuator commands. For a quadcopter the
output is the rotational speed of each of the four motors (normalized at 1).
For a fixed-wing, it is the actuation levels of elevator, ailerons, rudder and
throttle.
The three functions described above are staged onto three levels. These
levels are also shown in figure 3 below.
1. The path follower is the lowest level of the three. It has a fixed path
as input and its output is a command for the drone. Please, refer to
chapter 10 of the reference book for details on the theory.
2. The path manager uses the list of waypoints to extract a path that
the drone needs to follow to go to the next waypoint. The waypoints
comes from a simplified path planner and are fixed between calls. The
8
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
created path can then be given to the path follower. Please, refer to
chapter 11 of the reference book.
3. The path planner is at the highest level. Its input is the map and
a goal from which it computes the list of intermediary waypoints in
between. These waypoints will be the path manager’s input. Please,
refer to chapter 12 of the reference book.
We created different main scripts to test these functions. These can be found
later on in section 3.5 and can be run separately to test the different stages.
goal
Path planner (chap 12)
map
current status waypoints
commands
The main simulation parameters functions have names such as param 4 forces.m.
In this case, it corresponds to the fourth chapter in Beard and McLain [1] on
the subject of forces. These files go up to 12 for the path planner parameters
and creates the structure P. Each file calls the previous one so only the call
of the last required file is needed in the main script. When looking for a pa-
rameter to change, you need to understand at which level of the simulation
it is first used. For example the PID gains for the controller will be found in
9
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
param 6 controller.m.
5. main flocking
6. main GUI
The first four files corresponds to respectively chapters 6, 10, 11 and 12
in Beard and McLain’s book [1]. Each main adds functionality onto the
previous one. The GUI main is mainly used to test the graphical user in-
terface functionality and doesn’t add any functionality over the other scripts.
10
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
The output is a graph of the moving drone, centered on its center of mass.
An example with a simulation end time of 20 seconds is shown in figure 4.
11
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
In all main scripts, the function plot uav state variables can also be called
to plot the state values relative to time, as shown in figure 5. However this
second plot slows the simulation execution time so it is preferable to use it for
debugging only. Note that here in figure 5 only the tab ”Velocity” is shown,
but the figure also contains tabs for the position, acceleration, air-data, at-
titude, angle rates and actuators. For concerned variables, it plots the real
value in blue as well as the commanded one in red and the estimation from
the sensor values in green. The computation of this last value has still to be
coded.
The debug plot check-box in the user interface (see section 3.7) can be
used to use this function. It can be deactivated during the simulation and
restarted later one. However, the simulation needs to be first launched with
the check-box active to work.
These graphs are useful when testing a new controller for response time,
percent overshoot and other criteria. As it can be used with the different
autopilot versions described in 3.2, this main can be very useful in the design
12
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
of a controller. We can for example see that our drone takes almost two
seconds to change from -6 to 6 m/s.
The second difference is in the plotting functions. This time the external
environment can be active. This is set by a boolean variable: is env active.
If this boolean is set to true, the function drawBuildings will be called. As a
result, the view won’t be centered on the drone, but will be a fixed view of
the city’s building and how the drone moves inside. This is useful to see the
drone’s generated path as a whole.
You can see the path generated by function path manager quad chap10
in red. This path is only a straight line from the initial position in direction
[2;1;0]. Note that the waypoints are already defined and plotted (in blue)
but are not used for now.
This main can be used to test if the drone manage to follow its path
without too much error. Adding a measure of deviation could be useful to
determine if the path following algorithm is optimal.
13
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
For example, calling it with a path type of 2 (Dubins path) will return the
waypoint list shown in listing 5 and figure 7. The waypoints structure is in
order, north position, east position, altitude (negative for above ground), yaw
angle at waypoint and velocity at waypoint. Both the number of waypoints
and waypoints are stored inside the Drone class and are specific to each
drone.
1 nb wpts = 6 ;
2 wpt list = [0 , 0, −100 , 0, P . Va0 ; . . .
3 30 , 0, −100 , 45∗ p i / 1 8 0 , P . Va0 ; . . .
4 40 , 10 , −100 , 90∗ p i / 1 8 0 , P . Va0 ; . . .
5 40 , 50 , −100 , 45∗ p i / 1 8 0 , P . Va0 ; . . .
14
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
6 5 0 , 6 0 , −100 , 0∗ p i / 1 8 0 , P . Va0 ; . . .
7 8 0 , 6 0 , −100 , 45∗ p i / 1 8 0 , P . Va0 ] ;
Listing 5: Path returned by a call of path planner.m with path type 2
The main will then call the manager to decide which waypoint to follow
based on current progress. This will return two waypoints and a path will
be created in order to link these. The path follower will then be called to
create the actuators’ commands.
Please note that when the drone reaches the last waypoint it won’t stop and
will follow its last heading until infinity.
15
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
The algorithm used is the rapidly-exploring random tree (RRT). This al-
gorithm consists of growing a tree-like structured rooted on the staring point.
For each iteration, one branch grows in a random direction. The length is
fixed based on the number of iteration (smaller branches for the last itera-
tion). If the branch is feasible, in this case if it doesn’t interest any building,
it is drawn. Otherwise the algorithm switches to the next branch.
In the graph of figure 8, we can see then main branches in red and the
sub-branches in green.
When the algorithm reaches the end point, it checks if it could have done
the path in a shorter way. If that’s the case, it uses this alternative path.
On the graph, this is the final path shown in blue, as a direct line doesn’t
intersect any buildings in this case.
16
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
First if we run with three drones and no obstacles (see figure 9), we see
that they align with a common ”going forward” goal. They also keep a fixed
separation distance between themselves by aligning on a proximity net.
figs/main_swarm1.png figs/main_swarm2.png
1. centering: the drones form small flocks, four in the example of figure
10. In each of these swarms the drones try to get close to each other
17
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
the global movement on the y axis, mainly the two swarms on the right
moved from y between 120 and 200 to y between 250 and 350
18
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
The simulation mode is set by the boolean defined in the main: REAL-
ISTIC SIM. This parameter affects the update state function in the Drone
class. The piece of code managing it is shown in the listing 6 below.
1 i f o b j . i s r e a l i s t i c == t r u e
2 % Choose t h e a u t o p i l o t
3 i f o b j . d r o n e t y p e == 0% f i x e d −wing
4 temp3 = a u t o p i l o t w i n g ( obj , 0 , time ) ;
5 e l s e % quadcopter
6 temp3 = a u t o p i l o t q u a d ( obj , time ) ;
7 end
8
9 o b j . d e l t a = temp3 ( 1 : 4 ) ;
10 o b j . full command = temp3 ( 5 : end ) ;
11 % Update t r u e s t a t e
12 o b j . compute dynamics ( wind , time ) ;
13 o b j . c o m p u t e k i n e m a t i c s ( time ) ;
14 obj . update battery () ;
15 else
16 % Compute t h e new drone p o s i t i o n with E u l e r f o r w a r d method .
17 % This method do not t a k e t h e a t t i t u d e i n t o a c c o u n t .
18 % We s u p p o s e t h a t t h e a t t i t u d e i s always ( 0 , 0 , 0 ) , s o t h e
19 % v e l o c i t y i n t h e body frame c o r r e p o n d s t o t h e v e l o c i t y i n
the
20 % i n e r t i a l frame .
21 o b j . v e l x y z = o b j . command ( 2 : 4 ) ;
22 o b j . p o s n e d = o b j . p o s n e d + o b j . v e l x y z ∗ o b j . P . dt ;
23 o b j . a t t i t u d e ( 3 ) = o b j . command ( 1 ) ; % t o p l o t drone p s i a n g l e
24 end
Listing 6: Changes set by boolean realistic sim
In the case of a time efficient simulation, the state is updated using for-
ward Euler method. The new position is obtained with pos = old pos +
command ∗ T . As the command is in velocity, we consider that the drone
will immediately react to a new command by moving at the commanded
speed. In addition to that the wind is not used in the case of a limited rep-
19
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
The plotting functions can also change with a parameter SWARM VIEWER TYPE.
The different outputs are shown in figure 11 below. We can see that the rep-
resentation of figure 11b is limited as it can’t display current attitude or
heading. However, in most case only the position of the drone is needed and
the heading can be understood based on the current swarm movement. The
drone’s initial position is set randomly.
For one drone, running the function main controller with the boolean set
to true (accurate simulation) takes 2.71 seconds to simulate 10 seconds of
flight. When the boolean is switched to false, it takes 2.1 seconds. This rep-
resents a reduction of approximately 20% in simulation time. When running
a more complex simulation (up to the path manager, chapter 11), the time
goes from 8 seconds to 5.8 seconds to simulate 20 seconds (time required to
go to the end of the waypoints described in figure 7). This is a decrease of
1
All measures were done on a computer equipped with a Intel Pentium G4560 and a
graphical card GTX 1050Ti.
20
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
27.5%. For one drone and a short simulation, these are small reductions.
However if the simulation needs to run many parallel instances, it can make
an important difference.
We can test this difference by running main GUI with 50 drones with
the environment active. The program takes an average of 11 seconds to
run the simulation for one second in realistic mode. In limited mode, it takes
less than one second. In this scenario, the time reduction was more than 90%.
As the simulation is faster than real time in limited mode, the use of
a remote-controller becomes possible. This adds many possibilities to the
simulator. It could for example be possible to control the swarm direction in
real time.
21
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
We can see that the parameters are separated in four categories: drone,
swarm, environment and simulation. Some parameter won’t be possible to
change in some simulation modes. For example the orientation dial controls
the swarm migration, and will be active only when the chosen simulation is
the swarming one.
22
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
variable app. At each point of time inside the simulation, the code checks if
any parameter changed. If any did, the corresponding variable contained in
the script is changed to its new value.
It returns a wind field of dimension 4. The first dimension is for each time
step in the simulation, the second and third are the X and Y coordinates,
and the last is a 3x1 vector corresponding to the wind at each coordinate
and time step. The wind returned is in order z-x-y. We use this model with
an average wind speed of 5 m/s.
For the gust generation, we used random values generated using a normal
distribution. The mean is 0 is all x-y-z directions and we used a standard
deviation of 0.3. The values were then multiplied by a maximum value of 2
m/s. As a result, the gusts will be most of the time between -0.6 and 0.6
m/s. These values would need to be tuned to match more closely a real life
situation.
Both the steady wind and gusts are then modified using the variables
wind level and wind gust level found in the GUI. They act as a modification
in percentage of the obtained value. A wind level of 50 will reduce the ob-
tained values by a factor 2. It is also possible to completely deactivate the
2
https://www.mathworks.com/matlabcentral/fileexchange/
54491-3d-turbulent-wind-generation
23
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
This wind model can still be improved. For now, it doesn’t consider the
drone altitude. The gusts also do not dependent on position, but they take
random values. As a result, two drones side by side could have gusts in
opposite direction. Another drawback of this model is that it takes time to
compute the field when launching the simulation. For one drone, it took
between 3 and 6 seconds. Even with these flaws, this wind model enables
us to test the drone comportment in a wind-field. This brings it closer to a
realistic simulation.
24
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
References
[1] Randal W. Beard and Tomothy W. McLain. Small unmanned aircraft:
theory and practice. 2012.
[3] Catherine Massé, Olivier Gougeon, Duc-Tien Nguyen and David Saussié.
Modeling and control of a quadcopter flying in a wind field: a comparison
between LQR and structured H∞ control techniques.
25
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA
1 f u n c t i o n dxdt = k i n e m a t i c s O d e f ( t , x , xx , uu , P)
2 % D e f i n e s t h e e q u a t i o n system f o r t h e drone k i n e m a t i c s
3
4 % Speed and p o s i t i o n
5 vx = x(4) ;
6 vy = x(5) ;
7 vz = x(6) ;
8 p = x(10) ;
9 q = x(11) ;
10 r = x(12) ;
11
12 % F o r c e s and moments
13 fx = uu ( 1 ) ;
14 fy = uu ( 2 ) ;
15 fz = uu ( 3 ) ;
16 l = uu ( 4 ) ;
17 m = uu ( 5 ) ;
18 n = uu ( 6 ) ;
19
20 % Orientation
21 phi0 = xx ( 7 ) ;
22 t h e t a 0 = xx ( 8 ) ;
23 psi0 = xx ( 9 ) ;
24
25 % Trigonometry
26 cr = cos ( phi0 ) ;
27 cp = c o s ( t h e t a 0 ) ;
28 s r = s i n ( phi0 ) ;
29 tp = tan ( t h e t a 0 ) ;
30
a
Versatile Simulator for a Swarm of Quadcopters Enrica SORIA