-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAS5311.cpp
More file actions
208 lines (178 loc) · 5.19 KB
/
AS5311.cpp
File metadata and controls
208 lines (178 loc) · 5.19 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
#include "AS5311.h"
#include "Arduino.h"
AS5311::AS5311(uint8_t dataPin, uint8_t clockPin, uint8_t chipSelectPin)
: mDataPin(dataPin), mClockPin(clockPin), mSelectPin(chipSelectPin)
{
constexpr unsigned long Delay{100};
pinMode(mDataPin, INPUT_PULLUP);
pinMode(mClockPin, OUTPUT);
pinMode(mSelectPin, OUTPUT);
// Turn on the AS5311
digitalWrite(mSelectPin, HIGH);
digitalWrite(mClockPin, LOW);
delay(Delay);
}
void AS5311::updateData(ValueType valueData)
{
constexpr float MircoMeterPerPulse{0.48828125};
if (valueData == ValueType::PositionValue)
{
uint32_t totalValue{0};
for (int i = 0; i < mAverageFactor; ++i)
{
mAngularPositionData.rawValue = readChip(ValueType::PositionValue);
totalValue += mAngularPositionData.value;
}
mPreviousAvgValue = mCurrentAvgValue;
mCurrentAvgValue = totalValue / mAverageFactor;
mDistance = calculateDistance() * MircoMeterPerPulse;
}
else
{
mMagneticFieldData.rawValue = readChip(ValueType::MagneticValue);
}
}
int16_t AS5311::calculateDistance()
{
constexpr uint16_t MinPusleValue{0};
constexpr uint16_t MaxPusleValue{4095};
uint16_t diffDistanceOverLimit{0};
const uint16_t diffDistanceBetweenValues = abs(mPreviousAvgValue - mCurrentAvgValue);
if (mCurrentAvgValue > mPreviousAvgValue)
{
diffDistanceOverLimit = (MaxPusleValue - mCurrentAvgValue) + (mPreviousAvgValue - MinPusleValue);
}
else if (mPreviousAvgValue > mCurrentAvgValue)
{
diffDistanceOverLimit = (MaxPusleValue - mPreviousAvgValue) + (mCurrentAvgValue - MinPusleValue);
}
else
{
return 0;
}
if (diffDistanceBetweenValues < diffDistanceOverLimit)
{
return (mPreviousAvgValue > mCurrentAvgValue) ? (-1) * diffDistanceBetweenValues :
diffDistanceBetweenValues;
}
else
{
return (mCurrentAvgValue > mPreviousAvgValue) ? (-1) * diffDistanceOverLimit : diffDistanceOverLimit;
}
}
float AS5311::distance() const
{
return mDistance;
}
uint16_t AS5311::value(ValueType valueData) const
{
if (valueData == ValueType::PositionValue)
{
return mCurrentAvgValue;
}
else
{
return mMagneticFieldData.value;
}
}
bool AS5311::isValueValid(ValueType valueData) const
{
if (valueData == ValueType::PositionValue)
{
return (checkParityIsEven(mAngularPositionData.rawValue) && mAngularPositionData.OCF &&
not mAngularPositionData.COF && not mAngularPositionData.LIN);
}
else
{
return (checkParityIsEven(mMagneticFieldData.rawValue) && mMagneticFieldData.OCF &&
not mMagneticFieldData.COF && not mMagneticFieldData.LIN);
}
}
void AS5311::setAverageFactor(uint8_t factor)
{
if (mAverageFactor != factor)
{
mAverageFactor = factor;
}
}
uint8_t AS5311::averageFactor() const
{
return mAverageFactor;
}
uint32_t AS5311::readChip(ValueType valueData)
{
constexpr uint8_t NoOfBits{18};
// constexpr unsigned long Delay{1};
uint32_t rawValue{0};
digitalWrite(mSelectPin, HIGH);
if (valueData == ValueType::PositionValue)
{
digitalWrite(mClockPin, HIGH);
// delayMicroseconds(Delay);
digitalWrite(mSelectPin, LOW);
// delayMicroseconds(Delay);
digitalWrite(mClockPin, LOW);
// delayMicroseconds(Delay);
}
if (valueData == ValueType::MagneticValue)
{
digitalWrite(mClockPin, LOW);
// delayMicroseconds(Delay);
digitalWrite(mSelectPin, LOW);
// delayMicroseconds(Delay);
}
for (uint8_t c = 0; c < NoOfBits; ++c)
{
digitalWrite(mClockPin, HIGH);
// delayMicroseconds(Delay);
digitalWrite(mClockPin, LOW);
int inputstream = digitalRead(mDataPin);
rawValue = ((rawValue << 1u) + inputstream);
}
digitalWrite(mClockPin, HIGH);
digitalWrite(mSelectPin, HIGH);
return rawValue;
}
bool AS5311::checkParityIsEven(const uint32_t value) const
{
constexpr uint8_t NoOfBits{18};
constexpr uint8_t Two{2};
uint8_t count = 0;
uint32_t b = 1;
for (uint8_t i = 0; i < NoOfBits; ++i)
{
if (value & (b << i))
{
count++;
}
}
if ((count % Two))
{
return false;
}
else
{
return true;
}
}
void AS5311::rangeIndication()
{
constexpr uint32_t LowerLimit{0x20};
constexpr uint32_t UpperLimit{0x5f};
constexpr uint32_t OkayValue{0x3f};
if (mAngularPositionData.MagINC && mAngularPositionData.MagDEC && mAngularPositionData.LIN &&
(mMagneticFieldData.value < LowerLimit || mMagneticFieldData.value > UpperLimit))
{
// return red
}
if (mAngularPositionData.MagINC && mAngularPositionData.MagDEC && not mAngularPositionData.LIN &&
(mMagneticFieldData.value > LowerLimit && mMagneticFieldData.value < UpperLimit))
{
// return yellow
}
if (not(mAngularPositionData.MagINC && mAngularPositionData.MagDEC) && not mAngularPositionData.LIN &&
mMagneticFieldData.value == OkayValue)
{
// return greem
}
}