-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimu.cpp
More file actions
29 lines (23 loc) · 698 Bytes
/
imu.cpp
File metadata and controls
29 lines (23 loc) · 698 Bytes
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
// imu.cpp
#include "imu.h"
#include "tca9548a.h"
Adafruit_MPU6050 mpu;
bool imuInit() {
tcaSelect(0); // channel 0 for IMU
if (!mpu.begin()) return false;
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
return true;
}
ImuData imuRead() {
ImuData data;
sensors_event_t a, g, temp;
tcaSelect(0);
mpu.getEvent(&a, &g, &temp);
data.pitch = atan2(a.acceleration.y, a.acceleration.z) * 180 / PI;
data.roll = atan2(-a.acceleration.x,
sqrt(a.acceleration.y * a.acceleration.y +
a.acceleration.z * a.acceleration.z)) * 180 / PI;
return data;
}