Fuzzy Logic, Part 3: Design and Applications of a Fuzzy Logic Controller

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the last two videos we walked through how a fuzzy inference system works in general and so in this video we're going to take that knowledge and show one way to design a fuzzy inference system and to do that we'll develop a fuzzy inference based controller that can balance a pole on a cart and i want to show you how a fuzzy logic controller can be designed using just experience and intuition about the system no mathematical models necessary i think you'll be surprised at how simple it is to set up if you have a good understanding of how your system behaves so i hope you stick around for it i'm brian and welcome to a matlab tech talk all right this is the kart pole problem it's a pretty standard academic example so i thought it would be nice to use an example that you may already know how to solve using other control methods and so it works like this the pole is being pulled down by the constant force of gravity and is free to rotate about this pivot point the cart can roll back and forth and we can apply an external force to accelerate the cart left and right we have four states that we're measuring pole angle and angular velocity and cart position and velocity and if you want to try to copy this exact example these are the physical parameters of this particular system all right so the overall block diagram looks like this force enters the cart pole dynamic system and the four state variables come out then a fuzzy logic controller uses one or more of the output variables to determine the appropriate force that keeps the pole upright simple enough right well let's head over to simulink and start the design i've already put a few blocks in the simulink model this block is the dynamics of the carton pull system which you can see takes the cart force's input and outputs the four states the pole initializes perfectly balanced and since there are no other external disturbances in this model i disturb it by injecting a small impulse at time equals one second just to get the cart moving a bit and get the pole to fall over this block over here brings up the animation so that we can see what the system is doing more easily than looking at a plot so let's run this and see what happens the cart and pole start perfectly balanced and then the impulse gets the system moving and the pole falls and loops around and around and around i forgot to mention that there is no damping in this system and so the pole will never come to rest all right so that was fun but now let's design a system to control it if i had a model of the carton pull system then i could use something like full state feedback and a tuning technique like lqr to design the controller but for this example let's assume i don't have this model and that generating a reliable and accurate model would be difficult and this is one of the situations where using your experience with the system and a fuzzy logic based controller can be beneficial so let's walk through how that would work the left screen is a matlab live script where i'll design the fuzzy inference system and then once it's created i'll pull it back over to simulink to test it out so to begin i'm going to create a mamdani fuzzy inference object with the momfist function i need to specify the number of inputs and outputs and the number of membership functions for each now ultimately we'll probably want to use all four inputs however i want to start off really simply by just seeing if we can balance the pole using just poll angle i mean intuitively it makes sense that if the pole is leaning to one side you should be able to move the cart in the same direction and get it under the pole to push it back up and well that's the idea anyway so in this case there's only one input and to keep everything really simple i'll also only define two membership functions for it the input is poll angle theta and the range of this input is between minus pi and pi radians however even though the input could be anywhere in this range i'm going to set up the system to just try to keep the angle between plus and minus half a radian and the way i'm going to do this is with the input membership function shape i'm choosing a z shape for the negative membership function because it saturates at 100 membership below this specified lower bound which is negative 0.5 radians in this case and then it smoothly ramps down to zero percent membership at 0.5 radians and for the positive membership function i'm using an s shape which does the opposite you know what let me just plot it here so you have an idea of what these two shapes look like you can see the saturation occurring outside of the plus and minus half a radian and if i zoom in you can see how the two membership functions overlap over the range that i'm concerned with and the way that we can read this is that if theta is zero or the pole is perfectly balanced then that value belongs equally to the negative and positive membership functions otherwise the degree of membership is skewed more negative or more positive depending on theta and if i had just defined these membership functions between plus and minus half a radian then this controller would not produce a response if the pole happened to fall outside of that range but by defining the functions across the entire input range of plus and minus pi we guarantee that the controller will still label the input as fully negative or fully positive all right so i'll define the output in a similar way but this time i'll define the membership function shapes as a generalized bell function i don't need these functions to saturate and extend to some unknown and arbitrary large value since i know what force this actuator is capable of delivering also i know how much force i want to apply to the cart and so i'll situate these bell shapes within that range of about 12 newtons in either direction i'm going to call them nm for negative medium and pm for positive medium and the reason i'm calling these medium is that well spoiler alert later i'm going to add larger and smaller membership functions to the output and i want to keep the names consistent but for now we just have these two possible memberships so theta can either be positive or negative and the resulting force can either be positive medium or negative medium and now we can define the rules for inference and with such a simple system we only need two rules if theta is negative then force is negative medium and if theta is positive then force is positive medium these rules basically encode the intuition that we had earlier and that is to balance a pole we need to move the cart in the direction that it's leaning and remember this is the kind of experience and intuition you might have from just trying to balance a poll yourself i mean there is no need for a mathematical model to make this kind of inference all right so once we add these rules to our fuzzy inference system we're done with the design and we can pull it into simulink with a fuzzy logic controller block the output of the controller is cart force and i can add it to the impulse disturbance and feedback the variable theta and that's it we have our one input and our one output and we're ready to run it oh look at it go it's definitely keeping the pole upright but it's doing sort of a wiggle dance as the cart keeps trying to go under the pole but the momentum of the pole causes an overshoot and the cart has to come back causing another overshoot and another overshoot ad nauseam so once again we need to come back to our intuition and experience with the problem and realize that if we take into account the pole angular velocity and not just the angle then we can use that to help slow the pole rotation down right as it reaches the balance point so back over at the live script i'm going to change the number of inputs to 2 and add a second input for theta dot i'll keep the same membership function shapes but change the input range to plus and minus 5 radians per second to account for quick rotations now as for the output i think that we really want to restrict angular velocity of the pole by using a large force whenever motion is observed i don't want the pole to rotate too quickly ever so what i'm going to do is add two more membership functions to the output and make them centered around a larger force i'll call them negative large and positive large and this is what these membership functions look like so the same as medium just moved over a little bit now i can add two more rules to our inference system if theta dot is negative then force is negative large that is if the pole is rotating in the negative direction really try to stop that rotation by moving the cart with a large negative force and the opposite for a positive theta-dot now i want to let you know real quick that the way i'm setting up these rules where there's one input per rule isn't typical for a fuzzy inference system i'm just doing it this way because i feel it helps tell the story about how each of the inputs contribute to the overall controller usually you would want to set this up so that every rule contains all of the inputs anded together for example in this case i'd still have four rules but they would look like the following and setting up the rules in this way actually gives you more flexibility in the design than how i'm doing it in the video however again for simplicity and clarity we're going to stick with these rules where there's just a single input per rule all right so let's give this version of the controller a shot this now has two inputs so i need to mux together theta and theta dot and send both of them into the fuzzy logic controller otherwise everything else is exactly the same so now let's run this model oh look at that the pole is perfectly balanced it's it's rock solid since the cart is moving so quickly to stop any possible pole movement and this looks good well except for the fact that the cart is ever so slightly wandering away as it tries to balance this pole and that will not do so let's use the cart position and the cart velocity to stop this motion now i'm going to zip through this section pretty quickly since it's just more of the same i'm adding two new inputs with their own z and s shape membership functions and i'm going to add two more membership functions to the output that's negative small and positive small and if we look at the plot of the output functions we can talk about why i made these so much more narrow my thought is that since the cart is only slightly wandering away i didn't want to push it back with a whole lot of force possibly disrupting the balance of the pole so i think that just by ever so slightly nudging it we're going to be able to keep the pole nicely balanced and still be able to hold the cart steady so with that logic i'm adding two more rules that move the cart slightly in the opposite direction of the position error and then to keep the velocity of the cart low i'm adding two more rules that try to maintain zero velocity with a medium amount of force now it's not obvious at first why i'm moving the cart further away from the zero position point but here's my reasoning if the pole is perfectly balanced i want the cart to first move away to rotate the pole towards the goal position this will trigger the rule that then tries to keep the pole upright by moving the cart with a greater force in the opposite direction in this way the cart ultimately moves in the direction i want by nudging it the other way first okay so with the controller ready we need to make some updates on the simulink side let's give this controller a target position to follow i'll change the signal generator block to output a square wave with amplitude 1. this is the target position that will jump back and forth and then i'll subtract the measured position from it to produce a position error which is what the fuzzy logic controller will try to drive to zero we then have to add position error and velocity to our mux and input all four variables into our fuzzy logic controller so let's run this final version and see how it does all right that's way cool i mean how awesome is that i mean it's it's a bit slow so i could spend some time tweaking the rules and membership functions but for this video i think it shows the result of a fuzzy inference system quite nicely and as we showed we didn't need to derive the model of the cart pull system we were able to develop this controller using experience about the system and our intuition about how to control it now i wrote code to design the fuzzy controller because i think it's easier to follow along in video format however all of this code could have been bypassed and the design could have been done graphically using the fuzzy logic designer app with this app there is a click and drop interface where you can design the membership functions and the rules plus it allows you to visualize the rules which can make it easier to troubleshoot and tweak the controller so if you want to develop a fuzzy inference system you can choose your preference of what you like to use as an interface whether it's graphical or written code okay lastly i want to remind you that fuzzy inference systems have a lot of practical applications and have been used in many different industries we already know that we can develop a fuzzy logic controller which can take the place of other traditional control methods and other examples of this is within autonomous driving where a fuzzy logic controller has been developed that can automatically park a car this system uses range sensors on all four sides to guide the car into the empty spot and this is a really interesting example because the car has non-holonomic constraints basically that means that there are more degrees of freedom for the car than there are possible ways to move it for example the car cannot directly move sideways but instead must make a series of other motions to go in that direction and this is intuitive to a human driver and therefore can be a great candidate for a fuzzy logic based controller another famous example is the speed controller for the sendai subway system in japan using a fuzzy logic controller the engineers were able to make smoother starts and stops as well as be 10 more energy efficient than human controlled acceleration however we also talked about in earlier videos how fuzzy inference systems can be used for decision processes not just controllers and in the second video we talked about a loan risk example and it turns out that there's actually many practical examples of fuzzy inference decision systems in the field of finance and there's also some really interesting examples in the medical industry in fact one in particular is the use of fuzzy logic for an artificial pancreas researchers show that fuzzy logic is a viable alternative to other model based controllers for a control loop insulin delivery system because it does not require differential equations of the glucoregulatory system and allows clinicians to personalize dosing aggressiveness to meet individual patient requirements and this is actually a really interesting problem and we're going to dive into a version of an artificial pancreas in the next video so in this video we covered how to design a fuzzy inference system from experience but in the next video using the artificial pancreas example we'll show how to design a system when you have a lot of data so if you don't want to miss that or any other future tech talk videos 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
Info
Channel: MATLAB
Views: 3,036
Rating: 4.9142857 out of 5
Keywords: MATLAB, MathWorks, Simulink
Id: fU8Lyc8kzto
Channel Id: undefined
Length: 16min 44sec (1004 seconds)
Published: Tue Sep 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.