-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLE5009.cpp
298 lines (241 loc) · 8.85 KB
/
TLE5009.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
TLE5009 E series sensor library
From the datasheet:
"The TLE5009 is an angle sensor with analog outputs. It detects the orientation of a magnetic field by measuring
sine and cosine angle components with Giant Magneto Resistance (GMR) elements. It provides analog sine and
cosine output voltages that describe the magnet angle in a range of 0 to 360°."
Written by Bill Bigge for Creative Robotics Ltd.
Distrubuted under the FreeBSD License:
Copyright (c) 2018, Bill Bigge
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the TLE5009 library project.
*/
#include "TLE5009.h"
void TLE5009::begin(int sine_Pin, int cosine_Pin, int vgmr_Pin){
sinP_PIN = sine_Pin;
cosP_PIN = cosine_Pin;
vgmd_PIN = vgmr_Pin;
this->reset();
}
//---------------------------------------------------------------------------------------------
void TLE5009::begin(int sineP_Pin, int cosineP_Pin, int sineN_Pin, int cosineN_Pin, int vgmr_Pin){
sinP_PIN = sineP_Pin;
cosP_PIN = cosineP_Pin;
sinN_PIN = sineN_Pin;
cosN_PIN = cosineN_Pin;
vgmd_PIN = vgmr_Pin;
this->reset();
differentialMode = DIFFERENTIAL;
}
//---------------------------------------------------------------------------------------------
void TLE5009::debugPrint(){
Serial.print("rSinP,"); Serial.print(sinP);
Serial.print(",rSinN,"); Serial.print(sinN);
Serial.print(",rCosP,"); Serial.print(cosP);
Serial.print(",rCosN,"); Serial.print(cosN);
Serial.print(",MaxCos,"); Serial.print(maxCos);
Serial.print(",MinCos,"); Serial.print(minCos);
Serial.print(",MaxSin,"); Serial.print(maxSin);
Serial.print(",MinSin,"); Serial.print(minSin);
Serial.print(",sinD,"); Serial.print(sinD);
Serial.print(",cosD,"); Serial.print(cosD);
Serial.print(",sine,"); Serial.print(sine, 4);
Serial.print(",cosine,"); Serial.print(cosine, 4);
Serial.print(",sinScale,"); Serial.print(sinScale, 4);
Serial.print(",cosScale,"); Serial.print(cosScale, 4);
Serial.print(",sinVShift,"); Serial.print(sinVShift, 4);
Serial.print(",cosVShift,"); Serial.print(cosVShift, 4);
Serial.print(",sinHD,"); Serial.print(sinHalfDifference, 4);
Serial.print(",cosHD,"); Serial.print(cosHalfDifference, 4);
Serial.println();
}
//---------------------------------------------------------------------------------------------
void TLE5009::setCalibration(int sMin, int sMax, int cMin, int cMax){
maxCos = cMax;
minCos = cMin;
maxSin = sMax;
minSin = sMin;
udateScale();
}
//---------------------------------------------------------------------------------------------
void TLE5009::update(){
readSensors();
if(calibrationEnabled){
updateMinMax();
udateScale();
}
if(differentialMode){
sinD = sinP - sinN;
cosD = cosP - cosN;
sine = (float)sinD*sinScale;
cosine = (float)cosD*cosScale;
}else{
sine = ((float)sinP - sinVShift)*sinScale;
cosine = ((float)cosP - cosVShift)*cosScale;
}
sine = clampS(sine, 1.0);
cosine = clampS(cosine, 1.0);
oldAngle = angle;
angle = atan2(sine, cosine);
//angle = debugVar1;
updateDeltas();
updateRevolutions();
}
//---------------------------------------------------------------------------------------------
float TLE5009::getAngle(){
if(outputDegrees) return angle*TODEGREES;
else return angle;
}
//---------------------------------------------------------------------------------------------
float TLE5009::getDelta(){
if(outputDegrees) return angleDelta*TODEGREES;
else return angleDelta;
}
//---------------------------------------------------------------------------------------------
void TLE5009::resetRevolutions(){
//return the number of revolutions since reset or start
revCounts = 0;
revolutions = 0.0;
absAng = 0.0;
updateRevolutions();
}
//---------------------------------------------------------------------------------------------
void TLE5009::reset(){
cosP = 0;
sinP = 0;
cosN = 0;
sinN = 0;
vgmr = 0;
maxCos = 0;
minCos = MAXINT;
maxSin = 0;
minSin = MAXINT;
sinD = 0;
cosD = 0;
sine = 0.0;
cosine = 0.0;
angle = 0.0;
oldAngle = 0.0;
angleDelta = 0.0;
revCounts = 0;
revolutions = 0.0;
absAng = 0.0;
lastUpdateTime = millis();
sinHalfDifference = 0.0;
cosHalfDifference = 0.0;
sinScale = 0.0;
cosScale = 0.0;
sinVShift = 0.0;
cosVShift = 0.0;
angleOffset = 0.0;
outputDegrees = RADIANS; //false = radians
calibrationEnabled = false;
differentialMode = SINGLE_ENDED;
}
//---------------------------------------------------------------------------------------------
void TLE5009::resetCalibration(){
maxCos = 0;
minCos = MAXINT;
maxSin = 0;
minSin = MAXINT;
sinHalfDifference = 0.0;
cosHalfDifference = 0.0;
sinScale = 0.0;
cosScale = 0.0;
sinVShift = 0.0;
cosVShift = 0.0;
}
//---------------------------------------------------------------------------------------------
void TLE5009::readSensors(){
if(sinP_PIN != -1) sinP = analogRead(sinP_PIN);
if(cosP_PIN != -1) cosP = analogRead(cosP_PIN);
if(sinN_PIN != -1) sinN = analogRead(sinN_PIN);
if(cosN_PIN != -1) cosN = analogRead(cosN_PIN);
if(vgmd_PIN != -1) vgmr = analogRead(vgmd_PIN);
}
//---------------------------------------------------------------------------------------------
void TLE5009::updateMinMax(){
if(differentialMode){
if(maxSin < sinD) maxSin = sinD;
if(minSin > sinD) minSin = sinD;
if(maxCos < cosD) maxCos = cosD;
if(minCos > cosD) minCos = cosD;
}else{
if(maxSin < sinP) maxSin = sinP;
if(minSin > sinP) minSin = sinP;
if(maxCos < cosP) maxCos = cosP;
if(minCos > cosP) minCos = cosP;
}
}
//---------------------------------------------------------------------------------------------
void TLE5009::udateScale(){
sinHalfDifference = ( (float)maxSin-(float)minSin ) / 2.0;
cosHalfDifference = ( (float)maxCos-(float)minCos ) / 2.0;
sinVShift = ( (float)maxSin+(float)minSin ) / 2.0;
sinScale = 1.0/sinHalfDifference;
cosVShift = ( (float)maxCos+(float)minCos ) / 2.0;
cosScale = 1.0/cosHalfDifference;
}
//---------------------------------------------------------------------------------------------
void TLE5009::updateDeltas(){
//Currently does NOT handle angle wrap around.
float timeDelta = (float)( millis() - lastUpdateTime ) / 1000;
float angleDifference;
angleDifference = angleDiff(angle,oldAngle);
angleDelta = -(angleDifference/timeDelta);
lastUpdateTime = millis();
}
//---------------------------------------------------------------------------------------------
void TLE5009::updateRevolutions(){
//update the rev counter
//check for variable wrap - use this as the point to tick one revolution
if(angle < 0.0 && oldAngle > 0.0 && angleDelta > 0.0) revCounts++;
if(angle > 0.0 && oldAngle < 0.0 && angleDelta < 0.0) revCounts--;
//convert angle to an unsigned value
absAng = constrain(angle + PI, 0.0, TWOPI);
angleFraction = absAng/TWOPI;
revolutions = revCounts + angleFraction;
}
//---------------------------------------------------------------------------------------------
float TLE5009::clampS(float var, float limit){
//constrain to the limit value
if(var > limit) return limit;
else if(var < -limit) return -limit;
else return var;
}
//copied from stack overflow ..!
//Code for handling wrap arounds with floating point derivative of angle
float TLE5009::constrainAngle(float x){
x = fmod(x + PI, TWOPI);
if (x < 0)
x += TWOPI;
return x - PI;
}
float TLE5009::angleDiff(float a,float b){
float dif = fmod(b - a + PI, TWOPI);
if (dif < 0)
dif += TWOPI;
return dif - PI;
}
float TLE5009::bisectAngle(float a,float b){
return constrainAngle(a + angleDiff(a,b) * 0.5);
}