in the last video we walked through a PID tuning guide showing several different paths you could
take to tune the PID gains for your system now in this video we're going to demonstrate how
you can accomplish some of these paths with a DC motor control example using MATLAB and
Simulink specifically this video is going to focus on getting over to this model-based tuning section
of the flowchart now once you have your model and you understand how it was generated and its
limitations you'll be in a much better position to know whether to approach PID tuning using
your physical system or with your newly generated model I'm Brian and welcome to a MATLAB Tech Talk
here's the setup you have a DC motor and you want to develop a PID controller that will control the
motor speed when you apply a voltage to the motor the motor will accelerate up to some steady state
speed the higher the voltage the faster the motor will run now the PID controller is in charge of
generating the necessary voltage that will cause the motor to run at the commanded speed if it's
designed correctly then the motor will be able to follow the command quickly with minimum overshoot
and it will be stable now we could just use the hardware and guess at some PID gains run it to
see how it does and then tweak them as necessary that's this left portion of the flowchart now
this guess and check method might work but we have so many other options available to tune
the controller if we have a mathematical model of the system so with that in mind let's go through
a few ways to generate a model of this DC motor now a quick caveat before we begin I'm gonna walk
through the details of each of these methods pretty quickly but the point of this video is
to show that there are multiple ways to accomplish the same thing and then give you a general idea of
when one method is better suited over another now if you want more detailed information on modeling
systems definitely check out the links in the description of this video all right the first
method we're going to talk about is deriving a model from first principles if your system
is simple enough to understand then you can write out the equations directly that govern
its behavior this is sometimes referred to as the white box method because it's as if you shined a
flashlight into the box representing your system and was able to see all the bits and pieces that
make it up and since you know everything about it you can derive a model using whatever technique
you enjoy or are good at this might be Freebody diagrams Lagrangian mechanics or building a larger
model from individual components for example this is what it means to derive a model from
first principles you're going all the way back to the fundamental physical concepts and assumptions
now this type of modeling is usually what you learn first and with good reason it helps you
understand the intricacies of the system you're trying to control and forces you to think about
what the important components are and therefore what needs to be modeled and which components can
just be left out for example for the DC motor is it important to model static friction well
if the motor is always operating at some nonzero speed then probably not the model won't benefit
from having static friction and including it just creates a more complicated model and slows
the simulation down now a real quick side note sometimes you know the structure of the system
but you don't know the specific parameter values for example with our motor you know that the rotor
has inertia but you just don't know what the value is this is sometimes called the gray box
method because you know something about the system but the specifics elude you and this is a pretty
common situation and you can handle it by either testing your motor and measuring that value or
you can fit it to an existing model that has the correct structure and then have software find the
optimal parameters I'll touch on this a bit more when we discuss system identification for now
I'll go over to MATLAB and Simulink and open up a nonlinear model of a DC motor and speed sensor
that I created using first principles this model takes motor voltage as an input and outputs motor
speed in radians per second and you can see that it's not a terribly complicated model it's just a
handful of components but like I said earlier as long as you have insight into the structure and
parameters of your system you can build a model like this I want to quickly show you another way
to develop a model from first principles using sim scape with in Simulink sim scape essentially lets
you avoid writing out the ordinary differential equations like we just did and instead lets you
draw the mechanical and electrical circuits of the system directly this is a sim scape model of
a DC motor just to briefly show you what it looks like instead of integrators and gain blocks this
model is built with voltage sources and current sensors even the DC motor itself is modeled with
resistors inductors and mechanical components so two different ways to create a motor model
from first principles now we can use this model to close the loop with a PID controller and tune the
gains to get the behavior that we want and if this nonlinear model is a pretty good representation of
your real physical system then the gains we get using it can transfer over to the real hardware
and work there as well now I know you probably want to get into actually tuning this controller
but we're gonna cover that in the next video for now I want to move on to a different way
to develop a model let's say that your system is too complicated to derive a model directly
either with differential equations or mechanical and electrical components and instead you'd rather
create a model of the hardware using system identification techniques system identification
allows you to generate a model without knowing the details down to the fundamental physical
concepts instead you can rely on the relationship between the input signal and the output response
to infer a model of the system this is sometimes referred to as the black box method because you
don't have to have insight into the specifics of the system now as a rather absurd example imagine
a box with a hole on either side and a conveyor belt that moves through it you don't know what
happens inside the box but you do have access to items before and after they go through it
you place a loaf of bread at room temperature on the belt and send it through when it comes
out the bread is 10 degrees warmer now at this point you're pretty confident that whatever is
in the box supplies heat and by understanding the thermal properties of the bread you can infer how
much heat was imparted by the system and create a representative heating model now is this heating
done with an electric coil or with microwaves you don't know that at this point and perhaps
you don't care about that detail but this is an important concept with system identification you
get to avoid needing to understand the details of your system since they're lumped into general
behavior however you still need to understand your system well enough to know whether you generated
the right model structure for example if you based your model on how bread heats up and then
you send some metal through you might realize pretty quickly that you do in fact need to know
whether it's a coil or a microwave so this isn't a method that you can run with complete ignorance
all right let's use system identification with our DC motor we can apply a known signal into
our motor and measure the response now normally this is done using physical hardware but since
I don't have a real motor for this video we're going to pretend that the nonlinear model that
we have is our physical hardware and we'll just get our needed information from it instead alright
with the step input and response output measured I can use the system identification app to fit a
model to this data now the details on how to use this app are in the description of this video
but what you should take away from this is that there are existing system identification tools
available to you and these tools don't remove the need to understand how system identification
works they just make the whole process easier and faster from my input and output data the system
identification app came back with a second order transfer function about six point six s plus two
point four divided by s squared plus point nine s plus point two and now we can go back to Simulink
and put our new identified linear model to the test I'll add a transfer function block and write
out the numerator and denominator explicitly you could use the LTI block and just assign it to TF
one directly but I like writing it out explicitly in video so that you can see the transfer function
clearly in the block mask as a reminder this block represents a linear model of both the DC
motor plant and the sensor and we got this model using only an input and response signal from our
system now we can run both of them side-by-side and see that our estimated linear model matches
our nonlinear model pretty nicely but of course this looks good this is the data we use to fit
the model the real test is to see how our linear model compares against a wide range of arbitrary
inputs and again it worked pretty well so you can see how simple it can be to create
a model using system identification rather than slugging through a model with first principles
and now that we have this model we could wrap a PID controller around it and use the linear
model to tune the gains all right let's set this aside for a moment and talk about a third way to
generate a model using linearization techniques we just created a linear model using system
identification and that's a legitimate way to generate a linear model however when a nonlinear
model is available linearization or linearizing that model can produce more trustable and reliable
results than system identification and it might be easier to troubleshoot for these reasons it's
a technique worth learning linear systems are awesome or more accurately linear system models
are awesome because there is no such thing as a truly linear system real systems all have some
nonlinear behavior to them but that doesn't stop us from approximating a real system with a linear
model we just did that very thing with system identification and now we're going to start with
an existing nonlinear model and make it linear the hope is that your real system behaves linearly
enough that you're okay with this approximation and why are we trying to shoehorn all of these
systems into linear models well it's because we know how to solve linear models and we've built
up a whole lot of tools that we can use to design and tune control systems with linear plants do
you want to work in the Laplace domain do you want to do loop shaping or pole placement or
some other classical linear control technique then you're going to need a linear model well
for our DC motor the static friction that I mentioned earlier is nonlinear behavior
this is because the motor won't spin until the torque is strong enough to overcome the static
friction so there's this range of very low input voltages that don't actually produce a spinning
motor now had this been a linear motor then very low voltages would still cause the motor to spin
even if it's very slow so if we want to generate say a transfer function for our motor we need to
linearize it around some operating point and just to show you a quick demonstration of it we can do
that easily in Simulink the first thing we need to do is set the open-loop input and output points by
right-clicking on the signal lines and selecting linear analysis points this tells Simulink where
to begin and in the linearization i've set it up so that we linearized the motor plant and
sensor together with these set we can launch the linear analysis tool this tool is going to look
at our nonlinear model and remove all of the nonlinear components or estimate them as best
it can as linear components and then return the linearized model again I'm not going to go into
detail on how to use this tool you can find that information in the links below but just like with
system identification it's important to have an understanding of what it means to linearize a
system and what information you're losing in the process for us our linear model won't behave like
the real motor at very low rpms this is because we're essentially removing the static friction
component and with this information I know not to trust this linear model in these cases and
only rely on it for situations where the motor is spinning okay so here's our linear model
it's a second order transfer function that has two poles and no zeros let's head back over to
Simulink and try out our new linear model I'll copy the previous transfer function block and
then just change the values to these new values and hook it up to our step input and scope output
and then run a step response and there we have it three different models of the same physical
system the first we generated by writing out the equations directly the second we took input and
output data from the model or the physical hardware and used system identification to
fit that data to a model of our choice and the third we linearized our nonlinear model
around an operating point of our choice these three models all seem pretty different but you
can see that they produce similar results even with an arbitrary input it's now at this point
that we can start the process of developing a PID controller for our system and we have four
different systems that we can there's the physical system itself and then the three different models
we generated of it well in the next video we're gonna dive deeper into these tuning methods using
MATLAB and Simulink so 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 control theory topics there as well thanks for watching and I'll see you next time