-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathAS5047U.cpp
351 lines (249 loc) · 7.22 KB
/
AS5047U.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* AS5047U.cpp
*
* Created on: 24 Feb 2022
* Author: runger
*/
#include "./AS5047U.h"
AS5047U::AS5047U(SPISettings settings, int nCS) : settings(settings), nCS(nCS) {
// nix
}
AS5047U::~AS5047U() {
}
void AS5047U::init(SPIClass* _spi) {
spi = _spi;
if (nCS>=0) {
pinMode(nCS, OUTPUT);
digitalWrite(nCS, HIGH);
}
//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
spi->begin();
readRawAngle(); // read an angle
}
float AS5047U::getCurrentAngle(){
readCorrectedAngle();
return ((float)readCorrectedAngle())/(float)AS5047U_CPR * 2.0f * (float)PI;
}
float AS5047U::getFastAngle(){
return ((float)readCorrectedAngle())/(float)AS5047U_CPR * 2.0f * (float)PI;
}
uint16_t AS5047U::readRawAngle(){
uint16_t command = AS5047U_ANGLEUNC_REG | AS5047U_RW;
uint16_t lastresult = spi_transfer16(command)&AS5047U_RESULT_MASK;
return lastresult;
}
uint16_t AS5047U::readCorrectedAngle(){
uint16_t command = AS5047U_ANGLECOM_REG | AS5047U_RW;
uint16_t lastresult = spi_transfer16(command)&AS5047U_RESULT_MASK;
return lastresult;
}
uint16_t AS5047U::readMagnitude(){
uint16_t command = AS5047U_MAGNITUDE_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop16();
return result;
}
uint16_t AS5047U::readVelocity(){
uint16_t command = AS5047U_VELOCITY_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop16();
return result;
}
bool AS5047U::isErrorFlag(){
return errorflag;
}
bool AS5047U::isWarningFlag(){
return warningflag;
}
AS5047UError AS5047U::clearErrorFlag(){
uint16_t command = AS5047U_ERROR_REG | AS5047U_RW; // set r=1, result is 0x4001
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047UError result;
result.reg = nop16();
return result;
}
AS5047USettings1 AS5047U::readSettings1(){
uint16_t command = AS5047U_SETTINGS1_REG | AS5047U_RW; // set r=1, result is 0xC018
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047USettings1 result = {
.reg = nop16()
};
return result;
}
void AS5047U::writeSettings1(AS5047USettings1 settings){
writeRegister24(AS5047U_SETTINGS1_REG, settings.reg);
}
AS5047USettings2 AS5047U::readSettings2(){
uint16_t command = AS5047U_SETTINGS2_REG | AS5047U_RW; // set r=1, result is 0x4019
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047USettings2 result = {
.reg = nop16()
};
return result;
}
void AS5047U::writeSettings2(AS5047USettings2 settings){
writeRegister24(AS5047U_SETTINGS2_REG, settings.reg);
}
AS5047USettings3 AS5047U::readSettings3(){
uint16_t command = AS5047U_SETTINGS3_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047USettings3 result = {
.reg = nop16()
};
return result;
}
void AS5047U::writeSettings3(AS5047USettings3 settings){
writeRegister24(AS5047U_SETTINGS3_REG, settings.reg);
}
AS5047UDiagnostics AS5047U::readDiagnostics(){
uint16_t command = AS5047U_DIAGNOSTICS_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047UDiagnostics result = {
.reg = nop16()
};
return result;
}
uint8_t AS5047U::readAGC(){
uint16_t command = AS5047U_AGC_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop16();
return result & 0x00FF;
};
uint8_t AS5047U::readECCCHK(){
uint16_t command = AS5047U_ECCCHK_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop16();
return result & 0x007F;
};
AS5047UDisableSettings AS5047U::readDisableSettings(){
uint16_t command = AS5047U_DISABLE_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047UDisableSettings result = {
.reg = nop16()
};
return result;
};
void AS5047U::writeDisableSettings(AS5047UDisableSettings settings){
writeRegister24(AS5047U_DISABLE_REG, settings.reg);
};
AS5047UECCSettings AS5047U::readECCSettings(){
uint16_t command = AS5047U_ECC_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047UECCSettings result = {
.reg = nop16()
};
return result;
};
void AS5047U::writeECCSettings(AS5047UECCSettings settings){
writeRegister24(AS5047U_ECC_REG, settings.reg);
};
void AS5047U::enablePWM(bool enable, bool pwmOnWPin){
// AS5047UDisableSettings settings = readDisableSettings();
// if (settings.uvw_off==1) {
// settings.uvw_off = 0;
// writeDiableSettings(settings);
// }
AS5047USettings2 settings2 = readSettings2();
settings2.uvw_abi = pwmOnWPin?0:1;
settings2.pwm_on = enable;
writeSettings2(settings2);
}
void AS5047U::enableABI(bool enable){
AS5047UDisableSettings settings = readDisableSettings();
settings.abi_off = enable?0:1;
writeDisableSettings(settings);
delayMicroseconds(50);
if (enable) {
AS5047USettings2 settings2 = readSettings2();
settings2.uvw_abi = 0;
writeSettings2(settings2);
}
}
void AS5047U::enableUVW(bool enable){
AS5047UDisableSettings settings = readDisableSettings();
settings.uvw_off = enable?0:1;
writeDisableSettings(settings);
if (enable) {
AS5047USettings2 settings2 = readSettings2();
settings2.uvw_abi = 1;
writeSettings2(settings2);
}
}
uint16_t AS5047U::getZero(){
uint16_t command = AS5047U_ZPOSM_REG | AS5047U_RW;
spi_transfer16(command);
command = AS5047U_ZPOSL_REG | AS5047U_RW;
uint16_t result = spi_transfer16(command);
AS5047UZPosL posL = {
.reg = nop16()
};
return ((result&0x00FF)<<6) | posL.zposl;
}
uint16_t AS5047U::setZero(uint16_t value){
uint16_t command = AS5047U_ZPOSL_REG | AS5047U_RW;
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047UZPosL posL = {
.reg = nop16()
};
posL.zposl = value&0x003F;
writeRegister24(AS5047U_ZPOSL_REG, posL.reg);
writeRegister24(AS5047U_ZPOSM_REG, (value>>6)&0x00FF);
return getZero();
}
uint16_t AS5047U::nop16(){
uint16_t result = spi_transfer16(0xFFFF); // using 0xFFFF as nop instead of 0x0000, then next call to fastAngle will return an angle
return result&AS5047U_RESULT_MASK;
}
uint16_t AS5047U::spi_transfer16(uint16_t outdata) {
spi->beginTransaction(settings);
if (nCS>=0)
digitalWrite(nCS, 0);
uint16_t result = spi->transfer16(outdata);
if (nCS>=0)
digitalWrite(nCS, 1);
spi->endTransaction();
errorflag = ((result&AS5047U_ERROR)>0);
warningflag = ((result&AS5047U_WARNING)>0);
return result;
}
uint8_t AS5047U::calcCRC(uint16_t data){
uint8_t crc = 0xC4; // Initial value
for (int i = 0; i < 16; i++) {
if ((crc ^ data) & 0x8000) {
crc = (crc << 1) ^ 0x1D;
} else {
crc <<= 1;
}
data <<= 1;
}
return crc ^ 0xFF;
}
uint16_t AS5047U::writeRegister24(uint16_t reg, uint16_t data) {
uint8_t buff[3];
buff[0] = (reg>>8)&0x3F;
buff[1] = reg&0xFF;
buff[2] = calcCRC(reg);
spi->beginTransaction(settings);
if (nCS>=0)
digitalWrite(nCS, 0);
spi->transfer(buff, 3);
if (nCS>=0)
digitalWrite(nCS, 1);
spi->endTransaction();
errorflag = ((buff[0]&0x40)>0);
warningflag = ((buff[0]&0x80)>0);
buff[0] = (data>>8)&0x3F;
buff[1] = data&0xFF;
buff[2] = calcCRC(data);
spi->beginTransaction(settings);
if (nCS>=0)
digitalWrite(nCS, 0);
spi->transfer(buff, 3);
if (nCS>=0)
digitalWrite(nCS, 1);
spi->endTransaction();
errorflag = ((buff[0]&0x40)>0);
warningflag = ((buff[0]&0x80)>0);
delayMicroseconds(50);
return buff[0]<<8 | buff[1];
}