The system we are going to describe consists of an inverted pendulum mounted on a motorized cart. This is very similar when you want to try keep the broom upwards on the tip of your finger. But why is it challenging?
The problem is inherently unstable. Like our broomstick, once it starts to move away from its equilibrium position (straight up), gravity works to accelerate its fall. The system is also non-linear. The relation between inputs (force we exert on the broomstick) and outputs (the broomstick’s angle) isn’t directly proportional. That makes predicting and controlling its behavior a beautiful puzzle. This system is a classic example of a nonlinear and inherently unstable setup, but it’s also simple enough to understand and simulate.
To make our life easier, let’s consider a simplified two-dimensional case. This system consists of two parts: the cart and the pendulum. The cart can only move horizontally, and the pendulum can freely swing back and forth. We can control the system by applying a horizontal force, \(F\), on the cart. We get feedback from the system in the form of three observations: the angle of the pendulum, the angular velocity and its angular acceleration. Using the equations of motions can help us understand how to control and balance such a system.
Forces on the cart. Red denotes forces exerted on the cart, while green forces denote forces exerted on the pendulum. ‘x’ is the position of the cart.
Equations of motion
Cart’s Motion
Let’s start with the motion of the cart. When we push the cart, it experiences several forces: its own inertia due to its mass “M,” a damping factor “b” related to friction, and the force “N” coming from the pendulum.
\[\begin{equation}
M\ddot{x}+b\dot{x}+N = F
\end{equation}\]
\(M\) is the mass of the cart.
\(b\) friction coefficient.
\(\dot{x}\) is the velocity of the cart.
\(\ddot{x}\) is the acceleration of the cart.
\(N\) is the reaction force from the pendulum.
\(F\) is the force we apply to the cart.
The term \(M\ddot{x}\) represents the force due to the cart’s acceleration. Essentially, Newton’s second law tells us that the force exerted on an object is directly proportional to its mass and acceleration.
The next term, \(b\dot{x}\), accounts for the force opposing the cart’s movement, primarily due to friction. The friction coefficient, \(b\), quantifies the amount of resistive force the cart faces, which increases with the cart’s velocity \(\dot{x}\). So, the faster the cart is moving, the greater the friction force it encounters.
Lastly, \(N\) is the reaction force exerted by the pendulum on the cart. As the pendulum swings, it applies a force on the cart, which the cart must counteract to maintain balance. We don’t know \(N\) yet, that’s why we just noted it with an arbitrarily chosen letter. In the following section we are determining the value \(N\) takes.
Note that we only summed the forces on the horizontal axis. You can also sum the forces in the vertical direction for the cart, but since the cart is unable to move vertically, this is unnecessary.
Pendulum’s Motion
When discussing the motion of the pendulum, it’s crucial to consider both the horizontal and vertical forces acting on it. This is because the pendulum, unlike the cart, can move in both directions due to its rotational nature.
Horizontal
The horizontal forces acting on the pendulum are captured by the equation:
\[\begin{equation}
N = m\ddot{x}+ml\ddot{\theta}\cos\theta-ml\dot{\theta}^2\sin\theta
\end{equation}\]
\(m\) is the mass of the pendulum.
\(l\) is the length of the pendulum.
\(\theta\) is the angle of the pendulum.
\(\dot{\theta}\) is the angular velocity
\(\ddot{\theta}\) is the angular acceleration.
\(N\) is the pendulum’s reaction force.
This equation essentially describes how the pendulum’s motion is influenced by the cart’s movement and its own inertia. The term \(m\ddot{x}\) is the force exerted on the pendulum due to the acceleration of the cart. The term \(ml\ddot{\theta}\cos\theta\) describes the force on the pendulum due to its own angular acceleration, while −\(ml\dot{\theta}^2\sin\theta\) accounts for the centrifugal force acting on the pendulum as it swings.
Now, we can substitute this back in to the cart’s equation, which gives us: \[\begin{equation}
(M+m)\ddot{x}+b\dot{x}+ml\ddot{\theta}\cos\theta-ml\dot{\theta}^2\sin\theta=F
\label{eq:horizontal} \tag{1}
\end{equation}\]
Vertical
So far we only dealt with the horizontal forces. To get the vertical forces for this system, we sum the forces perpendicular to the pendulum:
\(P\) is the vertical component the cart is pushing back on the pendulum.
\(g\) is the acceleration due to gravity.
In this equation, \(P\) is the vertical component of the reaction force with which the cart pushes back on the pendulum. The gravitational force acting on the pendulum is represented by \(mg\sin\theta\), which tries to pull the pendulum downward. The right side of the equation relates to the forces due to the pendulum’s angular acceleration and the cart’s linear acceleration.
We need to get rid of \(P\) and \(N\). To do that we sum the moments about the centroid of the pendulum to get the following equation: \[\begin{equation}
-Pl\sin\theta-Nl\cos\theta=I\ddot{\theta}
\end{equation}\]
\(I\) is the mass moment of inertia of the pendulum.
Combining these last two expressions, we get the vertical forces of the system: \[\begin{equation}
(I+ml^2)\ddot{\theta}+mgl\sin\theta=-ml\ddot{x}\cos\theta
\label{eq:vertical} \tag{2}
\end{equation}\]
In our case \(I=m(\frac{l}{2})^2\), (you can look up the moment of inertia for different shaped objects).
Matrix Form of the System
From Equation \(\eqref{eq:horizontal}\) and Equation \(\eqref{eq:vertical}\) we can write the final equations of motion:
This is in the form \(As=b\), where we are looking for \(s\), which describes the current state of the system, such as the cart’s acceleration and pendulum’s angular acceleration. If we can solve the equation for \(s\), we can get the accelerations and run a simulation. This can be done, by getting the inverse of \(A\), such as: \(s=b^T*{(A^T)}^{-1}\). After we get \(s\), we can use Euler’s method to calculate the position of the cart and the pendulum.
Running a Simulation Using Euler’s Method:
The beauty of Euler’s method is its simplicity. We’re essentially predicting the future (next state of the system) based on the present (current state) and a tiny step forward in time. It is a numerical technique to approximate a solution, where the exact solution can’t be calculated or just too hard to calculate. Formally, given an initial state, the method estimates the next state by taking a small step based on the rate of change.
However, a word of caution: Euler’s method isn’t perfect. The smaller the step, the more accurate our predictions, but it also means more computational work. We can spead up the simulation by taking larger steps, but in turn this leads to inaccuracies.
In our pendulum+cart case, we actually never determined the exact position of the cart for a given time \(t\). We have no clue where is the pendulum e.g. after 12 seconds, by substituting the values back into the equation we derived above. We only described how the system changes from one moment to another. However, if we know how the system changes, we can still calculate how it will move around as time passes. If we have an initial state and by only doing very small steps (we can do this since we know how the system changes), we can approximate where the cart and pendulum will be as time passes.
So in a more formal way:
Start with initial conditions for \(x\), \(\dot{x}\), \(\theta\), and \(\dot{\theta}\).
Calculate accelerations \(\ddot{x}\) and \(\ddot{\theta}\) using the matrix equations.
Englert, Brunó B. (Jul 2023). 2D Physics of The Inverted Pendulum on a Cart. https://englert.ai/posts/001_inverted_pendulum/englert_ai_1_cart_physics.html.
Or
@article{englert2023physicscartpole, title ="2D Physics of The Inverted Pendulum on a Cart", author ="Englert, Brunó B.", journal ="englert.ai", year ="2023", month ="Jul", url ="https://englert.ai/posts/001_inverted_pendulum/englert_ai_1_cart_physics.html"}