Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/sensors/HallSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,19 @@ float HallSensor::getSensorAngle() {
*/
float HallSensor::getVelocity(){
noInterrupts();
long last_pulse_timestamp = pulse_timestamp;
long last_pulse_diff = pulse_diff;
unsigned long last_pulse_timestamp = pulse_timestamp;
unsigned long last_pulse_diff = pulse_diff;
interrupts();
if (last_pulse_diff == 0 || ((long)(_micros() - last_pulse_timestamp) > last_pulse_diff*2) ) { // last velocity isn't accurate if too old
if (last_pulse_diff == 0) {
return 0;
} else if ((_micros() - last_pulse_timestamp) > last_pulse_diff*2) {
// Last velocity isn't accurate if too old.
// Reset to avoid the diff being used again when the time wraps.
noInterrupts();
if (pulse_diff == last_pulse_diff) {
pulse_diff = 0;
}
interrupts();
return 0;
} else {
return direction * (_2PI / (float)cpr) / (last_pulse_diff / 1000000.0f);
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/HallSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class HallSensor: public Sensor{
// function pointer for on sector change call back
void (*onSectorChange)(int sector) = nullptr;

volatile long pulse_diff;
volatile unsigned long pulse_diff;

};

Expand Down
Loading