forked from Candas1/Arduino-FOC-drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAS5048A.cpp
118 lines (91 loc) · 2.97 KB
/
AS5048A.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
/*
* AS5048A.cpp
*
* Created on: 8 Mar 2021
* Author: runger
*/
#include "AS5048A.h"
AS5048A::AS5048A(SPISettings settings, int nCS) : settings(settings), nCS(nCS) {
// nix
}
AS5048A::~AS5048A() {
}
void AS5048A::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 AS5048A::getCurrentAngle(){
readRawAngle();
return ((float)readRawAngle())/(float)AS5048A_CPR * 2.0f * (float)PI;
}
float AS5048A::getFastAngle(){
return ((float)readRawAngle())/(float)AS5048A_CPR * 2.0f * (float)PI;
}
uint16_t AS5048A::readRawAngle(){
uint16_t command = AS5048A_ANGLE_REG | AS5048A_PARITY | AS5048A_RW; // set r=1 and parity=1, result ix 0xFFFF
uint16_t lastresult = spi_transfer16(command)&AS5048A_RESULT_MASK;
return lastresult;
}
uint16_t AS5048A::readMagnitude(){
uint16_t command = AS5048A_MAGNITUDE_REG | AS5048A_RW; // set r=1, result ix 0x7FFE
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop();
return result;
}
bool AS5048A::isErrorFlag(){
return errorflag;
}
AS5048Error AS5048A::clearErrorFlag(){
uint16_t command = AS5048A_ERROR_REG | AS5048A_RW; // set r=1, result ix 0x4001
/*uint16_t cmdresult =*/ spi_transfer16(command);
uint16_t result = nop();
AS5048Error err = {
.parityError = ((result&0x0004)!=0x0000),
.commandInvalid = ((result&0x0002)!=0x0000),
.framingError = ((result&0x0001)!=0x0000)
};
return err;
}
AS5048Diagnostics AS5048A::readDiagnostics(){
uint16_t command = AS5048A_DIAGNOSTICS_REG | AS5048A_RW; // set r=1, result ix 0x7FFD
/*uint16_t cmdresult =*/ spi_transfer16(command);
AS5048Diagnostics result = {
.reg = nop()
};
return result;
}
uint16_t AS5048A::setZero(uint16_t){
// TODO implement me!
return 0;
}
uint16_t AS5048A::enableOneTimeProgramming(){
// no plans to implement this at the moment. one-time-programming a $10 chip isn't really a "maker" thing to do. The zero
// position can be easily retained in software, stored on the MCU, and thereby the sensor can be reused in another project.
return 0;
}
uint16_t AS5048A::programZero(){
// no plans to implement this at the moment. one-time-programming a $10 chip isn't really a "maker" thing to do. The zero
// position can be easily retained in software, stored on the MCU, and thereby the sensor can be reused in another project.
return 0;
}
uint16_t AS5048A::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&AS5048A_RESULT_MASK;
}
uint16_t AS5048A::spi_transfer16(uint16_t outdata) {
if (nCS>=0)
digitalWrite(nCS, 0);
spi->beginTransaction(settings);
uint16_t result = spi->transfer16(outdata);
spi->endTransaction();
if (nCS>=0)
digitalWrite(nCS, 1);
// TODO check parity
errorflag = ((result&AS5048A_ERRFLG)>0);
return result;
}