Balancing an inverted pendulum
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A pole on a cart falls over in under a second if nobody acts. Model the rig, linearise it about upright, measure its four-number state and place the closed-loop poles with state feedback u = −Kx. The figure runs the full nonlinear simulation: the same 5.7° lean that falls flat in 0.76 s is caught and the cart brought home.
A pole on a cart
A 0.5 m pole stands on a free pivot on a cart. Our job: keep it upright by pushing the cart left and right. To start, it leans 0.1 rad, about 5.7°, to the right.
cart M = 1 kg on a straight track pole a light rod, l = 0.5 m bob m = 0.1 kg at the top pivot free: nothing holds the pole up lean θ = 0.1 rad (5.7°), + = to the right The only thing we can touch is the cart.
اقرأ الدرس خطوة بخطوة
A pole on a cart
A 0.5 m pole stands on a free pivot on a cart. Our job: keep it upright by pushing the cart left and right. To start, it leans 0.1 rad, about 5.7°, to the right.
cart M = 1 kg on a straight track pole a light rod, l = 0.5 m bob m = 0.1 kg at the top pivot free: nothing holds the pole up lean θ = 0.1 rad (5.7°), + = to the right The only thing we can touch is the cart.Upright is unstable
With no control, gravity pulls the bob further the more it leans. The lean passes 30° after 0.51 s, and the pole lies flat after 0.76 s. Upright is an unstable equilibrium: balanced, but any nudge grows.
u = 0 N (motor off), start θ = 5.7° θ = 30° after 0.51 s θ = 90° after 0.76 s (flat) equilibrium a pose where nothing changes stable a nudge dies away (a hanging pendulum) unstable a nudge grows (this one)Small leans, straight lines
Near upright, sin θ ≈ θ (0.17% off at 0.1 rad). The equations turn linear: θ̈ = 21.582·θ − 2u. With u = 0, one system pole (a growth rate, not the rod) is +4.65 /s. Positive means growth: the lean doubles every 0.15 s.
full: l·θ̈ = g·sin θ − ẍ·cos θ (M + m)ẍ + m·l·θ̈·cos θ − m·l·θ̇²·sin θ = u linear: ẍ = u − 0.981·θ θ̈ = 21.582·θ − 2·u open-loop poles 0, 0, ±√21.582 = ±4.65 rad/s +4.65 > 0 → e^(4.65·t) grows: doubles every ln 2 / 4.65 = 0.15 sThe only handle: the cart
A motor drives the cart along the track with a belt, pushing with force u. Push the cart right and the pivot moves under the bob, swinging the pole back to the left. The cart is the only handle we have.
u > 0 push right: ẍ = u − 0.981·θ cart speeds up θ̈ = 21.582·θ − 2·u lean falls Like balancing a broom on your hand: move your hand towards the side it is falling to, and get under it.Four numbers say everything
Two encoders count how far the motor and the pivot have turned, giving the cart position x and the lean θ. Their rates give the speeds v and ω. These four numbers are the state: given the force u, they fix everything that happens next.
state s = [x, v, θ, ω] x cart position 0 m v cart speed 0 m/s θ lean 0.1 rad ω lean rate 0 rad/s ṡ = A·s + B·u A = [[0, 1, 0, 0], [0, 0, −0.981, 0], [0, 0, 0, 1], [0, 0, 21.582, 0]] B = [0, 1, 0, −2]ᵀChoose where the poles go
Feed all four back: u = −K·s. Each gain moves the closed-loop poles. We choose −2, −3, −4 and −5, all in the left half, and match coefficients to get K. This is pole placement.
det(sI − A + BK) = s⁴ + (k2 − 2k4)s³ + (k1 − 2k3 − 21.582)s² − 19.62·k2·s − 19.62·k1 want (s+2)(s+3)(s+4)(s+5) = s⁴ + 14s³ + 71s² + 154s + 120 k1 = −120/19.62 = -6.116 k2 = −154/19.62 = -7.849 k3 = -49.349 k4 = -10.925 # python-control: K = control.place(A, B, [-2, -3, -4, -5]) # works because the system is controllable (rank 4)The rule in one line
At the start only θ is non-zero, so u = 49.3 × 0.1 = 4.93 N: push right, towards the lean. The controller reads the state and recomputes u every millisecond.
u = −(k1·x + k2·v + k3·θ + k4·ω) = −(−6.1·0 + −7.8·0 + −49.3·0.1 + −10.9·0) = +4.93 N (push right) The big angle gain does most of the work; the small position gains bring the cart home afterwards.Catch the lean
The cart darts right under the bob. The lean crosses zero after 0.28 s, swings to −2.5° at 0.6 s, then settles. The same start that fell over in 0.76 s now stands.
# balance the pole: nonlinear cart-pole, RK4, 1 ms import math M, m, l, g, dt = 1.0, 0.1, 0.5, 9.81, 0.001 k = [-6.1162079510703364, -7.8491335372069315, -49.349103975535172, -10.924566768603466] def f(s, u): x, v, th, w = s S, C = math.sin(th), math.cos(th) xdd = (u + m*S*(l*w*w - g*C)) / (M + m*S*S) return [v, xdd, w, (g*S - xdd*C) / l] s = [0.0, 0.0, 0.1, 0.0] for n in range(1000): # 1 s u = -(k[0]*s[0] + k[1]*s[1] + k[2]*s[2] + k[3]*s[3]) k1 = f(s, u) k2 = f([a + dt/2*b for a, b in zip(s, k1)], u) k3 = f([a + dt/2*b for a, b in zip(s, k2)], u) k4 = f([a + dt*b for a, b in zip(s, k3)], u) s = [a + dt/6*(p + 2*q + 2*r + z) for a, p, q, r, z in zip(s, k1, k2, k3, k4)] print(round(math.degrees(s[2]), 1)) # -0.9Bring the cart home
Catching the pole carried the cart 14 cm to the right (most at 0.7 s). The position gains then ease it back, slowly, because −2 is the slowest pole we chose. It is back within 1 mm after 3.8 s.
max cart travel 14 cm at 0.7 s |x| < 1 mm from t = 3.8 s on slowest pole −2 → e^(−2t): the last bit of error halves every 0.35 s. Faster poles bring it home sooner but ask the motor for bigger forces.What the model leaves out
The gains came from the linear model, yet they balance the full nonlinear one here. That holds only for small leans. Real rigs also have motor force limits, friction, noise and unmeasured speeds, which an observer can estimate.
designed on the linear model tested on the full nonlinear one peak force 4.93 N at t = 0 left out: motor saturation (a big lean needs more force than the motor has) track friction, belt stretch encoder noise; v and ω estimated by differencing or an observer
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01The linearised pendulum (motor off) has poles at 0, 0 and ±4.65 rad/s. What does the pole at +4.65 tell you?
A real pole in the right half-plane gives a mode that grows exponentially. Its growing part doubles every ln 2 / 4.65 ≈ 0.15 s.
02Which four numbers make up the state in this lesson?
Position and velocity of the cart plus angle and angular velocity of the pole fix how the rig moves next for any force u. Masses and length are fixed parameters, and the gains belong to the controller.
03At the start only θ = 0.1 rad is non-zero and k3 = −49.3. With u = −K·s, what force does the motor apply?
u = −(−49.3 × 0.1) = +4.93 N. The cart is pushed towards the side the pole leans to, moving the pivot back under the bob.
04Why could we place all four closed-loop poles wherever we wanted?
Pole placement by state feedback works for any set of poles exactly when the pair (A, B) is controllable. A single force on the cart reaches all four states here.
05The gains were designed on the linear model. What is a fair limit of that design?
In the figure the linear design balances the full nonlinear model from 5.7°. For large leans sin θ ≈ θ fails, and a real motor saturates; friction, noise and estimated speeds matter too.
المسرد
- Inverted pendulum
- A pole balanced upright on a moving base. Upright is an equilibrium, but an unstable one: any small lean grows.
- Linearisation
- Replacing a nonlinear equation by its straight-line approximation near an operating point, here sin θ ≈ θ and cos θ ≈ 1 near upright.
- State
- The smallest set of numbers that, with the input, fixes all future motion. Here [x, v, θ, ω].
- State feedback
- A controller that uses every state: u = −K·s, one gain per state.
- Pole (of a system)
- A root of the characteristic polynomial, that is an eigenvalue of A. Its real part sets whether a motion grows (> 0) or dies away (< 0). Not the same as the physical pole (the rod).
- Right half-plane
- The part of the s-plane where poles have a positive real part. Poles there grow, so the system is unstable.
- Pole placement
- Choosing K so the closed-loop poles (eigenvalues of A − BK) sit where you want them.
- Controllable
- A system whose input can steer every state. Then state feedback can put the poles anywhere.
الملاحظات والمصادر
- The rig is a model with illustrative numbers: a 1 kg cart, a 0.1 kg bob on a light 0.5 m pole, no friction. Every curve and number in the figure is simulated from the full nonlinear equations with a 1 ms time step, with the controller updating once per step.
- A pole of a system is a root of its characteristic polynomial (an eigenvalue of A). A pole with a positive real part sits in the right half-plane and grows; a negative one decays. The lesson "Poles and zeros on the s-plane" covers this.
- The gains were found from the linear model (sin θ ≈ θ, cos θ ≈ 1). Linearisation is the subject of the lesson "Linearising a nonlinear system"; the matrices and the state are those of "State-space models".
- Pole placement works only if the system is controllable: the matrix [B, AB, A²B, A³B] must have full rank, here 4. The lessons "Controllability and observability" and "State feedback and pole placement" explain why. In python-control, control.place(A, B, p) returns K such that A − BK has the eigenvalues p.
- Encoders measure x and θ directly. The speeds v and ω must be estimated, by differencing the encoder counts or with an observer, as in "Estimating hidden states with an observer".
- The Python on the step "Catch the lean" is plain Python 3 with the math module. It runs the same RK4 integration as the figure and prints the lean after 1 s.
- K. Ogata, Modern Control Engineering, 5th ed., Pearson (ch. 3 modelling; ch. 10 Control systems design in state space)
- G. F. Franklin, J. D. Powell, A. Emami-Naeini, Feedback Control of Dynamic Systems, 8th ed., Pearson (ch. 7 State-space design)
- N. S. Nise, Control Systems Engineering, 8th ed., Wiley (design via state space)
- R. Tedrake, Underactuated Robotics, MIT course notes: Acrobots, Cart-Poles, and Quadrotors (cart-pole equations of motion)
- MIT OpenCourseWare 16.06 Principles of Automatic Control (S. Hall)
- MIT OpenCourseWare 6.302 Feedback Systems (Roberge, Dawson, Lundberg)
- python-control documentation: control.place
اختبار يتاح بعد الخطوة الأخيرة.