text: import numpy as np
  from numbers import Number
  
  from common.numpy_fast import clip, interp
  from common.op_params import opParams
  from selfdrive.config import Conversions as CV
  
  
  def apply_deadzone(error, deadzone):
    if error > deadzone:
      error -= deadzone
    elif error < - deadzone:
      error += deadzone
    else:
      error = 0.
    return error
  
  class PIDController():
    def __init__(self, k_p, k_i, k_d, k_f=1., pos_limit=None, neg_limit=None, rate=100, derivative_period=1.):
      self.op_params = opParams()
      self._k_p = k_p  # proportional gain
      self._k_i = k_i  # integral gain
      self._k_d = k_d  # derivative gain
      self.k_f = k_f   # feedforward gain
      if isinstance(self._k_p, Number):
        self._k_p = [[0], [self._k_p]]
      if isinstance(self._k_i, Number):
        self._k_i = [[0], [self._k_i]]
      if isinstance(self._k_d, Number):
        self._k_d = [[0], [self._k_d]]
  
      self.pos_limit = pos_limit
      self.neg_limit = neg_limit
  
      self.i_unwind_rate = 0.3 / rate
      self.i_rate = 1.0 / rate
      self._d_period = round(derivative_period * rate)  # period of time for derivative calculation (seconds converted to frames)
  
      self.reset()
  
    @property
    def k_p(self):
      return interp(self.speed, self._k_p[0], self._k_p[1])
  
    @property
    def k_i(self):
      return interp(self.speed, self._k_i[0], self._k_i[1])
  
    @property
    def k_d(self):
      return interp(self.speed, self._k_d[0], self._k_d[1])
  
    def reset(self):
      self.p = 0.0
      self.i = 0.0
      self.f = 0.0
      self.control = 0
      self.errors = []
  
    def update(self, setpoint, measurement, speed=0.0, override=False, feedforward=0., deadzone=0., freeze_integrator=False):
      self.speed = speed
  
      error = float(apply_deadzone(setpoint - measurement, deadzone))
      self.p = error * self.k_p
      self.f = feedforward * self.k_f
  
      d = 0
      if len(self.errors) >= self._d_period:  # makes sure we have enough history for period
        d = (error - self.errors[-self._d_period]) / self._d_period  # get deriv in terms of 100hz (tune scale doesn't change)
        d *= self.k_d
  
      if override:
        self.i -= self.i_unwind_rate * float(np.sign(self.i))
      else:
        i = self.i + error * self.k_i * self.i_rate
        control = self.p + self.f + i + d
  
        # Update when changing i will move the control away from the limits
        # or when i will move towards the sign of the error
        if ((error >= 0 and (control <= self.pos_limit or i < 0.0)) or
            (error <= 0 and (control >= self.neg_limit or i > 0.0))) and \
           not freeze_integrator:
          self.i = i
  
      control = self.p + self.f + self.i + d
  
      self.errors.append(float(error))
      while len(self.errors) > self._d_period:
        self.errors.pop(0)
  
      self.control = clip(control, self.neg_limit, self.pos_limit)
      return self.control
  