Algorithm Search⌘K

Algorithmcore

Hybrid position/force control

Also called Raibert-Craig hybrid control.

Splits the task space by selection matrices into directions that control force and directions that control position, and runs one controller in each.

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
Marc H. Raibert, John J. Craig, 1981

Hybrid position/force control is a scheme for tasks in which a robot tool slides along a surface while it presses on it. The task space is split into directions in which the surface blocks motion, where the controller regulates the contact force, and the other directions, where the controller regulates the position. Diagonal selection matrices SfS_f and Sp=I−SfS_p = I - S_f in a task frame pick the two sets. Raibert and Craig introduced the scheme in 1981, using the natural and artificial constraints of Mason to choose the directions. Unlike impedance control, it controls the force explicitly, but it needs a model of where the constraints are.

In plain words

Think of wiping a table. Along the table you care about where the cloth goes. Down into the table you cannot move anyway, so you care about how hard you press. Hybrid control writes this down: in each direction of a frame fixed to the task, choose either "control the position" or "control the force", never both.

The table itself fixes the motion downward and the force sideways (if there is no friction). These are the natural constraints. The task adds the other half: a path along the table and a pressing force downward. These are the artificial constraints. The controller runs a position loop in the free directions and a force loop in the blocked ones.

The problem

The arm has nn joints and 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 FextF_{\text{ext}} is the wrench of the environment on the tool and JJ is square and nonsingular. In a task frame aligned with the contact, a diagonal matrix SfS_f with entries 0 or 1 marks the directions in which the environment blocks the motion, and Sp=I−SfS_p = I - S_f marks the free ones. A desired motion xd(t)x_d(t) is given in the free directions, and a desired force Fd(t)F_d(t) that the tool should apply in the blocked directions. The task is to choose τ\tau so that Sp(x−xd)→0S_p(x - x_d) \to 0 and Sf(F−Fd)→0S_f(F - F_d) \to 0, where F=−FextF = -F_{\text{ext}} is the measured force that the tool applies.

The algorithm

