Algorithm Search⌘K

Algorithmcore

Impedance control

Also called Hogan impedance control.

Makes the end effector behave as a chosen mass, spring and damper around a goal motion, so contact forces stay moderate and predictable.

Time
O(n^3) per_step
Space
O(n^2) extra
Where
n = the number of joints, equal to the task dimension (a nonredundant arm); the bounds are for one control step
Properties
deterministicnot exactonline
Origin
Neville Hogan, 1985

Impedance control is a way to control a robot that touches its environment. It does not command a position or a force. It commands a relation between the two: the end effector should move as if it were a chosen mass on a chosen spring and damper, attached to a goal motion x0(t)x_0(t). In free space the end effector then follows x0x_0; against an obstacle it pushes with a force that the spring sets, and the force stays bounded even when the position of the obstacle is not known exactly. Hogan introduced the idea in 1985. The dual scheme, which measures the force and commands a motion, is admittance control.

In plain words

A position-controlled robot that meets a wall it did not expect pushes as hard as its motors allow, because it tries to reach a point inside the wall. A person in the same case gives way. Impedance control makes the robot give way in a chosen manner.

The designer picks three numbers per direction: a mass, a stiffness and a damping. The robot then reacts to a push as a weight on a spring and a shock absorber would. A soft spring gives gentle contact but poor accuracy in free space, and a stiff spring gives the reverse. If the goal point is a little inside a surface, the robot presses on the surface with a force equal to the spring stiffness times that depth, or less if the surface is soft.

The problem

The arm has nn joints and its end effector has a task position x=f(q)∈Rnx = f(q) \in \mathbb{R}^n with Jacobian J(q)J(q), square and nonsingular in the region of interest. The environment applies a force FextF_{\text{ext}} to the end effector, so the arm obeys

M(q)q¨+C(q,q˙)q˙+g(q)=τ+J(q)TFext,M(q)\ddot q + C(q, \dot q)\dot q + g(q) = \tau + J(q)^T F_{\text{ext}},

by the duality of the Jacobian and statics. A goal motion x0(t)x_0(t) and target matrices MdM_d, DdD_d, KdK_d (symmetric positive definite) are given. With x~=x−x0\tilde x = x - x_0, the task is to choose τ\tau so that

Mdx~¨+Ddx~˙+Kdx~=Fext.M_d\ddot{\tilde x} + D_d\dot{\tilde x} + K_d\tilde x = F_{\text{ext}}.

The algorithm

