discrete-pid-controller
v0.3.1
Published
A discrete-time PID controller with anti-windup, derivative filtering, and output clamping.
Maintainers
Readme
discrete-pid-controller
A small, no-dependency PID controller for discrete control loops in JavaScript. It is the positional form of PID with the practical extras you need in the field:
- output clamping (
outputMin/outputMax) - conditional-integration anti-windup
- optional first-order derivative filtering (
tau) - derivative-on-measurement to suppress setpoint "kick"
- variable time step — pass
dton everyupdate()
Install
npm install discrete-pid-controllerQuick start
import { PIDController } from "discrete-pid-controller";
const pid = new PIDController({
kp: 1.2,
ki: 0.8,
kd: 0.05,
setpoint: 5,
outputMin: -10,
outputMax: 10,
tau: 0.02, // derivative filter time constant, seconds
});
// In your control loop:
let measurement = readSensor();
const dt = 0.01; // seconds since last update
const command = pid.update(measurement, dt);
applyActuator(command);Options
| Option | Default | Meaning |
| --- | --- | --- |
| kp, ki, kd | 1, 0, 0 | gains (ki per second, kd in seconds) |
| setpoint | 0 | target value |
| outputMin / outputMax | ±Infinity | actuator limits |
| tau | 0 | derivative low-pass time constant; 0 disables |
| derivativeOnMeasurement | true | differentiate the measurement, not the error |
reset() clears the integral and derivative history; setSetpoint(sp) retargets
without disturbing accumulated state.
A note on anti-windup
When the output saturates, integration is inhibited only while continued integration would push the output further past the limit. This "clamping" scheme keeps the integrator from charging up during saturation, so the loop recovers immediately once the error changes sign instead of overshooting.
License
Apache-2.0
