PID Controller Implementation

Hard C Control
Solve this problem

Implement a PID controller in C that computes the control output using proportional, integral, and derivative terms. The function takes setpoint, measurement, and dt.

Constraints

  • Use float type for computation.
  • Function prototype: float pid(float sp, float pv, float dt).
  • Global Kp, Ki, Kd constants must exist.
  • Integral must accumulate across calls.
  • Derivative uses previous error.
  • Clamp output to [-100, 100].
Starter codeC
1#include <stdio.h>2 3extern float Kp, Ki, Kd;4float pid(float sp, float pv, float dt);