In this dynamics tutorial, we explain how to derive the equations of motion (model) of a pendulum on a cart. We use Lagrange’s equations to derive the model. For brevity, in this tutorial, we call our system the cart-pendulum system. The YouTube tutorial accompanying this webpage tutorial is given below.
Before we start with derivations, let us first explain the main motivation for deriving the model of the cart-pendulum system. First of all, the cart-pendulum system is a classical nonlinear dynamical system that is often used to benchmark different linear and nonlinear estimation and control methods. Also, it is often used in the machine learning community to benchmark different machine learning and reinforcement learning algorithms. Finally, the cart-pendulum system is a starting point for modeling a number of real systems, such as self-balancing scooters (segway vehicles), rockets, bipedal robots, human biomechanics, sloshing of fuel in spacecraft, etc.
In this tutorial, we derive an equation of motion of the cart-pendulum system. In the second part of this tutorial, given here, we explain how to automatically derive a state-space model of this system in Python and how to simulate the state-space model in Python. In the third part of this tutorial, given here, we explain how to use the simulated state-space trajectories to animate the motion of the system by using Python’s Pygame library.
The YouTube video of the second tutorial part:
The YouTube video of the third tutorial part:
Derivation of Equation of Motion of Cart-Pendulum Mechanical System
Consider the figure shown below.
The system consists of a cart with a mass of
To model the system, we first need to select the so-called generalized coordinates. The generalized coordinates are usually selected as a set of independent coordinates or variables that uniquely define the configuration of the system. In our case, these generalized coordinates are
To derive Langrange’s equations, we first need to derive the expressions for the kinetic and potential energies of the system. Consequently, the next step is to derive the expressions for the kinetic and potential energy of the system in terms
(1)
where
(2)
where the magnitude of the velocity of the cart is denoted by
(3)
where
(4)
where
(5)
where
(6)
To compute these velocity components, we need to find the expressions for
(7)
By differentiating the previous expressions with respect to time, we obtain
(8)
By substituting (6) and (8) in (5) and later on in (4), we obtain
(9)
By combining (9) and (2), we obtain the final expression for the kinetic energy
(10)
The potential energy of the system is the potential energy of the point mass that is defined by
(11)
where
Next, we define Lagrange’s equations. In our case, Lagrange’s equations are defined by
(12)
where
(13)
By substituting (10) and (11) in (13), we obtain the final expression for the Lagrangian
(14)
The last equation defines the Lagrangian of the system. Next, in order to derive the equations of motion, we need to compute the derivatives in (12). We have
(15)
(16)
(17)
By substituting (16) and (17) in the first equation of (12), we obtain the first equation of motion
(18)
To obtain the second equation of motion, we compute the following partial derivatives
(19)
(20)
(21)
By substituting (20) and (21) in the second equation of (12), we obtain
(22)
From the last equation, we obtain
(23)
By dividing the last equation with
(24)
The two equations of motion are summarized below for completeness
(25)