-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAS5600.cpp
More file actions
321 lines (269 loc) · 8.8 KB
/
Copy pathAS5600.cpp
File metadata and controls
321 lines (269 loc) · 8.8 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "Arduino.h"
#include "AS5600.h"
AS5600::AS5600() {
Wire.begin();
}
/*
* Function: getPosition
* ----------------------------
* returns: the unscaled and unmodified angle from the RAW ANGLE register.
*/
uint16_t AS5600::getPosition() {
return getRawAngle();
}
/*
* Function: getAngle
* ----------------------------
* returns: the scaled output value available in the ANGLE register.
*/
uint16_t AS5600::getAngle() {
return _getRegisters2(_ANGLEAddressMSB, _ANGLEAddressLSB);
}
/*
* Function: getRawAngle
* ----------------------------
* returns: the unscaled and unmodified angle from the RAW ANGLE register.
*/
uint16_t AS5600::getRawAngle() {
return _getRegisters2(_RAWANGLEAddressMSB, _RAWANGLEAddressLSB);
}
/*
* Function: getScaledAngle
* ----------------------------
* returns: the raw angle as a value between 0 and 360 degrees.
*/
float AS5600::getScaledAngle() {
int ang_hi = _getRegister(_RAWANGLEAddressMSB);
int ang_lo = _getRegister(_RAWANGLEAddressLSB);
return ang_hi * 22.5 + ang_lo * 0.087890625;
}
/*
* Function: getStatus
* ----------------------------
* returns: The the value in the STATUS register. The STATUS register provides bits that indicate the current state of the AS5600.
*
* register format: X X MD ML MH X X X
*
* MH: AGC minimum gain overflow, magnet too strong
* ML: AGC maximum gain overflow, magnet too weak
* MD: Magnet was detected
*
*/
uint8_t AS5600::getStatus() {
return _getRegister(_STATUSAddress) & 0b00111000;
}
/*
* Function: isMagnetTooStrong
* ----------------------------
* returns: true if magnet is too close to AS5600.
*/
bool AS5600::isMagnetTooStrong() {
uint8_t _b=0;
_b = getStatus();
if (_b & (1<<3)) { return true; }
return false;
}
/*
* Function: isMagnetDetected
* ----------------------------
* returns: true if magnet is too far to AS5600.
*/
bool AS5600::isMagnetTooWeak() {
uint8_t _b=0;
_b = getStatus();
if (_b & (1<<4)) { return true; }
return false;
}
/*
* Function: isMagnetDetected
* ----------------------------
* returns: true if magnet is detected by AS5600.
*/
bool AS5600::isMagnetDetected() {
uint8_t _b=0;
_b = getStatus();
if (_b & (1<<5)) { return true; }
return false;
}
/*
* Function: getGain
* ----------------------------
* returns: the value contained in the Automatic Gain Control (AGC) register.
*
* In 5V operation, the AGC range is 0-255
* In 3.3V operation, the AGC range is 0-128
*/
uint8_t AS5600::getGain() {
return _getRegister(_AGCAddress);
}
/*
* Function: getMagnet
* ----------------------------
* returns: getGain().
*/
uint8_t AS5600::getMagnet() {
return getGain();
}
/*
* Function: getMagnitude
* ----------------------------
* returns: the value contained in the MAGNITUDE REGISTER. The MAGNITUDE register indicates the magnitude value of the
* internal Coordinate Rotation Digital Computer (CORDIC).
*/
uint16_t AS5600::getMagnitude() {
return _getRegisters2(_MAGNITUDEAddressMSB, _MAGNITUDEAddressLSB);
}
/*
* Function: setPowerMode
* ----------------------------
* powerMode: the desired power mode. Valid input values are 0, 1, 2, and 3, corresponding to Normal Mode, Low Power Mode 1, Low Power Mode 2, Low Power Mode 3.
*
* returns: boolean indicating is value was set.
*/
bool AS5600::setPowerMode(uint8_t powerMode) {
if (powerMode != POWER_MODE_NORM && powerMode != POWER_MODE_LPM1 && powerMode != POWER_MODE_LPM2 && powerMode != POWER_MODE_LPM3) {
return false;
}
uint8_t currentCONFLSB = _getRegister(_CONFAddressLSB);
uint8_t writeCONFLSB = currentCONFLSB & 0b11111100 | powerMode;
_writeRegister(_CONFAddressLSB, writeCONFLSB);
currentCONFLSB = _getRegister(_CONFAddressLSB);
return currentCONFLSB == writeCONFLSB;
}
/*
* Function: setHysteresis
* ----------------------------
* hysteresis: the desired hysteresis. Valid values are 0, 1, 2, and 3, corresponding to OFF, 1 LSB, 2 LSBs, 3 LSBs.
*
* returns: boolean indicating is value was set.
*/
bool AS5600::setHysteresis(uint8_t hysteresis) {
if (hysteresis != HYSTERESIS_OFF && hysteresis != HYSTERESIS_1LSB && hysteresis != HYSTERESIS_2LSB && hysteresis != HYSTERESIS_3LSB) {
return false;
}
uint8_t currentCONFLSB = _getRegister(_CONFAddressLSB);
uint8_t writeCONFLSB = currentCONFLSB & 0b11110011 | (hysteresis << 2);
_writeRegister(_CONFAddressLSB, writeCONFLSB);
currentCONFLSB = _getRegister(_CONFAddressLSB);
return currentCONFLSB == writeCONFLSB;
}
/*
* Function: setOutputStage
* ----------------------------
* outputStage: the desired outputStage. Valid values are 0, 1 and 2
*
* returns: boolean indicating is value was set.
*/
bool AS5600::setOutputStage(uint8_t outputStage) {
if (outputStage != OUTPUT_STAGE_ANALOG_FULL && outputStage != OUTPUT_STAGE_ANALOG_REDUCED && outputStage != OUTPUT_STAGE_DIGITAL_PWM) {
return false;
}
uint8_t currentCONFLSB = _getRegister(_CONFAddressLSB);
uint8_t writeCONFLSB = currentCONFLSB & 0b11001111 | (outputStage << 4);
_writeRegister(_CONFAddressLSB, writeCONFLSB);
currentCONFLSB = _getRegister(_CONFAddressLSB);
return currentCONFLSB == writeCONFLSB;
}
/*
* Function: setPWMFrequency
* ----------------------------
* frequency: the desired PWM frequency for PWM output. Valid values are 115 Hz, 230 Hz, 460 Hz, 920 Hz.
*
* returns: boolean indicating is value was set.
*/
bool AS5600::setPWMFrequency(uint8_t frequency) {
uint8_t currentCONFLSB = _getRegister(_CONFAddressLSB);
uint8_t writeCONFLSB = currentCONFLSB & 0b00111111 | (frequency << 6);
_writeRegister(_CONFAddressLSB, writeCONFLSB);
currentCONFLSB = _getRegister(_CONFAddressLSB);
return currentCONFLSB == writeCONFLSB;
}
/*
* Function: setSlowFilter
* ----------------------------
* outputStage: the desired outputStage. Valid values are 0, 1, 2.
*
* returns: boolean indicating is value was set.
*/
bool AS5600::setSlowFilter(uint8_t slowFilter) {
if (slowFilter != SLOW_FILTER_16X && slowFilter != SLOW_FILTER_8X && slowFilter != SLOW_FILTER_4X && slowFilter != SLOW_FILTER_2X) {
return false;
}
uint8_t currentCONFLSB = _getRegister(_CONFAddressLSB);
uint8_t writeCONFLSB = currentCONFLSB & 0b11111100 | slowFilter;
_writeRegister(_CONFAddressLSB, writeCONFLSB);
currentCONFLSB = _getRegister(_CONFAddressMSB);
return currentCONFLSB == writeCONFLSB;
}
uint8_t AS5600::getCONF() {
return _getRegister(_CONFAddressLSB);
}
/*
* Function: setFastFilterThreshold
* ----------------------------
* fastFilterThreshold: the desired fastFilterThreshold. Valid values are 0, 1, 2, 3, 4, 5, 6, 7
*
* returns: boolean indicating is value was set.
*/
bool AS5600::setFastFilterThreshold(uint8_t fastFilterThreshold) {
if (fastFilterThreshold != FAST_FILTER_THRESHOLD_SLOW &&
fastFilterThreshold != FAST_FILTER_THRESHOLD_6LSB &&
fastFilterThreshold != FAST_FILTER_THRESHOLD_7LSB &&
fastFilterThreshold != FAST_FILTER_THRESHOLD_9LSB &&
fastFilterThreshold != FAST_FILTER_THRESHOLD_18LSB &&
fastFilterThreshold != FAST_FILTER_THRESHOLD_21LSB &&
fastFilterThreshold != FAST_FILTER_THRESHOLD_24LSB &&
fastFilterThreshold != FAST_FILTER_THRESHOLD_10LSB) {
return false;
}
uint8_t currentCONFLSB = _getRegister(_CONFAddressLSB);
uint8_t writeCONFLSB = currentCONFLSB & 0b11100011 | (fastFilterThreshold << 3);
_writeRegister(_CONFAddressLSB, writeCONFLSB);
currentCONFLSB = _getRegister(_CONFAddressMSB);
return currentCONFLSB == writeCONFLSB;
}
/*
* Function: _getRegister
* ----------------------------
* register1: register address
*
* returns: the value within a register.
*/
uint8_t AS5600::_getRegister(byte register1) {
uint8_t _b=0;
Wire.beginTransmission(_AS5600Address);
Wire.write(register1);
Wire.endTransmission();
Wire.requestFrom(_AS5600Address, 1);
while (Wire.available() == 0) { }
_b = Wire.read();
return _b;
}
/*
* Function: _getRegisters2
* ----------------------------
* registerMSB: register address of Most Significant Byte (MMSB)
* registerLSB: register address of Least Significant Byte (MMSB)
*
* returns: the value of the 16 bit number stored in registerMSB and registerLSB.
*/
uint16_t AS5600::_getRegisters2(byte registerMSB, byte registerLSB) {
uint16_t _hi=0, _lo=0;
_hi = _getRegister(registerMSB);
_lo = _getRegister(registerLSB);
return (_hi<<8) | (_lo);
}
/*
* Function: _writeRegister
* ----------------------------
* registerAddress: register address to write to
* value: value to write to register at registerAddress
*
* returns: void
*/
void AS5600::_writeRegister(byte registerAddress, byte value) {
Wire.beginTransmission(_AS5600Address);
Wire.write((uint8_t)registerAddress); //module function register address
Wire.write((uint8_t)value); //data bytes
Wire.endTransmission();
}