-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.cpp
123 lines (83 loc) · 2.2 KB
/
encoder.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
#include "encoder.h"
#include "Arduino.h"
#include <Wire.h>
encoder::encoder(){
magStatus = 0;
rawLOW = 0;
rawHIGH = 0;
rawAngle = 0;
degAngle = 0;
startAngle = 0;
correctedAngle = 0;
totalAngle = 0;
quadrant = 0;
prev_quadrant = 0;
num_turns = 0;
}
void encoder::checkMagnet(){
while(magStatus&(1<<5) == 0){
magStatus = 0;
Wire.beginTransmission(0x36);
Wire.write(0x0B);
Wire.endTransmission();
Wire.requestFrom(0x36, 1);
while(Wire.available() == 0);
magStatus = Wire.read();
}
}
void encoder::readAngle(){
Wire.beginTransmission(0x36);
Wire.write(0x0D);
Wire.endTransmission();
Wire.requestFrom(0x36, 1);
while(Wire.available() == 0);
rawLOW = Wire.read();
Wire.beginTransmission(0x36);
Wire.write(0x0C);
Wire.endTransmission();
Wire.requestFrom(0x36, 1);
while(Wire.available() == 0);
rawHIGH = Wire.read();
rawHIGH = rawHIGH<<8;
rawAngle = rawHIGH|rawLOW;
degAngle = rawAngle*(360.0/4095);
}
void encoder::correctAngle(){
correctedAngle = degAngle - startAngle;
if(correctedAngle<0) correctedAngle += 360;
}
void encoder::checkQuadrantUpdateTurns(){
//Quadrant 1
if(correctedAngle >= 0 && correctedAngle <=90) quadrant=1;
//Quadrant 2
if(correctedAngle > 90 && correctedAngle <=180) quadrant=2;
//Quadrant 3
if(correctedAngle > 180 && correctedAngle <=270) quadrant=3;
//Quadrant 4
if(correctedAngle > 270 && correctedAngle <360) quadrant=4;
/*Update Turns*/
if(quadrant != prev_quadrant){
if(quadrant==1 && prev_quadrant==4) num_turns++;
else if(quadrant==4 && prev_quadrant==1) num_turns--;
}
prev_quadrant = quadrant;
totalAngle_prev = totalAngle;
totalAngle = correctedAngle + num_turns*360;
}
void encoder::init(){
checkMagnet();
readAngle();
startAngle = degAngle;
t1 = t2 = micros()/1e6;
}
float encoder::getAngle(){
t1 = t2;
t2 = micros()/1e6;
readAngle();
correctAngle();
checkQuadrantUpdateTurns();
return totalAngle;
}
float encoder::getVel(){
return (totalAngle - totalAngle_prev)/(t2 - t1);
}