Ben-PH / SimpleFOC-rs

An attempt at re-writing the SimpleFOC library in rust.
MIT License
12 stars 0 forks source link

Lowpass filter commentary #3

Open Copper280z opened 4 months ago

Copper280z commented 4 months ago

https://github.com/Ben-PH/SimpleFOC-rs/blob/47ea9ed03db2f3749ca7d6a038bf721271c66459/src/common/lp_filter.rs#L21

sfoc default works pretty well here, if negative, set to 1ms because something weird happened, else if it's been a long time (300ms) don't filter just return. I don't see the fast path return being of particular value, though.

An additional feature that is potentially very useful it to accommodate fixed timestep updates, as in either precalculate alpha (maybe in a setter function?) and just run the update assuming it's being called at the stated timestep, or accept dt as a function argument. This saves cycles if FOC is run from a timer interrupt, it's especially valuable for slower chips that don't have hardware floating point support, software floating point divisions take forever.

Partial example here, I didn't precalculate alpha in order to limit behavior changes but accept dt as an argument with a function overload. https://github.com/Copper280z/Arduino-FOC/blob/dual_motor/src/common/lowpass_filter.cpp I usually use this macro instead of the LPF class for things where I'm ok with alpha being a compile time constant.

#define LOWPASS( output, input, c_lowpass)  (output += (c_lowpass) * ((input) - (output)))

Finally, maybe a documentation suggestion more than anything, but the cutoff frequency of this filter is given by Fc = 1/(_2PI*time_constant), I think lots of people assume it's Fc = 1/time_constant.