diff --git a/README.md b/README.md index 43d1987..0c70dd0 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ project already defines those targets. Includes are namespaced by domain, e.g. | Category | Description | |--------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [Kinematics](doc/kinematics/README.md) | Forward Kinematics, Chain Pose Kinematics, Denavit-Hartenberg Parameters, Geometric Jacobian, Manipulability Index, Product of Exponentials, Redundancy Resolution, Inverse Kinematics (DLS), Pose Inverse Kinematics, Analytical IK (OPW), Nearest IK, Continuum Kinematics, Parallel Manipulator Kinematics, Mobile-Manipulator Kinematics | -| [Dynamics](doc/dynamics/README.md) | Euler-Lagrange, Newton-Euler, Recursive Newton-Euler (RNEA), Articulated Body Algorithm (ABA), Chain Dynamics Model, Composite Rigid Body Algorithm (CRBA), Coriolis Matrix and Inertial Regressor, Dynamic Parameter Identification, Forward-Dynamics Integration, Generic Joint Link, Generalized-Momentum Observer | +| [Dynamics](doc/dynamics/README.md) | Euler-Lagrange, Newton-Euler, Recursive Newton-Euler (RNEA), Articulated Body Algorithm (ABA), Chain Dynamics Model, Composite Rigid Body Algorithm (CRBA), Coriolis Matrix and Inertial Regressor, Dynamic Parameter Identification, Effective Inertia, Forward-Dynamics Integration, Generic Joint Link, Generalized-Momentum Observer | | [Planning](doc/planning/README.md) | Primitive Distance Queries, Artificial Potential Fields, Self-Collision Checking, Convex Collision (GJK and EPA), Grid Search (A* and Dijkstra), RRT-Connect, Dubins Shortest Paths, Dynamic Window Approach, Reeds–Shepp Shortest Paths | | [Trajectory](doc/trajectory/README.md) | Cubic Spline Trajectory, Cartesian Path with SLERP Orientation, Time-Optimal Path Parameterization (TOPP-RA) | | [Mobile Robots](doc/mobile/README.md) | Planar Odometry, Differential-Drive Kinematics, Car-Like Kinematics, Omnidirectional Kinematics, Skid-Steer Kinematics, Pure-Pursuit Path Tracking, Kanayama Trajectory Tracking, Stanley Path Tracking | diff --git a/doc/dynamics/EffectiveJointInertia.md b/doc/dynamics/EffectiveJointInertia.md new file mode 100644 index 0000000..c8712fc --- /dev/null +++ b/doc/dynamics/EffectiveJointInertia.md @@ -0,0 +1,86 @@ +# Effective Joint Inertia + +## Overview & Motivation + +How heavy does a joint feel? The answer depends on what the other joints do. + +- With every other joint **locked**, joint $i$ moves its own subtree as one rigid body, and the inertia it feels is the diagonal entry $M_{ii}$ of the mass matrix. +- With every other joint **free** (unactuated or back-drivable), part of a torque on joint $i$ is spent accelerating the neighbours through the coupling terms, and the joint feels a smaller inertia. + +The second value is the one that sizes a joint's controller when the other joints are not holding it: a gain chosen from $M_{ii}$ is too stiff for a joint that, in practice, drags light neighbours along. It is also what a single-joint step test measures on a real arm, and what a simulated plant should report as each joint's load. + +## Mathematical Theory + +### Definition + +The joint-space dynamics are $M(q)\,\ddot{q} = \tau - h(q, \dot{q})$. Apply a torque $\tau_i$ on joint $i$ alone, with every other joint torque zero. The joint accelerations are $\ddot{q} = M^{-1} e_i\, \tau_i$, so joint $i$ accelerates by + +$$\ddot{q}_i = \left(M^{-1}\right)_{ii} \tau_i$$ + +The effective inertia of joint $i$ is the ratio of torque to that acceleration: + +$$I_{\text{eff},i} = \frac{1}{\left(M^{-1}\right)_{ii}}$$ + +### Bounds + +For a symmetric positive-definite $M$, + +$$0 < I_{\text{eff},i} \le M_{ii}$$ + +with equality exactly when joint $i$ is decoupled from the others (row $i$ of $M$ is zero off the diagonal). The gap is the inertia the coupled joints absorb. + +### Computation Through the Cholesky Factor + +Inverting $M$ is unnecessary. With the Cholesky factor $M = L L^T$, + +$$\left(M^{-1}\right)_{ii} = e_i^T L^{-T} L^{-1} e_i = \left\lVert L^{-1} e_i \right\rVert^2$$ + +so each joint needs one forward substitution $L\,y = e_i$ and the squared norm of $y$. A mass matrix that has no Cholesky factor is not positive definite, which no physical chain produces; the computation then reports failure. + +## Complexity Analysis + +| Step | Time | Space | Notes | +|----------------------|----------|----------|-------------------------------------------| +| Cholesky factor | $O(n^3)$ | $O(n^2)$ | Once per mass matrix | +| Forward substitution | $O(n^2)$ | $O(n)$ | One per joint | +| Total | $O(n^3)$ | $O(n^2)$ | $n = 6$: one factor and six substitutions | + +## Step-by-Step Walkthrough + +Two coupled joints with + +$$M = \begin{bmatrix} 2 & 1 \\ 1 & 1 \end{bmatrix}, \qquad L = \begin{bmatrix} \sqrt{2} & 0 \\ 1/\sqrt{2} & 1/\sqrt{2} \end{bmatrix}$$ + +1. Joint 1: $L\,y = e_1$ gives $y = (1/\sqrt{2},\ -1/\sqrt{2})$, $\lVert y \rVert^2 = 1$, so $I_{\text{eff},1} = 1$. +2. Joint 2: $L\,y = e_2$ gives $y = (0,\ \sqrt{2})$, $\lVert y \rVert^2 = 2$, so $I_{\text{eff},2} = 0.5$. + +Joint 1 feels half its locked inertia $M_{11} = 2$, and joint 2 half of $M_{22} = 1$: each spends half of a torque accelerating the other. + +## Pitfalls & Edge Cases + +- **Configuration dependence**: $M$ depends on $q$, so the effective inertia does too. Gains sized at one pose may not suit another; recompute where the arm moves far. +- **Armature**: a geared joint's reflected rotor inertia belongs on the mass-matrix diagonal before the computation. Omitting it understates the effective inertia of the proximal joints badly. +- **Locked versus free is a modelling choice**: a joint held by a stiff controller behaves as locked for its neighbours. Use $M_{ii}$ when the other joints are servoed tightly, $I_{\text{eff},i}$ when they are free or soft. +- **Near-singular mass matrices**: a very light distal link makes $M$ ill-conditioned. The forward substitution stays stable, but the result inherits the model's uncertainty in that link. + +## Variants & Generalizations + +- **Partially locked chains**: lock a subset $\mathcal{L}$ and free the rest. The effective inertia of the free joints is the Schur complement of the locked block; of a single joint, $1 / (M_{\mathcal{F}\mathcal{F}}^{-1})_{ii}$ over the free set $\mathcal{F}$. +- **Operational-space inertia**: the same idea at the tool, $\Lambda = (J M^{-1} J^T)^{-1}$, gives the inertia felt along task directions. + +## Applications + +- Sizing per-joint controller gains and impedance stiffnesses from a bandwidth. +- Reporting each joint's load inertia in a simulated plant or a drive configuration tool. +- Predicting the result of a single-joint step or frequency-response test. + +## Connections to Other Algorithms + +- [Composite Rigid Body Algorithm](CompositeRigidBodyAlgorithm.md): computes the mass matrix this starts from. +- [Generic Joint Link](JointLink.md): carries the armature that belongs on the diagonal. +- The Cholesky factor and the triangular solve come from [numerical-toolbox-cpp](https://github.com/embedded-pro/numerical-toolbox-cpp). + +## References & Further Reading + +- O. Khatib, "Inertial Properties in Robotic Manipulation: An Object-Level Framework," *International Journal of Robotics Research*, 14(1), 1995. +- R. Featherstone, *Rigid Body Dynamics Algorithms*, Springer, 2008, chapter 6. diff --git a/doc/dynamics/README.md b/doc/dynamics/README.md index 052e25d..610fc95 100644 --- a/doc/dynamics/README.md +++ b/doc/dynamics/README.md @@ -11,6 +11,7 @@ Rigid-body dynamics algorithms for robotics and mechanical systems. | [Composite Rigid Body Algorithm](CompositeRigidBodyAlgorithm.md) | $O(n^2)$ joint-space mass matrix $M(q)$ from composite inertias, exactly symmetric | | [Coriolis Matrix and Inertial Regressor](CoriolisMatrixAndRegressor.md) | Modified RNEA for $C(q,\dot{q})\dot{q}_r$ in Christoffel form, $C^T\dot{q}$, and the 10-parameter-per-link regressor $Y\pi$ | | [Dynamic Parameter Identification](DynamicParameterIdentification.md) | Base-parameter estimation from measured torques with streaming rank-revealing least squares on the inertial regressor | +| [Effective Joint Inertia](EffectiveJointInertia.md) | Inertia each joint feels with the others free, $1/(M^{-1})_{ii}$, through one Cholesky factor and a forward substitution per joint | | [Generalized-Momentum Observer](MomentumObserver.md) | Sensorless collision detection: a first-order estimate of the external joint torque from commanded torque, $M(q)$, $g(q)$ and $C^T\dot{q}$ | | [Euler-Lagrange](EulerLagrange.md) | Forward and inverse dynamics via the Lagrangian formulation $M\ddot{q} + C\dot{q} + g = \tau$ | | [Forward-Dynamics Integration](ForwardDynamicsIntegrator.md) | Fixed-step simulation of a chain on the articulated-body dynamics — semi-implicit Euler or RK4 | diff --git a/robotics/dynamics/CMakeLists.txt b/robotics/dynamics/CMakeLists.txt index 7043788..ec5d154 100644 --- a/robotics/dynamics/CMakeLists.txt +++ b/robotics/dynamics/CMakeLists.txt @@ -19,6 +19,7 @@ target_sources(robotics.dynamics PRIVATE CompositeRigidBodyAlgorithm.hpp CoriolisMatrix.hpp DynamicParameterIdentification.hpp + EffectiveJointInertia.hpp EulerLagrangeDynamics.hpp EulerLagrangeSolver.hpp ForwardDynamicsIntegrator.hpp @@ -41,6 +42,7 @@ robotics_add_coverage_sources(robotics.dynamics CompositeRigidBodyAlgorithm.cpp CoriolisMatrix.cpp DynamicParameterIdentification.cpp + EffectiveJointInertia.cpp EulerLagrangeSolver.cpp ForwardDynamicsIntegrator.cpp JointLink.cpp diff --git a/robotics/dynamics/EffectiveJointInertia.cpp b/robotics/dynamics/EffectiveJointInertia.cpp new file mode 100644 index 0000000..09129c9 --- /dev/null +++ b/robotics/dynamics/EffectiveJointInertia.cpp @@ -0,0 +1,4 @@ +#include "robotics/dynamics/EffectiveJointInertia.hpp" + +template std::optional> dynamics::EffectiveJointInertia(const math::SquareMatrix&); +template std::optional> dynamics::EffectiveJointInertia(const math::SquareMatrix&); diff --git a/robotics/dynamics/EffectiveJointInertia.hpp b/robotics/dynamics/EffectiveJointInertia.hpp new file mode 100644 index 0000000..8d4454e --- /dev/null +++ b/robotics/dynamics/EffectiveJointInertia.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include "numerical/math/CholeskyDecomposition.hpp" +#include "numerical/math/Matrix.hpp" +#include "numerical/math/MatrixNorms.hpp" +#include "numerical/math/TriangularSolve.hpp" +#include +#include +#include + +namespace dynamics +{ + template + std::optional> EffectiveJointInertia(const math::SquareMatrix& massMatrix) + { + static_assert(std::is_floating_point_v, "EffectiveJointInertia supports floating-point types"); + + const auto factor{ math::CholeskyDecomposition::TryFactor(massMatrix) }; + if (!factor.has_value()) + return std::nullopt; + + math::Vector inertia{}; + for (std::size_t joint = 0; joint < N; ++joint) + { + math::Vector unit{}; + unit[joint] = T{ 1 }; + const auto column{ math::SolveLowerTriangular(*factor, unit) }; + inertia[joint] = T{ 1 } / math::DotProduct(column, column); + } + + return inertia; + } + +#ifdef ROBOTICS_TOOLBOX_COVERAGE_BUILD + extern template std::optional> EffectiveJointInertia(const math::SquareMatrix&); + extern template std::optional> EffectiveJointInertia(const math::SquareMatrix&); +#endif +} diff --git a/robotics/dynamics/test/CMakeLists.txt b/robotics/dynamics/test/CMakeLists.txt index 92b8253..e480f0d 100644 --- a/robotics/dynamics/test/CMakeLists.txt +++ b/robotics/dynamics/test/CMakeLists.txt @@ -15,6 +15,7 @@ target_sources(robotics.dynamics_test PRIVATE TestCompositeRigidBodyAlgorithm.cpp TestCoriolisMatrix.cpp TestDynamicParameterIdentification.cpp + TestEffectiveJointInertia.cpp TestEulerLagrangeSolver.cpp TestForwardDynamicsIntegrator.cpp TestJointLink.cpp diff --git a/robotics/dynamics/test/TestEffectiveJointInertia.cpp b/robotics/dynamics/test/TestEffectiveJointInertia.cpp new file mode 100644 index 0000000..1febdeb --- /dev/null +++ b/robotics/dynamics/test/TestEffectiveJointInertia.cpp @@ -0,0 +1,35 @@ +#include "numerical/math/Tolerance.hpp" +#include "robotics/dynamics/EffectiveJointInertia.hpp" +#include + +namespace +{ + using Matrix2 = math::SquareMatrix; + + class TestEffectiveJointInertia + : public ::testing::Test + {}; +} + +TEST_F(TestEffectiveJointInertia, uncoupled_joints_feel_their_own_inertia) +{ + const auto inertia{ dynamics::EffectiveJointInertia(Matrix2{ { 2.0f, 0.0f }, { 0.0f, 0.5f } }) }; + + ASSERT_TRUE(inertia.has_value()); + EXPECT_NEAR((*inertia)[0], 2.0f, math::Tolerance()); + EXPECT_NEAR((*inertia)[1], 0.5f, math::Tolerance()); +} + +TEST_F(TestEffectiveJointInertia, coupling_lowers_the_inertia_below_the_locked_one) +{ + const auto inertia{ dynamics::EffectiveJointInertia(Matrix2{ { 2.0f, 1.0f }, { 1.0f, 1.0f } }) }; + + ASSERT_TRUE(inertia.has_value()); + EXPECT_NEAR((*inertia)[0], 1.0f, math::Tolerance()); + EXPECT_NEAR((*inertia)[1], 0.5f, math::Tolerance()); +} + +TEST_F(TestEffectiveJointInertia, rejects_a_mass_matrix_that_is_not_positive_definite) +{ + EXPECT_FALSE(dynamics::EffectiveJointInertia(Matrix2{ { 1.0f, 2.0f }, { 2.0f, 1.0f } }).has_value()); +}