Skip to content

Commit 64937c0

Browse files
committed
Merge branch 'dev' of github.com:askuric/Arduino-FOC into dev
2 parents be49818 + 639f577 commit 64937c0

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/sensors/HallSensor.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void HallSensor::updateState() {
4545
long new_pulse_timestamp = _micros();
4646
hall_state = C_active + (B_active << 1) + (A_active << 2);
4747
int8_t new_electric_sector = ELECTRIC_SECTORS[hall_state];
48+
static Direction old_direction;
4849
if (new_electric_sector - electric_sector > 3) {
4950
//underflow
5051
direction = static_cast<Direction>(natural_direction * -1);
@@ -57,9 +58,16 @@ void HallSensor::updateState() {
5758
direction = (new_electric_sector > electric_sector)? static_cast<Direction>(natural_direction) : static_cast<Direction>(natural_direction * (-1));
5859
}
5960
electric_sector = new_electric_sector;
60-
pulse_diff = new_pulse_timestamp - pulse_timestamp;
61+
if (direction == old_direction) {
62+
// not oscilating or just changed direction
63+
pulse_diff = new_pulse_timestamp - pulse_timestamp;
64+
} else {
65+
pulse_diff = 0;
66+
}
67+
6168
pulse_timestamp = new_pulse_timestamp;
6269
total_interrupts++;
70+
old_direction = direction;
6371
if (onSectorChange != nullptr) onSectorChange(electric_sector);
6472
}
6573

@@ -86,8 +94,7 @@ float HallSensor::getAngle() {
8694
function using mixed time and frequency measurement technique
8795
*/
8896
float HallSensor::getVelocity(){
89-
90-
if (pulse_diff == 0 || ((_micros() - pulse_timestamp) > STALE_HALL_DATA_MICROS) ) { // last velocity isn't accurate if too old
97+
if (pulse_diff == 0 || ((_micros() - pulse_timestamp) > pulse_diff) ) { // last velocity isn't accurate if too old
9198
return 0;
9299
} else {
93100
return direction * (_2PI / cpr) / (pulse_diff / 1000000.0);

src/sensors/HallSensor.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
#include "../common/foc_utils.h"
77
#include "../common/time_utils.h"
88

9-
#ifndef STALE_HALL_DATA_MICROS
10-
#define STALE_HALL_DATA_MICROS 500000
11-
#endif
12-
139
// seq 1 > 5 > 4 > 6 > 2 > 3 > 1 000 001 010 011 100 101 110 111
1410
const int8_t ELECTRIC_SECTORS[8] = { -1, 0, 4, 5, 2, 1, 3 , -1 };
1511

0 commit comments

Comments
 (0)