Project · lecture note · control systems
Two rods on a cart: holding up what wants to fall
Numa Koudsie · for readers who know Newton’s laws and a little calculus
01Balancing from below
Stand a broom on the palm of your hand. It starts to tip, and without thinking you move your hand under it, in the direction it is falling. Your hand cannot pull the broom up; it can only move the bottom, and by moving the bottom it turns the broom back.
That is the whole idea of this page. The cart is the hand, the rail is the only direction it can move, and the rods are the broom. With one rod the problem is the classic cart-pole; with two rods hinged one on the other it is the double inverted pendulum on a cart, a standard test bench of control engineering because it is unstable, nonlinear and has more things to hold up (two angles) than things to push with (one force) [1, 2, 3].
The same problem appears outside the laboratory. A two-wheeled self-balancing vehicle is an inverted pendulum whose wheels play the cart: Grasser and colleagues built and balanced one at EPFL with state feedback [4]. A person standing still is closer to the double version: measurements of how people push on the floor are explained only by two links, the legs turning at the ankle and the trunk at the hip, and not by one [5]. The difference is that a person drives the joints directly with muscles, while here the joints are free and only the cart is driven. That makes our problem the harder one.
02The machine
Figure 1 shows the parts. The cart, of mass \(m_0\), rolls on a straight horizontal rail; its position along the rail is \(x\). A motor pushes it with a horizontal force \(u\), positive to the right, never larger in size than \(u_\text{max}\). Through the middle of the cart runs a slot, open at the top and bottom, and the cart rides on two rails, one on each side of it. Across the top of the slot sits a hinge, a pin through a bearing, which lets the lower rod turn freely in the plane of the picture and in no other way. The lower rod is a uniform bar of mass \(m_1\) and length \(L_1\). At its top end a second free hinge carries the upper rod, a uniform bar of mass \(m_2\) and length \(L_2\). No motor sits in either hinge.
How it moves with the motor off. Nothing pushes the cart sideways from outside, so the total horizontal momentum of cart and rods stays what it was. If the rods start upright and at rest, they stay there only in principle: the smallest lean grows, the rods fold and fall, and as their centres of mass swing to one side the cart rolls the other way to keep the total momentum at zero. Press Off and watch the cart recoil.
How it moves with the motor on. The force \(u\) accelerates the cart. Through the lower hinge the cart then drags the bottom of the lower rod with it, and a rod whose bottom is pulled to the right turns to the left about its centre, exactly as the broom does when you move your hand. The upper rod feels this only through the hinge between the rods. So one force at the bottom reaches both angles, but indirectly and differently; finding the push that brings both back at once is the controller’s job.
| Symbol | Meaning | Default (Bogdanov [1]) |
|---|---|---|
| \(m_0\) | cart mass | 1.5 kg |
| \(m_1, L_1\) | lower rod mass and length | 0.5 kg, 0.5 m |
| \(m_2, L_2\) | upper rod mass and length | 0.75 kg, 0.75 m |
| \(g\) | gravitational acceleration | 9.81 m/s² |
| \(u, u_\text{max}\) | motor force on the cart, and its limit | slider, 60 N |
03Positions and energy
Three numbers fix where everything is: \(x\), \(\theta_1\) and \(\theta_2\). We collect them as \(\boldsymbol\theta = (x, \theta_1, \theta_2)\). Put the origin of height at the lower hinge, \(y\) upward. The lower hinge is at \((x, 0)\). A rod leaning by \(\theta\) from the upward vertical has its far end displaced by \(L\sin\theta\) sideways and \(L\cos\theta\) up. The centre of mass of a uniform rod is at its middle, so
\[ \begin{aligned} x_1 &= x + \tfrac12 L_1\sin\theta_1, & y_1 &= \tfrac12 L_1\cos\theta_1, \\ x_2 &= x + L_1\sin\theta_1 + \tfrac12 L_2\sin\theta_2, & y_2 &= L_1\cos\theta_1 + \tfrac12 L_2\cos\theta_2 . \end{aligned} \tag{1} \]3.1Velocities
Differentiate (1) in time. Only \(x\) and the angles change; by the chain rule \(\sin\theta\) changes at the rate \(\dot\theta\cos\theta\) and \(\cos\theta\) at \(-\dot\theta\sin\theta\):
\[ \begin{aligned} \dot x_1 &= \dot x + \tfrac12 L_1\dot\theta_1\cos\theta_1, & \dot y_1 &= -\tfrac12 L_1\dot\theta_1\sin\theta_1, \\ \dot x_2 &= \dot x + L_1\dot\theta_1\cos\theta_1 + \tfrac12 L_2\dot\theta_2\cos\theta_2, & \dot y_2 &= -L_1\dot\theta_1\sin\theta_1 - \tfrac12 L_2\dot\theta_2\sin\theta_2 . \end{aligned} \tag{2} \]Square and add, using \(\cos^2+\sin^2 = 1\) and, for the mixed term of the upper rod, \(\cos a\cos b + \sin a\sin b = \cos(a-b)\):
\[ \begin{aligned} v_1^2 &= \dot x^2 + L_1\dot x\dot\theta_1\cos\theta_1 + \tfrac14 L_1^2\dot\theta_1^2, \\ v_2^2 &= \dot x^2 + L_1^2\dot\theta_1^2 + \tfrac14 L_2^2\dot\theta_2^2 + 2L_1\dot x\dot\theta_1\cos\theta_1 + L_2\dot x\dot\theta_2\cos\theta_2 + L_1L_2\dot\theta_1\dot\theta_2\cos(\theta_1-\theta_2). \end{aligned} \tag{3} \]3.2Kinetic energy
A rod that moves and turns carries two kinds of kinetic energy (König’s theorem): that of its centre of mass, \(\tfrac12 m v^2\), and that of turning about the centre of mass, \(\tfrac12 I\dot\theta^2\). For a uniform rod of length \(L\) the moment of inertia about its middle is \(I = \int_{-L/2}^{L/2} (m/L)\,s^2\,\mathrm ds = mL^2/12\). The cart only slides. So
\[ T = \tfrac12 m_0\dot x^2 + \tfrac12 m_1 v_1^2 + \tfrac1{24} m_1L_1^2\dot\theta_1^2 + \tfrac12 m_2 v_2^2 + \tfrac1{24} m_2 L_2^2\dot\theta_2^2 . \tag{4} \]Insert (3) and collect by products of velocities. The two rotational pieces combine with the \(\tfrac14 L^2\) pieces of (3): \(\tfrac18 + \tfrac1{24} = \tfrac16\), which is the familiar \(\tfrac12\cdot\tfrac13 mL^2\) of a rod turning about its end. The result is a quadratic form,
\[ T = \tfrac12\,\dot{\boldsymbol\theta}^{\mathsf T}\,\mathbf D(\boldsymbol\theta)\,\dot{\boldsymbol\theta}, \qquad \mathbf D = \begin{pmatrix} d_1 & d_2\cos\theta_1 & d_3\cos\theta_2 \\ d_2\cos\theta_1 & d_4 & d_5\cos(\theta_1-\theta_2) \\ d_3\cos\theta_2 & d_5\cos(\theta_1-\theta_2) & d_6 \end{pmatrix}, \tag{5} \] \[ \begin{aligned} d_1 &= m_0+m_1+m_2, & d_2 &= \left(\tfrac12 m_1+m_2\right)L_1, & d_3 &= \tfrac12 m_2L_2, \\ d_4 &= \left(\tfrac13 m_1+m_2\right)L_1^2, & d_5 &= \tfrac12 m_2L_1L_2, & d_6 &= \tfrac13 m_2L_2^2 . \end{aligned} \tag{6} \]Each constant has a meaning you can check by hand. \(d_1\) is everything that moves when the cart moves. \(d_4\) is the lower rod turning about its bottom end, carrying the upper rod’s whole mass at distance \(L_1\). \(d_2\) couples the cart and the lower angle: the factor \(\tfrac12 m_1 + m_2\) is the mass of the lower rod at its middle plus the upper rod hanging on its top. The factor 2 in front of a mixed product in (3) is absorbed by the two symmetric off-diagonal places of \(\mathbf D\).
3.3Potential energy
Each rod stores \(mg\) times the height of its centre of mass. From (1),
\[ V = m_1 g y_1 + m_2 g y_2 = f_1\cos\theta_1 + f_2\cos\theta_2, \qquad f_1 = \left(\tfrac12 m_1+m_2\right)L_1 g, \quad f_2 = \tfrac12 m_2 L_2 g . \tag{7} \]\(V\) is largest when both rods are upright, \(\cos\theta = 1\). The balanced position is the top of an energy hill, and that is why it is unstable: any step away from it releases energy into motion.
04Equations of motion
Lagrange’s equations, one per coordinate, are \(\frac{\mathrm d}{\mathrm dt}\frac{\partial \mathcal L}{\partial\dot q_i} - \frac{\partial\mathcal L}{\partial q_i} = Q_i\) with \(\mathcal L = T - V\) and \(Q_i\) the generalised force: the work a small change \(\delta q_i\) lets the outside forces do, divided by \(\delta q_i\). The motor force \(u\) does work \(u\,\delta x\) when the cart moves and none when only an angle changes, so \(\mathbf Q = (u, 0, 0)^{\mathsf T}\). The hinge forces do no work, because they act at points that do not slide along them; that is why Lagrange’s method never needs them.
Take the lower angle as the worked case. From (5), \(\partial T/\partial\dot\theta_1 = d_2\cos\theta_1\,\dot x + d_4\dot\theta_1 + d_5\cos(\theta_1-\theta_2)\dot\theta_2\). Its time derivative is
\[ d_2\cos\theta_1\,\ddot x + d_4\ddot\theta_1 + d_5\cos(\theta_1-\theta_2)\ddot\theta_2 - d_2\sin\theta_1\,\dot\theta_1\dot x - d_5\sin(\theta_1-\theta_2)(\dot\theta_1-\dot\theta_2)\dot\theta_2 . \]The other half is \(\partial T/\partial\theta_1 = -d_2\sin\theta_1\,\dot x\dot\theta_1 - d_5\sin(\theta_1-\theta_2)\dot\theta_1\dot\theta_2\) and \(\partial V/\partial\theta_1 = -f_1\sin\theta_1\). Subtracting, the terms in \(\dot x\dot\theta_1\) cancel and so does \(\dot\theta_1\dot\theta_2\), leaving
\[ d_2\cos\theta_1\,\ddot x + d_4\ddot\theta_1 + d_5\cos(\theta_1-\theta_2)\ddot\theta_2 + d_5\sin(\theta_1-\theta_2)\dot\theta_2^2 - f_1\sin\theta_1 = 0 . \tag{8} \]The row for \(x\). \(V\) does not contain \(x\), and \(T\) contains only \(\dot x\), so \(\partial\mathcal L/\partial x = 0\). From (5), \(\partial T/\partial\dot x = d_1\dot x + d_2\cos\theta_1\,\dot\theta_1 + d_3\cos\theta_2\,\dot\theta_2\), the total sideways momentum. Differentiating each product in time,
\[ d_1\ddot x + d_2\cos\theta_1\,\ddot\theta_1 + d_3\cos\theta_2\,\ddot\theta_2 - d_2\sin\theta_1\,\dot\theta_1^2 - d_3\sin\theta_2\,\dot\theta_2^2 = u . \tag{8a} \]This is Newton’s second law for the whole machine sideways: the rate of change of the total sideways momentum equals the only outside sideways force, \(u\). The two \(\sin\cdot\dot\theta^2\) terms are the sideways parts of the centripetal accelerations of the two centres of mass: a rod turning at \(\dot\theta\) pulls its centre of mass toward its hinge with \(\tfrac12 L\dot\theta^2\), and when the rod leans by \(\theta\) a share \(\sin\theta\) of that pull points sideways.
The row for \(\theta_2\). From (5), \(\partial T/\partial\dot\theta_2 = d_3\cos\theta_2\,\dot x + d_5\cos(\theta_1-\theta_2)\dot\theta_1 + d_6\dot\theta_2\). Its time derivative is
\[ d_3\cos\theta_2\,\ddot x + d_5\cos(\theta_1-\theta_2)\ddot\theta_1 + d_6\ddot\theta_2 - d_3\sin\theta_2\,\dot\theta_2\dot x - d_5\sin(\theta_1-\theta_2)(\dot\theta_1-\dot\theta_2)\dot\theta_1 . \]The other half is \(\partial T/\partial\theta_2 = -d_3\sin\theta_2\,\dot x\dot\theta_2 + d_5\sin(\theta_1-\theta_2)\dot\theta_1\dot\theta_2\) (the sign flips because \(\theta_2\) enters \(\cos(\theta_1-\theta_2)\) with a minus) and \(\partial V/\partial\theta_2 = -f_2\sin\theta_2\). Subtracting, the \(\dot x\dot\theta_2\) terms cancel, the two \(\dot\theta_1\dot\theta_2\) terms cancel, and
\[ d_3\cos\theta_2\,\ddot x + d_5\cos(\theta_1-\theta_2)\ddot\theta_1 + d_6\ddot\theta_2 - d_5\sin(\theta_1-\theta_2)\dot\theta_1^2 - f_2\sin\theta_2 = 0 . \tag{8b} \]Rows (8a), (8) and (8b) together, with the accelerations collected on the left by \(\mathbf D\), the squared velocities by \(\mathbf C\) and gravity by \(\mathbf G\), are
These are equations (2)–(9) of Moysis’s reproduction [2] of Bogdanov’s report [1], letter for letter. We checked them a second way: deriving (4)–(7) symbolically with SymPy and forming Lagrange’s equations reproduces \(\mathbf D\), \(\mathbf C\), \(\mathbf G\) and \(\mathbf H\) with zero difference in every entry.
Read row 1 as Newton’s law for the whole machine sideways: \(d_1\ddot x\) plus the sideways accelerations of the two centres of mass equals the only outside sideways force, \(u\). The \(\mathbf C\) terms are the centripetal parts of those accelerations: a turning rod’s centre of mass accelerates toward its hinge, and when the rod leans that pull has a sideways part. In rows 2 and 3, \(-f\sin\theta\) is gravity’s torque, which pushes a leaning rod further over.
The matrix \(\mathbf D\) is the kinetic energy’s matrix and is positive definite (\(T > 0\) for any motion), so it can always be inverted: the page computes the accelerations as \(\ddot{\boldsymbol\theta} = \mathbf D^{-1}(\mathbf Hu - \mathbf C\dot{\boldsymbol\theta} - \mathbf G)\) at every step.
05How fast it falls
Near the top every angle and every velocity is small, of some size \(\epsilon\). Keep in (9) only what is of first order in \(\epsilon\): \(\sin\theta = \theta + O(\epsilon^3)\); \(\cos\theta = 1 + O(\epsilon^2)\), so \(\mathbf D(\boldsymbol\theta)\ddot{\boldsymbol\theta} = \mathbf D(\mathbf 0)\ddot{\boldsymbol\theta} + O(\epsilon^3)\); and every entry of \(\mathbf C\dot{\boldsymbol\theta}\) is a sine times a squared velocity, \(O(\epsilon^3)\), so it drops out. With \(\mathbf G \approx -\operatorname{diag}(0, f_1, f_2)\boldsymbol\theta\), (9) becomes linear,
\[ \mathbf D_0\,\ddot{\boldsymbol\theta} = \mathbf F\boldsymbol\theta + \mathbf H u, \qquad \mathbf D_0 = \mathbf D(\mathbf 0), \quad \mathbf F = \operatorname{diag}(0, f_1, f_2). \tag{10} \]With the state \(\mathbf s = (\boldsymbol\theta, \dot{\boldsymbol\theta})\), this is \(\dot{\mathbf s} = \mathbf A\mathbf s + \mathbf B u\) with
\[ \mathbf A = \begin{pmatrix} \mathbf 0 & \mathbf I \\ \mathbf D_0^{-1}\mathbf F & \mathbf 0 \end{pmatrix}, \qquad \mathbf B = \begin{pmatrix} \mathbf 0 \\ \mathbf D_0^{-1}\mathbf H \end{pmatrix}. \tag{11} \]For Bogdanov’s values with \(g = 10\ \mathrm{m/s^2}\), (11) gives the rows \((0, -7.500, 0.804)\), \((0, 75.00, -33.75)\), \((0, -60.00, 52.14)\) for \(\ddot x, \ddot\theta_1, \ddot\theta_2\), which agree with the matrix printed in [2] to within 0.2 %.
5.1The fall rates
Switch the motor off, \(u = 0\). Row 1 of (10) then reads \(d_1\ddot x + d_2\ddot\theta_1 + d_3\ddot\theta_2 = 0\): no outside sideways force, so the centre of mass of the whole machine does not accelerate sideways. Solve it for \(\ddot x = -(d_2\ddot\theta_1 + d_3\ddot\theta_2)/d_1\) and put that into rows 2 and 3. Row 2 becomes \((d_4 - d_2^2/d_1)\ddot\theta_1 + (d_5 - d_2d_3/d_1)\ddot\theta_2 = f_1\theta_1\), and row 3 likewise, which leaves two equations for the angles alone,
\[ \mathbf M\ddot{\boldsymbol\varphi} = \operatorname{diag}(f_1, f_2)\,\boldsymbol\varphi, \qquad \boldsymbol\varphi = \begin{pmatrix}\theta_1\\ \theta_2\end{pmatrix}, \quad \mathbf M = \begin{pmatrix} d_4 - d_2^2/d_1 & d_5 - d_2d_3/d_1 \\ d_5 - d_2d_3/d_1 & d_6 - d_3^2/d_1 \end{pmatrix}. \tag{12} \]Try \(\boldsymbol\varphi \propto e^{\sigma t}\). A solution exists when \(\det(\sigma^2\mathbf M - \operatorname{diag}(f_1,f_2)) = 0\), a quadratic in \(\sigma^2\):
\[ \det\mathbf M\;\sigma^4 - \left(M_{11}f_2 + M_{22}f_1\right)\sigma^2 + f_1f_2 = 0 . \tag{13} \]Both roots \(\sigma^2\) are positive, so each gives a pair \(\pm\sigma\): one motion that dies away and one that grows as \(e^{\sigma t}\). For the default machine, \(\sigma = 10.39\ \mathrm{s^{-1}}\) and \(4.10\ \mathrm{s^{-1}}\). The fast one dominates: a lean grows by a factor \(e\) every \(1/\sigma = 96\) ms. The panel shows both rates live.
A check. Remove the upper rod, \(m_2 \to 0\). Then (13) leaves \(\sigma^2 = f_1/M_{11} = 6g(m_0+m_1)/[L_1(4m_0+m_1)]\). For a cart much heavier than the rod this tends to \(3g/(2L_1)\), the rate at which a rod falls over its fixed bottom end, which is the right limit: a very heavy cart does not recoil.
Why long rods are easier. Every \(d\) and \(f\) in (13) scales with the lengths so that \(\sigma^2 \propto g/L\) when both lengths grow by the same factor. Doubling both lengths slows the fall by \(\sqrt2\) (default machine: 10.39 to 7.35 s⁻¹). A broom is easier to balance than a pencil for exactly this reason.
06One push, two rods
Can one force at the bottom really bring two angles and the cart back? Kalman’s test answers it: the system (11) can be steered from any state to any other exactly when the six vectors \(\mathbf B, \mathbf A\mathbf B, \dots, \mathbf A^5\mathbf B\) are independent, that is when the matrix \([\mathbf B\;\mathbf A\mathbf B\;\cdots\;\mathbf A^5\mathbf B]\) has rank 6 [3]. The idea behind it: \(\mathbf B\) is where a push sends the state at once, \(\mathbf A\mathbf B\) where the motion carries that a moment later, and so on; if these directions fill all six dimensions, a suitable sequence of pushes can reach every state. For the default machine the rank is 6. The cart reaches the upper rod only through the lower one, but because the two rods fall at different rates (5.1) the cart can tell them apart.
07The controller
The controller reads the whole state and pushes in proportion to it, \(u = -\mathbf K(\mathbf s - \mathbf s_\text{ref})\), where \(\mathbf s_\text{ref}\) is upright and at rest at the target position \(x_\text{ref}\) of the buttons. The six gains in \(\mathbf K\) are chosen by the linear-quadratic regulator (LQR) [1, 3]: among all pushes, take the one that makes the total cost
\[ J = \int_0^\infty \left( \mathbf s^{\mathsf T}\mathbf Q\,\mathbf s + \rho\,u^2 \right)\mathrm dt \tag{14} \]smallest. The first term charges for being away from the target, with weights \(\mathbf Q = \operatorname{diag}(50, 50, 50, 20, 700, 700)\) on \((x, \theta_1, \theta_2, \dot x, \dot\theta_1, \dot\theta_2)\) (Bogdanov’s [1, 2], with the weight on \(x\) raised from 5 to 50 so that the cart reaches its target in seconds rather than tens of seconds). The second charges for force, at the rate \(\rho\) of the slider.
7.1Why the answer is a Riccati equation ★
Suppose a symmetric matrix \(\mathbf P\) satisfies
\[ \mathbf A^{\mathsf T}\mathbf P + \mathbf P\mathbf A - \rho^{-1}\mathbf P\mathbf B\mathbf B^{\mathsf T}\mathbf P + \mathbf Q = \mathbf 0 . \tag{15} \]Along any motion of (11), \(\frac{\mathrm d}{\mathrm dt}(\mathbf s^{\mathsf T}\mathbf P\mathbf s) = \mathbf s^{\mathsf T}(\mathbf A^{\mathsf T}\mathbf P + \mathbf P\mathbf A)\mathbf s + 2u\,\mathbf B^{\mathsf T}\mathbf P\mathbf s\). Replace \(\mathbf A^{\mathsf T}\mathbf P + \mathbf P\mathbf A\) using (15) and complete the square:
\[ \mathbf s^{\mathsf T}\mathbf Q\mathbf s + \rho u^2 = -\frac{\mathrm d}{\mathrm dt}\left(\mathbf s^{\mathsf T}\mathbf P\mathbf s\right) + \rho\left(u + \rho^{-1}\mathbf B^{\mathsf T}\mathbf P\mathbf s\right)^2 . \]Integrate from 0 to \(\infty\) for a motion that comes to rest: \(J = \mathbf s(0)^{\mathsf T}\mathbf P\,\mathbf s(0) + \rho\int(u + \rho^{-1}\mathbf B^{\mathsf T}\mathbf P\mathbf s)^2\,\mathrm dt\). The first term does not depend on what we do; the second is never negative and is zero only for
How the page solves (15) ★. Put \(\mathbf A\), \(\mathbf B\), \(\rho\) and \(\mathbf Q\) into the \(12\times12\) Hamiltonian matrix \(\mathbf Z = \begin{pmatrix}\mathbf A & -\rho^{-1}\mathbf B\mathbf B^{\mathsf T}\\ -\mathbf Q & -\mathbf A^{\mathsf T}\end{pmatrix}\). Suppose the six columns of \(\begin{pmatrix}\mathbf I\\ \mathbf P\end{pmatrix}\) span a space that \(\mathbf Z\) maps into itself, \(\mathbf Z\begin{pmatrix}\mathbf I\\ \mathbf P\end{pmatrix} = \begin{pmatrix}\mathbf I\\ \mathbf P\end{pmatrix}\boldsymbol\Lambda\). The top row says \(\boldsymbol\Lambda = \mathbf A - \rho^{-1}\mathbf B\mathbf B^{\mathsf T}\mathbf P\), the closed-loop matrix; the bottom row says \(-\mathbf Q - \mathbf A^{\mathsf T}\mathbf P = \mathbf P\boldsymbol\Lambda\), and inserting \(\boldsymbol\Lambda\) gives exactly (15). So \(\mathbf P\) comes from the space of \(\mathbf Z\) whose eigenvalues have negative real parts, which is also the one that makes the closed loop stable. To find it, the page iterates \(\mathbf Z \leftarrow \tfrac12(\mathbf Z + \mathbf Z^{-1})\), Newton’s method for the square root of \(\mathbf I\): each eigenvalue \(\lambda\) moves as \(\tfrac12(\lambda + 1/\lambda)\), which drives eigenvalues with negative real part to \(-1\) and the others to \(+1\). The limit \(\mathbf W\) therefore satisfies \(\mathbf W\begin{pmatrix}\mathbf I\\ \mathbf P\end{pmatrix} = -\begin{pmatrix}\mathbf I\\ \mathbf P\end{pmatrix}\), that is \(\begin{pmatrix}\mathbf W_{12}\\ \mathbf W_{22}+\mathbf I\end{pmatrix}\mathbf P = -\begin{pmatrix}\mathbf W_{11}+\mathbf I\\ \mathbf W_{21}\end{pmatrix}\), twelve equations for the six columns of \(\mathbf P\), which the page solves by least squares. Then (16) gives \(\mathbf K\).
The page repeats this every time a slider changes [3]. For the default machine and \(\rho = 1\), the gains on \((x, \theta_1, \theta_2, \dot x, \dot\theta_1, \dot\theta_2)\) are \((7.07,\ -504.0,\ 625.9,\ 15.32,\ -14.69,\ 97.94)\) in N per m, rad, m/s or rad/s; the closed loop’s slowest motion dies away in 1.23 s.
The signs say what the hand does with a broom. The lower rod leaning right alone (\(\theta_1 > 0\)) gives \(u = -K_2\theta_1 = +504\,\theta_1\): push right, under the lean. The upper rod leaning right alone (\(\theta_2 > 0\)) gives \(u = -K_3\theta_2 = -626\,\theta_2\): push left. That pulls the bottom of the lower rod to the left, so the lower rod tips right and carries the middle hinge out under the upper rod. The two large gains of opposite sign are the controller playing one rod against the other.
7.2A real controller samples
A computer does not watch the rods all the time. It reads the sensors at fixed instants, every \(T_s\), computes a force, sets the motor to it and holds it there until the next look. The page does exactly this: \(u\) changes only every \(T_s\) (the slider; 10 ms by default) and stays constant in between, while the rods keep falling according to (9). Engineers call the held force a zero-order hold: a staircase instead of a curve. Section 7.1 designed \(\mathbf K\) as if the controller acted at every instant. Here we ask what the machine does between two looks, why a loop with those gains breaks when \(T_s\) grows, and how to design gains for the loop period itself. A reader who wants the result alone can jump to (16h) and Table 1.
The machine between two looks. Between the look at time \(t_k = kT_s\) and the next, the linear model (11) runs with a constant input: \(\dot{\mathbf s} = \mathbf A\mathbf s + \mathbf B u_k\). This is a linear equation with a constant forcing, and we solve it the way one solves \(\dot z = az + b\): the free motion grows as \(e^{\mathbf A t}\), and the forcing is carried forward from every instant \(\tau\) at which it acted, each slice \(\mathbf B u_k\,\mathrm d\tau\) then growing freely for the remaining time \(t - \tau\). This is the variation of constants, and it gives \(\mathbf s(t_k + t) = e^{\mathbf A t}\mathbf s_k + \int_0^{t} e^{\mathbf A(t-\tau)}\,\mathrm d\tau\;\mathbf B u_k\). At the next look, \(t = T_s\):
Here \(e^{\mathbf A t}\) is the matrix exponential, the same series as for numbers, \(\mathbf I + \mathbf A t + \tfrac12\mathbf A^2t^2 + \cdots\); for the falling mode of section 5 it is the factor \(e^{\sigma t}\) by which a lean grows. \(\boldsymbol\Phi\) says where the machine drifts on its own in one period, \(\boldsymbol\Gamma\) what one newton held for one period does to it. For a short period \(\boldsymbol\Phi \approx \mathbf I + \mathbf A T_s\) and \(\boldsymbol\Gamma \approx \mathbf B T_s\), Euler’s step; (16a) itself is exact for every period. The page needs both matrices and gets them from one exponential. Put the force into the state, \(\mathbf z = (\mathbf s, u)\), with the rule \(\dot u = 0\) that says “held”; then \(\dot{\mathbf z} = \bar{\mathbf A}\mathbf z\) with \(\bar{\mathbf A} = \begin{pmatrix}\mathbf A & \mathbf B\\ \mathbf 0 & 0\end{pmatrix}\), a free linear motion, so \(\mathbf z(T_s) = e^{\bar{\mathbf A}T_s}\mathbf z(0)\). Reading off the top block row,
\[ e^{\bar{\mathbf A}T_s} = \begin{pmatrix}\boldsymbol\Phi & \boldsymbol\Gamma\\ \mathbf 0 & 1\end{pmatrix}. \tag{16b} \]The page computes \(e^{\mathbf M}\) by scaling and squaring: it halves \(\mathbf M\) \(q\) times until its entries are below 0.25, sums 18 terms of the series, and squares the result \(q\) times, since \(e^{\mathbf M} = (e^{\mathbf M/2^q})^{2^q}\). We checked (16b) in Wolfram against the integral computed directly: the two agree to \(4\times10^{-16}\).
7.3Why the loop breaks: one falling mode
Take the simplest machine that falls: one mode, \(\dot z = \sigma z + b u\), with a rate \(\sigma\) as in section 5. Act on it at every instant with \(u = -\kappa z\) and it decays as \(e^{(\sigma - \gamma)t}\), where \(\gamma = b\kappa\) is the strength of the feedback in the units of a rate; the loop works for any \(\gamma > \sigma\), however large. Now act on it every \(T_s\) and hold. Between looks (16a) applies with \(\Phi = e^{\sigma T_s}\) and \(\Gamma = (b/\sigma)(e^{\sigma T_s} - 1)\), and with \(u_k = -\kappa z_k\) each look multiplies \(z\) by
\[ \lambda = e^{\sigma T_s} - \frac{\gamma}{\sigma}\left(e^{\sigma T_s} - 1\right). \tag{16c} \]The loop holds when \(|\lambda| < 1\). The first condition, \(\lambda < 1\), is \(\gamma > \sigma\) again. The second, \(\lambda > -1\), is new: it fails when the correction overshoots, when one look’s push, held for the whole period, carries the lean past zero to a larger lean of the other sign, which the next look overcorrects in its turn. Solving \(\lambda = -1\) for the period,
\[ T^\ast = \frac1\sigma\ln\frac{\gamma + \sigma}{\gamma - \sigma} \;\xrightarrow{\ \sigma \to 0\ }\; \frac2\gamma . \tag{16d} \]Everyone has met this loop in a shower with a long pipe. You feel the water too cold, turn the tap, feel nothing for a while, turn further, and then it is too hot; the stronger your hand (large \(\gamma\)) and the longer the pipe (large \(T_s\)), the worse the swinging between hot and cold. In (16d) a stronger controller shortens the period it can bear, down to \(2/\gamma\). With the fall rate \(\sigma = 10.39\ \mathrm{s^{-1}}\) of the default machine and the fastest rate of its continuous closed loop, \(42.62\ \mathrm{s^{-1}}\) (so \(\gamma = \sigma + 42.62 = 53.0\ \mathrm{s^{-1}}\)), (16d) gives \(T^\ast = 38\) ms. The full six-dimensional loop (16a) with the continuous gains does the same thing a little sooner: in Wolfram its largest factor per look passes through \(-1\) just below \(T_s = 34.4\) ms (at 34.4 ms it is \(-1.0002\)), the signature of the hot–cold flip with period \(2T_s\). At 40 ms the factor is 1.45 per look, and the rods would be lost within a second if the motor were unlimited. With the 60 N limit they are not lost, but they never rest: in a 600 s run with the continuous gains at 40 ms the rods rattle by \(\pm4.9°\) with the motor thrown from \(-60\) to \(+60\) N, a limit cycle. Switch Gains designed for to Continuous time and set \(T_s\) to 40 ms to watch it. One mode is the picture, not the proof: the six modes couple, and the page’s 34.4 ms comes from the full matrix.
7.4The exact cost of a sampled loop
The remedy is to design the gains for the loop we actually run. The cost (14) is an integral over continuous time, but the controller decides only at the looks, so we write (14) as a sum over periods and do the integral inside each period exactly. Within one period the state is \(\mathbf s(t_k + t) = \boldsymbol\Phi(t)\mathbf s_k + \boldsymbol\Gamma(t)u_k\) with \(\boldsymbol\Phi(t) = e^{\mathbf A t}\) and \(\boldsymbol\Gamma(t) = \int_0^t e^{\mathbf A\tau}\mathrm d\tau\,\mathbf B\), the running versions of (16a). Insert this into \(\int_0^{T_s}(\mathbf s^{\mathsf T}\mathbf Q\mathbf s + \rho u_k^2)\,\mathrm dt\) and expand the square:
\[ J = \sum_{k=0}^{\infty}\left(\mathbf s_k^{\mathsf T}\mathbf Q_d\,\mathbf s_k + 2\,\mathbf s_k^{\mathsf T}\mathbf N_d\,u_k + R_d\,u_k^2\right), \qquad \mathbf Q_d = \int_0^{T_s}\boldsymbol\Phi^{\mathsf T}\mathbf Q\boldsymbol\Phi\,\mathrm dt, \quad \mathbf N_d = \int_0^{T_s}\boldsymbol\Phi^{\mathsf T}\mathbf Q\boldsymbol\Gamma\,\mathrm dt, \quad R_d = \int_0^{T_s}\left(\boldsymbol\Gamma^{\mathsf T}\mathbf Q\boldsymbol\Gamma + \rho\right)\mathrm dt. \tag{16e} \]Two things are new. The weights depend on the period: for a short one, \(\mathbf Q_d \approx \mathbf Q T_s\) and \(R_d \approx \rho T_s\), as a rectangle rule would give. And a cross term \(\mathbf N_d\) appears, of second order in the period, \(\mathbf N_d \approx \tfrac12 T_s^2\,\mathbf Q\mathbf B\): a force held for a while moves the state during the period, and the cost of that motion is charged to the force. A rectangle rule would miss it. All three integrals are one integral in the augmented variable of 7.2, because \(\mathbf z(t) = e^{\bar{\mathbf A}t}\mathbf z_k\) and the integrand is \(\mathbf z^{\mathsf T}\bar{\mathbf Q}\mathbf z\) with \(\bar{\mathbf Q} = \operatorname{diag}(\mathbf Q, \rho)\):
\[ \begin{pmatrix}\mathbf Q_d & \mathbf N_d\\ \mathbf N_d^{\mathsf T} & R_d\end{pmatrix} = \int_0^{T_s} e^{\bar{\mathbf A}^{\mathsf T}t}\,\bar{\mathbf Q}\,e^{\bar{\mathbf A}t}\,\mathrm dt = \mathbf F_{22}^{\mathsf T}\mathbf F_{12}, \qquad \begin{pmatrix}\mathbf F_{11} & \mathbf F_{12}\\ \mathbf 0 & \mathbf F_{22}\end{pmatrix} = \exp\left[\begin{pmatrix}-\bar{\mathbf A}^{\mathsf T} & \bar{\mathbf Q}\\ \mathbf 0 & \bar{\mathbf A}\end{pmatrix}T_s\right]. \tag{16f} \]The second equality is Van Loan’s identity [9], and it turns an integral of exponentials into one exponential of a \(14\times14\) matrix. Here is why it holds. Call the block matrix \(\mathbf C\) and \(\mathbf F(t) = e^{\mathbf Ct}\), which solves \(\dot{\mathbf F} = \mathbf C\mathbf F\) from \(\mathbf F(0) = \mathbf I\). Because \(\mathbf C\) is block upper triangular, so is every power of it and so is \(\mathbf F\). Its lower block obeys \(\dot{\mathbf F}_{22} = \bar{\mathbf A}\mathbf F_{22}\), so \(\mathbf F_{22} = e^{\bar{\mathbf A}t}\). Its upper-right block obeys \(\dot{\mathbf F}_{12} = -\bar{\mathbf A}^{\mathsf T}\mathbf F_{12} + \bar{\mathbf Q}\,e^{\bar{\mathbf A}t}\) from \(\mathbf F_{12}(0) = \mathbf 0\), the same kind of equation as in 7.2, a linear motion with a forcing, and variation of constants gives \(\mathbf F_{12}(T_s) = \int_0^{T_s} e^{-\bar{\mathbf A}^{\mathsf T}(T_s - \tau)}\,\bar{\mathbf Q}\,e^{\bar{\mathbf A}\tau}\,\mathrm d\tau\). Multiply from the left by \(\mathbf F_{22}^{\mathsf T} = e^{\bar{\mathbf A}^{\mathsf T}T_s}\); exponents of the same matrix add, \(e^{\bar{\mathbf A}^{\mathsf T}T_s}e^{-\bar{\mathbf A}^{\mathsf T}(T_s-\tau)} = e^{\bar{\mathbf A}^{\mathsf T}\tau}\), and what remains under the integral is exactly the integrand of (16f). Van Loan’s report proves a larger theorem the same way, block by block from the differential equation, and his algorithm takes the integral as \(\mathbf F_3^{\mathsf T}\mathbf G_2\), his names for our \(\mathbf F_{22}\) and \(\mathbf F_{12}\) [9]. We checked the identity in Wolfram on random matrices against the integral done numerically: agreement to \(8\times10^{-16}\).
7.5The discrete Riccati equation
Now repeat the argument of 7.1 for a sum instead of an integral. Suppose the cost still to pay from a state \(\mathbf s\), when every later force is chosen as well as possible, is \(V(\mathbf s) = \mathbf s^{\mathsf T}\mathbf P\mathbf s\) for some symmetric \(\mathbf P\). Then the best \(u\) at the present look is the one that makes this period’s cost plus the cost still to pay from the next state smallest:
\[ \mathbf s^{\mathsf T}\mathbf Q_d\mathbf s + 2\mathbf s^{\mathsf T}\mathbf N_d u + R_d u^2 + (\boldsymbol\Phi\mathbf s + \boldsymbol\Gamma u)^{\mathsf T}\mathbf P\,(\boldsymbol\Phi\mathbf s + \boldsymbol\Gamma u). \]This is a quadratic in the number \(u\). Collect its terms: the coefficient of \(u^2\) is \(R_d + \boldsymbol\Gamma^{\mathsf T}\mathbf P\boldsymbol\Gamma\), the coefficient of \(2u\) is \((\boldsymbol\Gamma^{\mathsf T}\mathbf P\boldsymbol\Phi + \mathbf N_d^{\mathsf T})\mathbf s\), and the rest is \(\mathbf s^{\mathsf T}(\mathbf Q_d + \boldsymbol\Phi^{\mathsf T}\mathbf P\boldsymbol\Phi)\mathbf s\). Complete the square in \(u\), exactly as \(au^2 + 2bu + c = a(u + b/a)^2 + c - b^2/a\):
\[ \left(R_d + \boldsymbol\Gamma^{\mathsf T}\mathbf P\boldsymbol\Gamma\right)\left(u + \mathbf K_d\mathbf s\right)^2 + \mathbf s^{\mathsf T}\Big[\mathbf Q_d + \boldsymbol\Phi^{\mathsf T}\mathbf P\boldsymbol\Phi - \left(\boldsymbol\Phi^{\mathsf T}\mathbf P\boldsymbol\Gamma + \mathbf N_d\right)\left(R_d + \boldsymbol\Gamma^{\mathsf T}\mathbf P\boldsymbol\Gamma\right)^{-1}\left(\boldsymbol\Gamma^{\mathsf T}\mathbf P\boldsymbol\Phi + \mathbf N_d^{\mathsf T}\right)\Big]\mathbf s . \]The first term is never negative and is zero for one choice of \(u\); the second does not depend on \(u\) at all. So the best force makes the first term vanish, and the least cost from \(\mathbf s\) is the second term. But we assumed that least cost to be \(\mathbf s^{\mathsf T}\mathbf P\mathbf s\): the bracket must be \(\mathbf P\) itself. That is the discrete Riccati equation,
\[ \mathbf P = \mathbf Q_d + \boldsymbol\Phi^{\mathsf T}\mathbf P\boldsymbol\Phi - \left(\boldsymbol\Phi^{\mathsf T}\mathbf P\boldsymbol\Gamma + \mathbf N_d\right)\left(R_d + \boldsymbol\Gamma^{\mathsf T}\mathbf P\boldsymbol\Gamma\right)^{-1}\left(\boldsymbol\Gamma^{\mathsf T}\mathbf P\boldsymbol\Phi + \mathbf N_d^{\mathsf T}\right), \tag{16g} \]and the force that attains it is
We checked the completed square in Wolfram on random matrices: the two sides agree to the last digit. The continuous law comes back as \(T_s \to 0\). Put the short-period forms into (16g): \(\boldsymbol\Phi = \mathbf I + \mathbf A T_s\), \(\boldsymbol\Gamma = \mathbf B T_s\), \(\mathbf Q_d = \mathbf Q T_s\), \(R_d = \rho T_s\), and \(\mathbf N_d\), of order \(T_s^2\), dropped. Then \(\boldsymbol\Phi^{\mathsf T}\mathbf P\boldsymbol\Phi = \mathbf P + T_s(\mathbf A^{\mathsf T}\mathbf P + \mathbf P\mathbf A) + O(T_s^2)\) and the last term is \(T_s\,\mathbf P\mathbf B\mathbf B^{\mathsf T}\mathbf P/\rho + O(T_s^2)\), so (16g) reads \(\mathbf P = \mathbf P + T_s\left(\mathbf Q + \mathbf A^{\mathsf T}\mathbf P + \mathbf P\mathbf A - \rho^{-1}\mathbf P\mathbf B\mathbf B^{\mathsf T}\mathbf P\right) + O(T_s^2)\). The bracket must vanish: that is (15). And (16h) becomes \(\mathbf K_d = (\rho T_s)^{-1}(\mathbf B^{\mathsf T}\mathbf P\,T_s) = \rho^{-1}\mathbf B^{\mathsf T}\mathbf P\), which is (16). Table 1 shows the gains climbing back to the continuous ones as the period shrinks.
How the page solves (16g) ★. First the cross term is removed by a change of variable, \(u = v - R_d^{-1}\mathbf N_d^{\mathsf T}\mathbf s\): inserted into (16e) and (16a), it makes the period cost \(\mathbf s^{\mathsf T}\hat{\mathbf Q}\mathbf s + R_d v^2\) with \(\hat{\mathbf Q} = \mathbf Q_d - \mathbf N_d R_d^{-1}\mathbf N_d^{\mathsf T}\) (the terms linear in \(v\) cancel in pairs) and the drift \(\hat{\boldsymbol\Phi} = \boldsymbol\Phi - \boldsymbol\Gamma R_d^{-1}\mathbf N_d^{\mathsf T}\); with \(\mathbf G = \boldsymbol\Gamma R_d^{-1}\boldsymbol\Gamma^{\mathsf T}\), (16g) takes the shorter form \(\mathbf P = \hat{\mathbf Q} + \hat{\boldsymbol\Phi}^{\mathsf T}\mathbf P(\mathbf I + \mathbf G\mathbf P)^{-1}\hat{\boldsymbol\Phi}\). The page then runs the structure-preserving doubling algorithm, in the form written out by Guo, Chu, Liang and Lin [10], from \(\mathbf A_0 = \hat{\boldsymbol\Phi}\), \(\mathbf G_0 = \mathbf G\), \(\mathbf H_0 = \hat{\mathbf Q}\):
\[ \mathbf W = (\mathbf I + \mathbf G_k\mathbf H_k)^{-1}, \qquad \mathbf A_{k+1} = \mathbf A_k\mathbf W\mathbf A_k, \qquad \mathbf G_{k+1} = \mathbf G_k + \mathbf A_k\mathbf W\mathbf G_k\mathbf A_k^{\mathsf T}, \qquad \mathbf H_{k+1} = \mathbf H_k + \mathbf A_k^{\mathsf T}\mathbf H_k\mathbf W\mathbf A_k . \]Each step composes the previous step’s map with itself, so \(\mathbf H_k\) is the cost of looking exactly \(2^k\) periods ahead; we checked this in Wolfram against the plain period-by-period recursion for \(k \le 5\) (agreement to \(10^{-15}\)), and \(\mathbf H_k \to \mathbf P\) as the horizon grows. The page stops when \(\mathbf H\) changes by less than \(10^{-13}\) of itself, after 12 steps at 10 ms, a horizon of 4096 looks, and takes (16h). For the default machine the result equals Wolfram’s DiscreteLQRegulatorGains [11] to 8 parts in \(10^{12}\) at 10 ms and to 1 part in \(10^{13}\) at 40 ms.
With the switch Gains designed for on The loop period (the default) the page uses \(\mathbf K_d\) and the \(\mathbf P\) of (16g) wherever the sections below write \(\mathbf K\) and \(\mathbf P\); on Continuous time it uses (15)–(16) whatever the slider says. The panel shows the largest factor per look, \(|\lambda|\), of the loop (16a) with the gains in use, computed as \(\|(\boldsymbol\Phi - \boldsymbol\Gamma\mathbf K)^n\|^{1/n}\) for \(n = 2^{40}\) by repeated squaring. After \(n\) looks the slowest part of the motion has shrunk by \(|\lambda|^n\), so it dies away in the time \(T_s/|\ln|\lambda||\), which is the settling time in the panel: 1.23 s at 10 ms, the same as the continuous loop’s slowest rate, \(0.815\ \mathrm{s^{-1}}\), gives.
| \(T_s\) | \(|\lambda|\), continuous gains (16) | \(|\lambda|\), gains (16h) | \(\mathbf K_d\) on \(\theta_1, \theta_2\) (N/rad) | \(\mathbf K_d\) on \(x\) (N/m) |
|---|---|---|---|---|
| 0 (continuous) | — | — | −504.0, 625.9 | 7.07 |
| 1 ms | 0.9992 | 0.9992 | −492.8, 610.4 | 6.87 |
| 10 ms | 0.9919 | 0.9919 | −408.0, 493.7 | 5.32 |
| 20 ms | 0.9839 | 0.9838 | −339.7, 399.8 | 4.09 |
| 30 ms | 0.9760 | 0.9758 | −289.4, 330.9 | 3.19 |
| 34.4 ms | 1.0002 | 0.9723 | −271.4, 306.3 | 2.88 |
| 40 ms | 1.449 | 0.9679 | −251.4, 279.0 | 2.53 |
| 60 ms | 3.417 | 0.9523 | −198.9, 207.8 | 1.64 |
| 120 ms | 14.23 | 0.9068 | −126.1, 111.2 | 0.52 |
| 260 ms | 111.6 | 0.8093 | −81.2, 55.6 | 0.05 |
Table 1: the default machine, \(\rho = 1\). With the continuous gains the loop (16a) holds up to 34.4 ms and then flips; designed for the period, it is stable at every period in the table, and the gains fall as the period grows, because a push that lasts long must be gentle. All numbers from Wolfram (DiscreteLQRegulatorGains and the eigenvalues of \(\boldsymbol\Phi - \boldsymbol\Gamma\mathbf K\)); the page’s own values agree to the digits shown.
08What the sliders teach
Each result below was computed with this page’s equations, RK4 at 1 ms, from the start \(\theta_1 = 2°\), \(\theta_2 = -1°\) unless said otherwise.
The loop period. Here “holds” means that the rods come to rest: in a 60 s run the largest angle in the last 10 s is below 1 mrad. With the continuous gains (16) the controller holds the rods for \(T_s\) up to 34 ms and not at 34.5 ms (tested in steps of 0.5 ms), exactly where the linear loop flips in 7.3: about a third of the 96 ms fall time. Above it the rods rattle in a limit cycle, at 40 ms by \(\pm4.9°\) with the motor at \(\pm60\) N, and from 60 ms they fall within a second. Double both lengths and the limit rises to 58 ms; halve them and it drops to 19 ms. Slow brains need long brooms. With gains designed for the period (16h) the linear loop is stable at every period we tried, up to one second, where the factor per look is still 0.47; what ends it on the real machine is the motor limit. From the standard start the designed gains hold up to 260 ms, two and a half fall times, and lose the rods at 262.5 ms: in a quarter of a second between looks a lean grows \(e^{10.39 \times 0.26} = 15\)-fold, and what one held push of at most 60 N can undo is bounded. Doubled lengths move that limit to 342.5 ms, halved lengths to 195 ms. Set \(T_s\) to 80 ms, the end of the slider, and switch between the two designs: with Continuous time the balancer loses the rods in 0.35 s, with The loop period it holds them still.
The motor limit. At \(u_\text{max} = 60\) N the controller catches a flick of the top of \(J = 0.2\) N·s and loses one of 0.3 N·s. A flick is a short horizontal blow at the top end, a force \(F(t)\) acting for a time \(\varepsilon\) with impulse \(J = \int F\,\mathrm dt\). The top end sits at \(x_\text{top} = x + L_1\sin\theta_1 + L_2\sin\theta_2\), so by the rule of section 4 the blow’s generalised force is \(F\,\partial x_\text{top}/\partial\boldsymbol\theta = F\,(1, L_1\cos\theta_1, L_2\cos\theta_2)^{\mathsf T}\). Integrate (9) over the blow: the positions cannot move in a time \(\varepsilon \to 0\), so \(\mathbf D\) stays fixed and \(\int\mathbf D\ddot{\boldsymbol\theta}\,\mathrm dt = \mathbf D\,\Delta\dot{\boldsymbol\theta}\), while \(\mathbf C\dot{\boldsymbol\theta}\), \(\mathbf G\) and \(u\) stay finite and contribute only \(O(\varepsilon)\). Hence
\[ \Delta\dot{\boldsymbol\theta} = \mathbf D^{-1}\begin{pmatrix}1\\ L_1\cos\theta_1\\ L_2\cos\theta_2\end{pmatrix} J . \tag{17} \]Pull it over and let go. Grab a rod in the figure and tilt it; while you hold a rod, everything stands still at rest. Let go, and the controller starts from that leaning state. With the default machine and \(u_\text{max} = 60\) N, the upper rod pulled to 13° comes back, and so does the lower rod pulled to −10°. The upper rod pulled to 45° is beyond the balancer: it asks for more than 60 N, the motor saturates, and the rods fall before the cart can get under them. The page then calms the rods and swings them back up (sections 9–11); switch Let them fall on to see the fall itself, or raise \(u_\text{max}\) and try again.
Take the cart. Grab the cart in the figure and drag it. While you hold it, your hand moves the cart, not the motor: the controller lets go, and the rods answer the cart’s motion alone. Carry a broom upright on your palm and jerk your hand to the right: the broom does not come along at once, it falls to the left. That is what you see here, with a twist. The page models your hand as a stiff spring with a damper that pulls the cart toward the pointer \(x_h\), as hard as a quick hand can:
\[ \ddot x = \operatorname{clip}\!\left(\omega_h^2\,(x_h - x) - 2\omega_h\,\dot x,\ \pm a_h\right), \qquad \omega_h = 15\ \text{s}^{-1},\quad a_h = 30\ \text{m/s}^2 . \tag{29} \]The two numbers are our choices. The damping \(2\omega_h\) is the critical one, so the cart never overshoots the pointer: after a jump \(D\) of the pointer it follows \(x = D\,[1 - (1 + \omega_h t)\,e^{-\omega_h t}]\), 95 % of the way in 0.32 s, and it trails a pointer moving steadily at speed \(c\) by \(2c/\omega_h\), 13 cm at 1 m/s (Wolfram’s DSolve on (29) gives both). With \(\ddot x\) given, the Euler–Lagrange equations of the rods, rows 2 and 3 of (9), no longer contain the force: they are two equations for the rods alone, driven by the cart,
and row 1 says how hard your hand pushes, the same row the calming law uses to turn an acceleration into a force (21). Wolfram derived (30) from the Lagrangian (4)–(7) with \(x(t)\) prescribed. The cart’s acceleration enters like a sideways gravity: in the cart’s frame every mass feels \(-\ddot x\) besides \(-g\). From upright and at rest, (30) gives \(\ddot\theta_1 = -2.47\,\ddot x\) and \(\ddot\theta_2 = +0.47\,\ddot x\) (in rad/s² per m/s², default machine). Here is the twist: jerk the cart right, and the lower rod falls back so fast that the middle hinge moves left, by \(\ddot x\,(1 - 2.47 \times 0.5) = -0.24\,\ddot x\), so the upper rod tips forward. The pair buckles like a folding ruler before gravity takes over. Your hand feels this too: at that first instant the force is \(m_\text{eff}\ddot x\) with \(m_\text{eff} = d_1 - \mathbf h^{\mathsf T}\mathbf D_{\theta\theta}^{-1}\mathbf h = 1.65\) kg, where \(\mathbf h = (d_2, d_3)^{\mathsf T}\) and \(\mathbf D_{\theta\theta}\) is the matrix of (30), and not the 2.75 kg the machine weighs, because the rods turn instead of following. Let go, and the controller starts from the state you leave behind. If the rods can still be caught, it catches them and brakes the cart to rest (11.5), or brings it back to where you let go; if not, it calms them and swings them up again.
The wrong-way start. Press to +1 m. The cart first creeps 3 mm forward, then backs up by 2.3 cm, half a second after the press, while both rods tip about 2° toward the target. Only then does it drive forward, leaning into the motion, and at the end the rods lean back about 1.2° to brake. It has to: to accelerate to the right with the rods upright on average, the rods must lean right, and the only way to tip them right is to pull their bottom out to the left first. Engineers call this a non-minimum-phase system [3]; you do the same with a broom when you want to walk forward with it.
The cost of force. Raise \(\rho\) and the controller pushes more gently and lets the rods wander further; lower it and the pushes become violent and the motor limit is reached sooner. The gains in the panel show this directly.
The masses. A heavier cart recoils less, so the rods fall closer to the rate of a fixed hinge. A heavier upper rod loads the lower one and changes which way the controller must play them. Watch the fall rates in the panel as you move each slider.
09Where the balancer is certain to win
Pull a rod further and further over before letting go. At some lean the balancer loses: not because of a rule about angles, but because the force it would need exceeds \(u_\text{max}\), or because the linear picture (10) it was designed on no longer describes the real motion (9). The first version of this page gave up at 60°. That number came from nowhere, and it was wrong both ways: measured from rest with the default machine, the largest lean the balancer brings back is between 14° and 44° depending on the direction of the lean, so some states under 60° were already hopeless, and with the rods moving some states past 60° could have been saved. What is needed is a test on the whole state, all six numbers, that says: from here, the balancer wins for certain.
Section 7.1 gave us the tool. The quantity
\[ W(\mathbf s) = (\mathbf s - \mathbf s_\text{ref})^{\mathsf T}\mathbf P\,(\mathbf s - \mathbf s_\text{ref}) \tag{19} \]is the cost the balancer still has to pay from the state \(\mathbf s\). Its level sets \(W = c\) are nested ellipsoids around the upright rest. On the linear model, (15) and (16) give \(\dot W = -\mathbf s^{\mathsf T}(\mathbf Q + \rho\mathbf K^{\mathsf T}\mathbf K)\mathbf s\), negative everywhere: the cost always falls. On the real machine, with the real equations (9) and the motor clipped at \(u_\text{max}\), it need not. Suppose we find a level \(c^\ast\) such that \(\dot W < 0\) at every point with \(0 < W \le c^\ast\), the whole ellipsoid except its centre. Then a motion that starts inside can never leave: to cross the surface \(W = c^\ast\) outward, \(W\) would have to rise there, and it cannot. Inside, \(W\) falls all the time, and since it is bounded below by zero and falls at a rate that does not die out away from the centre, it can only end at \(W = 0\), the upright rest. This is Lyapunov’s argument: a bowl in which the marble can only roll downhill. Negative \(\dot W\) on the surface alone would only keep the motion inside; it is the inside that brings it home, so the inside must be tested too.
The first version of this page tested exactly that: \(\dot W = 2(\mathbf s-\mathbf s_\text{ref})^{\mathsf T}\mathbf P\,\mathbf f(\mathbf s, \operatorname{sat}(-\mathbf K\mathbf s))\), with \(\mathbf f\) the true right-hand side of section 12, at 8000 random points of the surface \(W = c\) and 2000 more on each of five surfaces inside it, a climb uphill in \(\dot W\) from the worst twelve, and a bisection in \(c\). It gave \(c^\ast = 31.4\) for the default machine; an independent search in Mathematica, maximising \(\dot W\) on each surface with FindMaximum from several starts, brackets it between 30.56 and 30.69. The climb was needed: random points alone reported about 140, because the states where the guarantee fails form a thin patch that 8000 random points miss. The worst point lies close to upright, both rods leaning about 4° the same way and almost at rest, where the balancer asks for a fraction of a newton: there the curvature of (9), the cosines and centripetal terms that (10) threw away, briefly makes \(W\) rise. The level is the same at 30 N, 60 N and 200 N: the motor limit does not set it, the curvature of (9) does.
That test has one gap: it assumes a balancer that acts at every instant, while the page’s balancer looks every \(T_s\) (7.2). So the page now tests the loop it runs. Let \(\mathbf F(\mathbf s)\) be the state one period later when the real equations (9) run from \(\mathbf s\) with the push \(\operatorname{sat}(-\mathbf K\mathbf s)\) held, the true version of (16a). The certified level \(c^\ast\) is the largest \(c\) with
\[ W(\mathbf F(\mathbf s)) < W(\mathbf s)\quad\text{for every } \mathbf s \text{ with } 0 < W(\mathbf s) \le c . \tag{19a} \]The argument is the same as before, told in steps instead of in flow. If \(W(\mathbf s_k) \le c^\ast\) then \(W(\mathbf s_{k+1}) < W(\mathbf s_k) \le c^\ast\): once inside at a look, inside at every later look. The numbers \(W(\mathbf s_k)\) fall and are bounded below by zero, so they converge, and they can only converge to zero: were the limit some \(w > 0\), the states would stay on the closed shell \(w \le W \le c^\ast\), where the strict inequality (19a), being continuous in \(\mathbf s\), guarantees a fall of at least some fixed amount per look, and \(W\) would reach zero after finitely many looks after all. Between two looks the state may step outside the ellipsoid for a moment; it is back at the next look, and over one period the real motion from a bounded set stays bounded, so nothing escapes. On the linear loop the fall per look is \(W(\mathbf s) - W(\mathbf A_\text{cl}\mathbf s) = \mathbf s^{\mathsf T}(\mathbf P - \mathbf A_\text{cl}^{\mathsf T}\mathbf P\mathbf A_\text{cl})\mathbf s\) with \(\mathbf A_\text{cl} = \boldsymbol\Phi - \boldsymbol\Gamma\mathbf K\), and by (16g) with \(u = -\mathbf K_d\mathbf s\) that matrix is the period cost \(\mathbf Q_d - \mathbf N_d\mathbf K_d - \mathbf K_d^{\mathsf T}\mathbf N_d^{\mathsf T} + \mathbf K_d^{\mathsf T}R_d\mathbf K_d\), which is positive (checked in Wolfram at 40 ms: the two sides agree to \(2\times10^{-15}\)): linearly the cost always falls, here too. The real loop can differ from the linear one only where (9) differs from (10), so the search looks hardest where the linear fall is weakest. The page writes \(\mathbf P = \mathbf L\mathbf L^{\mathsf T}\), so that \(\mathbf s = \sqrt c\,\mathbf L^{-\mathsf T}\mathbf d\) with a unit vector \(\mathbf d\) lies on the surface \(W = c\); in the coordinates \(\mathbf d\) the linear fall is a quadratic form, and its three smallest eigenvectors are the weakest directions. For each trial \(c\) the page evaluates (19a) at 1500 random directions spread over four shells (\(W = c\), \(0.7c\), \(0.4c\) and \(0.15c\)), climbs uphill in \(W(\mathbf F(\mathbf s)) - W(\mathbf s)\) from the three weakest linear directions, both ways, on every shell, and from the worst six random points, and bisects \(c\) thirteen times between 1 and \(10^4\). It runs between frames, 0.2 to 0.35 s in Chromium, and while it runs the page skips the checks that need \(c^\ast\). For the default machine it gives \(c^\ast = 31.6\) at \(T_s = 1\) ms (the flow test gave 31.4), 37.3 at 10 ms and 116 at 40 ms. A longer search in Node.js, with more directions and more climbs, found slightly worse points and gave 36.9 and 114, so the page’s value is 1 to 2 % high, well inside the 10 % margin below. The level grows with the period for two reasons that have nothing to do with a slow controller being better: (19a) asks only that \(W\) be lower after a whole period, a weaker demand than falling at every instant, and the \(\mathbf P\) of (16g) is a different matrix at each period, so the levels are not even measured in the same units. With the continuous gains at 40 ms the search finds no safe level at all, \(c^\ast = 0\): the flip of 7.3 at work.
Two honest limits. The test is a search, not a proof: a bad spot it did not find could exist, which is what a 10 % margin is for: the page hands over only below \(0.9\,c^\ast\). And it is conservative. In simulation the balancer brings back states with \(W\) forty times larger, because a motion may let \(W\) rise for a moment and still come home. \(c^\ast\) marks where catching is certain, not the whole set that can be caught.
The page uses \(c^\ast\) in two places. While balancing, every 0.1 s it checks whether \(W > c^\ast\); if so it runs the equations 4 s ahead with the balancer in charge and, if the motion is still outside \(c^\ast\) then, declares the balancer beaten and starts the rescue of the next two sections. During a swing-up it hands over to the balancer the moment \(W < 0.9\,c^\ast\) (while the cart is to keep rolling, the \(W_\text{red}\) of 11.4).
10Calming: the cart drains the rods’ energy
Once the rods have fallen they swing as a double pendulum hanging from the cart, without friction, for ever. To bring them up again we first bring them to rest, hanging straight down, and that is a problem about energy. Think of a child on a swing. Moving the seat forward while it is going forward feeds the swing; moving it forward while it is coming back takes the energy out. The cart does the same to the rods through the lower hinge.
Treat the cart’s acceleration \(a = \ddot x\) as the input. The rods alone, hinged on a cart that moves as prescribed, carry the energy
\[ E_p = \tfrac12\dot{\boldsymbol\varphi}^{\mathsf T}\mathbf D_{pp}\dot{\boldsymbol\varphi} + f_1\cos\theta_1 + f_2\cos\theta_2, \qquad \mathbf D_{pp} = \begin{pmatrix} d_4 & d_5\cos(\theta_1-\theta_2) \\ d_5\cos(\theta_1-\theta_2) & d_6 \end{pmatrix}, \tag{20} \]the lower-right block of (5) and the whole of (7). Differentiate it along rows 2 and 3 of (9) with \(\ddot x = a\). The centripetal terms do no work, because \(\mathbf C\) is built so that \(\dot{\mathbf D} - 2\mathbf C\) is skew; gravity’s work is already counted in \(V\). What remains is the work the cart does on the rods through the hinge:
Here \(\mathcal P\) is the sideways momentum the rods carry because they turn, the part of row 1 of (9) that belongs to the rods. Accelerating the cart against it pumps energy in, the way you pump a swing. Accelerating with it, \(a = k\mathcal P\), drains energy at the rate \(k\mathcal P^2 \ge 0\), without any plan, from any state: that is calming. The lowest energy is the hanging rest, \(E_p = -(f_1+f_2)\), so calming always ends there. We checked (21) symbolically in Mathematica, where \(\dot E_p + a\mathcal P\) simplifies to zero, and against Åström and Furuta’s law for one rod [6], whose \(\mathrm dE/\mathrm dt = -mul\dot\theta\cos\theta\) is exactly (21) with the upper rod removed.
The page uses \(a = k\mathcal P - 4(x - x_c) - 4\dot x\) with \(k = 2\ \mathrm{kg^{-1}s^{-1}}\), the two extra terms a gentle spring and damper that keep the cart near its post \(x_c\), and \(|a| \le 8\ \mathrm{m/s^2}\); the motor force that produces this acceleration follows from (9), rows 2–3 giving the angular accelerations and row 1 the force. From a fast swinging start the rods are within 0.05 J of hanging rest after 15 to 22 s, the cart never further than 0.65 m from its post. Pumping alone cannot do the reverse job of choosing which way up each rod ends; our own pumping tests ended near hanging, off the rail, or with one rod up and one down. Draining works every time, because the bottom of an energy valley is one place.
11The swing-up: plan, track, catch
From hanging rest, or from any other state, we now want a force history \(u(t)\), \(0 \le t \le T\), that ends with both rods upright and at rest, keeps \(|u| \le u_\text{max}\), and keeps the cart on the rail. Laboratories solve this with two parts working together: a planned motion computed in advance, and linear feedback that keeps the real machine on it. Graichen, Treuer and Zeitz swung a real double pendulum on a cart up from hanging this way and showed it working in experiments [7]; Al Juboori and colleagues review this family of methods [8]. The page follows the same plan with a different way of computing the planned motion. Figure 2 shows the three stages.
11.1Plan
Guessing \(u(t)\) and integrating (9) forward from the start cannot work: section 5 says a small error near the top grows like \(e^{10t}\), tenfold every 0.22 s, so the end of the motion is hopelessly sensitive to its beginning. Instead the page cuts the time into \(M = 10T\) pieces of 0.1 s and treats the state at every cut, \(\mathbf z_1, \dots, \mathbf z_M\), as an unknown along with one constant force \(u_k\) per piece: multiple shooting, the method that Tedrake’s notes describe as the limit of direct transcription [3]. Over one piece, (18) with eight substeps carries \(\mathbf z_k\) to \(\Phi(\mathbf z_k, u_k)\). The unknowns are chosen to make the following residuals as small as possible, in the sense of least squares:
\[ \begin{aligned} &w_d\big(\mathbf z_{k+1} - \Phi(\mathbf z_k, u_k)\big) && \text{obey the equations of motion}\\ &w_T\big(\theta_{1,M} - \theta_1^\ast,\ \theta_{2,M} - \theta_2^\ast,\ \dot\theta_{1,M},\ \dot\theta_{2,M},\ 0.3\,\dot x_M\big) && \text{end upright, at rest}\\ &w_\ell\max(0, |x_k| - 1.5\ \mathrm m),\quad w_u\max(0, |u_k| - u_\text{max}) && \text{stay on the rail, within the motor}\\ &w_r\,u_k,\quad w_s\,(u_k - u_{k-1}) && \text{prefer gentle, smooth forces} \end{aligned} \tag{22} \]with targets \(\theta^\ast = 2\pi\operatorname{round}(\theta/2\pi)\), the nearest upright. The weights \(w_d = 100\), \(w_T = 30\), \(w_\ell = 20\), \(w_u = 1\), \(w_r = w_s = 0.02 \times 60\ \mathrm N/u_\text{max}\) are our choice (the price of force is set in units of the motor's limit, so that a strong motor is not held back by it; for 60 N it is 0.02 per newton); they make a defect of one milliradian cost as much as three milliradians of final error, so the physics wins over the wish. The first guess is a straight line in the angles. Levenberg–Marquardt then improves it, step after step, from the normal equations \(\mathbf J^{\mathsf T}\mathbf J\,\delta = -\mathbf J^{\mathsf T}\mathbf r\) with the Jacobian \(\mathbf J\) of (22) from finite differences of \(\Phi\). A plan is accepted when every defect is below \(3\times10^{-3}\) and the end is within 0.02 rad and 0.02 rad/s of upright rest.
The planning runs in the browser between frames, 8 ms at a time, while the machine keeps moving. That raises a question: a plan is made for one starting state, but by the time it is ready the machine has moved on. The page answers it with determinism. Calming (21) is a fixed law, and the integrator (18) is exact arithmetic, so the page can run the next 3 s of calming ahead of time, plan from the state it finds there, and when the clock arrives check that the machine is at that state to \(10^{-6}\). It always is, unless you pushed it in between; then the plan is dropped and a new one begun. If a plan is not ready in time, or no plan was found (the search starts from one straight-line guess and can fail), the page plans again from further ahead, with the other of two horizons (here 4 s and 6 s), while calming goes on; the search stops after 400 steps.
11.2Track
Playing the plan back open loop ends tens of metres down the rail, for the reason above. So a controller follows the plan. Every 10 ms the page linearises (9) along the planned motion, \(\mathbf s_{k+1} - \mathbf s^\text{plan}_{k+1} \approx \mathbf F_k(\mathbf s_k - \mathbf s^\text{plan}_k) + \mathbf G_k(u_k - u^\text{plan}_k)\), with \(\mathbf F_k, \mathbf G_k\) from finite differences of one 10 ms step, and solves the finite-horizon LQR problem backwards from the end, where the balancer’s own \(\mathbf P\) of (15) is the final cost [3]:
\[ \mathbf K_k = \frac{\mathbf G_k^{\mathsf T}\mathbf P_{k+1}\mathbf F_k}{\rho\Delta + \mathbf G_k^{\mathsf T}\mathbf P_{k+1}\mathbf G_k}, \qquad \mathbf P_k = \mathbf F_k^{\mathsf T}\mathbf P_{k+1}\mathbf F_k + \mathbf Q\Delta - \mathbf F_k^{\mathsf T}\mathbf P_{k+1}\mathbf G_k\,\mathbf K_k, \qquad \mathbf P_M = \mathbf P, \quad \Delta = 0.01\ \mathrm s, \tag{23} \]which is (16g) and (16h) step by step backwards from the end, with a different \(\mathbf F_k, \mathbf G_k\) at every step. The cost of one step is taken from (14) by the rectangle rule, \(\mathbf Q\Delta\) and \(\rho\Delta\), so that it is in the same units as the final cost \(\mathbf P\) (the exact period cost (16e) would need its own integral along the plan at every step; the rectangle rule is our choice, simpler and good enough for a law whose job ends at the hand-over). The force is then
clipped at \(\pm u_\text{max}\). The gains \(\mathbf K_k\) are large where the plan passes near the top and small while the rods still hang, as they should be.
11.3Catch, and what the tests say
Along the way the page watches \(W\) of (19). The moment \(W < 0.9\,c^\ast\) (or \(W_\text{red}\) of 11.4, when the cart is to keep rolling) it hands over to the balancer (16h), which from there is certain to finish. If the plan runs to its end without reaching \(c^\ast\), the balancer gets its chance anyway, and the check of section 9 decides within 4 s whether to start over; in the tests this happened often and the balancer won every time, which is the conservatism of \(c^\ast\) at work. In headless Chromium, with the same frame-by-frame loop as in your browser, 40 random states, any angles, rates up to \(\pm3\) rad/s and cart speeds up to \(\pm0.5\) m/s, all ended balanced at 60 N within 60 s, once with the plan arriving moving and the cart braked to rest afterwards and once with the plan arriving at rest (11.4). A plan took between 0.3 and 9.2 s of browser time. From hanging rest the whole rescue takes about 7 s: 3 s of calming while the plan is computed, 4 s of swing-up. Eight other machines, six random states each, were caught as well, both ways: a heavier cart, heavier or longer or shorter rods, \(u_\text{max}\) = 30 or 120 N, and the loop periods 40 and 60 ms with the gains of (16h), 48 of 48 each time. With the continuous gains at 40 ms none of six was: the rescue brought the rods up every time, and the balancer, which flips at 34.4 ms (7.3), let them fall again. The same study in Node.js, without the calming stage, found a direct plan for only 16 of the 40 states, which is why calming comes first. Forty cases are evidence, not a proof: a very weak motor, a very heavy cart or a push that throws the cart against the end of the rail can still beat it.
11.4Arriving moving
The plan (22) ends with the cart at rest, \(0.3\,\dot x_M\) among the residuals. Nothing in the physics asks for that. Look at (9) once more: it contains \(\ddot x\), the angles and their rates, but neither \(x\) nor \(\dot x\). Add a constant speed \(v\) to the whole motion, \(x \to x + vt\), and every term is unchanged: the rods do not know how fast the cart is going, only how hard it is being accelerated. This is Galileo’s observation that a ship’s cabin under way feels like one at anchor; a waiter balances a tray on a train at full speed exactly as on the platform. So the upright rest is not one state but a whole family,
\[ \mathbf s^\ast(t) = (x_0 + vt,\ 0,\ 0,\ v,\ 0,\ 0), \qquad u^\ast = 0, \qquad \text{for every } x_0 \text{ and } v, \tag{25} \]and the balancer holds any member of it when it measures its error from that member, \(u = -\mathbf K(\mathbf s - \mathbf s^\ast(t))\). The page’s balancer does so. Its reference is either a place, a member of (25) with \(v = 0\) at the target of the buttons, or a cruise, the member with the speed the machine had when the balancer took over.
Why insist, then, that the swing-up end at rest? A plan that must arrive at \(\dot x_M = 0\) spends force on braking the cart in its last second, and if the swing wanted the cart moving, that braking is a change of speed the task never asked for. With the switch After a swing-up on Arrive moving, then brake (the default) the page drops the \(\dot x_M\) residual from (22), so the plan may arrive on any member of (25): the planner’s end condition widens from a point in \((\theta_1, \theta_2, \dot\theta_1, \dot\theta_2, \dot x)\) to a line. The hand-over then tests \(W_\text{red}\), the cost (19) with the \(x\) and \(\dot x\) errors set to zero, because at the moment of hand-over the reference is set to the cart’s present place and speed, which makes those two errors zero by construction; the balancer then holds the moving equilibrium it arrived on, and brakes it to rest (11.5). We measured what this buys, in Node.js with the page’s planner and tracking law, 60 N, \(c^\ast = 30.6\). From 25 calmed starts (the rods near hanging rest, as after section 10) both versions found a plan and both caught 25 of 25; arriving moving cut the force integral \(\int u^2\,\mathrm dt\) from 944 to 844 N² s, 11 % less, and the total change of the cart’s speed from 22.1 to 19.4 m/s, 12 % less, with the same peak force (52 N). The arrival speeds were 0.1 to 0.5 m/s in fifteen cases and 3.7 to 4.2 m/s in ten. From 40 raw random starts, without calming, each version found a plan for the same 15, and arriving moving saved nothing: the speed change was equal (31.2 m/s) and the force integral 6 % larger. So the freedom helps from rest-like starts, modestly, and not from wild ones, where the planner’s effort goes into the rods and the cart’s end speed is the least of its costs. The two switches are there so that you can compare.
Leaving a moving equilibrium. Once the cart cruises, the target buttons no longer jump the reference: a step from a moving equilibrium to a resting one would ask the balancer for an instant stop, a large push and a large lean. Instead the reference glides from the member of (25) it is on to the resting member at the target, along the critically damped motion \(\ddot e + 2\omega\dot e + \omega^2 e = 0\) of the distance \(e = x_\text{ref} - x_\text{target}\), the fastest return without overshoot, from \(e(0) = e_0\) and \(\dot e(0) = v\):
\[ e(\tau) = \left(e_0 + (v + \omega e_0)\,\tau\right)e^{-\omega\tau}, \qquad \omega = 0.5\ \mathrm{s^{-1}}, \tag{26} \]with \(\dot x_\text{ref} = \dot e\) along with it (check: \(\dot e(0) = (v + \omega e_0) - \omega e_0 = v\)). The rate \(\omega\) is our choice; the glide from one metre away at rest comes within a centimetre after 13 s. Its largest acceleration is at the start, \(\ddot e(0) = -\omega^2 e_0 - 2\omega v\): 0.25 m/s² from one metre at rest, 1.25 m/s² when the cart also moves away at 1 m/s. A cart that accelerates at \(a\) can hold its rods only leaning: in the cart’s frame gravity tilts by the angle whose tangent is \(a/g\), and both rods must stand along it. The rule gives 1.5° at 0.25 m/s² and 7.3° at 1.25 m/s². The balancer’s reference keeps \(\theta = 0\) all the same, so it meets the lean as a small error and lets it stand. Measured with the page’s equations and the 10 ms gains, the rods lean at most 0.9° in the first glide and 5.3° in the second, less than the rule says, because the cart cannot take up the reference’s acceleration at once and never reaches its peak; the motor needs at most 0.5 and 2.7 N. When the reference is within a centimetre of the target the panel says Balancing again.
Arriving moving has a price: the cart keeps going. Until 5 November 2026 the page let it, and in the browser tests of 11.3 the cart got between 6 and 200 m from where it started within the 60 s of a run (with the plan made to arrive at rest, it never left \(\pm1.54\) m). A real rail has an end, so the page now brings the cart to rest after the catch, carefully. With the braking, the same tests keep the cart within 1 to 16 m of its start. How carefully is the subject of the next subsection.
11.5Coming to rest without being knocked over
Carry a tray of stacked glasses off a moving walkway. You cannot stop dead: your feet would stop and the glasses would not. You lean back a little and brake over a few steps, and you know without thinking how hard you may brake before the stack topples. The balancer has the same problem, and it can answer it with numbers.
A change of speed is a shove. The balancer never sees the cart’s speed as such; it sees only the error \(\mathbf e = \mathbf s - \mathbf s^\ast\) from the member of (25) it holds. Suppose at one look we tell it that the reference now moves at \(v - \Delta\) instead of \(v\). Nothing in the machine has changed, but the error has: its fourth component, \(\dot x - v\), has grown by \(\Delta\). By (25) the balancer cannot tell this from a sudden shove that made the cart \(\Delta\) faster: lowering the reference speed is a kick in the error, \(\mathbf e \to \mathbf e + \Delta\,\mathbf e_4\) for \(v > 0\), with \(\mathbf e_4 = (0, 0, 0, 1, 0, 0)\). The question “how hard may we brake?” becomes “how big a kick may we give?”.
What a kick costs. The remaining cost (19) after the kick is a quadratic in \(\Delta\). Expanding \((\mathbf e + \Delta\mathbf e_4)^{\mathsf T}\mathbf P(\mathbf e + \Delta\mathbf e_4)\) and using \(\mathbf P = \mathbf P^{\mathsf T}\):
\[ W(\mathbf e + \Delta\,\mathbf e_4) = W(\mathbf e) + 2\Delta\,(\mathbf P\mathbf e)_4 + \Delta^2 P_{44} . \tag{27} \]Section 9 gives the rule that makes a kick safe. After it, the reference is again a member of (25), moving at a constant speed until the next look, so the certificate (19a) applies to it exactly: if \(W \le c^\ast\) right after the kick, \(W\) falls over the next period, and the balancer is certain to win. So we choose a share \(\gamma \le 0.9\) of the certified level and allow, at each look, the largest kick with \(W(\mathbf e + \Delta\mathbf e_4) \le \gamma c^\ast\). Setting (27) equal to \(\gamma c^\ast\) and taking the larger root (Wolfram’s Solve on the symbolic quadratic gives it in this form):
For a cart moving left the signs of \(\Delta\) and \((\mathbf P\mathbf e)_4\) turn round. The page takes the bite \(\min(|v|, \Delta_\text{max})\) at every look; if no bite fits (the square root is imaginary or the root negative), it keeps the reference it has, and the certificate goes on. When the speed reaches zero the reference becomes a fixed place where the cart is. The number \(\varepsilon\) is the answer to the question in its plainest form: how much speed may be taken away in one go from a balanced cart, with certainty. With the default machine, Wolfram’s discrete Riccati solution gives \(P_{44} = 172.46\) at \(T_s = 10\) ms (the gains it finds equal the page’s to every printed digit), and with \(c^\ast = 37.3\) the certified bite is \(\varepsilon = 0.44\) m/s for \(\gamma = 0.9\) and 0.33 m/s for \(\gamma = 0.5\); at 40 ms, \(P_{44} = 173.60\), \(c^\ast = 116\), \(\varepsilon = 0.78\) and 0.58 m/s. The page shows \(\varepsilon\) for \(\gamma = 0.5\) among its readouts. In control engineering this kind of supervisor is a reference governor: at each instant it moves the command towards where we want it by the largest step that keeps a Lyapunov function below a level whose sublevel set is invariant [12]; the bite (27a) is that step, worked out in closed form for our \(W\).
Where to put the reference. Equation (25) leaves the place of the reference free, and that freedom matters. If we keep the old place, the braking cart trails behind a reference that moves on, and \(W\) charges for the trailing distance, though the rods do not care. So at each look the page also moves the reference to the cart, \(x_0 = x\), which sets the error in \(x\) to zero and lets the whole allowance go into speed. Is that allowed? Yes: the new reference is still a member of (25), and the bite is chosen with this new error, so (27) and (19a) still hold. (This step is ours: the reference governors of [12] move the command along a line towards its target; here the command is a whole equilibrium, and we choose two of its numbers at once.)
A bite at every look: steady braking. Take the largest bite at every look and the braking settles into a rhythm. Write \(\boldsymbol\Pi\) for the matrix that sets the \(x\) component of an error to zero, \(\mathbf A_\text{cl} = \boldsymbol\Phi - \boldsymbol\Gamma\mathbf K\) for the linear loop over one period (16a), and \(\mathbf e^+\) for the error just after a bite. Over a period \(\mathbf e^+\) becomes \(\mathbf A_\text{cl}\mathbf e^+\); the next look moves the reference to the cart and bites again: \(\mathbf e^+ \to \boldsymbol\Pi\mathbf A_\text{cl}\mathbf e^+ + \Delta\,\mathbf e_4\). In the rhythm \(\mathbf e^+\) repeats, so
\[ \mathbf e^+ = \Delta\,(\mathbf I - \boldsymbol\Pi\mathbf A_\text{cl})^{-1}\mathbf e_4, \qquad W(\mathbf e^+) = \gamma c^\ast \;\Rightarrow\; \Delta = \sqrt{\frac{\gamma c^\ast}{\mathbf z^{\mathsf T}\mathbf P\,\mathbf z}},\ \ \mathbf z = (\mathbf I - \boldsymbol\Pi\mathbf A_\text{cl})^{-1}\mathbf e_4, \qquad a = \frac{\Delta}{T_s} . \tag{28} \]Evaluated in Wolfram for the default machine at 10 ms: \(a = 0.65\) m/s² for \(\gamma = 0.5\) and 0.88 m/s² for \(\gamma = 0.9\) (keeping the old place instead gives only 0.24 and 0.32 m/s², and the best possible place, the one that minimises \(W\), only 1 % more than placing the reference at the cart); at 40 ms, 1.14 and 1.52 m/s². Two checks come out of (28) by themselves. In the rhythm both rods lean back by the same angle, 3.82° at 0.65 m/s², and \(\arctan(0.65/9.81) = 3.81°\); the force is 1.80 N, and \((m_0+m_1+m_2)\,a = 2.75 \times 0.654 = 1.80\) N. Those are the two laws of problem 12.6, which Wolfram derives from the Lagrangian (4)–(7) with \(\ddot x = a\) and the rods at rest relative to the cart: \(\tan\theta_1 = \tan\theta_2 = a/g\) and \(u = (m_0 + m_1 + m_2)\,a\). The linear sampled loop knows nothing of either law, and finds both.
How much can it take before it is lost? The certified bite says where losing is impossible, not where it begins. That edge lies outside every certificate we have, so we measured it, on the true loop: the nonlinear equations (9), the balancer looking every \(T_s\) and holding its push, the motor clipped at 60 N, in Node.js with equations written separately from the page’s. One sudden drop of the reference speed, from a balanced cruise: the balancer catches every drop up to 8.48 m/s at 10 ms and 7.36 m/s at 40 ms, and a scan in steps of 0.1 m/s found no drop below these that it loses. At the edge the rods lean 49° and the motor sits at its limit. A steady deceleration of the reference, long enough to settle: caught up to 9.2 m/s² with the place kept and 12.0 m/s² with the place at the cart at 10 ms (7.1 and 9.0 m/s² at 40 ms), the rods leaning 56°. So the certified bite of 0.44 m/s is about nineteen times smaller than the largest drop that is survived, for the reason section 9 gave: \(c^\ast\) is a set the cost never leaves, while the balancer also brings home many motions in which the cost rises for a while. And past the edge? Not chaos. The rods leave the region from which the balancer can bring them back, run away along the falling direction of section 5 at the rate \(\sigma_1\), and fall; then the rescue of sections 10 and 11 starts over.
What the page does, and what it costs. With Arrive moving, then brake (the default) the page catches the rods on whatever member of (25) the plan arrives on, then brakes with (27a) at \(\gamma = 0.5\), our choice: half the certified level, about one eighteenth of the deceleration the balancer measurably survives. Measured on the page, 8 random falls caught at 10 ms all came to rest: from 3.5 m/s in 5.4 s over 13 m, with the rods leaning at most 3.8°, the force at most 11 N (at the first bite, while the error left by the hand-over decays) and \(W\) never above \(0.5\,c^\ast\) after a bite, except right after a hand-over, when the governor waits below \(0.9\,c^\ast\) until a bite fits. The distance follows from (28): a cart caught at speed \(v\) rolls about \(v^2/2a\) further. Two honest limits. The guarantee is as good as \(c^\ast\), which is a search, not a proof (section 9). And (28) is a linear prediction; on the page and in Node.js the braking time and the lean come within a few per cent of it.
12On the screen, and what is left out
Write (9) as six first-order equations for \(\mathbf s = (\boldsymbol\theta, \dot{\boldsymbol\theta})\): \(\dot{\mathbf s} = \mathbf f(\mathbf s, u)\), whose first three components are \(\dot{\boldsymbol\theta}\) and whose last three are \(\ddot{\boldsymbol\theta} = \mathbf D^{-1}(\mathbf Hu - \mathbf C\dot{\boldsymbol\theta} - \mathbf G)\). One step of the classical fourth-order Runge–Kutta method of length \(h\) samples the slope four times, at the start, twice at the middle and at the end,
\[ \mathbf k_1 = \mathbf f(\mathbf s_n),\ \ \mathbf k_2 = \mathbf f(\mathbf s_n + \tfrac h2\mathbf k_1),\ \ \mathbf k_3 = \mathbf f(\mathbf s_n + \tfrac h2\mathbf k_2),\ \ \mathbf k_4 = \mathbf f(\mathbf s_n + h\mathbf k_3),\qquad \mathbf s_{n+1} = \mathbf s_n + \tfrac h6(\mathbf k_1 + 2\mathbf k_2 + 2\mathbf k_3 + \mathbf k_4), \tag{18} \]with \(u\) held fixed through the step, as the sampled controller holds it. Its error per unit time falls as \(h^4\).
The page integrates (9) with (18) at a fixed step of 1 ms, several steps per screen frame, in real time. The walkthrough below sets each equation of this note beside the lines of the page’s script that compute it. With the controller off, the same integrator tested against a high-accuracy solver keeps the energy \(T + V\) constant to \(2\times10^{-10}\) J over 10 s; against a 30-digit reference solution in Mathematica, the angles of freely whirling rods agree to \(3\times10^{-6}\) rad after 2 s, and halving the step cuts that error 18-fold, the \(2^4\) of a fourth-order method. The page runs one of three laws at a time: the balancer (16h) about a member of the family (25), calming (21) or tracking (24), under the rules of sections 9 and 11. When Let them fall is on and the balancer is beaten, the motor stops pushing (\(u = 0\)) and the page keeps solving (9) exactly as before: the rods fall all the way, swing down through the slot in the middle of the cart, between its two rails, and go on swinging as a double pendulum hanging from a free cart, without friction, so they never come to rest. The view widens to keep them in sight. Positions are drawn to scale; the view follows the cart.
Left out: friction in the hinges and wheels, the motor’s own dynamics (a real motor cannot change its force instantly), sensor noise, and a hard end of the rail (the planner keeps the cart within ±1.5 m; calming and balancing do not). Each is a known extension of this model [1, 3]. Every number on this page that is not derived or taken from a source is a choice of ours, and the text says so where it is made: the weight on \(x\), the ranges of the sliders, \(u_\text{max} = 60\) N, the planner’s weights and rail limit, the calming gains, the glide rate of (26), the share \(\gamma = 0.5\) of the certified level in the braking (27a), the spring and the largest acceleration of your hand in (29), the rectangle rule in (23), and the shells, directions and climbs of the certificate (19a).
12.1 Carry out the three steps of section 4 for the coordinate \(x\) and show that row 1 of (9) is the sum of the sideways forces on cart and rods.
12.2 Show from (12)–(13) that scaling both lengths by \(\lambda\) scales both fall rates by \(\lambda^{-1/2}\).
12.3 A blow at the middle hinge instead of the top: repeat the derivation of (17) and show that the upper rod then starts turning the other way from the lower rod.
12.4 Derive (21): differentiate (20) along rows 2 and 3 of (9) with \(\ddot x = a\), and show that the terms with \(d_5\sin(\theta_1-\theta_2)\) cancel in pairs.
12.5 Derive (16c) from (16a) for one mode, solve \(\lambda = -1\) to get (16d), and show that \(T^\ast \to 2/\gamma\) as \(\sigma \to 0\). For a stable mode (\(\sigma < 0\)) with \(\gamma T_s = 2\), what does the loop do?
12.6 Show that (25) solves (9) with \(u = 0\) for every \(v\), and that a cart accelerating steadily at \(a\) holds both rods still only if \(\tan\theta_1 = \tan\theta_2 = a/g\). What motor force does that take?
12.7 Derive (27a) from (27). Then show that with \(\mathbf e = \mathbf 0\) the certified bite does not depend on the loop period except through \(P_{44}\) and \(c^\ast\), and use (28) to estimate how far a cart caught at 3 m/s rolls before it rests at \(\gamma = 0.5\) and 10 ms. Why does braking with the reference kept at its old place come out almost three times slower?
12.7Sources
- A. Bogdanov, Optimal Control of a Double Inverted Pendulum on a Cart, Technical Report CSE-04-006, OGI School of Science and Engineering, OHSU, 2004.
- L. Moysis, Balancing a double inverted pendulum using optimal control and Laguerre functions, technical report, Aristotle University of Thessaloniki, 2016: the model of [1] written out in full, with its linearisation and LQR gains.
- R. Tedrake, Underactuated Robotics, chapter 3: Acrobots, Cart-Poles, and Quadrotors, MIT course notes, 2024: manipulator equations, linearisation, controllability and LQR balancing of the cart-pole; and chapter 10: Trajectory Optimization: direct transcription as the limit of multiple shooting, and finite-horizon LQR along a planned trajectory.
- F. Grasser, A. D’Arrigo, S. Colombi and A. Rufer, “JOE: a mobile, inverted pendulum”, IEEE Transactions on Industrial Electronics 49, 107–114 (2002).
- K. Shiozawa, J. Lee, M. Russo, D. Sternad and N. Hogan, “Frequency-dependent force direction elucidates neural control of balance”, Journal of NeuroEngineering and Rehabilitation 18, 145 (2021).
- K. J. Åström and K. Furuta, “Swinging up a pendulum by energy control”, IFAC World Congress, 1996; Automatica 36, 287–295 (2000): the energy law for one rod on an accelerated pivot, equations (1)–(4).
- K. Graichen, M. Treuer and M. Zeitz, “Swing-up of the double pendulum on a cart by feedforward and feedback control with experimental validation”, Automatica 43, 63–71 (2007): the swing-up as a boundary-value problem plus stabilising linear feedback, run on a laboratory machine.
- A. M. Al Juboori, M. T. Hussein and A. S. G. Qanber, “Swing-up control of double-inverted pendulum systems”, Mechanical Sciences 15, 47 (2024): a review of swing-up methods, with the hanging start (0, π, π) and LQR gains computed along the planned motion.
- C. F. Van Loan, Computing Integrals Involving the Matrix Exponential, Technical Report TR 77-298, Department of Computer Science, Cornell University, December 1976 (IEEE Transactions on Automatic Control 23, 395–404, 1978): the integrals (1.1)–(1.4), Theorem 1 and its proof by the block differential equations, and the identity \(\mathbf Q = \mathbf F_3^{\mathsf T}\mathbf G_2\) of the algorithm’s step 2, our (16f).
- Z.-C. Guo, E. K.-W. Chu, X. Liang and W.-W. Lin, “A decoupled form of the structure-preserving doubling algorithm with low-rank structures”, arXiv:2005.08288 (2020): the discrete Riccati equation in the form \(-\mathbf X + \mathbf A^{\mathsf T}\mathbf X(\mathbf I + \mathbf G\mathbf X)^{-1}\mathbf A + \mathbf H = \mathbf 0\) (their (3)) and the three recursions of the structure-preserving doubling algorithm (their (4)), which the page runs in full (its decoupled low-rank form is for large problems and is not used here).
- Wolfram Research,
DiscreteLQRegulatorGains, Wolfram Language documentation: gains with sampling period \(\tau\) for a continuous-time model, from its zero-order-hold model and a discrete equivalent of the continuous cost with a cross-coupling weight, used to check (16h). - E. Garone, S. Di Cairano and I. V. Kolmanovsky, “Reference and command governors for systems with constraints: A survey on theory and applications”, Automatica 75, 306–328 (2017); open manuscript, MERL TR2016-102: the scalar reference governor (their (9)) and the Lyapunov-function-based reference governor (their (46)), read for 11.5.
- E. Lauga and T. R. Powers, “The hydrodynamics of swimming microorganisms”, Reports on Progress in Physics 72, 096601 (2009): resistive force theory, eqs. (12) and (26)–(31).
- M. Gazzola, L. H. Dudte, A. G. McCormick and L. Mahadevan, “Forward and inverse problems in the mechanics of soft filaments”, Royal Society Open Science 5, 171628 (2018): eq. (43), the slender-body drag of the open-source code PyElastica.
- J. Gray and G. J. Hancock, “The propulsion of sea-urchin spermatozoa”, Journal of Experimental Biology 32, 802–814 (1955).
- MuJoCo documentation, “Fluid forces”: viscous and quadratic drag relative to the wind, added.
- iforce2d, “Box2D tutorial: buoyancy”: drag and lift on every edge of a polygon facing the flow.
- Wikipedia, “Drag coefficient”: wires and cables 1.0–1.3, a flat plate across the flow 1.28.
- Wikipedia, “Viscosity”: air 18.5 μPa s at 25 °C, water 1.0016 mPa s at 20 °C.
- Wikipedia, “Glycerol”: 1.261 g/cm³, 1.412 Pa s at 20 °C.
- Wikipedia, “Honey”: 1.38–1.45 kg/L at 20 °C; 70 poise at 30 °C with 16 % water.
- Wikipedia, “Log wind profile”: \(u = (u_*/\kappa)\ln(z/z_0)\), \(z_0\) 0.01–0.05 m over grassland.
- Wikipedia, “Bickley jet”: the plane laminar jet, \(F = \alpha\tanh\alpha\eta\), \(\eta = y/x^{2/3}\).
- Wikipedia, “Airy wave theory”: the orbital velocities and \(\omega^2 = gk\tanh kh\).
- Wikipedia, “Morison equation”: \(F = \rho V\dot u + \rho C_a V(\dot u - \dot v) + \tfrac12\rho C_d A(u - v)|u - v|\), the first term the Froude–Krylov force; its limits.
- Wikipedia, “Added mass”: \(\tfrac23\pi r^3\rho\) for a sphere.
- C. Batty, F. Bertails and R. Bridson, “A fast variational framework for accurate solid-fluid coupling”, ACM Transactions on Graphics 26(3), 100 (2007): the pressure step as the least kinetic energy, volume weights (4), the force on a body (8)–(10).
- C. S. Peskin, “The immersed boundary method”, Acta Numerica 11, 479–517 (2002): spreading and interpolation (4.18)–(4.19), the power identity (5.2), the four-point kernel (6.26)–(6.27).
- M. Schäfer and S. Turek, “Benchmark computations of laminar flow around a cylinder”, in Flow Simulation with High-Performance Computers II, Vieweg (1996): test case 2D-1, table 3, \(c_D\) between 5.57 and 5.59.
- J. N. Sørensen and W. Z. Shen, “Numerical modeling of wind turbine wakes”, Journal of Fluids Engineering 124, 393–399 (2002): blade forces from the local velocity, distributed along lines in a Navier–Stokes solver (the actuator line). Only the abstract was available to us.
13Air: the push of a flow
Stand at a bus stop as a lorry goes by: the gust pushes you, then pulls. Wade into a river: the water leans on your legs, harder the faster it runs, and harder on a leg standing across the stream than on a pole held along it. This section gives the machine such a fluid. Its particles move with a velocity field \(\mathbf v_f(x, y, t)\), chosen in the menu under the figure, and every piece of the cart and the rods that moves at a different velocity is pushed. The flow is given: the bodies feel it but do not change it. That one-way coupling is the right picture while the bodies are small next to the scale of the flow; section 13.5 says what it leaves out. Under a liquid, where the flow is also solved so that the bodies push the water and the water pushes back, the machine is project 06, with rods and a motor made for it.
13.1The media
A fluid enters through two numbers: its density \(\rho\), which sets how much momentum it carries, and its viscosity \(\mu\), which sets how strongly neighbouring layers drag on each other. Their ratio \(\nu = \mu/\rho\) is the kinematic viscosity. Which of the two wins near a body of size \(d\) moving through the fluid at speed \(|\mathbf w|\) is told by the Reynolds number \(\mathrm{Re} = \rho\,|\mathbf w|\,d/\mu\).
| Medium | \(\rho\) (kg/m³) | \(\mu\) (Pa s) | \(\nu\) (m²/s) | Re of a 2 cm rod at 1 m/s |
|---|---|---|---|---|
| air, 25 °C [19] | 1.184 | \(1.85\times10^{-5}\) | \(1.56\times10^{-5}\) | 1280 |
The density of air follows from the ideal gas law at 1 atm, \(\rho = p/(R_s T) = 101\,325/(287.05 \times 298.15)\) kg/m³; water (998.2 kg/m³, \(1.0016\times10^{-3}\) Pa s [19]), glycerol and honey are in project 06.
13.2From the Stokes equations to a force per metre
For slow, small or very viscous motion the fluid obeys the Stokes equations, with pressure \(p\):
\[ \mathbf 0 = -\nabla p + \mu\,\nabla^2\mathbf v, \qquad \nabla\cdot\mathbf v = 0 . \tag{31} \]They drop the fluid's inertia, which is right while \(\mathrm{Re} \ll 1\). A piece of surface with outward normal \(\mathbf n\) then feels the traction, a force per unit area,
\[ \mathbf f_s = \boldsymbol\sigma\cdot\mathbf n, \qquad \boldsymbol\sigma = -p\,\mathbf I + \mu\left(\nabla\mathbf v + \nabla\mathbf v^{\mathsf T}\right), \tag{32} \]and integrated around a rod it becomes a force per metre of rod. The simplest solution of (31) is the flow made by a point force \(\mathbf F\), the Stokeslet, \(\mathbf v = (\mathbf I/r + \mathbf x\mathbf x^{\mathsf T}/r^3)\,\mathbf F/(8\pi\mu)\) with \(p = \mathbf F\cdot\mathbf x/(4\pi r^3)\); Wolfram confirms that it satisfies (31) everywhere but at the point itself. Picture a rod as a chain of small beads, each dragging its neighbours along with its Stokeslet [13]. Adding up the chain (the sum over the other beads is \(2\ln(L/4a)\), as Wolfram integrates it) shows that a rod pulled sideways meets twice the resistance of a rod pulled lengthwise. With \(\mathbf w = \mathbf v_{\text{body}} - \mathbf v_f\) the velocity of a rod element through the fluid, \(\mathbf t\) the direction of the rod and \(\mathbf n\) its normal, the force per metre of rod is
\[ \mathbf f = -\xi_\parallel\,(\mathbf w\cdot\mathbf t)\,\mathbf t - \xi_\perp\,(\mathbf w\cdot\mathbf n)\,\mathbf n, \qquad \xi_\perp = 2\,\xi_\parallel = \frac{4\pi\mu}{\ln(L/r)} , \tag{33} \]resistive force theory, in the form of [13, eq. (31)] and of the open-source rod code PyElastica [14, eq. (43)]; \(r\) is the radius of the rod, 1 cm here. The thickness enters only through the logarithm: to a slow flow a rod is mostly a line. Formula (33) is the first term of an expansion in \(1/\ln(L/r)\), and at \(L/r = 50\) the next term changes \(\xi\) by tens of percent (the bead sum gives \(\ln(L/4a)\) where (33) has \(\ln(L/r)\)); that is its accuracy. Gray and Hancock used the same picture in 1955 to explain how a sperm cell swims [15].
Air and water at everyday speeds are not slow: by the table of 13.1, a 2 cm rod moving at 1 m/s has \(\mathrm{Re} \approx 1300\) in air and 20 000 in water. Inertia then wins: the fluid piles up in front of the rod and leaves a wake behind it, and the push grows with the square of the speed across the rod. Per metre of rod,
\[ \mathbf f_q = -\tfrac12\,\rho\,C_D\,d\;|\mathbf w\cdot\mathbf n|\,(\mathbf w\cdot\mathbf n)\,\mathbf n , \tag{34} \]with \(d = 2r\) and \(C_D = 1.2\), a long cylinder across the flow (1.0–1.3 for wires and cables [18]). The page adds (33) and (34), as the physics engine MuJoCo adds its viscous and its quadratic drag [16], so that whichever is larger rules. Across the rod they are equal where \(\mathrm{Re} = 8\pi/(C_D\ln(L/r)) \approx 5\); the line above the menus says which one rules at the top of the upper rod now.
The cart is a box 0.36 m long, 0.12 m high and 0.2 m deep. It gets the Stokes drag of a sphere of its mean half-size, \(6\pi\mu r_{\text{eq}}\,w\) with \(r_{\text{eq}} = 0.113\) m, as MuJoCo does [16], and the pressure drag \(\tfrac12\rho C_D h b\,|w|\,w\) of its face (\(C_D = 1.28\), a flat plate across the flow [18]), where \(w\) is the cart's speed minus the flow averaged over its height. Only the sideways part counts; the rail takes the rest. The physics tutorial of the engine Box2D treats every edge of a body this way, a force on each edge that faces the flow [17].
13.3The flows
\(x\) is measured along the rail, \(y\) upwards from it. The floor lies 3.3 m below the rail, room for the longest rods hanging, and in the water flows the surface lies 3.4 m above it. \(U\) is the speed slider. Where a flow has a stream function \(\psi\), its velocity is \((\partial_y\psi, -\partial_x\psi)\), so \(\nabla\cdot\mathbf v = 0\) holds by construction; Wolfram confirms \(\nabla\cdot\mathbf v = 0\) for every flow below and tests which ones solve the Navier–Stokes equations exactly (their vorticity equation \(\partial_t\omega + \mathbf v\cdot\nabla\omega = \nu\nabla^2\omega\)).
| Flow | Velocity field | What it is |
|---|---|---|
| steady breeze | \((U, 0)\) | exact |
| gusty wind | \(U\,g(t)\,(1, 0)\), \(g = 1 + 0.5\sin 2\pi(0.13\,t) + 0.3\sin(2\pi\,0.37\,t + 1) + 0.2\sin(2\pi\,0.71\,t + 2)\) | exact, driven by a pressure gradient that changes in time |
| wind over grass | \(u = U\ln(z/z_0)/\ln(3.3\text{ m}/z_0)\), \(z = y + 3.3\) m, \(z_0 = 3\) cm [22] | the measured mean of turbulent wind near the ground, not a Navier–Stokes solution |
| a lorry passing | \(U\,\ell(\tau)\,(1, 0)\), \(\ell = e^{-((\tau-2)/0.45)^2} - 0.35\,e^{-((\tau-3.1)/0.6)^2}\), \(\tau = t \bmod 12\) s | a pulse of the right size and length, the same over the few metres in view; made up, not solved |
| a fan | Bickley's plane jet [23], \(\psi = 6\nu_J\alpha\,x'^{1/3}\tanh(\alpha y'/x'^{2/3})\), \(x' = x + 6\) m, \(y' = y - 0.6\) m | solves Bickley's boundary-layer equation exactly (Wolfram); \(\alpha\) makes it 0.35 m wide at the cart, the eddy viscosity \(\nu_J\) makes its axis speed \(U\) there |
| a rolling eddy | a Lamb–Oseen vortex, \(u_\theta = \tfrac{\Gamma}{2\pi r}\bigl(1 - e^{-r^2/r_c^2}\bigr)\), \(r_c^2 = 0.45^2\text{ m}^2 + 4\nu t\), carried by a wind \(0.4\,U\); a new one every 20 s | exact |
| simple shear | \((U y/1\text{ m}, 0)\) | exact |
| two opposed jets | \(U\,(x, -y)/1\) m, a stagnation point at the rail | exact |
| Taylor–Green cells | \(U e^{-2\nu\pi^2 t}(\sin\pi x\cos\pi y,\ -\cos\pi x\sin\pi y)\), lengths in m | exact; in honey it fades by a factor e in about 10 s, in air not at all |
| a Kármán vortex street | a wind \(U\) and two staggered rows of vortices, spacing 1.6 m, rows 0.45 m apart (von Kármán's ratio 0.281), moving at \(0.8\,U\), each core smoothed over about 9 cm | a kinematic picture of the wake of a pole, divergence-free, not a Navier–Stokes solution |
| your own field | \((u(x, y, t),\ v(x, y, t))\), the two formulas you type under the menu | whatever you write; the page measures its divergence \(\partial u/\partial x + \partial v/\partial y\) at every arrow and says whether a liquid could flow so |
13.4Into the equations of motion
The force (33) + (34) acts all along both rods. By virtual work each element counts towards the generalised force of a coordinate as much as it moves when that coordinate moves:
\[ Q_i = \int \mathbf f\cdot\frac{\partial\mathbf r}{\partial q_i}\,ds, \qquad \mathbf D(\boldsymbol\theta)\,\ddot{\boldsymbol\theta} + \mathbf C\,\dot{\boldsymbol\theta} + \mathbf G = \mathbf H u + \mathbf Q , \tag{35} \]with \(\mathbf r(s) = (x + s\sin\theta_1,\ s\cos\theta_1)\) on the lower rod and \((x + L_1\sin\theta_1 + s\sin\theta_2,\ L_1\cos\theta_1 + s\cos\theta_2)\) on the upper. So \(Q_x\) is the whole sideways push on cart and rods, and \(Q_{\theta_1}\) counts the push on the upper rod with the lever \(L_1\). The page evaluates (35) by Gauss–Legendre quadrature, three panels of eight points on each rod. The fluid's power on the machine is
\[ P = \sum_i Q_i\,\dot q_i = \int \mathbf f\cdot\mathbf v_{\text{body}}\,ds, \qquad E - E_0 = W_{\text{motor}} + E_{\text{ext}} + \int P\,dt , \tag{36} \]an identity Wolfram checks for (33). \(P\) is negative while a still fluid brakes the rods; a wind can make it positive and pour energy in. The energy book of the data panel carries \(\int P\,dt\) as one more row, summed with the weights that RK4 gives the state.
In a liquid the rods also float a little: each metre is pushed up by \(\rho g\pi r^2\), at its middle. That lowers the gravity constants of (7) to
\[ f_1 = \left(\tfrac{m_1}{2} + m_2\right)L_1 g - \rho g\pi r^2\left(\tfrac{L_1^2}{2} + L_1 L_2\right), \qquad f_2 = \tfrac{m_2 L_2}{2}\,g - \rho g\pi r^2\,\tfrac{L_2^2}{2} . \tag{37} \]In water this takes 31 % off the weight of the default rods; a rod lighter than the liquid would stand up by itself. The controller is redesigned with (37), since an engineer knows what the machine stands in. It is also told the flow, and balances the rods where the flow and gravity balance (project 06, 13.8).
How it is checked. The data panel recomputes \(\mathbf Q\) from the stored state by Simpson's rule with 400 slices per rod. In Wolfram, at a fixed test state, an adaptive integral of (35) agrees with the page's 24 points to all 15 digits for the swell and the ground wind and to \(7\times10^{-5}\) of the force or better for the jet, the eddy, the cells and the vortex street; what is left is the kink of \(|w_n|\,w_n\) where the flow across a rod changes sign. With 8 points per rod the vortex street was 1.2 % off, which is why the page uses 24. The honesty tests replay every stored step bit for bit, and an independent reference, written from this note with the published table of Gauss points, agrees to \(10^{-15}\) in a vortex street, in a swell and with the cart in your hand in a breeze. For the added mass of 13.5 it takes the other road, the Christoffel symbols of \(\mathbf M_a\), and still agrees to \(10^{-15}\), in still water and in a swell.
13.5The water a rod carries, and the push of an accelerating flow
Push a hand sideways through a bath and the water does more than resist: some of it comes along and has to be speeded up too. Potential flow makes this exact. A cylinder of radius \(a\) moving across still fluid at speed \(U\) sets the fluid moving with kinetic energy \(\tfrac12\rho\pi a^2 U^2\) per metre (Wolfram integrates \(\tfrac12\rho|\nabla\phi|^2\) for \(\phi = -Ua^2\cos\vartheta/r\); the same integral for a sphere gives \(\tfrac23\pi a^3\rho\), the textbook value [26]). So each metre of rod carries an added mass \(\mu_a = C_a\rho\pi r^2\) with \(C_a = 1\), but only for motion across it: a thin rod moving along itself sets almost no water moving, as an oar slices edge-on. The water's share of the kinetic energy is
\[ T_a = \tfrac12\,\mu_a\!\int (\mathbf v\cdot\mathbf n)^2\,ds + \tfrac12\,m_c\,\dot x^2 = \tfrac12\,\dot{\mathbf q}^{\mathsf T}\mathbf M_a(\boldsymbol\theta)\,\dot{\mathbf q} , \tag{38} \]where \(m_c = \rho V_e\,\kappa_x/(2 - \kappa_x)\) is the added mass of the cart taken as an ellipsoid of its size, semi-axes 0.18, 0.06 and 0.10 m, with Tuckerman's \(\kappa_x = 0.2876\) as MuJoCo computes it [16]: 0.76 kg in water, half the cart's own mass. \(\mathbf M_a\) depends on the angles because \(\mathbf v\cdot\mathbf n\) does: when the cart moves, an upright rod moves across itself and carries its full share of water, a rod lying along the rail moves along itself and carries almost none. Lagrange's equations with \(T + T_a\) give the velocity terms \(\mathbf C_a\) of (40); Wolfram checks that they keep \(T_a\) constant when nothing else acts, and that they equal the terms built from the Christoffel symbols of \(\mathbf M_a\), the road the independent reference takes.
A flow that speeds up pushes too. Its own pressure gradient, \(-\nabla p = \rho\,D\mathbf v_f/Dt\) with \(D\mathbf v_f/Dt = \partial_t\mathbf v_f + (\mathbf v_f\cdot\nabla)\mathbf v_f\) the acceleration of the fluid's particles, acts on the volume of the rod (the Froude–Krylov force), and the water the rod would carry is accelerated by the flow as well. Per metre of rod, across it, as in Morison's equation [25],
\[ f_a = \left(\rho\pi r^2 + \mu_a\right)\frac{D\mathbf v_f}{Dt}\cdot\mathbf n , \tag{39} \]and on the cart \((\rho V_e + m_c)\) times the flow's acceleration along the rail. With both, the equations of motion (9) become
\[ \bigl(\mathbf D + \mathbf M_a\bigr)\,\ddot{\mathbf q} + \mathbf C\,\dot{\boldsymbol\theta} + \mathbf C_a(\boldsymbol\theta, \dot{\mathbf q}) + \mathbf G = \mathbf H u + \mathbf Q , \tag{40} \]with (39) inside \(\mathbf Q\). The page takes \(D\mathbf v_f/Dt\) by central differences 0.1 mm and 0.1 ms wide; Wolfram's exact derivative of each flow at a test point agrees to \(6\times10^{-7}\) of it or better. The controller is redesigned with \(\mathbf M_a\), as with the buoyancy, and so are its certificate and the swing-up planner, which use the same machine; the flow they are still not told. In water, buoyancy and added mass together slow both fall rates of (13) by 17 %: from 10.39 to 8.61 and from 4.10 to 3.40 per second.
Left out (with the given flow). The parts of the added mass that mix the rod's own motion with the flow's velocity as the rod turns; the rods' own wakes and the vortices they shed; the bodies changing the flow (project 06 adds these two, in its section 13.6); the water's surface; and the rail's own drag. Morison's equation is itself an approximation, poor where drag and inertia are of the same size [25]. In air all of this is small: at 8 m/s, the top of the slider, (34) gives about 0.9 N on every metre of rod across the wind.
13.6Under a liquid
In water, glycerol and honey this machine cannot lift its rods: a hanging rod must swing over the top faster than a liquid lets a 0.75 m rod turn, and nine tenths of the motor's work heat the liquid round the cart. Project 06 measures this, builds a machine that can (shorter, heavier rods, a 400 N motor, a slim cart), solves the water round it, and tells the controller the flow.
13.1 Upright rods at rest in a steady breeze \(U\), with one \(\xi_\perp\) for both rods and (33) alone: show from (35) that \(\mathbf Q = \xi_\perp U\,\bigl(L_1 + L_2,\ \tfrac12 L_1(L_1 + 2L_2),\ \tfrac12 L_2^2\bigr)\), and check it against the store's \(Q_i\) in a breeze.
13.2 From (34), how strong a steady breeze would the 60 N motor need before it could no longer hold the default rods? Is that wind found on Earth?
13.3 Show from (33) that a rod falling at 45° in a viscous fluid drifts sideways as it falls, and in which direction.
13.4 Derive \(\mathbf M_a\) of (38) for one rod hinged on the cart, and show that its \(xx\) entry \(\mu_a L\cos^2\theta\) vanishes for a rod lying along the rail. Why does the added mass of the rod's turning, \(\mu_a L^3/3\), not depend on \(\theta\)?