# Module 4.5: Feedforward and Compliant Control

- Course: [Course 4: Feedback Control & Robot Dynamics](https://roboticsconcepts.dev/courses/feedback-control-robot-dynamics.md)
- HTML Page: https://roboticsconcepts.dev/courses/feedback-control-robot-dynamics/feedforward-and-compliant-control/
- Key Concepts: Gravity compensation feedforward, Computed torque control, Virtual spring-damper impedance control

If you ask a stiff position controller to wipe a whiteboard, you're asking for trouble. Misjudge the wall's distance by two millimeters, and a stiff controller treats the wall like an error to crush — slamming motor current to maximum until either the marker snaps or the wrist faults out. Humans don't wipe tables by locking their joints like concrete; we keep our arm springy and let the surface guide our hand.

## Feedforward: paying gravity before the error happens

The cleanest upgrade you can give any feedback controller is feedforward: instead of waiting for gravity to pull the arm down so PID can notice the error, you compute g(q) ahead of time and add it directly to the motor command.

Take that idea one step further and you get computed torque control: compute the full model τ_ff = M(q)q̈_des + C(q,q̇)q̇ + g(q) in feedforward, and let a gentle PD loop clean up whatever small friction or modeling errors remain. Because feedforward handles the heavy lifting, your feedback gains don't have to be brutally stiff anymore.

## Impedance control: programming a virtual spring and damper

Once gravity compensation holds the arm weightless in mid-air, what should happen if someone bumps the wrist or a table blocks the gripper? With impedance control, we stop commanding rigid positions and instead make the end-effector behave like a tunable virtual spring and shock absorber attached to the target:

- When the path is clear, the virtual spring pulls the hand cleanly to the target.
- When a rigid surface or human hand pushes in, the arm yields smoothly along the virtual spring instead of fighting at max torque.
- Tuning stiffness K sets how firm the touch feels; tuning damping B keeps rebounds smooth and calm.

## Interactive Sandbox: Interactive Impedance Control Sandbox

In the Impedance Control Tester below, start in 'Stiff Position Control' and slide the virtual wall leftward into the target — watch the contact force vector spike into the danger zone. Then switch to 'Compliant Impedance Control' and dial K and B to feel how the arm yields gracefully with gentle, bounded force.

## Choosing stiffness for the task

Compliance doesn't mean floppy everywhere. When carrying a heavy tray across a room, you might dial K up high along vertical Z so the tray stays level, while keeping K soft along X and Y so bumping a doorframe doesn't spill anything. That directional stiffness — plus gravity compensation underneath — is what turns a rigid industrial robot into a safe collaborator.

## Gravity-compensated impedance control

```text
τ = g(q) + J(q)ᵀ [ K (x_target − x) − B · ẋ ]
```

Gravity compensation g(q) cancels weight ahead of time, while the virtual spring K and damper B translate end-effector deflection into safe, bounded joint torques via Jᵀ.

## Check Your Intuition

**Question:** A robot is trying to insert a snug peg into a chamfered hole that might be offset by 3 mm. Why does compliant impedance control beat stiff high-gain PID here?

- [ ] Impedance control ignores position targets completely
- [✓] Lower lateral stiffness lets the hole's chamfer gently guide the peg in without building up huge jamming forces
- [ ] Impedance control disables all motor encoders during contact

**Explanation:** With moderate stiffness K, a 3 mm misalignment produces a small, safe lateral force instead of maxing out the actuators, allowing the physical chamfer geometry to slide the peg home.

## Where You'll See This in Modern Robotics

Virtually every modern contact-rich manipulation stack — from surgical arms to diffusion policies inserting USB plugs — runs an underlying impedance or operational-space controller with gravity compensation so learned trajectories stay safe during real physical contact.

## Companion Python Notebook: 05 · Gravity Compensation & Impedance Control

Combine feedforward gravity compensation with Cartesian virtual spring-damper impedance control against a stiff contact wall.

- Open in molab (WASM): https://molab.marimo.io/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/course-4/05_feedforward_impedance_control.py/wasm
- Open in Google Colab: https://colab.research.google.com/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/colab/course-4/05_feedforward_impedance_control.ipynb
- Download .py: https://roboticsconcepts.dev/downloads/notebooks/course-4/05_feedforward_impedance_control.py
- Download .ipynb: https://roboticsconcepts.dev/downloads/notebooks/colab/course-4/05_feedforward_impedance_control.ipynb
- Run locally: `uvx marimo edit --sandbox notebooks/course-4/05_feedforward_impedance_control.py`
