-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gyro.cpp
159 lines (122 loc) · 3.15 KB
/
Gyro.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
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
/*
* Gyro.cpp
*
* Created on: 29 mar 2017
* Author: radoslawkubera
*/
#include "Arduino.h"
#include "Gyro.h"
#include "GlobalDefines.h"
#include "libraries/MPU6050/MPU6050.h"
#include "libraries/AutoSerial/AutoSerial.h"
extern MPU6050 mpu;
extern char rollOrientation;
extern char pitchOrientation;
extern char yawOrientation;
extern AutoSerial debugSerial;
//Gyro values
/**************************************************
* Gyro functions
* ************************************************/
void initGyro() {
//MPU6050
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
delay(500);
}
//disable MPU Low Pass Filter
mpu.setDLPFMode(MPU6050_DLPF_6);
debugSerial.println (F("MPU6050 ready"));
mpu.calibrateGyro();
}
void getRollPitch (float &my_roll, float &my_pitch) {
//static float lastRoll, lastPitch;
static uint32_t timer, tmp_timer = 0;
float accX, accY, accZ;
float gyroX, gyroY /*, gyroZ*/;
static float compAngleX, compAngleY; // Calculated angle using a Complimentary filter
static float gyroXangle, gyroYangle; // Angle calculate using the gyro only
Vector rawAccel = mpu.readRawAccel();
Vector rawGyro = mpu.readRawGyro();
if (rollOrientation==2) {
accX = rawAccel.YAxis;
gyroX = rawGyro.YAxis;
}
else if (rollOrientation==3) {
accX = rawAccel.ZAxis;
gyroX = rawGyro.ZAxis;
}
else {
accX = rawAccel.XAxis;
gyroX = rawGyro.XAxis;
}
if (pitchOrientation==2) {
accY = rawAccel.YAxis;
gyroY = rawGyro.YAxis;
}
else if (pitchOrientation==3) {
accY = rawAccel.ZAxis;
gyroY = rawGyro.ZAxis;
}
else {
accY = rawAccel.XAxis;
gyroY = rawGyro.XAxis;
}
if (yawOrientation==2) {
accZ = rawAccel.YAxis;
//gyroZ = rawGyro.YAxis;
}
else if (yawOrientation==3) {
accZ = rawAccel.ZAxis;
//gyroZ = rawGyro.ZAxis;
}
else {
accZ = rawAccel.XAxis;
//gyroZ = rawGyro.XAxis;
}
float calc_pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
float calc_roll = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
float dt = (float)(micros() - timer) / 1000000; // Calculate delta time
tmp_timer = micros();
if (dt>10000 || dt<=0) {
gyroXangle = calc_roll;
gyroYangle = calc_pitch;
compAngleX = calc_roll;
compAngleY = calc_pitch;
//lastRoll = calc_roll;
//lastPitch = calc_pitch;
my_roll = calc_roll;;
my_pitch = calc_pitch;
timer = tmp_timer;
}
else {
float gyroXrate = gyroX / 131.0; // Convert to deg/s
float gyroYrate = gyroY / 131.0; // Convert to deg/s
gyroXangle += gyroXrate * dt;
gyroYangle += gyroYrate * dt;
float tau=0.02;
float a=0.0;
a=tau/(tau+dt);
compAngleX = a * (compAngleX + gyroXrate * dt) + (1-a) * calc_roll; // Calculate the angle using a Complimentary filter
compAngleY = a * (compAngleY + gyroYrate * dt) + (1-a) * calc_pitch;
/*
if (isnan(compAngleX)) {
compAngleX = lastRoll;
}
if (isnan(compAngleY)) {
compAngleY = lastPitch;
}
if (isnan(gyroXangle)) {
gyroXangle = lastRoll;
}
if (isnan(gyroYangle)) {
gyroYangle = lastPitch;
}
my_roll = lastRoll = compAngleX;
my_pitch = lastPitch = compAngleY;
*/
my_roll = compAngleX;
my_pitch = compAngleY;
timer = tmp_timer;
}
}