braincore / pid-rs

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

Abstracted the calculation of `signed_error` into a `SignedError` trait to alternative implementations #21

Closed ripytide closed 8 months ago

ripytide commented 1 year ago

Quoting added docs in the PR:

A trait to generalise the notion of signed_error between two measurements. For example, between displacement measurements of -10 and 20 there is a signed_error of -30 which is calculated as (-10) - (20). However this subtraction does not work for other measurements, take for example rotation measurements represented in radians, imagine the signed_error between a measurement of 2*PI and 4*PI. Using the subtraction method would give us an signed_error of -2*PI which may not be expected if we consider 2*PI and 4*PI to be equivalent and hence expect a signed_error of 0. Hence, this trait allows you to implement your own method of calculating the signed_error between two measurements. A implementation of the regular subtraction method which is commonly most appropriate is provided via [RegularSignedError].

Feel free to ignore the PR, I offer the changes in case others feel they are useful.

ripytide commented 8 months ago

Actually, I think you can just make a newtype and implement core::ops::Sub on it with the required SignedError behaviour rather than making a new trait.