This is the task-space form with a dynamic model. The integral ∫(Fd−F) dt\int(F_d - F)\,dt of the force error is kept as a state.

  1. HYBRID(q, q', x_d, x_d', x_d'', F_d, F, ∫(F_d - F), S_f)
  2. 1x ← f(q); x' ← J(q) q'
  3. 2a_p ← x_d'' + K_d (x_d' - x') + K_p (x_d - x) ▷ position loop
  4. 3F_f ← F_d + K_fi ∫(F_d - F) - K_v x' ▷ force loop with active damping
  5. 4Λ ← (J M⁻¹ Jᵀ)⁻¹; μ ← Λ (J M⁻¹ (C q' + g) - J' q')
  6. 5F_cmd ← Λ (I - S_f) a_p + S_f F_f + μ
  7. 6τ ← Jᵀ F_cmd
  8. 7return τ

Line L4 computes the task-space inertia Λ\Lambda and the bias force μ\mu of the operational space formulation. Line L5 uses them so that the position loop sees a unit mass in the free directions, while the force command passes through directly. Line L6 maps the task force to joint torques by Jacobian-statics duality. The term −Kvx′-K_v x' in line L3 adds damping in the force directions; it is zero when the contact is rigid.

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 at x=1x = 1 m acts as a stiff spring with ke=105k_e = 10^5 N/m. The force direction is the wall normal xx, so Sf=diag⁡(1,0)S_f = \operatorname{diag}(1, 0), with Fd=10F_d = 10 N. The position direction is yy, with yd(t)=0.5+0.1sin⁡2ty_d(t) = 0.5 + 0.1\sin 2t m. The gains are Kp=400K_p = 400, Kd=40K_d = 40 (critical damping at 20 rad/s), Kv=600K_v = 600 N s/m and Kfi=0K_{fi} = 0. The arm starts at rest at (1,0.5)(1, 0.5), touching the wall with zero force.

At the start, q=(−0.514,1.955)q = (-0.514, 1.955) rad, and line L4 gives Λ=[[1.019,0.149],[0.149,2.144]]\Lambda = [[1.019, 0.149], [0.149, 2.144]] kg and μ=(1.19,18.95)\mu = (1.19, 18.95) N. The position error is zero, but the speed error is y˙d=0.2\dot y_d = 0.2 m/s, so line L2 gives ap,y=8a_{p,y} = 8 m/s2^2. Line L5 gives Fcmd=(1.19+10+1.19, 17.15+18.95)=(12.38,36.10)F_{\text{cmd}} = (1.19 + 10 + 1.19,\ 17.15 + 18.95) = (12.38, 36.10) N, and line L6 gives τ=(29.91,−7.62)\tau = (29.91, -7.62) N m.

A simulation with RK4 and a step of 1 ms gives:

time (s) force on the wall (N) error in yy (mm)
0.01 8.48 −1.64-1.64
0.02 9.95 −2.69-2.69
0.05 10.000 −3.68-3.68
0.10 10.000 −2.71-2.71
0.50 10.000 −0.005-0.005

The force settles in about 50 ms, with a peak of 10.0012 N. The yy error is −0.2 t e−20t-0.2\,t\,e^{-20 t} m, the critically damped response to the initial speed error, as if the wall were not there. The tool presses 0.1 mm into the spring wall, which is Fd/keF_d/k_e.

With a gravity model in which the second mass is 0.8 kg instead of 1 kg, and a fixed goal yd=0.5y_d = 0.5, the force settles at 10.152 N with Kfi=0K_{fi} = 0. With Kfi=20K_{fi} = 20 s−1^{-1} it settles at 10.000 N. The yy position still ends 2.5 mm low, because the position loop has no integral term.

Correctness

The claim is: with an exact model, the closed loop in task coordinates is

x¨=Spap+Λ−1(SfFf+Fext).\ddot x = S_p a_p + \Lambda^{-1}(S_f F_f + F_{\text{ext}}).

If the contact is rigid and frictionless, the constraint keeps Sfx¨=0S_f\ddot x = 0 and SpFext=0S_p F_{\text{ext}} = 0. Because Λ−1\Lambda^{-1} is positive definite, these two facts force SfFf+Fext=0S_f F_f + F_{\text{ext}} = 0. So the force on the environment equals SfFfS_f F_f, which is SfFdS_f F_d when Kfi=0K_{fi} = 0, and the free directions obey e¨+Kde˙+Kpe=0\ddot e + K_d\dot e + K_p e = 0 exactly. Against a frictionless spring surface, the same argument applies at every rest point: the position error is zero and the force is SfFdS_f F_d. With Kfi>0K_{fi} > 0, the force at rest is SfFdS_f F_d even when the model is wrong, because the integral state can stop only when the force error is zero. The full proof is in the proof file.

Proof: Hybrid control decouples the free directions and sets the contact forcefull

The arm obeys Mq¨+Cq˙+g=τ+JTFextM\ddot q + C\dot q + g = \tau + J^T F_{\text{ext}} with MM positive definite and JJ square and nonsingular, and x=f(q)x = f(q), so x˙=Jq˙\dot x = J\dot q and x¨=Jq¨+J˙q˙\ddot x = J\ddot q + \dot J\dot q. The matrices SfS_f and Sp=I−SfS_p = I - S_f are diagonal with entries 0 or 1, so Sf2=SfS_f^2 = S_f, Sp2=SpS_p^2 = S_p and SfSp=0S_f S_p = 0. The gains KpK_p, KdK_d, KfiK_{fi} and KvK_v are diagonal with positive entries (KfiK_{fi} may be zero). Write If=∫(Fd−F) dtI_f = \int(F_d - F)\,dt for the integral state, with I˙f=Sf(Fd−F)\dot I_f = S_f(F_d - F) and If(0)=0I_f(0) = 0, and F=−FextF = -F_{\text{ext}} for the force that the tool applies. Parts 1 to 4 assume that the controller uses the exact model.

Lemma 1 (closed loop in task space). Under the torque of lines L1 to L7,

x¨=Spap+Λ−1(SfFf+Fext).\ddot x = S_p a_p + \Lambda^{-1}(S_f F_f + F_{\text{ext}}).

Proof. Let h=Cq˙+gh = C\dot q + g. Then q¨=M−1(JTFcmd+JTFext−h)\ddot q = M^{-1}(J^T F_{\text{cmd}} + J^T F_{\text{ext}} - h), so

x¨=JM−1JT(Fcmd+Fext)−JM−1h+J˙q˙=Λ−1(Fcmd+Fext)−Λ−1μ,\ddot x = J M^{-1} J^T (F_{\text{cmd}} + F_{\text{ext}}) - J M^{-1} h + \dot J\dot q = \Lambda^{-1}(F_{\text{cmd}} + F_{\text{ext}}) - \Lambda^{-1}\mu,

using Λ−1=JM−1JT\Lambda^{-1} = J M^{-1} J^T and Λ−1μ=JM−1h−J˙q˙\Lambda^{-1}\mu = J M^{-1} h - \dot J\dot q from line L4. The matrix JM−1JTJ M^{-1} J^T is positive definite because JJ is nonsingular, so Λ\Lambda exists. Putting in Fcmd=ΛSpap+SfFf+μF_{\text{cmd}} = \Lambda S_p a_p + S_f F_f + \mu from line L5 gives the claim. □\square

Lemma 2. Let Λ−1\Lambda^{-1} be positive definite and let w=Sfww = S_f w. If SfΛ−1w=0S_f\Lambda^{-1} w = 0, then w=0w = 0.

Proof. wTΛ−1w=(Sfw)TΛ−1w=wTSfΛ−1w=0w^T\Lambda^{-1}w = (S_f w)^T\Lambda^{-1}w = w^T S_f\Lambda^{-1}w = 0, and positive definiteness gives w=0w = 0. □\square

Theorem 1 (rigid frictionless contact). Let the environment be a rigid frictionless constraint along the force directions: Sfx˙=0S_f\dot x = 0 for all time and SpFext=0S_p F_{\text{ext}} = 0. Then at all times the applied force is F=Sf(Fd+KfiIf)F = S_f(F_d + K_{fi} I_f) and the position error e=Sp(x−xd)e = S_p(x - x_d) satisfies e¨+Kde˙+Kpe=0\ddot e + K_d\dot e + K_p e = 0. With Kfi=0K_{fi} = 0, F=SfFdF = S_f F_d. With Kfi>0K_{fi} > 0, IfI_f stays zero and F=SfFdF = S_f F_d too.

Proof. Let w=SfFf+Fextw = S_f F_f + F_{\text{ext}}. Since Fext=SfFextF_{\text{ext}} = S_f F_{\text{ext}}, w=Sfww = S_f w. Multiply Lemma 1 by SfS_f: 0=Sfx¨=SfSpap+SfΛ−1w=SfΛ−1w0 = S_f\ddot x = S_f S_p a_p + S_f\Lambda^{-1}w = S_f\Lambda^{-1}w. By Lemma 2, w=0w = 0, so F=−Fext=SfFf=Sf(Fd+KfiIf−Kvx˙)F = -F_{\text{ext}} = S_f F_f = S_f(F_d + K_{fi}I_f - K_v\dot x). The last term is zero because SfKvx˙=KvSfx˙=0S_f K_v\dot x = K_v S_f\dot x = 0 for diagonal KvK_v. Lemma 1 now reads x¨=Spap\ddot x = S_p a_p, so Spx¨=SpapS_p\ddot x = S_p a_p, and with diagonal gains Spap=Spx¨d+KdSp(x˙d−x˙)+KpSp(xd−x)S_p a_p = S_p\ddot x_d + K_d S_p(\dot x_d - \dot x) + K_p S_p(x_d - x). Subtracting Spx¨dS_p\ddot x_d gives e¨+Kde˙+Kpe=0\ddot e + K_d\dot e + K_p e = 0. For the integral, I˙f=Sf(Fd−F)=−KfiSfIf\dot I_f = S_f(F_d - F) = -K_{fi}S_f I_f, and since If(0)=0I_f(0) = 0, If=0I_f = 0 for all time. □\square

With positive diagonal gains, each component of ee is a stable second-order linear system, so e→0e \to 0 exponentially, and with Kd2=4KpK_d^2 = 4K_p it does not overshoot.

Theorem 2 (rest points against a spring surface). Let xdx_d be constant and let the environment push only along the force directions, SpFext=0S_p F_{\text{ext}} = 0, with any law Fext(x)F_{\text{ext}}(x). At every rest point of the closed loop (q˙=0\dot q = 0, q¨=0\ddot q = 0, I˙f=0\dot I_f = 0), Spx=SpxdS_p x = S_p x_d and F=SfFdF = S_f F_d.

Proof. At rest, x˙=0\dot x = 0 and x¨=0\ddot x = 0, and Spap=KpSp(xd−x)S_p a_p = K_p S_p(x_d - x). Let w=SfFf+Fextw = S_f F_f + F_{\text{ext}}, so w=Sfww = S_f w. Lemma 1 gives Λ−1w=−Spap\Lambda^{-1}w = -S_p a_p. Then wTΛ−1w=−wTSfSpap=0w^T\Lambda^{-1}w = -w^T S_f S_p a_p = 0, so w=0w = 0, and then Spap=0S_p a_p = 0, which gives Spx=SpxdS_p x = S_p x_d. From w=0w = 0, F=SfFf=Sf(Fd+KfiIf)F = S_f F_f = S_f(F_d + K_{fi}I_f). In each force direction, either Kfi=0K_{fi} = 0 and the force is that component of FdF_d, or Kfi>0K_{fi} > 0 and I˙f=0\dot I_f = 0 gives the same. □\square

Theorem 3 (stability of one force direction). Let there be one force direction ff, a spring surface with Fext,f=−ke(xf−xw)F_{\text{ext},f} = -k_e(x_f - x_w) in contact, SpFext=0S_p F_{\text{ext}} = 0 and constant FdF_d. Hold c=(Λ−1)ff>0c = (\Lambda^{-1})_{ff} > 0 fixed and put mf=1/cm_f = 1/c. Then the force subsystem is exponentially stable if and only if Kfi<Kv/mfK_{fi} < K_v/m_f (for Kfi>0K_{fi} > 0); for Kfi=0K_{fi} = 0, its motion part is exponentially stable for all Kv>0K_v > 0.

Proof. The vector w=SfFf+Fextw = S_f F_f + F_{\text{ext}} has only entry ff nonzero, equal to Ff+Fext,fF_f + F_{\text{ext},f}, and the entry ff of SpapS_p a_p is zero. So the ff row of Lemma 1 is x¨f=c(Ff+Fext,f)\ddot x_f = c(F_f + F_{\text{ext},f}), which does not involve the position directions. With z=xf−xwz = x_f - x_w and Ff=Fd+KfiI−Kvz˙F_f = F_d + K_{fi}I - K_v\dot z,

mfz¨=Fd+KfiI−Kvz˙−kez,I˙=Fd−kez.m_f\ddot z = F_d + K_{fi}I - K_v\dot z - k_e z,\qquad \dot I = F_d - k_e z.

Differentiating the first equation and using the second gives mfz...+Kvz¨+kez˙+Kfikez=KfiFdm_f\dddot z + K_v\ddot z + k_e\dot z + K_{fi}k_e z = K_{fi}F_d. The characteristic polynomial is mfs3+Kvs2+kes+Kfikem_f s^3 + K_v s^2 + k_e s + K_{fi}k_e. For Kfi>0K_{fi} > 0, the Routh-Hurwitz test for a cubic with positive coefficients says that all roots have negative real part if and only if Kvke>mfKfikeK_v k_e > m_f K_{fi}k_e, which is Kfi<Kv/mfK_{fi} < K_v/m_f. For Kfi=0K_{fi} = 0, the variable II does not enter, and the motion obeys mfz¨+Kvz˙+kez=Fdm_f\ddot z + K_v\dot z + k_e z = F_d, which is exponentially stable for Kv,ke>0K_v, k_e > 0. □\square

Remark (wrong model). If the controller uses a wrong model, Lemma 1 fails, and so does the conclusion of Theorem 2 for Kfi=0K_{fi} = 0. The step "I˙f=0\dot I_f = 0 gives F=SfFdF = S_f F_d" does not use the model, so with Kfi>0K_{fi} > 0 every rest point still has the exact force SfFdS_f F_d. The position directions have no integral term, so their rest error remains. ■\blacksquare

Complexity

In the real-RAM model, with nn joints and task dimension nn, one step needs the Jacobian (O(n2)O(n^2) entries), the inertia matrix and its inverse (O(n3)O(n^3)), the product JM−1JTJ M^{-1} J^T and its inverse (O(n3)O(n^3)), and a few matrix-vector products (O(n2)O(n^2)). So it takes O(n3)O(n^3) time and O(n2)O(n^2) memory. For a 6-joint arm this is a fixed number of operations. The original joint-space form without Λ\Lambda costs O(n3)O(n^3) too, because of J−1J^{-1}.

Properties

  • Explicit force regulation. Unlike impedance control, the force in the constrained directions follows FdF_d directly and does not depend on the stiffness of the environment at rest.
  • Orthogonal split. Position and force are never controlled in the same direction. This is consistent only when the selection matches the real constraints in a frame where they are axis-aligned.
  • Needs the contact geometry. The task frame and SfS_f come from a model of the contact. If the surface is tilted or curved and the frame is not updated, the position loop fights the surface and the force loop pushes into free space.
  • Needs contact. In a force direction without contact, the force loop accelerates the tool until it hits something. Switching between free motion and contact needs care.

Variants and improvements

  • Original joint-space form. Raibert and Craig mapped the position errors to joint space through J−1J^{-1} and added a separate force term through JTJ^T, with gains in joint space. Later analyses found that this form can be unstable in some arm configurations; the task-space form with Λ\Lambda above avoids the problem.
  • Constraint-based form. Lynch and Park write the constraints as A(q)q˙=0A(q)\dot q = 0 and use a projection matrix that depends on the state instead of a constant SfS_f. This handles constraints that are not aligned with fixed axes.
  • Hybrid impedance control. Use an impedance law in the position directions, so that the tool stays compliant when it meets an unexpected obstacle there.
  • Force control on a position-controlled arm. Realize the force loop as an outer loop that adjusts the position command, as in admittance control with Ka=0K_a = 0 in the force directions.
  • Parallel force/position control. Let both loops act in all directions, with the force loop given priority by an integral term, which avoids the need for an exact selection matrix.

When to use it

Use it for contact tasks with known, simple geometry: pressing a tool on a flat surface while moving along it (grinding, polishing, deburring), turning a crank, or pushing a peg along the axis of a hole while keeping the side forces at zero. When the contact geometry is uncertain or contact starts and ends often, impedance control is usually more robust.

Pitfalls

  • Wrong frame. The selection matrix is meaningful only in a task frame aligned with the contact. Using it in a fixed base frame when the surface is tilted leads to conflicts between the loops.
  • Stiff contact and force gains. The force loop includes the environment stiffness. Against a stiff surface, high force gains or too little damping KvK_v make the contact chatter. For one force direction against a spring wall, with the effective mass mf=1/(Λ−1)ffm_f = 1/(\Lambda^{-1})_{ff} held fixed, the loop is stable if and only if Kfi<Kv/mfK_{fi} < K_v/m_f.
  • Friction. With friction, the surface also applies forces in the position directions, so SpFext≠0S_p F_{\text{ext}} \ne 0 and the position loop sees a disturbance. Friction also lets the surface hold the tool against forces inside the friction cone.
  • Loss of contact. If the force command is negative or the surface ends, the tool leaves the surface and the force loop has no plant to act on. Clamp the force command and detect the loss of contact.
  • Singularities. Λ\Lambda and J−1J^{-1} fail near a kinematic singularity.

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.

hybrid_position_force_control.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hybrid-position-force-control/. The authors are listed in prov/YQZW8R.jsonl.

"""Hybrid position/force control of a planar 2R arm against a wall.

The wall is the line x = x_w, and the tool slides on it without friction.
The x direction (the wall normal) is force controlled and the y direction
is position controlled, with the selection matrices S_f = diag(1, 0) and
S_p = I - S_f. The law works in task space with the task inertia
Lam = (J M^-1 J^T)^-1:

    F_cmd = Lam S_p a_p + S_f F_f + mu,    tau = J^T F_cmd.

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."""

    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):
        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):
        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):
        l1, l2 = self.l1, self.l2
        c2 = (x[0] ** 2 + x[1] ** 2 - l1 * l1 - l2 * l2) / (2 * l1 * l2)
        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, c, g, j = self.mass(q), self.coriolis(q, qd), self.gravity(q), 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 inv2(a):
    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 [[a[1][1] / det, -a[0][1] / det], [-a[1][0] / det, a[0][0] / det]]


