-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLE_IMU.cpp
61 lines (53 loc) · 1.6 KB
/
BLE_IMU.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
#include <MahonyAHRS.h>
#include "BLE_IMU.h"
#include <Wire.h>
#include "MSP.h"
#include "Config.h"
float IMURoll,IMUPitch,IMUHead; // cooked IMU data
float Acc[3], Gyro[3], Mag[3]; // raw IMU data
float throttleCorrection;
Mahony filter;
MyBoschSensor myIMU(Wire);
void read_data() {
if (myIMU.accelerationAvailable()){
myIMU.readAcceleration(Acc[0], Acc[1], Acc[2]);
}
if (myIMU.gyroscopeAvailable()){
myIMU.readGyroscope(Gyro[0], Gyro[1], Gyro[2]);
}
if (myIMU.magneticFieldAvailable()){
myIMU.readMagneticField(Mag[0], Mag[1], Mag[2]);
}
}
void initIMU(){
//callback function to read data from
myIMU.onInterrupt(read_data);
myIMU.begin();
filter.begin(100); // filter to expect 100 measurements per second
}
void calcIMU(){
// TODO: calibrate the Bosh gyroscope for scaling
// Update the Mahony filter, with scaled gyroscope
float gyroScale = 0.001; // TODO: the filter updates too fast
Gyro[0] = Gyro[0] * gyroScale;
Gyro[1] = Gyro[1] * gyroScale;
Gyro[2] = Gyro[2] * gyroScale;
filter.update(Gyro[0],Gyro[1],Gyro[2], Acc[0], Acc[1], Acc[2], Mag[0], Mag[1], Mag[2]);
IMURoll = filter.getRoll();
IMUPitch = filter.getPitch();
IMUHead = filter.getYaw();
Serial.print("IMURoll:");
Serial.println(IMURoll);
Serial.print("IMUPitch:");
Serial.println(IMUPitch);
Serial.print("IMUHead:");
Serial.println(IMUHead);
}
//---------------------
void writeMSP_ATTITUDE() {
mspWriteStart(MSP_ATTITUDE);
mspWriteWord((int16_t)(IMURoll*10));
mspWriteWord((int16_t)(IMUPitch*10));
mspWriteWord((int16_t)(IMUHead));
mspWriteEnd();
}