-
Notifications
You must be signed in to change notification settings - Fork 719
Expand file tree
/
Copy pathEncoder.cpp
More file actions
241 lines (210 loc) · 6.5 KB
/
Copy pathEncoder.cpp
File metadata and controls
241 lines (210 loc) · 6.5 KB
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
#include "Encoder.h"
/*
Encoder(int encA, int encB , int cpr, int index)
- encA, encB - encoder A and B pins
- cpr - counts per rotation number (cpm=ppm*4)
- index pin - (optional input)
*/
Encoder::Encoder(int _encA, int _encB , float _ppr, int _index){
// Encoder measurement structure init
// hardware pins
pinA = _encA;
pinB = _encB;
// counter setup
pulse_counter = 0;
pulse_timestamp = 0;
cpr = _ppr;
#ifdef INTEGER_ANGLE
steps_per_revolution = cpr;
#endif
A_active = 0;
B_active = 0;
I_active = 0;
// index pin
index_pin = _index; // its 0 if not used
// velocity calculation variables
prev_Th = 0;
pulse_per_second = 0;
prev_pulse_counter = 0;
prev_timestamp_us = _micros();
// extern pullup as default
pullup = Pullup::USE_EXTERN;
// enable quadrature encoder by default
quadrature = Quadrature::ON;
}
// Encoder interrupt callback functions
// A channel
void Encoder::handleA() {
bool A = digitalRead(pinA);
switch (quadrature){
case Quadrature::ON:
// CPR = 4xPPR
if ( A != A_active ) {
pulse_counter += (A_active == B_active) ? 1 : -1;
pulse_timestamp = _micros();
A_active = A;
}
break;
case Quadrature::OFF:
// CPR = PPR
if(A && !digitalRead(pinB)){
pulse_counter++;
pulse_timestamp = _micros();
}
break;
}
}
// B channel
void Encoder::handleB() {
bool B = digitalRead(pinB);
switch (quadrature){
case Quadrature::ON:
// // CPR = 4xPPR
if ( B != B_active ) {
pulse_counter += (A_active != B_active) ? 1 : -1;
pulse_timestamp = _micros();
B_active = B;
}
break;
case Quadrature::OFF:
// CPR = PPR
if(B && !digitalRead(pinA)){
pulse_counter--;
pulse_timestamp = _micros();
}
break;
}
}
// Index channel
void Encoder::handleIndex() {
if(hasIndex()){
bool I = digitalRead(index_pin);
if(I && !I_active){
index_found = true;
// align encoder on each index
long tmp = pulse_counter;
// corrent the counter value
pulse_counter = round((double)pulse_counter/(double)cpr)*cpr;
// preserve relative speed
prev_pulse_counter += pulse_counter - tmp;
}
I_active = I;
}
}
// Sensor update function. Safely copy volatile interrupt variables into Sensor base class state variables.
void Encoder::update() {
// Copy volatile variables in minimal-duration blocking section to ensure no interrupts are missed
noInterrupts();
angle_prev_ts = pulse_timestamp;
long copy_pulse_counter = pulse_counter;
interrupts();
#ifdef INTEGER_ANGLE
setAngleContinuous(copy_pulse_counter%steps_per_revolution);
#else
// TODO: numerical precision issue here if the pulse_counter overflows the angle will be lost
full_rotations = copy_pulse_counter / (int)cpr;
angle_prev = _2PI * ((copy_pulse_counter) % ((int)cpr)) / ((float)cpr);
#endif
}
/*
Shaft angle calculation
*/
angle_type Encoder::getSensorAngle(){
#ifdef INTEGER_ANGLE
return angle_prev % steps_per_revolution;
#else
return _2PI * (pulse_counter) / ((float)cpr);
#endif
}
/*
Shaft velocity calculation
function using mixed time and frequency measurement technique
*/
float Encoder::getVelocity(){
// Copy volatile variables in minimal-duration blocking section to ensure no interrupts are missed
noInterrupts();
long copy_pulse_counter = pulse_counter;
long copy_pulse_timestamp = pulse_timestamp;
interrupts();
// timestamp
long timestamp_us = _micros();
// sampling time calculation
float Ts = (timestamp_us - prev_timestamp_us) * 1e-6f;
// quick fix for strange cases (micros overflow)
if(Ts <= 0 || Ts > 0.5f) Ts = 1e-3f;
// time from last impulse
float Th = (timestamp_us - copy_pulse_timestamp) * 1e-6f;
long dN = copy_pulse_counter - prev_pulse_counter;
// Pulse per second calculation (Eq.3.)
// dN - impulses received
// Ts - sampling time - time in between function calls
// Th - time from last impulse
// Th_1 - time form last impulse of the previous call
// only increment if some impulses received
float dt = Ts + prev_Th - Th;
pulse_per_second = (dN != 0 && dt > Ts/2) ? dN / dt : pulse_per_second;
// if more than 0.05f passed in between impulses
if ( Th > 0.1f) pulse_per_second = 0;
// velocity calculation
float velocity = pulse_per_second / ((float)cpr) * (_2PI);
// save variables for next pass
prev_timestamp_us = timestamp_us;
// save velocity calculation variables
prev_Th = Th;
prev_pulse_counter = copy_pulse_counter;
return velocity;
}
// getter for index pin
// return -1 if no index
int Encoder::needsSearch(){
return hasIndex() && !index_found;
}
// private function used to determine if encoder has index
int Encoder::hasIndex(){
return index_pin != 0;
}
// encoder initialisation of the hardware pins
// and calculation variables
void Encoder::init(){
// Encoder - check if pullup needed for your encoder
if(pullup == Pullup::USE_INTERN){
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
if(hasIndex()) pinMode(index_pin,INPUT_PULLUP);
}else{
pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
if(hasIndex()) pinMode(index_pin,INPUT);
}
// counter setup
pulse_counter = 0;
pulse_timestamp = _micros();
// velocity calculation variables
prev_Th = 0;
pulse_per_second = 0;
prev_pulse_counter = 0;
prev_timestamp_us = _micros();
// initial cpr = PPR
// change it if the mode is quadrature
if(quadrature == Quadrature::ON) cpr = 4*cpr;
// we don't call Sensor::init() here because init is handled in Encoder class.
}
// function enabling hardware interrupts of the for the callback provided
// if callback is not provided then the interrupt is not enabled
void Encoder::enableInterrupts(void (*doA)(), void(*doB)(), void(*doIndex)()){
// attach interrupt if functions provided
switch(quadrature){
case Quadrature::ON:
// A callback and B callback
if(doA != nullptr) attachInterrupt(digitalPinToInterrupt(pinA), doA, CHANGE);
if(doB != nullptr) attachInterrupt(digitalPinToInterrupt(pinB), doB, CHANGE);
break;
case Quadrature::OFF:
// A callback and B callback
if(doA != nullptr) attachInterrupt(digitalPinToInterrupt(pinA), doA, RISING);
if(doB != nullptr) attachInterrupt(digitalPinToInterrupt(pinB), doB, RISING);
break;
}
// if index used initialize the index interrupt
if(hasIndex() && doIndex != nullptr) attachInterrupt(digitalPinToInterrupt(index_pin), doIndex, CHANGE);
}