def mul2(a, b):
    return [[sum(a[i][k] * b[k][j] for k in range(2)) for j in range(2)] for i in range(2)]


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 solve2(a, b):
    return matvec2(inv2(a), b)


def transpose2(a):
    return [[a[0][0], a[1][0]], [a[0][1], a[1][1]]]


def task_inertia(model, q, qd):
    """Return Lam = (J M^-1 J^T)^-1 and mu = Lam (J M^-1 (C q' + g) - J' q')."""
    j = model.jacobian(q)
    minv = inv2(model.mass(q))
    lam = inv2(mul2(mul2(j, minv), transpose2(j)))
    c, g = model.coriolis(q, qd), model.gravity(q)
    h = [c[i][0] * qd[0] + c[i][1] * qd[1] + g[i] for i in range(2)]
    jm = matvec2(mul2(j, minv), h)
    jq = model.jdot_qdot(q, qd)
    return lam, matvec2(lam, [jm[i] - jq[i] for i in range(2)])


def hybrid_control(model, q, qd, x_des, xd_des, xdd_des, f_des, f_meas, f_int, sf, gains):
    """Return the joint torques of hybrid position/force control.

    sf is the diagonal of S_f (1 for a force direction, 0 for a position
    direction). f_des and f_meas are the desired and measured forces that the
    tool applies to the environment, and f_int is the integral of
    f_des - f_meas. gains = (kp, kd, kfi, kv) are lists of diagonal gains.
    """
    kp, kd, kfi, kv = gains
    j = model.jacobian(q)
    x, xd = model.fk(q), matvec2(j, qd)  # @line L1
    ap = [xdd_des[i] + kd[i] * (xd_des[i] - xd[i]) + kp[i] * (x_des[i] - x[i]) for i in range(2)]  # @line L2
    ff = [f_des[i] + kfi[i] * f_int[i] - kv[i] * xd[i] for i in range(2)]  # @line L3
    lam, mu = task_inertia(model, q, qd)  # @line L4
    pa = matvec2(lam, [(1.0 - sf[i]) * ap[i] for i in range(2)])  # @line L5
    fcmd = [pa[i] + sf[i] * ff[i] + mu[i] for i in range(2)]
    tau = matvec2(transpose2(j), fcmd)  # @line L6
    return tau  # @line L7


