You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Matlab code for simulating and controling the inverted pendulum on a cart problem.
Cart-Pole Control Problem
An inverted pendulum is a classic problem in control theory and robotics. It involves a
rigid pendulum that is mounted on a cart, where the objective is to keep the pendulum
balanced in the upright position by moving the cart back and forth. This
problem is challenging because it requires precise control of the cart’s position and velocity
to maintain the pendulum’s equilibrium.
The problem can be solved using various control strategies. The classical control approach
involves designing a controller based on mathematical models of the system, while more modern
control techniques use advanced control algorithms to achieve better performance. Optimal control methods aim to minimize a certain cost function while maintaining the pendulum’s
balance.
Mathematical Statement
Given the Lagrangian of the system, $L(x,x',\theta,\theta')$, which is given by the difference of the kinnetic and potential energy of the system:
Now we have a system of second order ordinary differential equations describing the problem.
Numerical Discretization Using the $\text{4}^{th}$ Order Runge-Kutta Method
For the numerical discretization we will use the $\text{4}^{th}$ order Runge-Kutta method. For this purpose we define the following change of variable:
The update step for $x$ is given by $x_{i+1} = x_i + \frac{\Delta t}{6} \left( K_{1x} + 2K_{2x} + 2K_{3x} + K_{4x}\right)$.
The aproximation functions and update step of the remaining variables are analogous.
The following images are results of simulations with timestep $\Delta t = 0.005$ in the time interval $[0, 10]$.
Results of the Simulation with initial conditions $(x,x',\theta,\theta') = (0,0,0.15,0)$, of variable $x$ (right), and $\theta$ (left)
Results of the Simulation with initial conditions $(x,x',\theta,\theta') = (0,0.1,1,0)$, of variable $x$ (right), and $\theta$ (left)
Linearization
Next, we can consider the control problem associated with the pendulum. In this case, we
assume that our control u is an external force applied in the x direction on the cart. In
this case, the equations would be modified using the second law of Newton, and we obtain:
If we now introduce the vector of variables $Y = (y_1, y_2, y_3, y_4)^T
= (x, x'
, \theta, \theta'
)$, the previous equation can be written as a nonlinear ordinary differential system:
To solve this nonlinear problem, we can use different methods, the most common ones
involve linearization. Thus, if we linearize the previous system around 0 (since this is our objective
equilibrium point), we can rewrite the system as follows:
$$\frac{d}{dt}Y = F(0) + DF(0)Y + G(0)u$$
The Jacobian of $F$ at 0 $(DF(0))$ and the vector $G(0)$ are given by:
We can find an open loop control using a gradient method. For this purpose, we will find the adjoint equation and the gradient.
The functional $J$ can be written as $J(y,u) = \frac{1}{2} \left< Sy(T),y(T) \right> + \frac{1}{2}\int_{0}^{T} \left< Py(s),y(s) \right>+ \left< Qu(s),u(s) \right> ds$, where: