forked from simplefoc/Arduino-FOC-drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMA735.cpp
297 lines (285 loc) · 7.88 KB
/
MA735.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
#include "MA735.h"
MA735::MA735(SPISettings settings, int nCS) : settings(settings), nCS(nCS) {
};
MA735::~MA735() {
};
void MA735::init(SPIClass* _spi) {
spi = _spi;
if (nCS >= 0) {
pinMode(nCS, OUTPUT);
digitalWrite(nCS, HIGH);
}
};
float MA735::getCurrentAngle() {
return (readRawAngle() * _2PI)/MA735_16BIT;//It doesn't matter that it is divided by 65536, because the raw angle fills empty data bits with empty zeros so sensor resolution doesn't affect angle calculation
}; // angle in radians, return current value
uint16_t MA735::readRawAngle() {
uint16_t angle = transfer16(0x0000);
return angle;
}; // 9-13bit angle value
uint16_t MA735::getZero() {
uint16_t result = readRegister(MA735_REG_ZERO_POSITION_MSB)<<8;
result |= readRegister(MA735_REG_ZERO_POSITION_LSB);
return result;
};
uint8_t MA735::getBiasCurrentTrimming() {
return readRegister(MA735_REG_BCT);
};
bool MA735::isBiasCurrrentTrimmingX() {
return (readRegister(MA735_REG_ET) & 0x01)==0x01;
};
bool MA735::isBiasCurrrentTrimmingY() {
return (readRegister(MA735_REG_ET) & 0x02)==0x02;
};
uint16_t MA735::getPulsesPerTurn() {
uint16_t result = readRegister(MA735_REG_ILIP_PPT_LSB)>>6;
result |= ((uint16_t)readRegister(MA735_REG_PPT_MSB))<<2;
return result+1;
};
uint8_t MA735::getIndexLength() {
return (readRegister(MA735_REG_ILIP_PPT_LSB)>>2)&0x0F;
};
uint8_t MA735::getRotationDirection() {
return (readRegister(MA735_REG_RD)>>7);
};
uint8_t MA735::getFieldStrengthHighThreshold() {
return (readRegister(MA735_REG_MGLT_MGHT)>>2)&0x07;
};
uint8_t MA735::getFieldStrengthLowThreshold() {
return (readRegister(MA735_REG_MGLT_MGHT)>>5)&0x07;
};
FieldStrength MA735::getFieldStrength() {
return (FieldStrength)(readRegister(MA735_REG_MGH_MGL)>>6);
};
uint8_t MA735::getFilterWindow() {
return readRegister(MA735_REG_FW);
};
uint8_t MA735::getHysteresis() {
return readRegister(MA735_REG_HYS);
};
float MA735::getResolution() {
//All I could find in the datasheet was a table with the correlation, no function to convert Filter window to res.
uint8_t reg = readRegister(MA735_REG_FW);
float result;
switch (reg) {
case 51:
result = 9.0;
break;
case 68:
result = 9.5;
break;
case 85:
result = 10.0;
break;
case 102:
result = 10.5;
break;
case 119:
result = 11.0;
break;
case 136:
result = 11.5;
break;
case 153:
result = 12.0;
break;
case 170:
result = 12.5;
break;
case 187:
result = 13.0;
break;
default:
result = 11.0;
break;
}
return result;
};
int MA735::getUpdateTime() {
//All I could find in the datasheet was a table with the correlation, no function to convert Filter window to update time.
//Returns result in microseconds
uint8_t reg = readRegister(MA735_REG_FW);
int result;
switch (reg) {
case 51:
result = 64;
break;
case 68:
result = 128;
break;
case 85:
result = 256;
break;
case 102:
result = 512;
break;
case 119:
result = 1024;
break;
case 136:
result = 2048;
break;
case 153:
result = 4096;
break;
case 170:
result = 8192;
break;
case 187:
result = 16384;
break;
default:
result = 1024;
break;
}
return result;
};
void MA735::setZero(uint16_t value) {
writeRegister(MA735_REG_ZERO_POSITION_MSB, value>>8);
writeRegister(MA735_REG_ZERO_POSITION_LSB, value&0x00FF);
};
void MA735::setBiasCurrentTrimming(uint8_t value) {
writeRegister(MA735_REG_BCT, value);
};
void MA735::setBiasCurrrentTrimmingEnabled(bool Xenabled, bool Yenabled) {
uint8_t val = Xenabled ? 0x01 : 0x00;
val |= (Yenabled ? 0x02 : 0x00);
writeRegister(MA735_REG_ET, val);
};
void MA735::setPulsesPerTurn(uint16_t value) {
uint16_t pptVal = value - 1;
writeRegister(MA735_REG_PPT_MSB, pptVal>>2);
uint8_t val = readRegister(MA735_REG_ILIP_PPT_LSB);
val &= 0x3F;
val |= (pptVal&0x03)<<6;
writeRegister(MA735_REG_ILIP_PPT_LSB, val);
};
void MA735::setIndexLength(uint8_t value) {
uint8_t val = readRegister(MA735_REG_ILIP_PPT_LSB);
val &= 0xC0;
val |= ((value<<2)&0x3F);
writeRegister(MA735_REG_ILIP_PPT_LSB, val);
};
void MA735::setRotationDirection(uint8_t value) {
if (value==0)
writeRegister(MA735_REG_RD, 0x00);
else
writeRegister(MA735_REG_RD, 0x80);
};
void MA735::setFieldStrengthThresholds(uint8_t high, uint8_t low) {
uint8_t val = (low<<5) | (high<<2);
writeRegister(MA735_REG_MGLT_MGHT, val);
};
void MA735::setFilterWindow(uint8_t value) {
writeRegister(MA735_REG_FW, value);
};
void MA735::setHysteresis(uint8_t value) {
writeRegister(MA735_REG_HYS, value);
};
void MA735::setResolution(float res) {
//All I could find in the datasheet was a table with the correlation, no function to convert Filter window to res.
uint8_t value;
uint8_t res_int = res * 10;//It has to be a basic type for the switch case
switch (res_int) {
case 90:
value = 51;
break;
case 95:
value = 68;
break;
case 100:
value = 85;
break;
case 105:
value = 102;
break;
case 110:
value = 119;
break;
case 115:
value = 136;
break;
case 120:
value = 153;
break;
case 125:
value = 170;
break;
case 130:
value = 187;
break;
default:
value = 119;
break;
}
writeRegister(MA735_REG_FW, value);
};
void MA735::setUpdateTime(int microsec) {
//All I could find in the datasheet was a table with the correlation, no function to convert Filter window to update time.
//time in microseconds
uint8_t value;
switch (microsec) {
case 64:
value = 51;
break;
case 128:
value = 68;
break;
case 256:
value = 85;
break;
case 512:
value = 102;
break;
case 1024:
value = 119;
break;
case 2048:
value = 136;
break;
case 4096:
value = 153;
break;
case 8192:
value = 170;
break;
case 16384:
value = 187;
break;
default:
value = 119;
break;
}
writeRegister(MA735_REG_FW, value);
};
uint16_t MA735::transfer16(uint16_t outValue) {
spi->beginTransaction(settings);
if (nCS >= 0)
digitalWrite(nCS, LOW);
uint16_t value = spi->transfer16(outValue);
if (nCS >= 0)
digitalWrite(nCS, HIGH);
spi->endTransaction();
return value;
};
uint8_t MA735::readRegister(uint8_t reg) {
uint16_t cmd = 0x4000 | ((reg&0x001F)<<8);
uint16_t value = transfer16(cmd);
delayMicroseconds(1);
value = transfer16(0x0000);
return value>>8;
};
uint8_t MA735::writeRegister(uint8_t reg, uint8_t value) {
uint8_t write_check = readRegister(reg);
//no need to rewrite, it is the exact same value
if (write_check == value) {
return write_check;
}
else {
uint16_t cmd = 0x8000 | ((reg&0x1F)<<8) | value;
uint16_t result = transfer16(cmd);
delay(20); // 20ms delay required
result = transfer16(0x0000);
return result>>8;
}
};