def wall(x_wall, k_env):
    """Force of a frictionless spring wall x >= x_wall on the tool."""

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

    return force


def rk4_step(f, t, x, dt):
    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, model, env, goal, f_des, sf, gains, q0, dt, steps):
    """Simulate the closed loop; the state is (q, q', integral of the force error).

    goal(t) returns (x_des, xd_des, xdd_des). The sensor reads the true
    force that the tool applies, -F_ext. Return a list of (t, q, qd, f_applied).
    """

    def f(t, s):
        q, qd, fi = s[:2], s[2:4], s[4:]
        fe = env(arm.fk(q))
        applied = [-fe[0], -fe[1]]
        xs, xds, xdds = goal(t)
        tau = hybrid_control(model, q, qd, xs, xds, xdds, f_des, applied, fi, sf, gains)
        err = [sf[i] * (f_des[i] - applied[i]) for i in range(2)]
        return qd + arm.accel(q, qd, tau, fe) + err

    s = list(q0) + [0.0, 0.0, 0.0, 0.0]
    out = []
    for k in range(steps + 1):
        fe = env(arm.fk(s[:2]))
        out.append((k * dt, s[:2], s[2:4], [-fe[0], -fe[1]]))
        if k < steps:
            s = rk4_step(f, k * dt, s, dt)
    return out
