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
352
353
354
355
356
357
358
359
360
|
#include "sensor.hpp"
#include "../utils/serial.hpp"
#include "../utils/time.hpp"
#include "registers.hpp"
Sensor::Sensor(uint8_t address, TwoWire *wire, SerialStream sout,
unsigned int throttle_time)
: _last_time(0), _sout(sout)
{
_address = address;
_wire = wire;
// Set default values
accel_cal_x = 0;
accel_cal_y = 0;
accel_cal_z = 0;
gyro_cal_x = 0;
gyro_cal_y = 0;
gyro_cal_z = 0;
_throttle_enabled = true;
_throttle_time = throttle_time;
_status = SensorStatus::OK;
_accel_sensitivity = 0;
_accel_to_g_force = 1.0 / 16384.0;
_gyro_sensitivity = 0;
_ang_rate_to_dps = 1.0 / 131.0;
// Reset the sensor
reset();
}
bool Sensor::begin()
{
_wire->begin();
if (isConnected())
{
_wire->beginTransmission(_address);
_wire->write(SensorRegisters::PWR_MGMT_1);
_wire->write(SENSOR_WAKEUP);
return (_wire->endTransmission() == 0);
}
return false;
}
bool Sensor::isConnected()
{
_wire->beginTransmission(_address);
return (_wire->endTransmission() == 0);
}
void Sensor::reset()
{
_accel_raw_x = 0;
_accel_raw_y = 0;
_accel_raw_z = 0;
_gyro_raw_x = 0;
_gyro_raw_y = 0;
_gyro_raw_z = 0;
_accel_x = 0;
_accel_y = 0;
_pitch = 0;
_roll = 0;
_yaw = 0;
}
bool Sensor::read()
{
Time now = time_now();
if (_throttle_enabled && now.diff(_last_time).millisecs() < _throttle_time)
{
_status = SensorStatus::THROTTLED;
return false;
}
_wire->beginTransmission(_address);
_wire->write(SensorRegisters::ACCEL_XOUT_H);
if (_wire->endTransmission() != 0)
{
_status = SensorStatus::ERR_WRITE;
return false;
}
int8_t response_length = _wire->requestFrom(_address, (uint8_t)14);
if (response_length != 14)
{
_status = SensorStatus::ERR_READ;
return false;
}
// Accelerometer
_accel_raw_x = _readHighLow();
_accel_raw_y = _readHighLow();
_accel_raw_z = _readHighLow();
_sout << "\nAccel raw x: " << _accel_raw_x << "\n"
<< "Accel raw y: " << _accel_raw_y << "\n"
<< "Accel raw z: " << _accel_raw_z << "\n"
<< endl;
// Gyroscope
_gyro_raw_x = _readHighLow();
_gyro_raw_y = _readHighLow();
_gyro_raw_z = _readHighLow();
_sout << "\nGyro raw x: " << _gyro_raw_x << "\n"
<< "Gyro raw y: " << _gyro_raw_y << "\n"
<< "Gyro raw z: " << _gyro_raw_z << "\n"
<< endl;
// Duration interval
now.update();
float duration = now.diff(_last_time).secs();
_last_time = now;
// Convert raw acceleration to g:s (g-force)
_accel_raw_x *= _accel_to_g_force;
_accel_raw_y *= _accel_to_g_force;
_accel_raw_z *= _accel_to_g_force;
_sout << "\nAccel raw x g:s: " << _accel_raw_x << "\n"
<< "Accel raw y g:s: " << _accel_raw_y << "\n"
<< "Accel raw z g:s: " << _accel_raw_z << "\n"
<< endl;
// Error correct raw acceleration
_accel_raw_x += accel_cal_x;
_accel_raw_y += accel_cal_y;
_accel_raw_z += accel_cal_z;
_sout << "\nAccel raw x correct: " << _accel_raw_x << "\n"
<< "Accel raw y correct: " << _accel_raw_y << "\n"
<< "Accel raw z correct: " << _accel_raw_z << "\n"
<< endl;
// Prepare for Pitch Roll Yaw
float accel_y_pow_two = pow(_accel_raw_y, 2);
float accel_z_pow_two = pow(_accel_raw_z, 2);
_accel_x = atan2(_accel_raw_y, _accel_raw_z) * 180 / PI;
_accel_y = atan2(-_accel_raw_x, sqrt(accel_y_pow_two + accel_z_pow_two)) * 180 / PI;
/*
_accel_x =
atan(_accel_raw_y / sqrt(accel_x_pow_two + accel_z_pow_two)) * RAD_TO_DEGREES;
_accel_y = atan(-1.0 * _accel_raw_x / sqrt(accel_y_pow_two + accel_z_pow_two)) *
RAD_TO_DEGREES;
*/
// Convert raw Gyro to degrees/s
_gyro_raw_x *= _ang_rate_to_dps;
_gyro_raw_y *= _ang_rate_to_dps;
_gyro_raw_z *= _ang_rate_to_dps;
_sout << "\nGyro raw x dps: " << _gyro_raw_x << "\n"
<< "Gyro raw y dps: " << _gyro_raw_y << "\n"
<< "Gyro raw z dps: " << _gyro_raw_z << "\n"
<< endl;
// Error correct raw gyro measurements.
_gyro_raw_x += gyro_cal_x;
_gyro_raw_y += gyro_cal_y;
_gyro_raw_z += gyro_cal_z;
_sout << "\nGyro raw x correct: " << _gyro_raw_x << "\n"
<< "Gyro raw y correct: " << _gyro_raw_y << "\n"
<< "Gyro raw z correct: " << _gyro_raw_z << "\n"
<< endl;
_gyro_x += _gyro_raw_x * duration;
_gyro_y += _gyro_raw_y * duration;
_gyro_z += _gyro_raw_z * duration;
_sout << "\nGyro raw x w/o time: " << _gyro_raw_x << "\n"
<< "Gyro raw y w/o time: " << _gyro_raw_y << "\n"
<< "Gyro raw z w/o time: " << _gyro_raw_z << "\n"
<< endl;
_pitch = 0.96 * _gyro_y + 0.04 * _accel_y;
_roll = 0.96 * _gyro_x + 0.04 * _accel_x;
_yaw = _gyro_z;
return true;
}
bool Sensor::setAccelSensitivity(uint8_t sensitivity)
{
if (sensitivity > 3)
sensitivity = 3;
_accel_sensitivity = sensitivity;
uint8_t accel_config = getRegister(SensorRegisters::ACCEL_CONFIG);
if (_status != SensorStatus::OK)
return false;
// No need to write same value
if (((accel_config >> 3) & 3) != _accel_sensitivity)
{
accel_config &= 0xE7;
accel_config |= (_accel_sensitivity << 3);
if (!setRegister(SensorRegisters::ACCEL_CONFIG, accel_config))
return false;
}
// Calculate conversion factor.
_accel_to_g_force = (1 << _accel_sensitivity) * RAW_TO_G_FACTOR;
return true;
}
bool Sensor::setGyroSensitivity(uint8_t sensitivity)
{
if (sensitivity > 3)
sensitivity = 3;
_gyro_sensitivity = sensitivity;
uint8_t gyro_config = getRegister(SensorRegisters::GYRO_CONFIG);
if (_status != SensorStatus::OK)
{
return false;
}
// No need to write same value
if (((gyro_config >> 3) & 3) != _gyro_sensitivity)
{
gyro_config &= 0xE7;
gyro_config |= (_gyro_sensitivity << 3);
if (!setRegister(SensorRegisters::GYRO_CONFIG, gyro_config))
{
return false;
}
}
_ang_rate_to_dps = (1 << _gyro_sensitivity) * RAW_TO_DPS_FACTOR;
return true;
}
float Sensor::getAccelX()
{
return _accel_raw_x;
}
float Sensor::getAccelY()
{
return _accel_raw_y;
}
float Sensor::getAccelZ()
{
return _accel_raw_z;
}
float Sensor::getAngleX()
{
return _accel_x;
}
float Sensor::getAngleY()
{
return _accel_y;
}
float Sensor::getGyroX()
{
return _gyro_raw_x;
}
float Sensor::getGyroY()
{
return _gyro_raw_y;
}
float Sensor::getGyroZ()
{
return _gyro_raw_z;
}
float Sensor::getPitch()
{
return _pitch;
}
float Sensor::getRoll()
{
return _roll;
}
bool Sensor::setRegister(uint8_t reg, uint8_t value)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value);
if (_wire->endTransmission() != 0)
{
_status = SensorStatus::ERR_WRITE;
return false;
}
return true;
}
uint8_t Sensor::getRegister(uint8_t reg)
{
_wire->beginTransmission(_address);
_wire->write(reg);
if (_wire->endTransmission() != 0)
{
_status = SensorStatus::ERR_WRITE;
return 0;
}
uint8_t response_length = _wire->requestFrom(_address, (uint8_t)1);
if (response_length != 1)
{
_status = SensorStatus::ERR_READ;
return 0;
}
return _wire->read();
}
SensorStatus Sensor::getStatus()
{
SensorStatus status = _status;
_status = SensorStatus::OK;
return status;
}
int16_t Sensor::_readHighLow()
{
int16_t high = _wire->read();
int16_t low = _wire->read();
return high << 8 | low;
}
|