hey everyone welcome back to control system lectures uh specifically welcome to the introduction to discrete control so far I've only addressed designing Control Systems using the frequency domain and only with continuous systems that is we've been working in the S domain with transfer functions now we're going to stick with frequency design methods with transfer functions for the next few videos but instead of working in the continuous s domain we're going to switch over and start to understand how to design control systems in the discret or Z domain I'm going to cover this topic over the course of multiple videos I'm not sure yet exactly how many but they will be numbered sequentially so you can watch them in order now this is the first video and here we're just going to set up the problem give a little background and context and then walk through thinking about the problem intuitively rather than mathematically in the videos following this we'll cover more of the mathematics so I apologize in advance for not touching on the math here but I think this background will help give an understanding of how things change when working in the Z domain let's get started let's imagine a world where you've been asked to design a feedback control system around the first order plant 1 over 2s + 1 with the requirements that the steady state error is less than 2% for a ramp input and the phase margin is greater than 48° the first thing we should do is quickly check to see if this current system meets our requirements already if it does then we're done and now in order to do that we need to check the steady state error for a ramp input we can do that by hand but for this I want to show you how to plot it in mat lab now when working with transfer functions in mat lab I always like to start by setting the variable s to the transfer function s that way we can build any transfer function we want easily using this variable I'll assign the variable G to our plant 1 over 2s + 1 now in mat lab it's pretty straight forward to get the step response using the step function just type out step of G however there isn't a similar ramp function that we can easily use but plotting the ramp response really isn't that hard now to understand why let's go back to the Blackboard and figure it out we have our plant and we need to know its ramp response that is we want to know what the output is to a ramp input we can get a ramp input by integrating a step function and since an integral in the S domain is just 1 / s we can simply divide our plant by S plot the step function and we'll get a ramp response so let's go back over to mat lab and we can take the step of G over s and it sure does look like a ramp response but it's kind of hard to see what the steady state error is let's compare our system's response to just a ramp 1/ s and we'll plot both of them on top of each other there are now two plots there but they're so similar it's really hard to see finally we can check their difference or the error simply by subtracting the two and you can see that our open loop system has a steady state error of 20% not 2% however since it is a first order open loop system it has infinite phase margin that means no amount of additional phase lag will cause this system to go unstable it's stable regardless so phase margin is good but the steady state error requirement is not therefore we clearly need to design a controller to get our steady State error down to 2% at this point you have the option of designing your controller any way you want I've used this exact example in two of my videos one on designing a lead compensator and one on designing a lag compensator with bod plots now rather than go through that again let's assume you've designed the controller from the lag video and we'll use it here the link to that video is in the description below if you'd like a refresher on how to do this but we're left with the controller 500s plus 50 over 100 s^2 + S all right so where are we we have a model of our plant we've developed an S doain controller for it and we think it will meet the needs of our system and again we can check that in mat lab to be sure let's create the controller transfer function C and then using C and our plant transfer function G we can create the feedback system and find the Clos Loop transfer function using the feedback command at this point we can check the ramp steady state error like we did before and it definitely looks different than the open loop system there's a jump and error at the beginning and then it reduces down to 2% error over time now to find the phase margin for our system we look at the Bodi plot for the open loop system C * G and from the plot we can show all stability margins to see that our phase margin for this system is 50.5 de which meets our 48 degree requirement and since the steady state error has been reduced to 2% this controller does in fact meet all of our requirements okay great so what do we even do with this controller that is how do we implement this in a real system to see if it is actually working properly this is a continuous domain controller which means we need to build it out of continuous domain parts that is the controller is continuously adjusting its output based on continuous measurements there are no time gaps between successive measurements or successive actuations it is an everchanging and smooth operation and we have several ways which we can Implement a continuous controller like this I'll show three ways but there are many we could use a Pneumatic or hydraulic controller that operates using air or liquid pressure to adjust valves and regulate liquid flow rates an example of this is the water pressure regulator that adjusts the street pressure of the water entering your house from a pressure that might damage your water pipes to to some lower and adjustable amount another example are the temperature controllers in green houses the temperature is controlled using B metallic louvers that open the windows when the inside temperature is too high and closes them when they're too low two dissimilar metals are attached to each other and have different coefficients of thermal expansion therefore when the temperature changes they curl up and cause a torque that moves the windows and the final example is probably the one people are most familiar with analog Electronics these are electrical circuits that are created solely from analog components like resistors capacitors inductors dodes and transistors these circuits operate with a continuously variable voltage or current signal and can be implemented in a way to accomplish a number of tasks that can range from something as simple as a voltage divider circuit and as complex as an analog audio recording studio okay so which method do you want to use to build out your continuous controller well there's a snag in our plan and and I'm sure you've guessed what it is in this case the controller isn't going to be continuous because it's going to be implemented on a digital computer which means that it will only run and have access to measurements and command the actuators at specific and discrete times so here we are 7 minutes into this video and we're finally entering the world of digital control design the questions we want answered right now are how do you even get a controller that you designed in the S domain to run on a digital computer and will it still work the way way you expect it to digital computers are discrete systems that is they work with sampled data but they still typically interact with continuous systems when you go from a continuous system to a discrete system there is a loss of information and depending on what information is lost this can negatively impact your control system performance for example the air temperature in your house is a continuous system there is a value for temperature for all time and the temperature changes arbitrarily smoothly throughout the day there are no jumps or missing data the temperature however is controlled by a digital thermostat the thermostat doesn't have access to the continuously changing temperature only discrete measurements that are sampled at specific times the temperature may change drastically between those samples however the thermostat can only know the values it reads if the temperature is changing too fast then information is lost between samples and the thermostat can't act on it appropriately a Contin controller would see this highfrequency motion and would act on it if the bandwidth of our controller was high enough however the discret controller would not unless we decreased the time between samples that is the furnace in our example might not turn on and off fast enough if the house temperature is changing faster than our sample time an extreme version of this is if your thermostat only takes a temperature measurement once a day and then holds the resulting furnace command for 24 hours you can imagine how this increased sample time would negatively impact your control system performance digital systems also quantize signals that is they round the real analog value to the nearest digital value in our thermostat example the temperature measurement might get digitized to the nearest whole number in that case the air temperature could be measured by an analog sensor as 24.5 1° but then get rounded to 25° when it gets digitized in the computer that rounding introduces nearly a half a degree of quantization error this error can be minimized by reducing the step size between the discrete values say round to the nearest half a degree this would increase the number of bits needed to store the value or reduce the range of measurements that could be made with a step size of half a degree our quantized value is 24.5 de with a quantization error of 01 de there is always a trade-off between the number of bits needed to represent a value the range of measurement values and the quantization error error that you can handle but no matter how small the quantization size there will always be some amount of error in the digital system and that needs to be taken into account in addition to loss of information discrete systems add delay into your feedback loop that's because it takes time to read a measurement digitize it and then run the control algorithms by the time your commands are issued the measurement that that command is based off of is old this delay increases lag and can limit the band WID of your controller that is the upper limit on the frequency that your control system can respond to so why do we use digital computers for control if they cause loss of information and delays well for one thing the loss of information and delays are usually not that big of a deal that's because in most cases the speed at which modern computers run is a lot faster than the bandwidth of the control law that you're developing in addition they can be incredibly cheap to implement plus you have way more flexibility you could build a system try out a digital control ER and then update that controller and software later as needed rather than having to make costly Hardware modifications also digital systems behave more predictably in a changing environment because for example their behavior isn't as susceptible as analog systems are to things like variation in external temperature all of this makes it worthwhile for you to want to build a digital controller and learn how to do it let's update the block diagram for our feedback system with what it would look like for a digital controller the plant and sensors are still in the continuous s domain the measurement from the system though is discretized and quantized with an analog to digital converter by the way there may be an analog filter before the ADC to keep the highfrequency noise from aliasing to lower frequencies but we're not going to worry about that right now in this diagram the discrete measurements are compared to discret set points to generate an error term the error term is fed into a discrete controller which is represented in the Z domain and finally the discrete commands are converted back to the continuous domain with a digital to analog converter a common method to do this was just with a zero order hold this takes the last commanded value and just holds it there until a new command is issued the discret controller is represented as a z domain transfer function so let's take a moment to briefly explain what that means both Z and S domain transfer functions are the impulse responses of the system in their respective domain but the Z transform is applied to difference equations whereas the S transform is applied to differential equations that might seem subtle but difference equations can be implemented easily on a digital computer because their solution depends on past and present inputs and past outputs at discrete sample times a differential equation requires continuous knowledge of inputs and outputs and solving them on a digital computer requires numerical methods which is essentially a different difference equation and that's what the Z domain does for us it creates the difference equation that we can Implement on a digital computer so with that let's think about the different ways that we can create this digital Z domain controller so that it controls our continuous system appropriately and there are three General approaches which I'll highlight here but we'll go into more detail on in a later video first we already have a perfectly good continuous domain controller we could just take that continuous controller and convert it to an equivalent discrete controller that includes the effects of sampling in the zero order hold and then just see how it does a second approach is to convert the other half of the system The Continuous plant and the continuous sensor models we can convert that over to discrete systems and then design a new discrete controller completely in the Z domain then we can use that discrete controller back on our continuous System model and just see how it does lastly you can design a discret controller by tuning and manipulating it alongside the continuous model using a modelbased design program like simulink or something else in this last case you use the S domain transfer functions that model your system to craft a digital control law in the modeling environment you can tune in place and then have pretty good confidence that your controller will work in the real world as long as your plant and sensor models are accurate enough actually this reminds me simulink has no problem running continuous controllers on the computer that is it can solve ordinary differential equations so why can't you just do the same thing with your system and Implement a continuous controller well you can sort of simulink isn't actually running a continuous model it's on a digital computer after all what it's doing is reducing the sample time and running high order solvers that very closely approximate the solution of ordinary differential equations it's turning them into low error difference equations you could implement the same thing but while you might be making your job easier you definitely making the software and Hardware engineer job harder remember the measurements and the algorithms need to run within the sample period if you shrink that sample period and increase the number of calculations needed for the OD solvers then you are increasing the requirements on the performance of the computer making it harder to build and more expensive the rest of your team would be much happier if you found a way to develop a control law that could run with as long a sample time as possible but of course as with everything there is a balance to be struck because if you slow your system down too much it may no longer work no matter what method you choose from above to illustrate this I created a very simple JavaScript game that demonstrates the effects a digital system has on your controller performance of course I'm using the term game very lightly here here's how it works the white circle is the reference Target or the set point of the control system and it randomly moves around the screen it's moving right now but just really slowly the goal of the game is to follow the target with the mouse which is highlighted with a red dot I'm the control system my eyes are the sensors and my arm and the mouse are the actuators I'll hide my actual Mouse pointer because it just gets in the way now the light gray Circle shrinks if the Red Dot is within it and it grows if the Red Dot leaves the circle that light gray circle is analogous to the performance of the control system you know the idea is to keep the mouse as close to the Target as possible at all times to have the best performing controller the score in the upper corner which I've added just to show you quantitatively how well I'm doing of course this game is relatively easy if the bandwidth of the Target or the range of frequency that it randomly moves at is very low but if I increase the bandwidth it becomes a little more difficult at this point though it's still similar to a continuous control system the sample time of my monitor and my eyes and my brain is fast enough to make it look like a continuous system to me but I can switch over to the discrete domain by adding in a sample time to the measurement now the white circle is still moving smoothly behind the scenes but I can only see it at specified times this makes following it much more difficult the speed at which it moves hasn't changed just my ability to see where it is lastly I can add delay to the system by causing the Red Dot to lag behind my mouse you can see how great of a delay there is it's a couple seconds now I'll hide my mouse pointer again so it isn't distracting and try once more now the combination of delay and sampling makes following the high bandwidth motions of the white dot nearly impossible at least not with the same performance and just like with a real digital control system I could do better by decreasing the delay or decreasing the sample time or limiting the bandwidth of the target again there's a tradeoff there with computer complexity now if you'd like to play around with this game and see the effects yourself there's a link to the GitHub repository in the description where you can download it or better yet if you want to make a pull request and submit some improvements to the game I'm all for it and everybody will be better off because fair warning this is the first thing I've ever created in JavaScript and the code is atrocious now I told you we wouldn't go into the math in this video we'll save that for the next few however to end this video I did want to quickly zip through an example in simulink that way it'll tie together some of the things I just talked about and give you something to look forward to and think about in the meantime okay back in mat lab we've already defined our continuous controller C we can convert to a discrete transfer function in the Z domain using the function C Tod which stands for continuous to discreet and there are several methods we can use to do this but if you don't specify one the default is zero order hold which is what we want I'll give our new controller a sample time of 0.1 seconds the resulting discret controller is similar to The Continuous one but their differences can be seen in a bod plot you can see that their gain and lowf frequency phase shift is almost identical it's only at higher frequencies do the phase shifts start to deviate this makes sense since sampling causes a delay in the system at higher frequencies now we can show the Clos Loop response of these two controllers with A continuous plant in simulink again I'm going to go into more detail on this later so for now I'm just going to speed through the creation of this model model I now have two different models one that uses the continuous controller and one that uses the discrete controller to get the performance of these systems I want to look at the open Loop B plot and the closed loop step response I can do that with a linear analysis plot for the first plot I'll choose the bod plot set the input to the open loop input signal and the output to the open loop output signal now when I run the simulation it will generate my open loop Bodie plot and you can see that it looks exactly like the Bodi plot we generated near the beginning of this video now let me quickly set up a linear step response for the closed loop system and then copy those down to the discrete controller model as well now I'm generating four plots when I run the simulation and with the Bodi plots we can show stability margins and see that the continuous controller has 50.5 de of phase margin just as we saw before but the discret controller only has 26.6 de of margin both are stable but we've lost some stability by going to the discrete controller you can see that more clearly in the linear step response the discrete controller has a lot more oscillations in the response than the continuous controller again that makes sense because we've basically delayed our system by 0.1 seconds which increases phase lag now we can make the situation even worse if we change our sample time to 1 second on our controller I'll update the model and then rerun the simulation now we have infinite phase margin because the gain never drops below zero DB but we have a negative gain margin that means that our closed loop system is unstable and sure enough if we look at the step response we see that it blows up to Infinity okay I think this video is long enough now but hopefully this has given you a more intuitive understanding of how a discrete controller and the sample time and delay that comes with it impacts your system in the next few videos I'll walk through more of the math in designing a discrete control system and some tools you can use to analyze the performance if you have any questions or comments please leave them below and I'll try to answer them and don't forget to subscribe so you don't miss any future videos thanks for watching and a huge thank you to my patreon supporters for making this video possible if you'd like to support me and my efforts on YouTube you can from the patreon link in the description below for any amount of support you can download a digital copy of my book in progress on control theory now I'm still actively writing the book so it's not complete but if you would like a copy of what I have so far and you don't want to support me through patreon just email me at controlsystem lectures gmail.com and I'll just send you a copy for free that way we can spread the knowledge and help everyone on their Quest becoming better control system Engineers thanks everyone