text: # AUTOGENERATED! DO NOT EDIT! File to edit: nbs/01_pid.ipynb (unless otherwise specified).
  
  __all__ = ['Pid']
  
  # Cell
  from .imports import *
  from .core import kwargs2str
  
  # Cell
  class Pid:
      def __init__(self, Kp=0, Ki=0, Kd=0, set_point=0, sample_time=0.01) -> None:
          self.Kp, self.Ki, self.Kd = Kp, Ki, Kd
          self.set_point = set_point
          self.sample_time = sample_time
          self._last_error, self._proportional, self._integral, self._derivative = 0, 0, 0, 0
          self._last_time, self._last_output, self._last_input = None, None, None
  
      def __call__(self, input):
          error = self.set_point - input
          current_time = time.time()
          dt = current_time - self._last_time if self._last_time is not None else 1e-16
          d_input = input - (self._last_input if (self._last_input is not None) else input)
          if self.sample_time is not None and dt < self.sample_time and self._last_output is not None: return self._last_output
          self._proportional = self.Kp * error
          self._integral += self.Ki * error * dt
          self._derivative = -self.Kd * d_input / dt
          output = self._proportional + self._integral + self._derivative
          self._last_output = output
          self._last_input = input
          self._last_time = current_time
          return output
  
      def __repr__(self):
          return f"Pid params:\n{kwargs2str(kp=self.Kp, ki=self.Ki, kd=self.Kd, set_point=self.set_point, sample_time=self.sample_time)}\n"