test_hybrid_position_force_control.py 79 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hybrid-position-force-control/. The authors are listed in prov/YQZW8R.jsonl.

import math
import random

from hybrid_position_force_control import (
    TwoLinkArm,
    hybrid_control,
    inv2,
    matvec2,
    simulate,
    task_inertia,
    wall,
)

SF = [1.0, 0.0]
F_DES = [10.0, 0.0]
K_ENV = 1e5


def gains(kfi):
    return ([400.0, 400.0], [40.0, 40.0], [kfi, kfi], [600.0, 600.0])


def slide(t):
    return [0.0, 0.5 + 0.1 * math.sin(2 * t)], [0.0, 0.2 * math.cos(2 * t)], [0.0, -0.4 * math.sin(2 * t)]


def test_task_space_closed_loop():
    # x'' = S_p a_p + Lam^-1 (S_f F_f + F_ext) for any state and contact force.
    arm = TwoLinkArm()
    rng = random.Random(2)
    kp, kd, kfi, kv = gains(5.0)
    for _ in range(20):
        q = [rng.uniform(-2, 2), rng.uniform(0.3, 2.8)]
        qd = [rng.uniform(-2, 2) for _ in range(2)]
        xs, xds, xdds, fd, fm, fi, fe = ([rng.uniform(-1, 1) for _ in range(2)] for _ in range(7))
        tau = hybrid_control(arm, q, qd, xs, xds, xdds, fd, fm, fi, SF, (kp, kd, kfi, kv))
        qdd = arm.accel(q, qd, tau, fe)
        j = arm.jacobian(q)
        jq = arm.jdot_qdot(q, qd)
        xdd = [matvec2(j, qdd)[i] + jq[i] for i in range(2)]
        x, xd = arm.fk(q), matvec2(j, qd)
        ap = [xdds[i] + kd[i] * (xds[i] - xd[i]) + kp[i] * (xs[i] - x[i]) for i in range(2)]
        ff = [fd[i] + kfi[i] * fi[i] - kv[i] * xd[i] for i in range(2)]
        lam, _ = task_inertia(arm, q, qd)
        extra = matvec2(inv2(lam), [SF[i] * ff[i] + fe[i] for i in range(2)])
        for i in range(2):
            assert abs(xdd[i] - ((1 - SF[i]) * ap[i] + extra[i])) < 1e-9


