-
Notifications
You must be signed in to change notification settings - Fork 582
/
Copy pathHallSensor.cpp
257 lines (217 loc) · 7.22 KB
/
HallSensor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "HallSensor.h"
#include "./communication/SimpleFOCDebug.h"
// seq 1 > 5 > 4 > 6 > 2 > 3 > 1 000 001 010 011 100 101 110 111
const int8_t ELECTRIC_SECTORS[8] = { -1, 0, 4, 5, 2, 1, 3 , -1 };
/*
HallSensor(int hallA, int hallB , int cpr, int index)
- hallA, hallB, hallC - HallSensor A, B and C pins
- pp - pole pairs
*/
HallSensor::HallSensor(int _hallA, int _hallB, int _hallC, int _pp, HallType _hall_type){
// hardware pins
pinA = _hallA;
pinB = _hallB;
pinC = _hallC;
hall_type = _hall_type;
last_print_type = hall_type;
for (size_t i = 0; i < sizeof(previous_states); i++)
{
previous_states[i] = -1;
}
// hall has 6 segments per electrical revolution
cpr = _pp * 6;
// extern pullup as default
pullup = Pullup::USE_EXTERN;
}
// HallSensor interrupt callback functions
// A channel
void HallSensor::handleA() {
A_active= digitalRead(pinA);
updateState();
}
// B channel
void HallSensor::handleB() {
B_active = digitalRead(pinB);
updateState();
}
// C channel
void HallSensor::handleC() {
C_active = digitalRead(pinC);
updateState();
}
/**
* Updates the state and sector following an interrupt
*/
void HallSensor::updateState() {
int8_t new_hall_state = C_active + (B_active << 1) + (A_active << 2);
// glitch avoidance #1 - sometimes we get an interrupt but pins haven't changed
if (new_hall_state == hall_state_raw) return;
hall_state_raw = new_hall_state;
static const int num_previous_states = sizeof(previous_states);
//flip a line maybe
if (hall_type != HallType::UNKNOWN)
{
new_hall_state ^= static_cast<int8_t>(hall_type);
}
long new_pulse_timestamp = _micros();
if (hall_type == HallType::UNKNOWN) //Store previous steps for hall config detection
{
for (int i = num_previous_states - 2; i >= 0; i--)
{
previous_states[i+1] = previous_states[i];
}
previous_states[0] = new_hall_state;
//7 and 0 are illegal in 120deg mode, so we're gonna try to see which line hel up during that time and flip it so it doesn't happen
if ((previous_states[1] == 0b111 || previous_states[1] == 0b000) && previous_states[2] != -1)
{
if (previous_states[2] == previous_states[0])
{
//went back, can't do anything
}
else
{
hall_type = static_cast<HallType>((0b111 - previous_states[0] ^ previous_states[2])%8);
previous_states[0] ^= static_cast<int8_t>(hall_type);
}
}
if (abs(electric_rotations) > 2)
{
hall_type = HallType::HALL_120;
}
}
int8_t new_electric_sector;
new_electric_sector = ELECTRIC_SECTORS[new_hall_state];
int8_t electric_sector_dif = new_electric_sector - electric_sector;
if (electric_sector_dif > 3) {
//underflow
direction = Direction::CCW;
electric_rotations += direction;
} else if (electric_sector_dif < (-3)) {
//overflow
direction = Direction::CW;
electric_rotations += direction;
} else {
direction = (new_electric_sector > electric_sector)? Direction::CW : Direction::CCW;
}
electric_sector = new_electric_sector;
// glitch avoidance #2 changes in direction can cause velocity spikes. Possible improvements needed in this area
if (direction == old_direction) {
// not oscilating or just changed direction
pulse_diff = new_pulse_timestamp - pulse_timestamp;
} else {
pulse_diff = 0;
}
pulse_timestamp = new_pulse_timestamp;
total_interrupts++;
old_direction = direction;
if (onSectorChange != nullptr) onSectorChange(electric_sector);
}
/**
* Optionally set a function callback to be fired when sector changes
* void onSectorChange(int sector) {
* ... // for debug or call driver directly?
* }
* sensor.attachSectorCallback(onSectorChange);
*/
void HallSensor::attachSectorCallback(void (*_onSectorChange)(int sector)) {
onSectorChange = _onSectorChange;
}
// Sensor update function. Safely copy volatile interrupt variables into Sensor base class state variables.
void HallSensor::update() {
// Copy volatile variables in minimal-duration blocking section to ensure no interrupts are missed
if (use_interrupt){
noInterrupts();
}else{
A_active = digitalRead(pinA);
B_active = digitalRead(pinB);
C_active = digitalRead(pinC);
updateState();
}
angle_prev_ts = pulse_timestamp;
long last_electric_rotations = electric_rotations;
int8_t last_electric_sector = electric_sector;
if (use_interrupt) interrupts();
angle_prev = ((float)((last_electric_rotations * 6 + last_electric_sector) % cpr) / (float)cpr) * _2PI ;
full_rotations = (int32_t)((last_electric_rotations * 6 + last_electric_sector) / cpr);
if (last_print_type != hall_type)
{
last_print_type = hall_type;
switch (hall_type)
{
case HallType::HALL_120 :
SIMPLEFOC_DEBUG("HALL: Found type: HALL_120");
break;
case HallType::HALL_60A :
SIMPLEFOC_DEBUG("HALL: Found type: HALL_60A");
break;
case HallType::HALL_60B :
SIMPLEFOC_DEBUG("HALL: Found type: HALL_60B");
break;
case HallType::HALL_60C :
SIMPLEFOC_DEBUG("HALL: Found type: HALL_60C");
break;
default:
SIMPLEFOC_DEBUG("HALL: Type unknown! Wtf!");
break;
}
}
}
/*
Shaft angle calculation
TODO: numerical precision issue here if the electrical rotation overflows the angle will be lost
*/
float HallSensor::getSensorAngle() {
return ((float)(electric_rotations * 6 + electric_sector) / (float)cpr) * _2PI ;
}
/*
Shaft velocity calculation
function using mixed time and frequency measurement technique
*/
float HallSensor::getVelocity(){
noInterrupts();
long last_pulse_timestamp = pulse_timestamp;
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
return 0;
} else {
return direction * (_2PI / (float)cpr) / (last_pulse_diff / 1000000.0f);
}
}
int HallSensor::needsSearch()
{
return hall_type == HallType::UNKNOWN;
}
// HallSensor initialisation of the hardware pins
// and calculation variables
void HallSensor::init(){
// initialise the electrical rotations to 0
electric_rotations = 0;
// HallSensor - check if pullup needed for your HallSensor
if(pullup == Pullup::USE_INTERN){
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
pinMode(pinC, INPUT_PULLUP);
}else{
pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
pinMode(pinC, INPUT);
}
// init hall_state
A_active = digitalRead(pinA);
B_active = digitalRead(pinB);
C_active = digitalRead(pinC);
updateState();
pulse_timestamp = _micros();
// we don't call Sensor::init() here because init is handled in HallSensor class.
}
// function enabling hardware interrupts for the callback provided
// if callback is not provided then the interrupt is not enabled
void HallSensor::enableInterrupts(void (*doA)(), void(*doB)(), void(*doC)()){
// attach interrupt if functions provided
// A, B and C callback
if(doA != nullptr) attachInterrupt(digitalPinToInterrupt(pinA), doA, CHANGE);
if(doB != nullptr) attachInterrupt(digitalPinToInterrupt(pinB), doB, CHANGE);
if(doC != nullptr) attachInterrupt(digitalPinToInterrupt(pinC), doC, CHANGE);
use_interrupt = true;
}