This is the part 6 in a series of blog posts devoted to the classic control problem of an inverted pendulum on a driven cart. This series is still in production (Nov. 2020) but when finished they will be structured as follows.
- Derivation of Simple Pendulum (Python Simulation) - link
- Building a Physical Inverted Pendulum
- Controlling Inverted Pendulum with PID controller
- State Space Model of Inverted Pendulum - link
- Designing the Optimal Controller for an Inverted Pendulum (LQR control) - link
- Simulating an Inverted Pendulum with an LQR Controller (Python) - link
- Deploying an LQR Controller on a Physical Inverted Pendulum
The associated code can be found on GitHub and a video series on YouTube (not currently released as of Nov. 2020). While this project aims to be encompassing and self contained, it does assume familiarity with classical mechanics, linear algebra, differential equations, some control theory, and programming (Python).
In the previous 2 blog posts I have explored the state space model of an inverted pendulum and derived the optimal LQR controller for stabilizing the system. Before going to the trouble of building a physical inverted pendulum lets first verify that the controller works as expected in simulation.
If you wish to dive right into the code it can be found on my GitHub
To provide an explanation of the code line by line is difficult outside of video format. As such I will touch on the highlights. There are two files: variables.py and simulation.py. If you have read the previous 2 blog posts the variables.py file should be fairly clear, I have just defined all the relevant variables that have been previously discussed. Feel free to adjust the mass of the cart, rod, damping etc. You can also experiment with different values in the Q and R matrices, as discussed in the previous blog post.
The file simulation.py builds off the existing code from part one of this series, the animate() function is largely unchanged. The primary difference is that the non-linear equation for the simple pendulum has been replaced by the function StateSpace(t, i). This function evaluates the non-linear inverted pendulum with the applied LQR input control.