in the last video we learned how
accurate nonlinear models are great for
simulation but they don't lend
themselves well to linear analysis and
design for that we need a linear model
now it won't be as accurate as our
simulation model but we will be able to
use it for tuning the six PID
controllers in our control architecture
and that's what we're gonna do in this
video I'm Brian and welcome to a MATLAB
Tech Talk
we're gonna spend the majority of this
video working in Simulink but rather
than start here I think everything will
make more sense if I set the context
first so what do we have so far we have
a set of nonlinear models wrapped around
a model of the full flight control
software this is the software that set
up for automatic code generation and it
has the controllers and state estimators
that are directly in the control loops
but also other logic like fault
protection and data logging every single
one of these have nonlinear components
to them so it makes sense that in order
to have a linear model of the entire
system
we need to linearize both the flight
software and the models that are wrapped
around it however in my experience it's
hard to have flight software that is set
up well for automatic code generation as
well as for linearization this is
because flight software has if
statements and switches and state
machines and all sorts of stuff that are
needed for the code to run but are
difficult or impossible to linearize
therefore we usually build a completely
separate model for controller design
specifically one that is capable of
being linearized so in this video i'll
start with the full up quadcopter model
that comes with the aerospace block set
and then start removing a bunch of stuff
that we don't need for controller design
specifically all of that stuff that
makes linearization difficult and once
we have that stripped-down model will
use the PID tuner app in Simulink to
linearize this model and tune the PID
controllers if you recall from the
second video our control architecture
looks like this with several different
control loops and six different PID
controllers now we're gonna start by
tuning just a single loop the altitude
loop remember this is independent of the
other loop so we can tweak and adjust
the altitude without
affecting roll pitch or yaw to make sure
that they're out of the equation
completely we'll just set the commands
to zero for roll pitch and yaw will also
make the assumption that the sensor
dynamics and noise don't meaningfully
impact the controller design if this is
true then we can remove the sensor
models and the state estimation logic as
well basically we'll assume that our
controller knows the true altitude of
the drone perfectly and after we
complete our controller tuning we'll
test the results on the full nonlinear
model and see if that assumption is good
if it doesn't work then we'll add the
sensors back in and try again I like to
start with the simplest model possible
and only go more complicated if
necessary okay at this point we'll
linearize the altitude loop and adjust
the gains to get the altitude
performance we're after now I'm only
going to tune this one controller in the
video but the process is nearly the same
for the others as well
after the altitude loop I'd move on to
tuning the yaw controller keeping roll
and pitch constant and holding altitude
fixed once that's done I'd move on to
the roll and then the pitch and once
those inner loops are all tuned then I'd
move to the outer loop position
controller and tune that while the inner
loop controllers are active and
maintaining orientation in this way we
would step through each of the six PID
controllers and at the end have each of
them working together all right that's
the quick overview of what we're about
to do I'm gonna add some more context as
we go but that should be enough to
understand what I'm doing as I start
moving Simulink blocks around so let's
head over to Simulink and get to it the
first thing i want to do is just see how
well the stock altitude controller does
i'll pick off the altitude from the
state bus and plot it with a scope the
controller is trying to hold an altitude
of 0.7 meters and remember that altitude
is measured in the drone reference frame
which has positive z-axis pointing down
so our controller is actually driving
the altitude to negative 0.7 and as you
can see there is a slight overshoot with
the way that the stock controller is
tuned but it settles out nicely at
negative 0.7 well now let's see if we
can tune the controller for different
performance
this model is our simulation model and I
don't want to change it instead I'll
just copy the whole thing and paste it
in its own model that we can modify and
used for controller tuning all right now
that we have that let's start removing
things first we don't need any of the
visualization or the scope that I just
added we can also remove the sensor
block since we're just going to feedback
a perfect altitude State okay now if I
go into the flight control system
there's a lot in here that I don't need
remember we're just focusing on the
altitude controller for now so inside
the flight controller subsystem I'll
grab just the altitude reference and the
controller plus the motor mixing
algorithm and the thrust to motor
command block these will be the only
parts of the flight code that we need to
complete the altitude loop I'll bring
those blocks up to the top level of the
model and remove the rest of the flight
control software the output of these
blocks are the motor speed commands
which we can feed directly into the
airframe model and now I'll set the roll
pitch and yaw torques to zero ensuring
that as long as there's no other
external forces and torques on the
airframe like wind gusts or something
then the airframe can only go up and
down and I know that the environment
block doesn't model external
disturbances so we're good with just
setting the commands to zero now we need
to feedback our perfect altitude state
into the altitude controller and we have
the simplified closed loop system that I
showed you earlier so now at this point
we can go into the altitude block and
see what to do with the PID controller
we saw this controller in the third
video but I just want to briefly
describe what's going on again first off
this is just a PD controller and the
derivative path is not fed by an actual
derivative but rather by the altitude
rate that's estimated by the common
filter remember this is a good way to
set up the PD controller because we're
not taking a derivative of a noisy
signal we're estimating the rate
directly however this setup doesn't work
when we feedback the actual state rather
than going through the state estimator
that's because we're not also feeding
back a true rate State but that's okay
because for this model we have removed
the
sensor block and the noise associated
with them and have a pretty clean
altitude signal so we can feed that
altitude signal into a Simulink PID
block which will take care of the
derivative for us as well as add some
filtering if needed I'll send it to a PD
controller and set the gains to the
current values
next i'll remove the existing pd gains
and logic and replace it with the PID
controller block and now this is ready
for auto tuning we have our altitude
reference at minus 0.7 meters which is
then compared to the true altitude in
this case the error is fed into a PD
controller and then a feed-forward
gravity term is added in afterwards
this basically adds in the amount of
thrust needed to offset the weight of
the drone so that the PD controller just
needs to add positive thrust to go up
and negative thrust to go down we could
remove that feed forward path by adding
an integral to our PD controller but I'm
gonna leave it like this since this is
the way that the model is already set up
all right now we can open the PD block
and hit the tune button to kick off the
auto tuner the first thing the auto
tuner does is linearize the entire
control loop remember now that we've
removed all of the difficult components
this system is capable of being
linearized the tool creates this linear
model of the plant that we can export
and use to design our PID gains manually
if we choose to we can check out the
model it came up with by looking at the
data browser and double-clicking the
plant here we can inspect the state
space matrices and get a sense of the
plant that we're dealing with this
particular model looks like a double
integrator with a gain applied now we're
gonna stay within the PID tuning app
because it plots the closed loop
response of the controller and the
linearized plant for us and we can
simply adjust the response time and
transient behavior of the loop with the
sliders at the top you can see that the
stock design that's the dashed line has
a similar behavior to what we saw with
the simulation model it's not going to
be exactly the same because we removed
all of those nonlinear components but
the goal is that it's close enough for
gain selection we'll test that out in
just a bit
so now we can move the sliders around
and adjust the behavior and for this
design I want to have certain frequency
domain performance by setting the
bandwidth to 5 radians per second and
the phase margin to 60 degrees
this produces a proportional gain of
about 0.32 and a derivative gain still
around 0.3 so let
leave our design model now and head back
to the full simulation model and see how
these new gains behave
I'll go back to the altitude controller
and put 0.32 in the proportional path
and leave 0.3 in the derivative path and
then head back up to the top level to
run the simulation
and look at that with these new gains
we've changed the behavior of our hover
control system you'll notice though that
the overshoot is gone but based on our
linear analysis we would have expected
the drone to still overshoot a little
bit this difference is due to us doing
our analysis with an imperfect linear
model however the result is still close
to what we designed and so linear
analysis gave us a really good starting
point
if we weren't completely happy with this
response we could now tweak the gains a
little bit and see if we could improve
the performance the real test however is
not testing in a simulation environment
but testing it on the real physical
hardware now if the hardware behaves
exactly like this model then we know
that our model based design tuning will
work for it as well but we don't have to
assume that since we can generate code
for the parrot mini-drone from the
Simulink model we could just try out our
new gains on the actual hardware
I mentioned this last time but it bears
repeating remember your safety goggles
you can never be a hundred percent
confident that your control law won't
cause the vehicle to go out of control
and since it's practically a flying
lawnmower that can be dangerous all
right here we go
I have the new gains and the flight code
loaded onto my mini drone ready for
takeoff
okay that didn't work so well so what
happened well I'm not a hundred percent
sure but I do know something and that's
that the hardware doesn't behave like
the model there's something that the
model is missing or has modelled
incorrectly for my hardware that gave me
the impression that the control law
would work and having a good model is
the foundation of model-based design and
if you use model-based design I think
you'll find you'll spend a lot more time
creating and validating a model of your
system then you will developing a
control law but that's time well spent
because once you have a good model then
designing simulating and verifying your
system becomes so much easier than
having to do all of that with the
physical hardware so some things that
definitely aren't modeled that may be
important is how well the ultrasound
works at low altitudes or how the
aerodynamics change when flying close to
the ground and one problem in my case is
that this model was developed for the
general parrot mini-drone
and my specific drone may have
parameters that are different the
battery voltage may be lower the mass is
off the motor torques are different and
so on
I would now want to investigate through
system identification or other physical
tests where this model differs from
reality and make the necessary changes
but unfortunately I don't have the time
in this video to go into a detailed
investigation of the model so I'm just
going to have to leave that for another
time
okay I changed my mind I don't want to
leave this video with a failed
experiment
I guess it's thinking about the behavior
and came up with a possible issue this
feed forward term is calculated to
produce the thrust needed to perfectly
cancel the weight of the drone that way
the PD controller just needs to adjust
the thrust slightly up and down to
change the altitude but what if this
term is too low or another way of
putting it what if the model thinks that
the weight of the drone is lower or the
thrust produced by the rotors is higher
than what it actually is then there will
be some residual weight that the PD
controller needs to handle and by
lowering the proportional path we've
reduced the ability to overcome that
weight and the drone will have trouble
taking off just like we saw so I did a
test where I removed the feedback
altitude controller and just relied on
the gravity offset term to raise the
drone the stock value with my drone
caused it to just sit on the ground and
not take off I then raised the value by
10% and tried it again
and then by 20% and that 20% the drone
just barely was able to rise off the
ground so I think this is a more
appropriate feed forward term for my
hardware I then added the feedback term
back in and ran the test one final time
and check this out it works now this was
a quick fix to identify the problem I
still need to adjust the model of the
drone to reflect this and that sounds
like a pretty fun project to me so if
you find yourself trying this at home
I think this will be a good introduction
to model-based design and give you a
chance to learn how to take a model that
somebody else provided to you and tweak
it for your particular circumstances at
the very least it gives you an excuse to
play around with developing control laws
for quadcopters I hope you find it as
exciting as I have if you don't want to
miss the next tech talk video don't
forget to subscribe to this channel also
if you want to check out my channel
control system lectures I cover more
controlled theory topics there as well
thanks for watching and I'll see you
next time