Cart pole Simulation
Solo project · Dynamics Modelling, Control systems, MuJoCo, Simulation
GitHub RepositoryObjective
Model and simulate a basic cart pole system in MuJoCo using 4 different controllers - PID, Pole Placement, LQR and MPC.
Dynamics Modelling
- Cart modelled as a capsule of size 0.08 x 0.25 m with mass 1 Kg.
- Pole modelled as a cylinder of size 0.03 x 1 m with mass 0.1 Kg.
- Equations of motions were derived by hand using lagrangian approach.
The generalized coordinates are -
$$ q = \begin{bmatrix} x \\ \theta \end{bmatrix} $$thus the equations of motion are -
$$ \begin{aligned} (M+m)\ddot{x} + ml\cos\theta\ddot{\theta} - ml\sin\theta\dot{\theta}^2 &= u, \\ ml\cos\theta\ddot{x} + ml^2\ddot{\theta} - mgl\sin\theta &= 0. \end{aligned} $$State space model
For controller design, the nonlinear dynamics were linearized around the upright equilibrium:
$$ \theta = 0,\qquad \dot{x}=0,\qquad \dot{\theta}=0 $$with the state vector
$$ \mathbf{x} = \begin{bmatrix} x & \dot{x} & \theta & \dot{\theta} \end{bmatrix}^{T}. $$The resulting linear state-space representation is
$$ \dot{\mathbf{x}} = A\mathbf{x}+B u $$where
$$ A = \begin{bmatrix} 0 & 1 & 0 & 0 \\ 0 & 0 & -0.7178 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 15.792 & 0 \end{bmatrix} $$and
$$ B = \begin{bmatrix} 0 \\ 0.9756 \\ 0 \\ -1.4634 \end{bmatrix}. $$
Model in MuJoCo
Controller
Proportional-Integral-Derivative
A simple cascaded PID controller was built to stabilize the pole in the upright position and at the same time keep the cart position fixed. Although this cascaded PID works well in stabilizing the pole, the cart position keeps oscillating and never settles.

PID block diagram
Pole Placement
The dynamics equations were linearized about the upright position and converted to state space form. Now assuming the control input to be u = -kx, where x is the state vector, it is possible to find the vector K given the desired location of closed loop poles. This was implemented using the scipy’s place_poles function. It is observed that both the pole position and cart position is stabilized as per the chosen closed loop poles.
Linear-Quadratic-Regulator
The same linearized state space model was used to find the optimal values for the K vector which minimizes a quadratic cost on the state and control effort.The Q matrix is used to decide the penalty on the states and R matrix for the control effort. scipy’s solve_continuous_are function is used to solve the continuous time riccati equation to obtain the optimum k vector. It is observed that the both cart and pole position is stabilized.
Comparison

Controller Comparison
| Metric | PID | Pole Placement | LQR |
|---|---|---|---|
| ROA | 30° | 23° | 33° |
| x settling time (s) | ∞ | 3.59 | 4.70 |
| θ settling time (s) | 2.96 | 2.24 | 2.33 |
| Peak force (N) | 4.74 | 2.83 | 1.66 |
| Total Control Effort | 32.56 | 22.83 | 19.7 |
Region of Attraction (max angle recovered)
The initial velocities were set to 0 and the initial angle (calculated from the upright position with respect to vertical) was changed until the controller could not stabilize.
Peak force (Impulse disturbance)
A force impulse of magnitude 15 N for 0.1 s duration was applied to the pole and the peak control force was measured.
Control effort
Under the same impulse disturbance, the total control effort was calculated by summing the square of the control input multiplied with timestep. The total control effort is defined as
\[ J = \sum_{k=0}^{N} u_k^2 \, \Delta t \]where \(u_k\) is the control input (force) at timestep \(k\) and \(\Delta t\) is the simulation timestep.
Settling time
Under the same impulse disturbance, the settling time for both the cart position and pole angle was calculated. The tolerance band for the position was chosen as 0.025 m and for the pole angle it was chosen as 0.1 rad.