def test_force_and_slide_with_exact_model():
    arm = TwoLinkArm()
    q0 = arm.ik([1.0, 0.5])
    out = simulate(arm, arm, wall(1.0, K_ENV), slide, F_DES, SF, gains(0.0), q0, 1e-3, 1500)
    for t, q, _, fa in out:
        err = arm.fk(q)[1] - slide(t)[0][1]
        # The y error is the critically damped response to the initial speed error 0.2.
        assert abs(err + 0.2 * t * math.exp(-20.0 * t)) < 1e-5
        if t >= 0.5:
            assert abs(fa[0] - F_DES[0]) < 1e-4
            assert abs(arm.fk(q)[0] - (1.0 + F_DES[0] / K_ENV)) < 1e-9


def test_integral_term_removes_force_offset_of_a_wrong_model():
    arm = TwoLinkArm()
    model = TwoLinkArm(m2=0.8)
    q0 = arm.ik([1.0, 0.5])

    def hold(t):
        return [0.0, 0.5], [0.0, 0.0], [0.0, 0.0]

    errs = []
    for kfi in (0.0, 20.0):
        out = simulate(arm, model, wall(1.0, K_ENV), hold, F_DES, SF, gains(kfi), q0, 2e-3, 1500)
        errs.append(abs(out[-1][3][0] - F_DES[0]))
    assert errs[0] > 0.05
    assert errs[1] < 1e-6

