m-lundberg / simple-pid

A simple and easy to use PID controller in Python
MIT License
792 stars 216 forks source link

adding a filter to derivative term #44

Open CamDavidsonPilon opened 3 years ago

CamDavidsonPilon commented 3 years ago

👋 what are your thoughts on allowing users to add a hook into the class to add a filter infront of the derivative term. I've read in a few places that adding a filter infront of the derivative term is a good idea (i.e. instead of passing the raw input_ to d_input, we pass in some filtered version).

High level:

def my_filter(input_, previous_input_):
   # compute things    
   return some_float

pid = PID(...)
pid.add_derivative_hook(my_filter)

low level:


   def _compute_derivative(self, input, last_input):
       return input - (last_input if (last_input is not None) else input)

   def add_derivative_hook(self, callable):
       self._compute_derivative = callable

   def __call__(...):
        ...
        # Compute error terms
        error = self.setpoint - input_
        d_input = self._compute_derivative(input, self._last_input)
       ...
yrik commented 4 months ago

Would love this feature as well, ideally with some built in filter