Lagrangian Dynamics of an Open-Chain Robot
Learning Objectives
- Write the kinetic and potential energy of a two-link planar arm whose links have mass, a center of mass, and a moment of inertia, with every parameter stated as an assumed value with units.
- Apply the Euler-Lagrange equation by hand and obtain the joint torques as functions of , and .
- Confirm the result four independent ways: symbolic differentiation with SymPy, a recursive Newton-Euler computation, a finite-difference Euler-Lagrange computation, and energy conservation in a simulation.
- Read the structure of the result: linear in , quadratic in , trigonometric in .
Context & Motivation
static-forces-and-joint-torques held the arm still. Once it moves, the motors must also accelerate the links, and moving links push on each other. Two classical routes give the equations of motion. Newton-Euler writes and the moment balance for each link, including the forces at the joints that hold the links together, and then eliminates those internal forces. Lagrange starts from two scalar functions, the kinetic and potential energy written in the joint angles, and the internal forces never appear. Modern Robotics says of the two that the resulting equations "are, and must be, identical". This lesson derives the two-link arm by Lagrange, by hand, and then checks that statement numerically.
Core Theory
The Euler-Lagrange equation with joint torques
Choose generalized coordinates that fix the configuration; for an open chain with every joint actuated, the joint angles serve. The generalized forces are the quantities for which is the power delivered, which here are the joint torques. With the Lagrangian ,
Gravity enters through , so is everything else: the motor torques, and, if they are modeled, friction and contact torques. Putting gravity both in and in counts it twice. The equation assumes rigid links and ideal joints whose constraint forces do no work.
The arm and its assumed parameters
Same conventions as before: horizontal, up, from the axis to link 1, of link 2 relative to link 1, counterclockwise positive, gravity along . Each link is modeled as a uniform slender rod, so its center of mass is at mid-length and its moment of inertia about the center of mass, for rotation in the plane, is . All values are assumptions chosen for the example:
| Link | Length | Mass | Center of mass from its joint | Inertia about center of mass |
|---|---|---|---|---|
| 1 | m | kg | m | |
| 2 | m | kg | m |
Kinetic energy
A rigid body moving in the plane has kinetic energy : translation of the center of mass plus rotation about it. Link 1 rotates about the fixed joint: and . For link 2 the center of mass is at
and squaring, with ,
The angular velocity of link 2 is absolute: , not . Collecting terms gives with
With the values above, , and , all in . Only appears: rotating the whole arm about joint 1 does not change its inertia, while folding the elbow does. Asada's notes for MIT 2.12 read through the parallel-axis theorem: the inertia seen by joint 1 is largest with the arm extended () and smallest with it folded ().
Potential energy
The derivation, by hand
Write , so that , and is constant.
Joint 1. . Its time derivative brings in and :
does not depend on , so .
Joint 2. , whose time derivative is . Now does depend on :
Subtracting, the terms of joint 2 cancel, and the result is
Here , and the gravity coefficients are N m and N m. The equations are linear in , quadratic in and trigonometric in , which Modern Robotics notes holds for serial chains of revolute joints in general.
A check against a published case. Setting , and turns the links into point masses at their ends. A SymPy simplify of the difference between these equations, so reduced, and Equation (8.9) of Modern Robotics returns for both joints. With , the reduced is at and at , the matrices printed in the book's Figure 8.3.
Worked Examples
Example 1: one state, term by term
At , rad/s and , with :
The velocity terms, with : for joint 1, N m; for joint 2, N m. Gravity gives N m. The total is
At this slow speed gravity is most of . the-manipulator-equation-inertia-coriolis-gravity shows how the balance shifts as the motion speeds up.
Example 2: SymPy does the differentiation
SymPy builds the Lagrangian from positions, not from , so it does not inherit a mistake made while collecting terms:
plaintextt = sp.symbols('t'); q1, q2 = sp.Function('q1')(t), sp.Function('q2')(t) x2 = L1*sp.cos(q1) + lc2*sp.cos(q1 + q2); y2 = L1*sp.sin(q1) + lc2*sp.sin(q1 + q2) K = (m1*lc1**2*q1.diff(t)**2 + I1*q1.diff(t)**2 + m2*(x2.diff(t)**2 + y2.diff(t)**2) + I2*(q1.diff(t) + q2.diff(t))**2) / 2 P = m1*g*lc1*sp.sin(q1) + m2*g*y2 L = K - P tau = [sp.simplify(L.diff(qi.diff(t)).diff(t) - L.diff(qi)) for qi in (q1, q2)]
With all parameters kept as symbols, sp.simplify(tau[i] - hand[i]) returns 0 for both joints, where hand holds the two equations above. The hand derivation is correct for every choice of masses, lengths and inertias, not only for the table.
Example 3: an independent recursive Newton-Euler computation
Newton-Euler uses none of the energy expressions. A forward pass computes, from the base outward, each link's angular velocity and acceleration (, then ) and the acceleration of each center of mass, with gravity included by giving the base an upward acceleration . A backward pass, from the tip inward, computes the force each link needs, , adds the force passed on from the next link, and takes moments about each joint to get , which includes . On three states drawn with numpy.random.default_rng(1) (angles uniform on , velocities on rad/s, accelerations on , drawn in that order for each state):
textq (rad) qdot (rad/s) qddot (rad/s^2) tau Lagrange tau Newton-Euler (0.074, 2.830) (-2.135, 2.692) (-1.882, -0.767) (12.734756, -3.459386) (12.734756, -3.459386) (2.059, -0.571) (0.298, -2.835) (2.535, 0.381) (-3.914117, 1.050836) (-3.914117, 1.050836) (-1.070, 1.812) (-1.181, -0.279) (-3.660, -0.969) (8.151353, 2.843422) (8.151353, 2.843422)
The largest difference is N m, rounding error. At the state of Example 1, Newton-Euler gives N m.
Example 4: finite differences and energy
Two more checks need nothing but numbers.
Euler-Lagrange by finite differences. Code directly from the center-of-mass velocities, approximate and by central differences with step , and approximate by a central difference with step s along the path that passes through the state of Example 1 with the given and . The result is N m, within of the closed form.
Energy conservation. With and no friction, must stay constant. Solving with scipy.integrate.solve_ivp, method DOP853, rtol=1e-10, atol=1e-12, from rest at (arm horizontal, ) for 5 s, the arm falls and swings irregularly, and , evaluated at 5001 evenly spaced times, stays below J. Energy conservation also exposes a wrong model. Replace with in , a factor that is easy to drop by hand, and the same simulation drifts by J. A model that passes the energy test can still be wrong in terms that do no work, which is why the other three checks are run as well.
Common Misconceptions & Pitfalls
- "Link 2 rotates at ." Its absolute angular velocity is . Using drops from and .
- "Add to the inertia about the joint." Use the inertia about the center of mass together with , or the inertia about a fixed axis alone. Mixing the two counts the same mass twice.
- " has velocity terms." depends only on , so for joint 1 only gravity remains. All the velocity terms of come from differentiating in time.
- "Lagrange and Newton-Euler give different dynamics." They give the same torques (Example 3, to ). They differ in how the work is organized and what it costs, not in the answer.
- "The mass matrix is constant." goes from at to at , a factor of 2.75.
- "Energy is conserved, so the model is right." The energy test catches errors that create or destroy energy, as the dropped factor of 2 did, but not every error.
Summary
For a planar two-link arm with distributed links, the Lagrangian and the Euler-Lagrange equation give, by hand, and , with . For the assumed arm, , , and . SymPy agreed symbolically, a recursive Newton-Euler computation agreed to , a finite-difference Euler-Lagrange computation agreed to , and the free motion conserved energy to within J while a model with one dropped factor of 2 drifted by 18.6 J. With point masses the result reduces to Equation (8.9) of Modern Robotics.
Documentation Links
- Lynch and Park (2017): Modern Robotics: Mechanics, Planning, and Control (preprint PDF)
- Asada (2005): MIT OCW 2.12 Introduction to Robotics, Chapter 7: Dynamics
- SymPy documentation: Calculus tutorial
- SymPy documentation: Kane's Method and Lagrange's Method (API)
- SciPy documentation: scipy.integrate.solve_ivp