DEV Community

vmodal_ai
vmodal_ai

Posted on

PID Control for Robot Motors and Actuators

PID Control for Robot Motors and Actuators

PID control remains one of the most useful techniques for robot motor and actuator control. It is simple, computationally efficient, and effective when properly tuned.

A PID controller uses the difference between a desired target and the measured state to calculate a control command.

PID Equation

The classic controller is:

u(t) = Kp e(t)
     + Ki ∫e(t)dt
     + Kd de(t)/dt
Enter fullscreen mode Exit fullscreen mode

Where:

  • Kp controls the proportional response
  • Ki removes steady-state error
  • Kd responds to changes in error
  • e(t) is target minus measured value

Robot Control Loop

Desired Position
       ↓
     Error
       ↓
   PID Controller
       ↓
 Motor Command
       ↓
    Motor
       ↓
 Encoder
       ↓
 Measured Position
       └────────────→ feedback
Enter fullscreen mode Exit fullscreen mode

1. Start with Position Control

Suppose a joint should move to:

target = 1.5 rad
Enter fullscreen mode Exit fullscreen mode

The encoder reports:

position = 1.2 rad
Enter fullscreen mode Exit fullscreen mode

Then:

error = 1.5 - 1.2
      = 0.3 rad
Enter fullscreen mode Exit fullscreen mode

The controller uses this error to calculate an actuator command.

2. Implement a Simple PID Controller

double update(
    double target,
    double measurement,
    double dt)
{
    double error = target - measurement;

    integral += error * dt;

    double derivative =
        (error - previous_error) / dt;

    double output =
        kp * error +
        ki * integral +
        kd * derivative;

    previous_error = error;

    return output;
}
Enter fullscreen mode Exit fullscreen mode

In production, protect the implementation against invalid dt, numerical problems, and actuator saturation.

3. Add Output Limits

Motors have physical limits.

output = std::clamp(
    output,
    -MAX_COMMAND,
    MAX_COMMAND
);
Enter fullscreen mode Exit fullscreen mode

Without limits, a controller can request more torque, velocity, or voltage than the actuator can safely provide.

4. Prevent Integral Windup

Suppose the motor reaches its maximum output while the error remains large.

The integral term can continue growing:

Large Error
   ↓
Integral grows
   ↓
Output saturated
   ↓
Integral keeps growing
Enter fullscreen mode Exit fullscreen mode

When the target becomes reachable, the stored integral can cause a large overshoot.

Use anti-windup techniques such as integral clamping or conditional integration.

5. Tune Kp, Ki, and Kd

A practical starting process is:

Ki = 0
Kd = 0
     ↓
Increase Kp
     ↓
Observe response
     ↓
Add Kd if oscillation is excessive
     ↓
Add Ki for persistent steady-state error
Enter fullscreen mode Exit fullscreen mode

Do not tune on a robot with unrestricted motion. Use safe limits and an emergency stop.

6. Position vs Velocity PID

Different control loops can use different feedback variables.

Position PID
Target position → measured position

Velocity PID
Target velocity → measured velocity

Torque / Effort Control
Target effort → measured/estimated effort
Enter fullscreen mode Exit fullscreen mode

Many robot systems use cascaded loops:

Position Controller
       ↓
Velocity Target
       ↓
Velocity Controller
       ↓
Motor Command
Enter fullscreen mode Exit fullscreen mode

7. PID with ros2_control

ros2_control includes a PID controller among its controller ecosystem. Controllers consume hardware state interfaces and write to command interfaces. ros2_controllers

A conceptual architecture is:

Joint Encoder
     ↓
State Interface
     ↓
PID Controller
     ↓
Command Interface
     ↓
Motor Driver
Enter fullscreen mode Exit fullscreen mode

8. Handle Derivative Noise

Encoder measurements can contain noise.

The derivative term amplifies rapid changes:

Noise
  ↓
Derivative
  ↓
Large command fluctuations
Enter fullscreen mode Exit fullscreen mode

Use appropriate filtering and sampling strategies rather than blindly increasing Kd.

9. Sample at a Predictable Rate

PID behavior depends on dt.

For example:

100 Hz → dt = 0.01 s
Enter fullscreen mode Exit fullscreen mode

If the update interval varies significantly, controller behavior can change.

Keep the control loop deterministic and measure actual timing.

10. Add Safety Constraints

A production motor controller should include:

  • Position limits
  • Velocity limits
  • Effort limits
  • Temperature limits
  • Communication watchdogs
  • Emergency stop
  • Fault states

PID is a control algorithm, not a safety system.

Conclusion

PID control is still highly relevant in modern robotics because it provides predictable low-level behavior and is computationally inexpensive.

The best results come from combining careful tuning, stable timing, anti-windup, filtering, physical limits, and a robust hardware interface.

Useful Links

Website: www.v-modal.com
SDK Flutter: https://github.com/v-modal/vmodal_sdk_flutter
SDK Android: https://github.com/v-modal/vmodal_sdk_android
Discord: https://discord.gg/K72z28KUx

Top comments (0)