Cart Pole Swing up Gif

Swing-Up of an Inverted Pendulum on a Cart

Inverted Pendulum Recap

In this post, we’re going to talk a little about how to balance an inverted pendulum on a cart. This is the second post in a 3 part series about balancing an inverted pendulum

  1. Part 1 – Balancing an inverted pendulum
  2. Part 2 – Swing-Up of an Inverted Pendulum on a Cart
  3. Part 3 – Kapitza’s Pendulum

Equations of Motion

Let’s start as we often do, by draw a diagram.

We’re going to acquire the equations of motion through the Lagrangian approach. This requires us to break up the problem and find the kinetic and potential energy for both the cart and the pole. First, let us start with the cart because it’s easiest. The cart can only move horizontally so its potential energy is 0

V_{cart}=0

Its kinetic energy is also simple to write out

T_{cart}=\frac{1}{2}M\dot{x}^2

where M is the carts mass. Turning to the pole, it’s potential energy is 

V_{pole}= m g l \cos(\theta)

where m,g, and l are the mass at the end of the pole, the gravitational force, and the poles lengths. Unfortunately, the poles kinetic energy is a bit more complex as it interacts with the cart. 

T_{pole}= \frac{m\dot{\theta}^2l^2+(2\cos(\theta)\dot{\theta}\dot{x}l + \dot{x}^2)}{2}

We can then combine these to form the Lagrangian

L= T_{cart}+T_{pole}-V_{cart}-V_{pole}

Using the following Euler-Lagrange equation,

\frac{d}{dt}\frac{\partial L}{\partial\dot{q_i}}=\frac{\partial L}{\partial q_i}

and then after an annoying amount of algebra, we arrive at the final set of equations

\ddot{x}=\frac{F+m\sin(\theta)(g\cos(\theta)+l\dot{\theta}^2)}{M+m\sin(\theta)^2}

\ddot{\theta}= - \frac{F\cos(\theta)+(M+m)g\sin(\theta)+lm\dot{\theta}^2\sin(\theta)\cos(\theta)}{l(M+m\sin(\theta)^2)}

Where F is the force on the cart.

How are we Going to Control it?

In past posts on control, we’ve focused on looking at guiding our object to a destination from the entire phase space. In common terms, this means we could start the pendulum with any angular position and velocity and using the pendulums current state, compute a control that would make it stand upright. Unfortunately, now we have a much more complicated system and finding a global controller is much harder. Instead, let us make a simple assumption; we’re starting with the cart at rest, and the pendulum at rest hanging down. Now, we only need to find the control inputs that guide the pendulum to stand upright solely from the stated starting position, and not from any initial condition. By changing from a global solution to a single initial condition we have gone from designing a controller/policy to designing a trajectory.

The Lede

Cart Pole Swing up Gif

Trajectory design, while simple to say, actually covers a wide range of topics and techniques and I couldn’t figure out how to fit them, the methods, as well as how I used them here in this post, so instead I’m planning on having separate posts on things like collocation and non-linear programming. Once we have a foundation built up, I plan on returning to showing how to solve the swing-up problem. If you want a jump start on the topics I highly recommend this book on trajectory design.

Want More Gereshes?

If you want to receive new Gereshes blog post directly to your email when they come out, you can sign up for that here!

Don’t want another email? That’s ok, Gereshes also has a twitter account and subreddit!