-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadratureEncoder.cpp
228 lines (146 loc) · 5.19 KB
/
QuadratureEncoder.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
#include "QuadratureEncoder.h"
volatile float time_now_array[4] = {0,0,0,0};
volatile float last_time_array[4] = {0,0,0,0};
volatile bool direction_array[4] = {true,true,true,true};
volatile bool new_speed_array[4] = {false,false,false,false};
volatile int64_t increment_array[4] = {0,0,0,0};
volatile float old_time_now_array[4] = {0,0,0,0};
volatile float old_last_time_array[4] = {0,0,0,0};
volatile bool old_direction_array[4] = {true,true,true,true};
void measurePulse(int encoder){
last_time_array[encoder] = time_now_array[encoder];
time_now_array[encoder] = micros();
}
void measureSign(int encoder){
if((micros()-time_now_array[encoder]) < (time_now_array[encoder]-last_time_array[encoder])/3.0)
direction_array[encoder] = true;
else
direction_array[encoder] = false;
old_last_time_array[encoder] = last_time_array[encoder];
old_time_now_array[encoder] = time_now_array[encoder];
old_direction_array[encoder] = direction_array[encoder];
new_speed_array[encoder] = true;
if(direction_array[encoder] == true)
increment_array[encoder]++;
else
increment_array[encoder]--;
}
// ------------- Encoder 1 ------------- //
void hall_A_ISR1()
{
measurePulse(ENCODER_1);
}
void hall_B_ISR1()
{
measureSign(ENCODER_1);
}
// ------------- Encoder 2 ------------- //
void hall_A_ISR2()
{
measurePulse(ENCODER_2);
}
void hall_B_ISR2()
{
measureSign(ENCODER_2);
}
// ------------- Encoder 3 ------------- //
void hall_A_ISR3()
{
measurePulse(ENCODER_3);
}
void hall_B_ISR3()
{
measureSign(ENCODER_3);
}
// ------------- Encoder 4 ------------- //
void hall_A_ISR4()
{
measurePulse(ENCODER_4);
}
void hall_B_ISR4()
{
measureSign(ENCODER_4);
}
// --------------------- Quadrature Encoder Class ------------------- //
QuadratureEncoder::QuadratureEncoder(int encoder, int encoder_pin_a, int encoder_pin_b, int pulses_per_turn, float wheel_radius){
pinMode(encoder_pin_a, INPUT);
pinMode(encoder_pin_b, INPUT);
this->encoder = encoder;
this->pulses_per_turn = pulses_per_turn;
this->wheel_radius = wheel_radius;
if (encoder == ENCODER_1){
attachInterrupt(digitalPinToInterrupt(encoder_pin_a), hall_A_ISR1, RISING);
attachInterrupt(digitalPinToInterrupt(encoder_pin_b), hall_B_ISR1, RISING);
}
if (encoder == ENCODER_2){
attachInterrupt(digitalPinToInterrupt(encoder_pin_a), hall_A_ISR2, RISING);
attachInterrupt(digitalPinToInterrupt(encoder_pin_b), hall_B_ISR2, RISING);
}
if (encoder == ENCODER_3){
attachInterrupt(digitalPinToInterrupt(encoder_pin_a), hall_A_ISR3, RISING);
attachInterrupt(digitalPinToInterrupt(encoder_pin_b), hall_B_ISR3, RISING);
}
if (encoder == ENCODER_4){
attachInterrupt(digitalPinToInterrupt(encoder_pin_a), hall_A_ISR4, RISING);
attachInterrupt(digitalPinToInterrupt(encoder_pin_b), hall_B_ISR4, RISING);
}
}
float QuadratureEncoder::getPosition(String units){
int64_t current_pulses = 0;
float wheel_position = 0;
current_pulses = increment_array[encoder];
// Set units
if(units.equals("rad"))
wheel_position = current_pulses * RAD_PER_PULSE;
if(units.equals("deg"))
wheel_position = current_pulses * DEG_PER_PULSE;
return wheel_position;
}
void QuadratureEncoder::resetPosition(){
increment_array[encoder] = 0;
}
float QuadratureEncoder::getSpeed(String units){
float velocity_value = 0;
float time_now = 0, last_time = 0;
float period_per_pulse, period_per_turn;
float f, rpm, w, v;
bool direction = true;
int encoder = this->encoder;
if(new_speed_array[encoder] == true){
time_now = time_now_array[encoder];
last_time = last_time_array[encoder];
direction = direction_array[encoder];
new_speed_array[encoder] = false;
}
else{
time_now = old_time_now_array[encoder];
last_time = old_last_time_array[encoder];
direction = old_direction_array[encoder];
}
period_per_pulse = (time_now - last_time)/1000000;
f = 1.0 / period_per_pulse;
period_per_turn = period_per_pulse * this->pulses_per_turn;
//Wheel velocities
rpm = f*60; //rpm
w = 2*PI*(1/period_per_turn); // rad/s
v = w * this->wheel_radius; // m/s
if(units.equals("m/s"))
velocity_value = v;
if(units.equals("rad/s"))
velocity_value = w;
if(units.equals("Hz"))
velocity_value = f;
if(units.equals("rpm"))
velocity_value = rpm;
if(direction){
velocity_value = 1*abs(velocity_value);
}
else{
velocity_value = -1*abs(velocity_value);
}
float current_period_per_pulse = (micros() - last_time)/1000;
if (current_period_per_pulse > ENCODER_THRESHOLD){
velocity_value = 0;
}
return velocity_value;
}