This is the form with a force sensor at the wrist and a full dynamic model. Diagonal target matrices are the usual choice.

  1. IMPEDANCE(q, q', x_0, x_0', x_0'', F_ext, M_d, D_d, K_d)
  2. 1x ← f(q); x' ← J(q) q'
  3. 2x̃ ← x - x_0; x̃' ← x' - x_0'
  4. 3a_x ← x_0'' + M_d⁻¹ (F_ext - D_d x̃' - K_d x̃) ▷ wanted task acceleration
  5. 4a_q ← J(q)⁻¹ (a_x - J'(q, q') q')
  6. 5τ ← M(q) a_q + C(q, q') q' + g(q) - J(q)ᵀ F_ext
  7. 6return τ

Lines L4 and L5 are computed-torque control toward the acceleration axa_x. The term −JTFext-J^T F_{\text{ext}} in line L5 cancels the measured contact force, and line L3 puts it back, scaled by Md−1M_d^{-1}. If the target mass is not changed (MdM_d equal to the task-space inertia of the arm), the force sensor is not needed, and the law reduces to

τ=J(q)T(Kd(x0−x)−Ddx˙)+g(q)\tau = J(q)^T\big(K_d(x_0 - x) - D_d\dot x\big) + g(q)

for a fixed goal. This simple form is the one most often used on torque-controlled arms. It is PD control with gravity compensation with the spring and damper placed in task space.

Example

Take the planar 2R arm with L1=L2=1L_1 = L_2 = 1 m and point masses of 1 kg at the link ends, in a vertical plane with gravity along −y-y. A frictionless wall fills x≥1x \ge 1 m and acts as a stiff spring, Fext=(−ke(x−1),0)F_{\text{ext}} = (-k_e(x - 1), 0) for x>1x > 1 with ke=104k_e = 10^4 N/m. The targets are Md=IM_d = I kg, Kd=400IK_d = 400 I N/m and Dd=40ID_d = 40 I N s/m, which is critical damping. The arm starts at rest with its end point at (0.9,0.5)(0.9, 0.5) m, and the goal is fixed at x0=(1.05,0.5)x_0 = (1.05, 0.5), 5 cm inside the wall.

At the start, x~=(−0.15,0)\tilde x = (-0.15, 0), so line L3 gives ax=(60,0)a_x = (60, 0) m/s2^2 and lines L4 and L5 give τ=(−10.38,−59.64)\tau = (-10.38, -59.64) N m. The table shows a simulation with RK4 and a step of 1 ms.

time (s) end point xx (m) wall force (N)
0.100 0.98910 0
0.115 first contact 0
0.131 peak 67.5
0.200 1.00321 32.1
0.500 1.00192 19.24
1.500 1.0019231 19.231

At rest the target equation gives Kdx~=FextK_d\tilde x = F_{\text{ext}}. Together with the wall law this gives the rest position x=(Kdx0+kexw)/(Kd+ke)=1.0019231x = (K_d x_0 + k_e x_w)/(K_d + k_e) = 1.0019231 m and the contact force

keKdke+Kd(x0−xw)=104⋅40010400⋅0.05=19.231 N.\frac{k_e K_d}{k_e + K_d}(x_0 - x_w) = \frac{10^4 \cdot 400}{10400} \cdot 0.05 = 19.231 \text{ N}.

The force is that of the target spring and the wall spring in series. The impact peak of 67.5 N is set by the target mass and the speed at contact, not by the spring alone. The yy coordinate stays at 0.5 m within 10−910^{-9} m during the whole motion, because the target matrices are diagonal and the law cancels the arm's own inertia. The simple law without a force sensor reaches the same rest force, but its yy coordinate moves by up to 1.8 mm during the impact, because the arm's inertia couples the two directions.

Correctness

The claim is: with an exact model, a nonsingular J(q)J(q) and an exact force measurement, the closed loop satisfies Mdx~¨+Ddx~˙+Kdx~=FextM_d\ddot{\tilde x} + D_d\dot{\tilde x} + K_d\tilde x = F_{\text{ext}} for every force FextF_{\text{ext}}. Putting line L5 into the arm equation, the CC, gg and JTFextJ^T F_{\text{ext}} terms cancel, so q¨=aq\ddot q = a_q. Then x¨=Jq¨+J˙q˙=ax\ddot x = J\ddot q + \dot J\dot q = a_x by line L4, and line L3 rearranges to the target equation. For a passive environment and a fixed goal, the energy 12x~˙TMdx~˙+12x~TKdx~\tfrac12\dot{\tilde x}^T M_d\dot{\tilde x} + \tfrac12\tilde x^T K_d\tilde x plus the energy stored in the environment does not grow, which bounds the motion and the contact force. The full proof, including the rest force against a spring wall, is in the proof file.

Proof: Impedance control gives the target impedancefull

The arm obeys M(q)q¨+C(q,q˙)q˙+g(q)=τ+J(q)TFextM(q)\ddot q + C(q, \dot q)\dot q + g(q) = \tau + J(q)^T F_{\text{ext}}, where x=f(q)x = f(q) and x˙=J(q)q˙\dot x = J(q)\dot q. We assume that the model in line L5 is exact, that J(q)J(q) is nonsingular along the motion, and that the value FextF_{\text{ext}} passed to IMPEDANCE is the true contact force. MdM_d, DdD_d and KdK_d are symmetric positive definite.

Theorem 1 (target dynamics). Under these assumptions, Mdx~¨+Ddx~˙+Kdx~=FextM_d\ddot{\tilde x} + D_d\dot{\tilde x} + K_d\tilde x = F_{\text{ext}} at every time, where x~=x−x0\tilde x = x - x_0.

Proof. Put the torque of line L5 into the arm equation:

Mq¨+Cq˙+g=Maq+Cq˙+g−JTFext+JTFext.M\ddot q + C\dot q + g = M a_q + C\dot q + g - J^T F_{\text{ext}} + J^T F_{\text{ext}}.

The terms Cq˙C\dot q, gg and JTFextJ^T F_{\text{ext}} cancel, so Mq¨=MaqM\ddot q = M a_q, and q¨=aq\ddot q = a_q because MM is invertible. Differentiating x˙=Jq˙\dot x = J\dot q gives x¨=Jq¨+J˙q˙=Jaq+J˙q˙\ddot x = J\ddot q + \dot J\dot q = J a_q + \dot J\dot q. By line L4, Jaq=ax−J˙q˙J a_q = a_x - \dot J\dot q, so x¨=ax\ddot x = a_x. By line L3, Md(ax−x¨0)=Fext−Ddx~˙−Kdx~M_d(a_x - \ddot x_0) = F_{\text{ext}} - D_d\dot{\tilde x} - K_d\tilde x. With x~¨=x¨−x¨0=ax−x¨0\ddot{\tilde x} = \ddot x - \ddot x_0 = a_x - \ddot x_0 this is the target equation. □\square

Theorem 2 (bounded energy with a passive environment). Let the goal be fixed (x˙0=0\dot x_0 = 0). Let the environment be a spring with energy Ue(x)≥0U_e(x) \ge 0 and force Fext=−∇Ue(x)F_{\text{ext}} = -\nabla U_e(x). Then W=12x~˙TMdx~˙+12x~TKdx~+Ue(x)W = \tfrac12\dot{\tilde x}^T M_d\dot{\tilde x} + \tfrac12\tilde x^T K_d\tilde x + U_e(x) satisfies W˙=−x~˙TDdx~˙≤0\dot W = -\dot{\tilde x}^T D_d\dot{\tilde x} \le 0.

Proof. With a fixed goal, x~˙=x˙\dot{\tilde x} = \dot x. By Theorem 1 and the symmetry of MdM_d and KdK_d,

W˙=x˙T(Fext−Ddx˙−Kdx~)+x~TKdx˙+∇Ue(x)Tx˙=x˙TFext−x˙TDdx˙+∇UeTx˙.\dot W = \dot x^T(F_{\text{ext}} - D_d\dot x - K_d\tilde x) + \tilde x^T K_d\dot x + \nabla U_e(x)^T\dot x = \dot x^T F_{\text{ext}} - \dot x^T D_d\dot x + \nabla U_e^T\dot x.

The first and last terms cancel because Fext=−∇UeF_{\text{ext}} = -\nabla U_e. □\square

So x~\tilde x and x˙\dot x stay bounded by W(0)W(0), and the contact force stays bounded on the bounded set of positions that the motion can reach.

Theorem 3 (rest force against a spring wall). Let the task directions be decoupled (MdM_d, DdD_d, KdK_d diagonal), and let the wall act along the first coordinate with Fext,1=−ke(x1−xw)F_{\text{ext},1} = -k_e(x_1 - x_w) for x1>xwx_1 > x_w and 00 otherwise, with ke>0k_e > 0 and x0,1>xwx_{0,1} > x_w. If the closed loop is at rest, then x1=(kdx0,1+kexw)/(kd+ke)x_1 = (k_d x_{0,1} + k_e x_w)/(k_d + k_e) and the force on the wall is kekdke+kd(x0,1−xw)\frac{k_e k_d}{k_e + k_d}(x_{0,1} - x_w), where kdk_d is the first diagonal entry of KdK_d.

Proof. At rest, x~˙=x~¨=0\dot{\tilde x} = \ddot{\tilde x} = 0, so Theorem 1 gives kd(x1−x0,1)=Fext,1k_d(x_1 - x_{0,1}) = F_{\text{ext},1}. If x1≤xwx_1 \le x_w, the right side is 00, so x1=x0,1>xwx_1 = x_{0,1} > x_w, which is a contradiction. So x1>xwx_1 > x_w and kd(x1−x0,1)=−ke(x1−xw)k_d(x_1 - x_{0,1}) = -k_e(x_1 - x_w). Solving gives the stated x1x_1. The force on the wall is ke(x1−xw)=ke(kdx0,1+kexwkd+ke−xw)=kekdkd+ke(x0,1−xw)k_e(x_1 - x_w) = k_e\big(\frac{k_d x_{0,1} + k_e x_w}{k_d + k_e} - x_w\big) = \frac{k_e k_d}{k_d + k_e}(x_{0,1} - x_w). □\square

Law without a force sensor. For τ=JT(Kd(x0−x)−Ddx˙)+g(q)\tau = J^T(K_d(x_0 - x) - D_d\dot x) + g(q) with a fixed goal, the function W′=12q˙TMq˙+12x~TKdx~+Ue(x)W' = \tfrac12\dot q^T M\dot q + \tfrac12\tilde x^T K_d\tilde x + U_e(x) satisfies W˙′=−x˙TDdx˙\dot W' = -\dot x^T D_d\dot x. The computation is the one of PD control with gravity compensation: W˙′=q˙T(τ+JTFext−Cq˙−g)+12q˙TM˙q˙+x~TKdx˙−FextTx˙\dot W' = \dot q^T(\tau + J^T F_{\text{ext}} - C\dot q - g) + \tfrac12\dot q^T\dot M\dot q + \tilde x^T K_d\dot x - F_{\text{ext}}^T\dot x, the skew symmetry removes the CC and M˙\dot M terms, and q˙TJT=x˙T\dot q^T J^T = \dot x^T removes the rest except −x˙TDdx˙-\dot x^T D_d\dot x. At rest, JT(Kd(x0−x)+Fext)=0J^T(K_d(x_0 - x) + F_{\text{ext}}) = 0, and since JJ is nonsingular, Kdx~=FextK_d\tilde x = F_{\text{ext}}, so Theorem 3 holds for this law too.

Complexity

In the real-RAM model, with nn joints and a task of dimension nn:

  • Lines L1 and L2 need the forward kinematics and the geometric Jacobian. For a serial chain, the joint frames take O(n)O(n) operations and the n×nn \times n Jacobian then takes O(n2)O(n^2).
  • Line L4 solves a linear system with JJ, which takes O(n3)O(n^3) operations by Gaussian elimination.
  • Line L5 is one inverse dynamics call, O(n)O(n) with the recursive Newton-Euler algorithm, plus JTFextJ^T F_{\text{ext}} in O(n2)O(n^2).

So one step costs O(n3)O(n^3) time and O(n2)O(n^2) memory. For a 6-joint arm this is a fixed small number of operations. The law without a force sensor needs only JTJ^T times a vector and the gravity torque, which is O(n2)O(n^2).

Properties

  • Bounded contact force. At rest against a spring-like surface, the force is keKd/(ke+Kd)k_e K_d/(k_e + K_d) times the depth of the goal inside the surface, so it is at most KdK_d times that depth, whatever the stiffness of the surface.
  • One law for free motion and contact. No switching is needed when contact starts or ends. This is the main practical advantage over hybrid position/force control.
  • Passivity. The simple law makes the arm a passive system seen from the contact point. Coupled to any passive environment, the system stays stable, which is not true for most position or force controllers.
  • No force tracking. The force is not controlled directly. It follows from the depth of the goal and the stiffness of the environment, and it changes if the surface is not where the model says.

Variants and improvements

  • Without inertia shaping. If MdM_d is the natural task-space inertia Λ(q)=(JM−1JT)−1\Lambda(q) = (J M^{-1} J^T)^{-1}, no force sensor is needed. This is the simple law above, also called compliance control or Cartesian stiffness control when the target mass is ignored.
  • Admittance control. On an arm with stiff position servos and no torque control, measure the force, integrate the target dynamics to get a reference motion, and send it to the position loop. See admittance control.
  • Orientation. For rotations the spring needs care: a spring on Euler angles depends on the chosen angles. Springs on the axis-angle error, or on the rotation matrix, give a stiffness that does not depend on the coordinates.
  • Redundant arms. With more joints than task directions, the operational space formulation gives the task impedance and leaves a null space for a second task.
  • Variable impedance. The stiffness and damping can change with time or with the task phase, but a changing stiffness can inject energy and needs its own stability check.

When to use it

Use it whenever the robot touches its environment and the position of the contact surface is uncertain: assembly such as peg-in-hole insertion, polishing, opening doors, and physical work with people. It needs a torque-controlled arm or at least a joint torque interface. For an arm with only position control, use admittance control. When a precise contact force must be held, combine it with an explicit force loop or use hybrid position/force control.

Pitfalls

  • Stiff environments and impact. The impact force depends on the target mass and the approach speed. A small target mass needs a fast inner loop and accurate force measurement; with a slow loop or noisy sensors, a small MdM_d against a stiff wall can make the contact unstable.
  • Model errors. The full law cancels the inertia of the arm. Errors in the model or in the force measurement leave the target equation only approximately true, and friction in the joints hides small forces.
  • Singularities. Line L4 needs J−1J^{-1}, which does not exist at a kinematic singularity. Near one, the joint torques grow large for a small task force.
  • Coordinates of the spring. A diagonal stiffness in one frame is not diagonal in another. Choose the frame in which the task has natural directions, such as the normal of the surface.
  • Goal inside the obstacle. The contact force is set by how far the goal lies inside the surface. A goal far inside a stiff surface gives a large force; plan the goal with the surface position in mind.

Implementations

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.

impedance_control.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/impedance-control/. The authors are listed in prov/TVY2W5.jsonl.

"""Impedance control of the end point of a planar 2R arm.

The arm obeys M(q) q'' + C(q, q') q' + g(q) = tau + J(q)^T F_ext, where
F_ext is the force of the environment on the end point. The law with a
force sensor makes the end point error x~ = x - x0 obey

    Md x~'' + Dd x~' + Kd x~ = F_ext.

The law without a force sensor, tau = J^T (Kd (x0 - x) - Dd x') + g(q), gives
the same spring and damper at rest but not the chosen mass.

Matrices are lists of rows and vectors are lists. Only the standard library.
"""

import math

GRAVITY = 9.81


class TwoLinkArm:
    """Planar 2R arm in a vertical plane, with point masses at the link ends.

    q1 is measured from the horizontal x axis, q2 from link 1. The y axis
    points up, and gravity acts along -y.
    """

    def __init__(self, m1=1.0, m2=1.0, l1=1.0, l2=1.0, grav=GRAVITY):
        self.m1, self.m2, self.l1, self.l2, self.grav = m1, m2, l1, l2, grav

    def mass(self, q):
        m1, m2, l1, l2 = self.m1, self.m2, self.l1, self.l2
        c2 = math.cos(q[1])
        a = (m1 + m2) * l1 * l1 + m2 * l2 * l2 + 2.0 * m2 * l1 * l2 * c2
        b = m2 * l2 * l2 + m2 * l1 * l2 * c2
        return [[a, b], [b, m2 * l2 * l2]]

    def coriolis(self, q, qd):
        h = self.m2 * self.l1 * self.l2 * math.sin(q[1])
        return [[-h * qd[1], -h * (qd[0] + qd[1])], [h * qd[0], 0.0]]

    def gravity(self, q):
        m1, m2, l1, l2, gr = self.m1, self.m2, self.l1, self.l2, self.grav
        c1, c12 = math.cos(q[0]), math.cos(q[0] + q[1])
        return [(m1 + m2) * gr * l1 * c1 + m2 * gr * l2 * c12, m2 * gr * l2 * c12]

    def fk(self, q):
        """Position of the end point."""
        q12 = q[0] + q[1]
        return [
            self.l1 * math.cos(q[0]) + self.l2 * math.cos(q12),
            self.l1 * math.sin(q[0]) + self.l2 * math.sin(q12),
        ]

    def jacobian(self, q):
        s1, c1 = math.sin(q[0]), math.cos(q[0])
        s12, c12 = math.sin(q[0] + q[1]), math.cos(q[0] + q[1])
        l1, l2 = self.l1, self.l2
        return [[-l1 * s1 - l2 * s12, -l2 * s12], [l1 * c1 + l2 * c12, l2 * c12]]

    def jdot_qdot(self, q, qd):
        """The term J'(q, q') q' of the end point acceleration."""
        w1, w12 = qd[0], qd[0] + qd[1]
        s1, c1 = math.sin(q[0]), math.cos(q[0])
        s12, c12 = math.sin(q[0] + q[1]), math.cos(q[0] + q[1])
        l1, l2 = self.l1, self.l2
        return [
            -l1 * c1 * w1 * w1 - l2 * c12 * w12 * w12,
            -l1 * s1 * w1 * w1 - l2 * s12 * w12 * w12,
        ]

    def ik(self, x, elbow=1.0):
        """Joint angles that put the end point at x (elbow = +1 or -1)."""
        l1, l2 = self.l1, self.l2
        c2 = (x[0] ** 2 + x[1] ** 2 - l1 * l1 - l2 * l2) / (2 * l1 * l2)
        if abs(c2) > 1.0:
            raise ValueError("point out of reach")
        q2 = elbow * math.acos(c2)
        q1 = math.atan2(x[1], x[0]) - math.atan2(l2 * math.sin(q2), l1 + l2 * math.cos(q2))
        return [q1, q2]

    def accel(self, q, qd, tau, f_ext):
        """Solve M q'' = tau + J^T F_ext - C q' - g for q''."""
        m = self.mass(q)
        c = self.coriolis(q, qd)
        g = self.gravity(q)
        j = self.jacobian(q)
        r = [
            tau[i] + j[0][i] * f_ext[0] + j[1][i] * f_ext[1] - c[i][0] * qd[0] - c[i][1] * qd[1] - g[i]
            for i in range(2)
        ]
        return solve2(m, r)


def solve2(a, b):
    """Solve the 2 by 2 system a x = b by Cramer's rule."""
    det = a[0][0] * a[1][1] - a[0][1] * a[1][0]
    if det == 0.0:
        raise ValueError("singular 2 by 2 matrix")
    return [(b[0] * a[1][1] - a[0][1] * b[1]) / det, (a[0][0] * b[1] - b[0] * a[1][0]) / det]


def matvec2(a, v):
    return [a[0][0] * v[0] + a[0][1] * v[1], a[1][0] * v[0] + a[1][1] * v[1]]


def impedance_control(arm, q, qd, x0, x0d, x0dd, f_ext, md, dd, kd):
    """Impedance law with a force sensor and a full dynamic model.

    f_ext is the measured force of the environment on the end point.
    md, dd and kd are lists of diagonal task-space gains.
    """
    j = arm.jacobian(q)
    x, xd = arm.fk(q), matvec2(j, qd)  # @line L1
    xt = [x[i] - x0[i] for i in range(2)]  # @line L2
    xtd = [xd[i] - x0d[i] for i in range(2)]
    ax = [x0dd[i] + (f_ext[i] - dd[i] * xtd[i] - kd[i] * xt[i]) / md[i] for i in range(2)]  # @line L3
    jq = arm.jdot_qdot(q, qd)
    aq = solve2(j, [ax[i] - jq[i] for i in range(2)])  # @line L4
    m, c, g = arm.mass(q), arm.coriolis(q, qd), arm.gravity(q)  # @line L5
    tau = [
        sum(m[i][k] * aq[k] + c[i][k] * qd[k] for k in range(2)) + g[i] - (j[0][i] * f_ext[0] + j[1][i] * f_ext[1])
        for i in range(2)
    ]
    return tau  # @line L6


def simple_impedance(arm, q, qd, x0, dd, kd):
    """Impedance law without a force sensor: tau = J^T (Kd (x0 - x) - Dd x') + g(q)."""
    j = arm.jacobian(q)
    x, xd = arm.fk(q), matvec2(j, qd)
    f = [kd[i] * (x0[i] - x[i]) - dd[i] * xd[i] for i in range(2)]
    g = arm.gravity(q)
    return [j[0][i] * f[0] + j[1][i] * f[1] + g[i] for i in range(2)]


def wall(x_wall, k_env):
    """A frictionless wall x <= x_wall with stiffness k_env, as F_ext(x, x')."""

    def force(x, xd):
        pen = x[0] - x_wall
        return [-k_env * pen, 0.0] if pen > 0.0 else [0.0, 0.0]

    return force


def free_space(x, xd):
    return [0.0, 0.0]


def rk4_step(f, t, x, dt):
    """One classical Runge-Kutta step for x' = f(t, x)."""
    k1 = f(t, x)
    k2 = f(t + dt / 2, [x[i] + dt / 2 * k1[i] for i in range(len(x))])
    k3 = f(t + dt / 2, [x[i] + dt / 2 * k2[i] for i in range(len(x))])
    k4 = f(t + dt, [x[i] + dt * k3[i] for i in range(len(x))])
    return [x[i] + dt / 6 * (k1[i] + 2 * k2[i] + 2 * k3[i] + k4[i]) for i in range(len(x))]


def simulate(arm, law, env, q0, qd0, dt, steps):
    """Simulate the arm with law(t, q, qd, f_ext) and the environment env.

    The sensor reads the true contact force. Return the list of (t, q, qd).
    """

    def f(t, s):
        q, qd = s[:2], s[2:]
        x, xd = arm.fk(q), matvec2(arm.jacobian(q), qd)
        fe = env(x, xd)
        tau = law(t, q, qd, fe)
        return qd + arm.accel(q, qd, tau, fe)

    s = list(q0) + list(qd0)
    out = [(0.0, s[:2], s[2:])]
    for k in range(steps):
        s = rk4_step(f, k * dt, s, dt)
        out.append(((k + 1) * dt, s[:2], s[2:]))
    return out
test_impedance_control.py 120 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/impedance-control/. The authors are listed in prov/TVY2W5.jsonl.

import math
import random

from impedance_control import (
    TwoLinkArm,
    free_space,
    impedance_control,
    matvec2,
    simple_impedance,
    simulate,
    wall,
)

MD = [1.0, 1.0]
KD = [400.0, 400.0]
DD = [40.0, 40.0]  # critical damping: Dd = 2 sqrt(Kd Md)
K_ENV = 1e4
X_WALL = 1.0
X0 = [1.05, 0.5]


def test_kinematics_against_finite_differences():
    arm = TwoLinkArm(l1=0.9, l2=0.7)
    rng = random.Random(1)
    for _ in range(10):
        q = [rng.uniform(-3, 3) for _ in range(2)]
        qd = [rng.uniform(-2, 2) for _ in range(2)]
        h = 1e-6
        xp = arm.fk([q[i] + h * qd[i] for i in range(2)])
        xm = arm.fk([q[i] - h * qd[i] for i in range(2)])
        v = matvec2(arm.jacobian(q), qd)
        jp = matvec2(arm.jacobian([q[i] + h * qd[i] for i in range(2)]), qd)
        jm = matvec2(arm.jacobian([q[i] - h * qd[i] for i in range(2)]), qd)
        jq = arm.jdot_qdot(q, qd)
        for i in range(2):
            assert abs((xp[i] - xm[i]) / (2 * h) - v[i]) < 1e-7
            assert abs((jp[i] - jm[i]) / (2 * h) - jq[i]) < 1e-6
        x = arm.fk(arm.ik(arm.fk(q), 1.0 if math.sin(q[1]) > 0 else -1.0))
        assert max(abs(x[i] - arm.fk(q)[i]) for i in range(2)) < 1e-12


def test_closed_loop_is_the_target_impedance():
    # For any state and any contact force, Md x~'' + Dd x~' + Kd x~ = F_ext.
    arm = TwoLinkArm()
    rng = random.Random(4)
    for _ in range(20):
        q = [rng.uniform(-2, 2), rng.uniform(0.3, 2.8)]
        qd = [rng.uniform(-2, 2) for _ in range(2)]
        x0, x0d, x0dd, fe = ([rng.uniform(-1, 1) for _ in range(2)] for _ in range(4))
        tau = impedance_control(arm, q, qd, x0, x0d, x0dd, fe, MD, DD, KD)
        qdd = arm.accel(q, qd, tau, fe)
        j = arm.jacobian(q)
        x, xd = arm.fk(q), matvec2(j, qd)
        jq = arm.jdot_qdot(q, qd)
        xdd = [matvec2(j, qdd)[i] + jq[i] for i in range(2)]
        for i in range(2):
            lhs = MD[i] * (xdd[i] - x0dd[i]) + DD[i] * (xd[i] - x0d[i]) + KD[i] * (x[i] - x0[i])
            assert abs(lhs - fe[i]) < 1e-9


def test_free_space_error_matches_closed_form():
    arm = TwoLinkArm()
    w = 20.0

    def goal(t):
        return (
            [0.9 + 0.1 * math.sin(t), 0.5 + 0.1 * math.cos(t)],
            [0.1 * math.cos(t), -0.1 * math.sin(t)],
            [-0.1 * math.sin(t), -0.1 * math.cos(t)],
        )

    def law(t, q, qd, fe):
        x0, x0d, x0dd = goal(t)
        return impedance_control(arm, q, qd, x0, x0d, x0dd, fe, MD, DD, KD)

    q0 = arm.ik([0.85, 0.65])
    out = simulate(arm, law, free_space, q0, [0.0, 0.0], 1e-3, 1000)
    e0 = [arm.fk(q0)[i] - goal(0.0)[0][i] for i in range(2)]
    ed0 = [-goal(0.0)[1][i] for i in range(2)]
    for t, q, _ in out[::50]:
        x = arm.fk(q)
        for i in range(2):
            want = (e0[i] + (ed0[i] + w * e0[i]) * t) * math.exp(-w * t)
            assert abs((x[i] - goal(t)[0][i]) - want) < 1e-7


def test_contact_force_at_rest():
    arm = TwoLinkArm()
    env = wall(X_WALL, K_ENV)

    def law(t, q, qd, fe):
        return impedance_control(arm, q, qd, X0, [0.0, 0.0], [0.0, 0.0], fe, MD, DD, KD)

    out = simulate(arm, law, env, arm.ik([0.9, 0.5]), [0.0, 0.0], 1e-3, 1500)
    x = arm.fk(out[-1][1])
    force = env(x, None)
    want = K_ENV * KD[0] / (K_ENV + KD[0]) * (X0[0] - X_WALL)
    assert abs(-force[0] - want) < 1e-6
    assert abs(x[0] - (KD[0] * X0[0] + K_ENV * X_WALL) / (KD[0] + K_ENV)) < 1e-9
    # The impedances are decoupled: the y motion never sees the contact.
    assert max(abs(arm.fk(q)[1] - X0[1]) for _, q, _ in out) < 1e-9


def test_law_without_force_sensor():
    arm = TwoLinkArm()
    env = wall(X_WALL, K_ENV)

    def law(t, q, qd, fe):
        return simple_impedance(arm, q, qd, X0, DD, KD)

    out = simulate(arm, law, env, arm.ik([0.9, 0.5]), [0.0, 0.0], 1e-3, 2500)
    x = arm.fk(out[-1][1])
    want = K_ENV * KD[0] / (K_ENV + KD[0]) * (X0[0] - X_WALL)
    assert abs(-env(x, None)[0] - want) < 1e-4
    assert abs(x[1] - X0[1]) < 1e-6
    # Without inertia shaping the arm's own mass couples x and y on the way.
    assert max(abs(arm.fk(q)[1] - X0[1]) for _, q, _ in out) > 1e-4

History

Robot force control had several schemes by the early 1980s, such as stiffness control, damping control and the hybrid scheme of Raibert and Craig, and Mason described compliant motion with natural and artificial constraints in 1981. Neville Hogan published "Impedance control: an approach to manipulation" in three parts in the Journal of Dynamic Systems, Measurement, and Control in March 1985. He argued that a manipulator interacts with its environment as a physical system, so the controller should set its impedance, and that the robot should look like an impedance because the environment usually looks like an admittance. The idea became the base of most later interaction control, including the control of collaborative robots.

References

  1. Neville Hogan, Impedance control: an approach to manipulation: Part I, Theory, Journal of Dynamic Systems, Measurement, and Control 107(1), 1985, pages 1 to 7; Parts II (implementation) and III (applications) follow in the same issue
  2. Kevin M. Lynch and Frank C. Park, Modern Robotics, Mechanics, Planning, and Control, Cambridge University Press, 2017, Chapter 11, Section 11.7 (impedance control) and Section 11.7.1 (impedance-control algorithm)
  3. Bruno Siciliano, Lorenzo Sciavicco, Luigi Villani and Giuseppe Oriolo, Robotics, Modelling, Planning and Control, Springer, 2009, Chapter 9 (force control, compliance and impedance control)
  4. Bruno Siciliano and Oussama Khatib (editors), Springer Handbook of Robotics, second edition, Springer, 2016, Chapter 9 (Force Control, by Luigi Villani and Joris De Schutter)
  5. Mark W. Spong, Seth Hutchinson and M. Vidyasagar, Robot Modeling and Control, second edition, Wiley, 2020, Chapter 10 (force control)

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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.per_step
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example