diff --git a/src/sensors/HallSensor.cpp b/src/sensors/HallSensor.cpp index 6c2367ff..7d1d9f64 100644 --- a/src/sensors/HallSensor.cpp +++ b/src/sensors/HallSensor.cpp @@ -113,10 +113,11 @@ float HallSensor::getMechanicalAngle() { function using mixed time and frequency measurement technique */ float HallSensor::getVelocity(){ - if (pulse_diff == 0 || ((long)(_micros() - pulse_timestamp) > pulse_diff) ) { // last velocity isn't accurate if too old + long last_pulse_diff = pulse_diff; + if (last_pulse_diff == 0 || ((long)(_micros() - pulse_timestamp) > last_pulse_diff) ) { // last velocity isn't accurate if too old return 0; } else { - float vel = direction * (_2PI / (float)cpr) / (pulse_diff / 1000000.0f); + float vel = direction * (_2PI / (float)cpr) / (last_pulse_diff / 1000000.0f); // quick fix https://github.com/simplefoc/Arduino-FOC/issues/192 if(vel < -velocity_max || vel > velocity_max) vel = 0.0f; //if velocity is out of range then make it zero return vel;