-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathAS5047.cpp
246 lines (192 loc) · 5.8 KB
/
AS5047.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
/*
* AS5047.cpp
*
* Created on: 2 May 2021
* Author: runger
*/
#include "./AS5047.h"
AS5047::AS5047(SPISettings settings, int nCS) : settings(settings), nCS(nCS) {
// nix
}
AS5047::~AS5047() {
}
void AS5047::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 AS5047::getCurrentAngle(){
readCorrectedAngle();
return ((float)readCorrectedAngle())/(float)AS5047_CPR * 2.0f * (float)PI;
}
float AS5047::getFastAngle(){
return ((float)readCorrectedAngle())/(float)AS5047_CPR * 2.0f * (float)PI;
}
uint16_t AS5047::readRawAngle(){
uint16_t command = AS5047_ANGLEUNC_REG | AS5047_RW; // set r=1 and parity=0, result is 0x7FFE
uint16_t lastresult = spi_transfer16(command)&AS5047_RESULT_MASK;
return lastresult;
}
uint16_t AS5047::readCorrectedAngle(){
uint16_t command = AS5047_ANGLECOM_REG | AS5047_PARITY | AS5047_RW; // set r=1 and parity=1, result is 0xFFFF
uint16_t lastresult = spi_transfer16(command)&AS5047_RESULT_MASK;
return lastresult;
}
uint16_t AS5047::readMagnitude(){
uint16_t command = AS5047_MAGNITUDE_REG | AS5047_RW; // set r=1, result is 0x7FFD
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop();
return result;
}
bool AS5047::isErrorFlag(){
return errorflag;
}
AS5047Error AS5047::clearErrorFlag(){
uint16_t command = AS5047_ERROR_REG | AS5047_RW; // set r=1, result is 0x4001
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop();
AS5047Error err = {
.framingError = ((result&0x0001)!=0x0000),
.commandInvalid = ((result&0x0002)!=0x0000),
.parityError = ((result&0x0004)!=0x0000)
};
return err;
}
AS5047Settings1 AS5047::readSettings1(){
uint16_t command = AS5047_SETTINGS1_REG | AS5047_PARITY | AS5047_RW; // set r=1, result is 0xC018
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047Settings1 result = {
.reg = nop()
};
return result;
}
void AS5047::writeSettings1(AS5047Settings1 settings){
uint16_t command = AS5047_SETTINGS1_REG; // set r=0, result is 0x0018
spi_transfer16(command);
spi_transfer16(calcParity(settings.reg));
}
AS5047Settings2 AS5047::readSettings2(){
uint16_t command = AS5047_SETTINGS2_REG | AS5047_RW; // set r=1, result is 0x4019
spi_transfer16(command);
AS5047Settings2 result = {
.reg = nop()
};
return result;
}
void AS5047::writeSettings2(AS5047Settings2 settings){
uint16_t command = AS5047_SETTINGS2_REG | AS5047_PARITY; // set r=0, result is 0x8019
spi_transfer16(command);
spi_transfer16(calcParity(settings.reg));
}
AS5047Diagnostics AS5047::readDiagnostics(){
uint16_t command = AS5047_DIAGNOSTICS_REG | AS5047_PARITY | AS5047_RW; // set r=1, result is 0xFFFC
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5047Diagnostics result = {
.reg = nop()
};
return result;
}
void AS5047::enablePWM(bool enable){
AS5047Settings1 settings = readSettings1();
if (settings.pwmon == enable) return; // no change
settings.pwmon = enable;
writeSettings1(settings);
}
void AS5047::enableABI(bool enable){
AS5047Settings1 settings = readSettings1();
uint8_t val = enable?0:1;
if (settings.uvw_abi == val) return; // no change
settings.uvw_abi = val;
writeSettings1(settings);
}
void AS5047::enableDEAC(bool enable){
AS5047Settings1 settings = readSettings1();
uint8_t val = enable?0:1;
if (settings.daecdis == val) return; // no change
settings.daecdis = enable?0:1;
writeSettings1(settings);
}
void AS5047::useCorrectedAngle(bool useCorrected){
AS5047Settings1 settings = readSettings1();
uint8_t val = useCorrected?0:1;
if (settings.dataselect == val) return; // no change
settings.dataselect = val;
writeSettings1(settings);
}
void AS5047::setHysteresis(uint8_t hyst){
if (hyst>3) hyst = 3;
uint8_t val = 3-hyst;
AS5047Settings2 settings = readSettings2();
if (settings.hys == val) return; // no change
settings.hys = val;
writeSettings2(settings);
}
void AS5047::setABIResolution(AS5047ABIRes res){
AS5047Settings1 settings = readSettings1();
uint8_t val = (res>>3)&0x01;
if (settings.abibin!=val || settings.uvw_abi!=0) {
settings.abibin = val;
settings.uvw_abi = 0;
writeSettings1(settings);
}
AS5047Settings2 settings2 = readSettings2();
val = (res&0x07);
if (settings2.abires!=val) {
settings2.abires = val;
writeSettings2(settings2);
}
}
uint16_t AS5047::setZero(uint16_t value){
uint16_t command = AS5047_ZPOSL_REG | AS5047_PARITY | AS5047_RW;
spi_transfer16(command);
AS5047ZPosL posL = {
.reg = nop()
};
command = AS5047_ZPOSM_REG | AS5047_PARITY;
spi_transfer16(command);
spi_transfer16(calcParity((value>>6)&0x00FF));
command = AS5047_ZPOSL_REG;
posL.zposl = value&0x003F;
spi_transfer16(command);
spi_transfer16(calcParity(posL.reg));
return getZero();
}
uint16_t AS5047::getZero() {
uint16_t command = AS5047_ZPOSM_REG | AS5047_RW;
spi_transfer16(command);
command = AS5047_ZPOSL_REG | AS5047_PARITY | AS5047_RW;
uint16_t result = spi_transfer16(command);
AS5047ZPosL posL = {
.reg = nop()
};
return ((result&0x00FF)<<6) | posL.zposl;
}
uint16_t AS5047::nop(){
uint16_t result = spi_transfer16(0xFFFF); // using 0xFFFF as nop instead of 0x0000, then next call to fastAngle will return an angle
return result&AS5047_RESULT_MASK;
}
uint16_t AS5047::calcParity(uint16_t data){
data = data & 0x7FFF;
int sum = 0;
for (int i=0;i<15;i++)
if ((data>>i)&0x0001)
sum++;
return (sum&0x01)==0x01?(data|0x8000):data;
}
uint16_t AS5047::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();
// TODO check parity
errorflag = ((result&AS5047_ERRFLG)>0);
return result;
}