Skip to content
Open
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
21 changes: 19 additions & 2 deletions src/sensors/HallSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ HallSensor::HallSensor(int _hallA, int _hallB, int _hallC, int _pp){

// extern pullup as default
pullup = Pullup::USE_EXTERN;

// initialise the state variables - they would otherwise be indeterminate
// for sensors that do not live in the BSS section (heap/stack instances)
use_interrupt = false;
hall_state = 0;
electric_sector = 0;
electric_rotations = 0;
total_interrupts = 0;
pulse_diff = 0;
pulse_timestamp = _micros();
A_active = B_active = C_active = 0;
direction = Direction::UNKNOWN;
old_direction = Direction::UNKNOWN;
}

// HallSensor interrupt callback functions
Expand Down Expand Up @@ -156,11 +169,15 @@ void HallSensor::init(){
pinMode(pinC, INPUT);
}

// init hall_state
// adopt the current hall state as the starting point instead of calling
// updateState(): that would compare the measured sector with the zeroed
// one and count a spurious +-1 electric rotation for 2 of the 6 power-up
// positions, offsetting the angle and the full-rotation counter
A_active = digitalRead(pinA);
B_active = digitalRead(pinB);
C_active = digitalRead(pinC);
updateState();
hall_state = C_active + (B_active << 1) + (A_active << 2);
electric_sector = ELECTRIC_SECTORS[hall_state];

pulse_timestamp = _micros();

Expand Down