Definitioncore
Joint-space inertia matrix
Also called mass matrix of a robot, generalized inertia matrix.
The symmetric positive definite matrix M(q) with kinetic energy (1/2) q'^T M(q) q'. It is the sum over the links of J^T G J, and it multiplies q'' in the manipulator equation.
The joint-space inertia matrix of a robot is the matrix that turns joint velocities into kinetic energy, . It is the matrix that multiplies the joint accelerations in the manipulator equation, and it is symmetric and positive definite for any real arm. This page defines and its main properties. The manipulator equation page gives the full equation of motion, and Euler-Lagrange manipulator dynamics shows how produces the Coriolis and gravity terms.
Definition
Take a robot of rigid links, joined by joints into a serial chain or a tree, with joint variables . Link has mass , center of mass , and rotational inertia about its center of mass in a frame fixed to the link at the center of mass. Let be the Jacobian with , and let be the Jacobian of the angular velocity of link in coordinates. The joint-space inertia matrix is
It is the unique symmetric matrix with for all , where is the total kinetic energy of the links. In the notation of Lynch and Park, with the body Jacobian of the center-of-mass frame of link and the spatial inertia , the same matrix is (Section 8.1.2). Siciliano et al. call it ; Spong et al. call it .
Properties.
- Symmetry. Each term is symmetric.
- Positive definiteness. , and only if every link is at rest. So is positive definite exactly when every nonzero moves some mass or turns some link with nonzero inertia. A real link has positive definite, so for a real arm is positive definite at every .
- Column meaning. Column of is the vector of joint torques needed to give joint a unit acceleration from rest, with all other accelerations zero, no gravity and no external force. This is the basis of the composite-rigid-body algorithm, which forms in for a serial chain.
- The diagonal. For a revolute joint in a serial chain, is the moment of inertia of all links from outward, taken as one rigid body, about the axis of joint .
- Dependence on . does not depend on : moving joint 1 moves the whole arm rigidly along a fixed axis, which does not change the kinetic energy of any motion. In the same way, in a serial chain depends only on the joints with .
- Bounds. For an arm with only revolute joints, there are constants with for all , because is continuous and positive definite on a compact set of angles (Spong et al., Section 6.5.2). With prismatic joints, can grow with .
- Sparsity in trees. In a tree, when neither of the joints and lies on the path from the base to the other. Featherstone (2008, Chapter 6) calls this branch-induced sparsity and uses it to factor faster.
Examples and non-examples
Planar 2R arm with point masses. Take m, a point mass of 1 kg at each link end, and relative joint angles. Link 1 has , and link 2 has . The sum gives
At , , with eigenvalues , that is 0.17157 and 5.82843. At , , with eigenvalues , that is 0.58579 and 3.41421. At the arm is folded back, the outer mass sits on joint 1, and . In every case the determinant is , so is positive definite. At and rad/s, J. A direct check: mass 1 moves at speed 1 m/s and has 0.5 J, and mass 2 has velocity m/s and 2.5 J. Property 4 gives the same first entry: .
The same arm with general links. Let link have its center of mass at distance from joint and inertia about it. Then , and , with , and . The point-mass arm is .
A Cartesian gantry. Three prismatic joints move along , then , then , and each stage carries the next. Joint 1 moves all three stages, joint 2 moves stages 2 and 3, and joint 3 moves stage 3. So , which is constant. The Coriolis term of such a robot is zero.
A single pendulum. One revolute joint and one link with mass , center of mass at distance and inertia about it give the matrix .
Non-example: mass on the joint axis. If the second link of the 2R arm is a point mass placed exactly at joint 2 ( and ), then turning joint 2 moves no mass, the second row and column of are zero, and is only positive semidefinite. The matrix formula still applies, but the model has lost the dynamics of joint 2. Real links have , and a geared motor adds its reflected rotor inertia to the diagonal, which keeps positive definite.
Non-example: the task-space inertia. The matrix of the operational space formulation gives the kinetic energy in terms of the end-effector twist. It is a different matrix: it depends on the choice of end-effector frame, and it does not exist where loses rank.
Related definitions
- Manipulator equation: . Its Coriolis matrix comes from the derivatives of through the Christoffel symbols.
- Spatial inertia: the inertia of one body. is the sum of the spatial inertias pulled back to joint space by the Jacobians.
- Articulated-body inertia: the inertia that a body shows when the bodies beyond it are free to move. The articulated-body algorithm uses it to avoid forming .
- Composite-rigid-body algorithm: the standard way to compute , in for a serial chain.
- Cholesky decomposition: exists because is symmetric positive definite, and it is the usual way to solve in forward dynamics.
- Centroidal momentum matrix: the map from to the momentum of the whole robot about its center of mass, for a floating base.
- Dynamically consistent generalized inverse: a pseudoinverse of weighted by .
- In differential geometry, is a Riemannian metric on the configuration space, and the velocity terms of the equation of motion are the Christoffel symbols of that metric.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/joint-space-inertia-matrix/. The authors are listed in prov/FNGKHS.jsonl.
"""Joint-space inertia matrix of a planar arm with n revolute joints.
The arm moves in a vertical plane with x horizontal and y up. Joint i is at
p_i, link i has length L[i], mass m[i], its center of mass at distance r[i]
from joint i along the link, and rotational inertia I[i] about its center of
mass (about the axis normal to the plane). Angles q[i] are relative, so link
i points at the absolute angle q[0] + ... + q[i].
M(q) = sum_i m_i Jv_i^T Jv_i + I_i Jw_i^T Jw_i, where Jv_i is the 2 x n
Jacobian of the center of mass of link i and Jw_i is the 1 x n Jacobian of
its angle (ones in the first i + 1 columns).
"""
import math
class PlanarArm:
def __init__(self, L, m, r, I):
n = len(L)
if not (len(m) == len(r) == len(I) == n):
raise ValueError("L, m, r and I must have the same length")
self.L = list(L)
self.m = list(m)
self.r = list(r)
self.I = list(I)
self.n = n
def points(arm, q):
"""Return (joints, coms): the joint positions p_i and the centers of mass c_i."""
x = y = a = 0.0
joints, coms = [], []
for i in range(arm.n):
a += q[i]
joints.append((x, y))
coms.append((x + arm.r[i] * math.cos(a), y + arm.r[i] * math.sin(a)))
x += arm.L[i] * math.cos(a)
y += arm.L[i] * math.sin(a)
return joints, coms
def com_jacobians(arm, q):
"""Return lists Jv, Jw. Column j of Jv[i] is z x (c_i - p_j) for j <= i."""
joints, coms = points(arm, q)
n = arm.n
Jv, Jw = [], []
for i in range(n):
cx, cy = coms[i]
rows = [[0.0] * n, [0.0] * n]
for j in range(i + 1):
px, py = joints[j]
rows[0][j] = -(cy - py)
rows[1][j] = cx - px
Jv.append(rows)
Jw.append([1.0 if j <= i else 0.0 for j in range(n)])
return Jv, Jw
def mass_matrix(arm, q):
"""The joint-space inertia matrix M(q), a symmetric n x n list of rows."""
n = arm.n
Jv, Jw = com_jacobians(arm, q)
M = [[0.0] * n for _ in range(n)]
for i in range(n):
for a in range(n):
for b in range(n):
M[a][b] += arm.m[i] * (Jv[i][0][a] * Jv[i][0][b] + Jv[i][1][a] * Jv[i][1][b])
M[a][b] += arm.I[i] * Jw[i][a] * Jw[i][b]
return M
def kinetic_energy(arm, q, qd):
"""K = (1/2) qd^T M(q) qd."""
M = mass_matrix(arm, q)
n = arm.n
return 0.5 * sum(qd[a] * M[a][b] * qd[b] for a in range(n) for b in range(n))
def gravity_torque(arm, q, g=9.81):
"""g(q) = dP/dq with P = sum_i m_i g y_ci; component j is sum_{i >= j} m_i g (x_ci - x_pj)."""
joints, coms = points(arm, q)
out = []
for j in range(arm.n):
out.append(sum(arm.m[i] * g * (coms[i][0] - joints[j][0]) for i in range(j, arm.n)))
return out
def mass_matrix_derivatives(arm, q, h=1e-6):
"""dM/dq_k for each k, by central differences."""
out = []
for k in range(arm.n):
qp = list(q)
qm = list(q)
qp[k] += h
qm[k] -= h
Mp = mass_matrix(arm, qp)
Mm = mass_matrix(arm, qm)
out.append([[(Mp[a][b] - Mm[a][b]) / (2 * h) for b in range(arm.n)] for a in range(arm.n)])
return out
def coriolis_matrix(arm, q, qd, h=1e-6):
"""C(q, qd) from the Christoffel symbols c_ijk = (dM_kj/dq_i + dM_ki/dq_j - dM_ij/dq_k) / 2.
C[k][j] = sum_i c_ijk qd_i.
"""
n = arm.n
dM = mass_matrix_derivatives(arm, q, h)
C = [[0.0] * n for _ in range(n)]
for k in range(n):
for j in range(n):
s = 0.0
for i in range(n):
s += 0.5 * (dM[i][k][j] + dM[j][k][i] - dM[k][i][j]) * qd[i]
C[k][j] = s
return C
def inverse_dynamics(arm, q, qd, qdd, g=9.81):
"""tau = M(q) qdd + C(q, qd) qd + g(q)."""
n = arm.n
M = mass_matrix(arm, q)
C = coriolis_matrix(arm, q, qd)
G = gravity_torque(arm, q, g)
return [sum(M[k][j] * qdd[j] + C[k][j] * qd[j] for j in range(n)) + G[k] for k in range(n)]
def cholesky(A):
"""Lower triangular L with A = L L^T. Raises ValueError if A is not positive definite."""
n = len(A)
Lm = [[0.0] * n for _ in range(n)]
for i in range(n):
for j in range(i + 1):
s = A[i][j] - sum(Lm[i][k] * Lm[j][k] for k in range(j))
if i == j:
if s <= 0.0:
raise ValueError("matrix is not positive definite")
Lm[i][i] = math.sqrt(s)
else:
Lm[i][j] = s / Lm[j][j]
return Lm
def two_link_point_mass():
"""The check arm: L1 = L2 = 1 m, point masses of 1 kg at the link ends."""
return PlanarArm([1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [0.0, 0.0])
test_joint_space_inertia_matrix.py 176 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/joint-space-inertia-matrix/. The authors are listed in prov/FNGKHS.jsonl.
import math
import random
from joint_space_inertia_matrix import (
PlanarArm,
cholesky,
coriolis_matrix,
gravity_torque,
inverse_dynamics,
kinetic_energy,
mass_matrix,
mass_matrix_derivatives,
points,
two_link_point_mass,
)
G = 9.81
def close(a, b, tol):
return abs(a - b) <= tol
def random_arm(rng, n):
L = [rng.uniform(0.3, 1.5) for _ in range(n)]
m = [rng.uniform(0.5, 5.0) for _ in range(n)]
r = [rng.uniform(0.0, 1.0) * L[i] for i in range(n)]
I = [rng.uniform(0.01, 0.5) for _ in range(n)]
return PlanarArm(L, m, r, I)
def test_check_values_two_link():
arm = two_link_point_mass()
M0 = mass_matrix(arm, [0.0, 0.0])
assert all(close(M0[a][b], [[5, 2], [2, 1]][a][b], 1e-12) for a in range(2) for b in range(2))
M1 = mass_matrix(arm, [0.0, math.pi / 2])
assert all(close(M1[a][b], [[3, 1], [1, 1]][a][b], 1e-12) for a in range(2) for b in range(2))
for q2 in (0.0, 0.4, 1.0, 2.5, math.pi):
M = mass_matrix(arm, [0.7, q2])
c2 = math.cos(q2)
assert close(M[0][0], 3 + 2 * c2, 1e-12)
assert close(M[0][1], 1 + c2, 1e-12)
assert close(M[1][1], 1.0, 1e-12)
def test_general_two_link_closed_form():
rng = random.Random(7)
for _ in range(20):
arm = random_arm(rng, 2)
q = [rng.uniform(-math.pi, math.pi) for _ in range(2)]
a1 = arm.I[0] + arm.m[0] * arm.r[0] ** 2 + arm.m[1] * arm.L[0] ** 2
a2 = arm.I[1] + arm.m[1] * arm.r[1] ** 2
a3 = arm.m[1] * arm.L[0] * arm.r[1]
c2 = math.cos(q[1])
M = mass_matrix(arm, q)
assert close(M[0][0], a1 + a2 + 2 * a3 * c2, 1e-12)
assert close(M[0][1], a2 + a3 * c2, 1e-12)
assert close(M[1][0], a2 + a3 * c2, 1e-12)
assert close(M[1][1], a2, 1e-12)
def test_euler_lagrange_torques_two_link():
# Closed form of the Euler-Lagrange equations of the check arm.
arm = two_link_point_mass()
cases = [
((0.0, math.pi / 2), (1.0, 1.0), (0.0, 0.0), 0.0, (-3.0, 1.0)),
((0.0, math.pi / 2), (1.0, 1.0), (0.0, 0.0), G, (2 * G - 3.0, 1.0)),
((0.0, 0.0), (0.0, 0.0), (0.0, 0.0), G, (3 * G, G)),
]
for q, qd, qdd, g, want in cases:
tau = inverse_dynamics(arm, q, qd, qdd, g)
assert close(tau[0], want[0], 1e-6) and close(tau[1], want[1], 1e-6)
rng = random.Random(11)
for _ in range(20):
q = [rng.uniform(-3, 3) for _ in range(2)]
qd = [rng.uniform(-2, 2) for _ in range(2)]
qdd = [rng.uniform(-2, 2) for _ in range(2)]
s2, c2 = math.sin(q[1]), math.cos(q[1])
c1, c12 = math.cos(q[0]), math.cos(q[0] + q[1])
t1 = (3 + 2 * c2) * qdd[0] + (1 + c2) * qdd[1] - s2 * (2 * qd[0] * qd[1] + qd[1] ** 2) + 2 * G * c1 + G * c12
t2 = (1 + c2) * qdd[0] + qdd[1] + s2 * qd[0] ** 2 + G * c12
tau = inverse_dynamics(arm, q, qd, qdd, G)
assert close(tau[0], t1, 1e-6) and close(tau[1], t2, 1e-6)
def test_symmetric_positive_definite():
rng = random.Random(3)
for n in (1, 2, 3, 5):
for _ in range(10):
arm = random_arm(rng, n)
q = [rng.uniform(-math.pi, math.pi) for _ in range(n)]
M = mass_matrix(arm, q)
for a in range(n):
for b in range(n):
assert close(M[a][b], M[b][a], 1e-12)
cholesky(M)
def test_singular_when_mass_is_on_the_axis():
# Link 2 is a point mass at joint 2: turning joint 2 moves no mass.
arm = PlanarArm([1.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0])
M = mass_matrix(arm, [0.3, 0.9])
assert close(M[1][1], 0.0, 1e-12) and close(M[0][1], 0.0, 1e-12)
try:
cholesky(M)
except ValueError:
return
raise AssertionError("cholesky accepted a singular M")
def test_kinetic_energy_matches_direct_sum():
rng = random.Random(5)
h = 1e-6
for _ in range(10):
arm = random_arm(rng, 3)
q = [rng.uniform(-math.pi, math.pi) for _ in range(3)]
qd = [rng.uniform(-2, 2) for _ in range(3)]
_, cp = points(arm, [q[i] + h * qd[i] for i in range(3)])
_, cm = points(arm, [q[i] - h * qd[i] for i in range(3)])
K = 0.0
for i in range(3):
vx = (cp[i][0] - cm[i][0]) / (2 * h)
vy = (cp[i][1] - cm[i][1]) / (2 * h)
w = sum(qd[: i + 1])
K += 0.5 * arm.m[i] * (vx * vx + vy * vy) + 0.5 * arm.I[i] * w * w
assert close(kinetic_energy(arm, q, qd), K, 1e-6)
arm = two_link_point_mass()
assert close(kinetic_energy(arm, [0.0, math.pi / 2], [1.0, 1.0]), 3.0, 1e-12)
def test_gravity_is_gradient_of_potential():
rng = random.Random(9)
h = 1e-6
for _ in range(10):
arm = random_arm(rng, 3)
q = [rng.uniform(-math.pi, math.pi) for _ in range(3)]
def P(qq):
_, coms = points(arm, qq)
return sum(arm.m[i] * G * coms[i][1] for i in range(3))
gq = gravity_torque(arm, q, G)
for k in range(3):
qp = list(q)
qm = list(q)
qp[k] += h
qm[k] -= h
assert close(gq[k], (P(qp) - P(qm)) / (2 * h), 1e-5)
def test_skew_symmetry_of_mdot_minus_2c():
rng = random.Random(13)
for _ in range(10):
arm = random_arm(rng, 3)
q = [rng.uniform(-math.pi, math.pi) for _ in range(3)]
qd = [rng.uniform(-2, 2) for _ in range(3)]
dM = mass_matrix_derivatives(arm, q)
Md = [[sum(dM[k][a][b] * qd[k] for k in range(3)) for b in range(3)] for a in range(3)]
C = coriolis_matrix(arm, q, qd)
N = [[Md[a][b] - 2 * C[a][b] for b in range(3)] for a in range(3)]
for a in range(3):
for b in range(3):
assert close(N[a][b], -N[b][a], 1e-6)
def test_first_joint_does_not_change_m():
rng = random.Random(17)
arm = random_arm(rng, 4)
q = [rng.uniform(-math.pi, math.pi) for _ in range(4)]
M = mass_matrix(arm, q)
q2 = [q[0] + 1.234] + q[1:]
M2 = mass_matrix(arm, q2)
assert all(close(M[a][b], M2[a][b], 1e-12) for a in range(4) for b in range(4))
References
- Kevin M. Lynch and Frank C. Park, Modern Robotics, Mechanics, Planning, and Control, Cambridge University Press, 2017, Chapter 8, Section 8.1.2 (general formulation), Section 8.1.3 (understanding the mass matrix), Section 8.4 (dynamic equations in closed form) and Section 8.9 (actuation, gearing and friction)
- Mark W. Spong, Seth Hutchinson and M. Vidyasagar, Robot Modeling and Control, second edition, Wiley, 2020, Chapter 6, Section 6.2 (kinetic and potential energy, the inertia tensor), Section 6.3 (equations of motion) and Section 6.5.2 (bounds on the inertia matrix)
- Roy Featherstone, Rigid Body Dynamics Algorithms, Springer, 2008, Chapter 6 (the joint-space inertia matrix, the composite-rigid-body algorithm and branch-induced sparsity)
- Bruno Siciliano, Lorenzo Sciavicco, Luigi Villani and Giuseppe Oriolo, Robotics, Modelling, Planning and Control, Springer, 2009, Chapter 7 (the inertia matrix B(q) in the Lagrange formulation)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag FNGKHS Edit this page History Report an error Markdown