Skip to content
Tactiverge

□ Control systems · feedback loops

PID step response

Simulate a PID controller driving a first-order system and see why integral action removes steady-state error.

  • Level 4 · Systems Engineer
  • 6 tests
  • technically checked

Your task

A heater, a motor and a drone's height all behave roughly like a first-order system: dy/dt = (u − y) / τ, where u is the controller's output.

A PID controller computes u from the error e = setpoint − y: u = kp·e + ki·∫e dt + kd·de/dt

Write pidStep(kp, ki, kd, setpoint, tau, dt, steps) returning steps + 1 outputs, starting at y = 0. Each step, in this order:

  1. e = setpoint − y
  2. integral += e × dt
  3. derivative = (e − prevError) / dt, where prevError starts as the first error (no 'kick'), then prevError = e
  4. u = kp·e + ki·integral + kd·derivative
  5. y = y + dt × (u − y) / τ, and push y

Also write finalOutput(kp, ki, kd): the last value of pidStep(kp, ki, kd, 1, 1, 0.05, 400).

Hints (0 of 3 shown)

    What gets checked

    • pidStep(2, 0, 0, 1, 1, 0.1, 3) → [0, 0.2, 0.34, 0.438]
    • finalOutput(2, 0, 0) → ≈ 0.6667 (± 0.001)

    Plus 4 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.

    Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments

    Starting the sandbox…

    Press Run to see what your program prints.

    Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.