Feedback

Understanding PID Control, Part 5: 3 Ways to Build a Model

From the series: Understanding PID Control

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. 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. Once you have a 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 with your physical system or with your newly generated model. I’m Brian, and welcome to a MATLAB Tech Talk.

Here’s the set up: 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. 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. 

We could just use the hardware and guess at some PID gains, run it to see how how it does, and then tweak them as necessary. That’s this left portion of the flowchart. 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. A quick caveat before we begin. I’ll 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 give you a general idea of when one method is better suited over another. If you would like more detailed information on modeling systems definitely check out the links in the description of this video. Alright, 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 were able to see all of 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 free body diagrams, Lagrangian mechanics, or building a larger model from individual components. This is what it means to derive a model from first principles: You’re going all the way back to fundamental physical concepts and assumptions.

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 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 non-zero 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.

A quick side note: sometimes you know the structure of the system, but don’t know the specific parameter values. For example, with our motor, you know the rotor has inertia, but you’re just know sure what the value is. This is sometimes called the grey box method because you know something about the system but the specifics elude you. This is a pretty common situation and you can handle it either by testing your motor and measuring that value or you can fit an existing model that has the correct structure and 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 rad/s. And you can see that it’s not a terribly complicated model - just a handful of components. 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 Simscape within Simulink. Simscape essentially lets you avoid writing out the ordinary differential equations like we just did, and instead lets you draw the electrical and mechanical components directly. This is a Simscape model of a DC motor just to briefly show you what it looks like. Instead of integrators and gain blocks, this model is built using voltage sources and current sensors. Even the DC motor itself is modeled with resistors, inductors, and mechanical components.  

Now, we can use this model to close the loop with a PID controller and tune the gains to get the behavior 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 as well.

I know you probably want to get into actual tuning, but we’ll cover that in the next video. For now, I want to move onto 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 insight into the specifics of the system.

As a rather absurd example, imagine a box with a hole on either side and a conveyer belt that moves through it. You don’t know what happens inside the box but you 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. At this point, you’re pretty confident that whatever is in the box supplies heat. 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 the 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.

This is an important concept with system identification; you get to avoid needing to understanding the details of your system, since they are just 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 you do need to know whether it’s a coil or microwave. So this isn’t a method that you can run with complete ignorance. 

Let’s use system identification with our DC motor. We can apply a known signal into our motor and measure the response. 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 get our needed information from it instead.

With the step input and response output measured, I can use the System Identification app to fit a model to this data. 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. 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 2nd order transfer function: 6.6s + 2.4 / s^2 + 0.9s + 0.2.

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 tf1 directly, but I like writing it out explicitly in videos 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 used to fit the model. The real test is to see how our linear model compares across a wide range of arbitrary inputs. And again, it worked pretty well.

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 this linear model to tune the gains. 

Alright, 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, so that is 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 also 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.  

The hope is that your real system behaves linearly enough that you’re OK with this approximation. And why are we trying to shoehorn all of these systems into linear models? Well, 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 loop shaping, pole placement, or some other classical linear control technique? Then you’re going to need a linear model.

For our DC motor, the static friction I mentioned earlier is a nonlinear behavior. This is because the motor won’t spin until the torque is strong enough to overcome the static friction. So there is this range of very low input voltages that don’t actually produce a spinning motor. Had this been a linear motor then very low voltages would still cause the motor to spin, even if it’s very slowly. 

It is precisely this static friction that causes our DC motor model to be nonlinear. So if we want to generate say a transfer function of 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 end the linearization. I’ve set it up so that we linearize the motor plant and the 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 as it can as linear components - and 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 a 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. 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.

OK, so here is our linearized model, a 2nd order transfer function that has 2 poles and no zeros. Let’s head back to Simulink and try out our new linear model. 

I’ll copy the previous transfer function block, change the values, and hook it up to our step input and scope output and 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 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 is 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 use. In the next video we’ll 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. I’ll see you next time.

Product Focus

Other Resources