History

In 1981, Mason published a formalism of natural and artificial constraints for compliant motion, and Raibert and Craig published the hybrid position/force controller in the Journal of Dynamic Systems, Measurement, and Control. Their controller used a selection matrix to choose force or position control in each direction of a constraint frame. Later work moved the scheme to task space with the dynamic model, as in Khatib's operational space formulation, and generalized the selection matrix to projections onto the constraint and motion subspaces.

References

  1. M. H. Raibert and J. J. Craig, Hybrid position/force control of manipulators, Journal of Dynamic Systems, Measurement, and Control 103(2), 126-133, 1981, the whole paper
  2. Matthew T. Mason, Compliance and force control for computer controlled manipulators, IEEE Transactions on Systems, Man, and Cybernetics 11(6), 1981, pages 418 to 432
  3. Kevin M. Lynch and Frank C. Park, Modern Robotics, Mechanics, Planning, and Control, Cambridge University Press, 2017, Chapter 11, Section 11.5 (force control) and Section 11.6 (hybrid motion-force control)
  4. Bruno Siciliano, Lorenzo Sciavicco, Luigi Villani and Giuseppe Oriolo, Robotics, Modelling, Planning and Control, Springer, 2009, Chapter 9 (force control, natural and artificial constraints, hybrid force/motion control)
  5. 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)

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