braincore / pid-rs

A PID controller for Rust projects.
Apache License 2.0
92 stars 26 forks source link

Derivative kick: why don't you divide by the period? #11

Closed g-berthiaume closed 1 year ago

g-berthiaume commented 2 years ago

In the library, the derivative term is computed that way in order to mitigate the derivative kick:

        // Mitigate derivative kick: Use the derivative of the measurement
        // rather than the derivative of the error.
        let d_unbounded = -match self.prev_measurement.as_ref() {
            Some(prev_measurement) => measurement - *prev_measurement,
            None => T::zero(),
        } * self.kd;
        self.prev_measurement = Some(measurement);
        let d = apply_limit(self.d_limit, d_unbounded);

Shouldn't you also divide by the period?


I'm new to control, so I could be wrong. ^_^ Here's my math to explain where this question is coming from. image

ua-kxie commented 2 years ago

Currently the lib assumes constant timesteps:

Assumptions Measurements occur at equal spacing. (t(i) = t(i-1) + C) Output limits per term are symmetric around 0 (-limit <= term <= limit).

so the time delta is just some constant which the code currently assumes to be 1.

This is an assumption I'd like to see get dropped

braincore commented 1 year ago

Thanks for responding @ua-kxie with the correct answer.