LQR Control for an Inverted Pendulum on a Cart

From testwiki
Jump to navigation Jump to search

The purpose of this page is to show the derivation of the linearized dynamics for the highly non-linear cart-pole system and to propose an algorithm to calculate the optimal balancing input using a LQR controller. This system is classically studied in non-linear controls.

A LQR controller is used to stabilize the cart-pole system around its unstable equilibrium with the cart at the origin and the pole in its upright position.The cart-pole is described by the following dynamics:

(mc+mp)x¨+mplθ¨cosθmplθ˙2sinθ=u

mplx¨cosθ+mpl2θ¨+mpglsinθ=0

This may be put into standard form with q=[x,θ]T:

𝐇(𝐪)𝐪¨+𝐂(𝐪,𝐪˙)𝐪˙+𝐆(𝐪)=𝐁𝐮

Where the above matrices are defined as:

File:Matrix def.jpg

The linearized dynamics are found by performing a Taylor expansion around the fixed point of interest:

File:Linear matrix.jpg

With the linearized dynamics a stable LQR controller may be calculated using the cost function:

g(x,u)=(xTQx+uTRu)

k=0γkg(xk,uk)

The optimal solution to this cost function may be found using techniques in dynamic programming and is of the form:

𝐊i+1=(𝐑+γ𝐁T𝐏i𝐁)1𝐁T𝐏i𝐀

𝐏i+1=𝐐+𝐊i+1T𝐑𝐊i+1+γ(𝐀+𝐁𝐊)T𝐏i(𝐀+𝐁𝐊i+1)

The following code may be used in MATLAB to converge on the optimal gain matrix:

File:Lqr code.jpg

For the given system, the optimal gain matrix can be found to be:

𝐊=[21.13320.7430.2370.18]

With the Qand R weights as follows:

𝐐=[50000001000000000000],R=1

And the optimal control input can be calculated.

u(t)=𝐊x(t)

This policy can be empirically shown to balance the pendulum at its upright position:

File:Plot2.jpg