Arduino-FOC
Arduino-FOC copied to clipboard
[BUG] LowPassFilter::operator() returns nan
Describe the bug LowPassFilter::operator() returns nan when called with zero dt and zero Tf.
I have a multi-threaded setup running where I asynchronously call FOCMotor::shaftAngle() from different threads. Occasionally my setup breaks down because the LPF returns nan. That happens because the LPF is initialized with zero Tf in the FOCMotor class and occasionally dt is also zero which then breaks the formula in LowPassFilter::operator():
float alpha = Tf/(Tf + dt);
I'm actually surprised that nobody else had this problem yet, so I'm wondering if I am doing sth wrong.
hardware setup
- Motor: GB4106
- Driver: DRV8313
- MCU: ESP32
- Sensor: AS5048
- No current sensing
IDE
- Platformio
Suggested Solution Catch the zero case by turning
if (dt < 0.0f ) dt = 1e-3f;
into
if (dt <= 0.0f ) dt = 1e-3f;
But I think that 1e-3f is actually a little too large and would suggest to go with 1e-4f or 1e-5f