Pole Placement for the Inverted Pendulum on a Cart [Control Bootcamp] - https://www.youtube.com/watch?v=M_jchYsTZvM Okay, welcome back. So now we're ready to design a control law to stabilize the inverted pendulum on a cart. Okay, I moved the picture down here so I could have some breathing room. Last time we took the nonlinear system describing the lagrangian mechanics of this inverted cart on a pendulum on a cart problem. We simulated it, then we linearized about the pendulum up position to obtain an a matrix and a B matrix. I didn't go through all of the math, but that's kind of basic physics. So, so I showed you that there is a linearization. And then we checked that the rank of the controllability matrix was in fact four, which means that we span all of our state space with this controllability subspace, meaning that the system is controllable. And of course, now we know that if the system is controllable, then with full state feedback, by measuring the full state of the position and velocity of the cart and the pendulum, so this full state, then I can design this closed loop system so that the eigenvalues are anywhere I want. Okay, and so now what we're going to use is the place command in Matlab. So we're literally going to say k equals place, and we're going to give it a b and we're going to give it a vector of desired eigenvalues. Eigs. Okay, and so, let's do this. We knew that the system, if I just look at the eig of a, we know that there's this unstable eigenvalue. So let's just manually say I want my eigenvalues to be like negative one, negative 1.1, negative 1.2 and negative 1.3. So let's say I equals negative 1.1, negative 1.2, negative 1.3 and negative 1.4. So my desired eigenvalues, I want to cook up a k so that my closed loop system has those eigenvalues. Okay, and this seems like a lot to ask, right? I just pulled these out of a hat. They're stable. Eigenvalues should be great. I'm going to hit enter. Okay, so those are my I's. And now I'm going to type in k equals place ab I. It's that simple. In Matlab, it is so simple to cook up a controller k so that it has the poles, the eigenvalues where you want them to be. So that's my controller k. It's just a matrix. And now if I look at I's of a minus b times k, and I'm hoping that it's minus k and not plus k. We'll try this. Ooh, I of a minus b times k. Lo and behold, I get exactly the eigenvalues that I specified. Everyone follow that. Let me just. Let me expand this a little bit. Okay. So we said Eigs equals this. We specified the eigenvalues we wanted. And then all we did was we said k equals place. Abigail. This is a built in MATLAB function that tells me what is the controller k. This proportional feedback controller that will shape the closed loop eigenvalues to be the eig's that I specified. And this is that k. So now I just say I want to verify that this closed loop system does, in fact, have the right eigenvalues. So I'm going to do a minus b times k. And the closed loop system, a minus b times k does in fact, have these desired eigenvalues. So it's really that simple to place your eigenvalues where you want with closed loop feedback. If your system's controllable. If your system was not controllable, you wouldn't be able to do this. So we get this because our system's controllable. Because force on the cart is a good control that allows me the authority to stabilize this pendulum. Okay. Okay. Excellent. And so now once we have this, we can actually try to use this K matrix. So I'm just gonna type k again. So we can try to use this K gain matrix to actually stabilize the nonlinear system, which is really going to be fun. Okay, so I'm going to go back to my script. Okay. And I have a bunch of different poll locations that we're going to try, but we're not going to do that yet. So let me just comment those. Comment those out. Let me see if I can remember how to do this in Matlab. Okay, I'm going to comment those out. And I'm going to try a few different pole placements. A few different placements of the eigenvalues. Because we're going to play around. Like, where do I want to put the eigenvalues of this closed loop system? We're going to try a bunch of different ones. But for now, let's just try this one, -0.3 -0.4 -0.5 -0.6 more or less. I'm just pulling it out of a hat. Okay. So I do this place k equals place a, b p, where p is my list of eigenvalues. And then I'm going to essentially integrate the same vector field, that same cart penned vector field, except in the u position where the control is. Before I had zero, I had zero control. Now I'm going to plug in minus k times my state, minus a reference value. Okay? And so basically what I want is y my state. So I don't know why, but I'm calling this a vector y and dt of this is y of f of y and all of these become y's. Whatever. In Matlab, what I want is for my state, I want this to approach a reference value of 10 PI zero. Okay? And so what that means is I want this thing to walk over to x equals one and keep the pendulum in the vertical position. And with that k gain matrix, I should be able to do this because I've stabilized my system. Okay, so let's try this out. And again, I can just run this code. Sorry, I'm trying to move this up. And at the end of my code I'm going to draw the solution. So once I run this simulation, I'm going to plot the pendulum doing what I want it to do. Okay, so let's do poll place, cart, pen. That's my script. I'm going to hit enter. I'm going to hope for the best. Oh, it went off the screen, but then it came back. Okay, so let's see if I can do better. I think I need to integrate for longer. Let's try this one more time. It's not the most impressive. Okay, so it starts vertical, but a little ways off. It actually goes off screen, but then it very slowly comes back to where I told it. This reference value of one in the x position, zero velocity, PI in the vertical in the theta direction, zero velocity. So this pole placement did stabilize and move the system over, which is pretty cool. Okay. So it actually worked, but it took forever to walk over there. And I started at an initial condition, I think at x equals minus four or something like that. Let's see. Yeah. So my initial condition was x was minus three and it was a little bit off. Vertical PI plus 0.1. So let's say I want to make this more aggressive. I want to make it faster to move over to the right. So what would I do to these poles, these eigenvalues, I call them poles. People call them poles or eigenvalues. They're the same thing. What would I do to these to make them faster? So this thing is more aggressive? Well, I could move them farther into the left half plane. I could make them have a more negative real part that should make them faster and more aggressive. So let's try this one. Okay, so I'm just making them more negative, negative one, negative 1.1, negative 1.2, negative 1.3. So this is more aggressive eigenvalue placement. I should have faster, faster convergence. Let's try this out. Pole place cart penned I this thing's going to run very fast, and it goes right where it's supposed to. So it didn't go off screen. It didn't barely control. It did its control pretty fast, which is nice. That's what we wanted it to do. So moving the eigenvalues into the left half plane farther seemed to be a good thing. Okay, now could we just keep doing this indefinitely? What if we do, what if we make them more stable? Okay, so here I'm going to make, you know, minus two -2.1 and so on and so forth. So these are even more aggressive. Let's try this. Maybe this will just go right to the point and be good. Okay. It's really fast. Okay. Didn't take any time getting there. It was very aggressive. But you could also see it started getting pretty jerky. Right. It's probably pretty expensive. It's really cranking on these, these tires to get this thing moving over there. So if I don't have a big engine or big motor in here, I might not be able to realize this controller. I'm assuming if I plotted you, it would be really big. Okay. Because this thing's really hauling. And I can go even farther. I can go even farther. Let's keep going one more. One more level of aggressive. So I'm moving it even farther into the left half plane, and I'm going to decrease my time. I don't need to integrate to 30 anymore. And you can see super jerky. Okay. And I'm not going to show you this, but if I increase, if I move those poles any farther into the left half plane, the system actually breaks. It becomes unstable. The non linear dynamics are unstable. If I move my poles of the linearized system too far into the left half plane. So it's interesting, actually, we think of the eigenvalues being farther in the left half plane having a more negative real part as being more stable, what that really means is it's faster. Those dynamics are faster. This thing will approach its desired equilibrium faster, but it's actually less robust. The farther I push these eigenvalues into the left half plane. In a lot of cases, in this case, it becomes less robust. We're kind of on this knife's edge where we're really throwing the system around. And these nonlinear. The nonlinear system might not act exactly like the linear system where we built our controller. And so at some super aggressive control, it'll actually break. And so there, it turns out there's a sweet spot, an ideal set of eigenvalues that we can place for this system that will have the best trade off of performance of how fast the system moves to where it wants to move, and not spending too much control effort in doing so. Okay? And so that's what we're gonna work on next. It's called the linear quadratic regulator. Okay? This is LQR, and it's one of the most important results in optimal control theory. It essentially gives you the optimal gain matrix k, that balances how fast you stabilize your state and how much control energy you're expending to do so. So it's kind of a sweet spot of pole placement. And again, in Matlab, super simple, one line of Matlab code. Okay, so that's where we're going next.