-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalancingbicopter-teamproj.ino
261 lines (225 loc) · 6.49 KB
/
balancingbicopter-teamproj.ino
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
#include <Wire.h>
#include <Servo.h>
Servo right_prop;
Servo left_prop;
/////SETTINGS
#define IMU_CALIBRATE true
/////IMU mpu6050
#define MPU 0x68
#define ACCEL 0x3B
#define GYRO 0x43
float temp;
float AccRawX1, AccRawY1, AccRawZ1, GyRawX1, GyRawY1, GyRawZ1;
float AccRawX2, AccRawY2, AccRawZ2, GyRawX2, GyRawY2, GyRawZ2;
float AccAngleX, AccAngleY, AccAngleZ, GyAngleX, GyAngleY, GyAngleZ;
float roll, pitch, yaw;
float mpuTimer, mpuDTime;
//--calibration
float AccRawXc, AccRawYc, AccRawZc, GyRawXc, GyRawYc, GyRawZc;
/////time
// float tim3;
// float elapsedTime;
// unsigned long kalTime;
/////kalman
float kalmanR;
float kalmanP;
float Xt, Xt_update, Xt_prev;
float Pt, Pt_update, Pt_prev;
float Kt;
float R, Q;
float elapsedTimes, time, timePrev;
int i;
float PID, pwmLeft, pwmRight, error, previous_error;
float pid_p = 0;
float pid_i = 0;
float pid_d = 0;
/////////////////PID CONSTANTS/////////////////
double kp = 3.4; //3.55
double ki = 0.0000000001; //0.003
double kd = 2.72; //2.05
double throttleR = 1150;
double throttleL = 1290; //initial value of throttle to the motors
float desired_angle = 0;
void setup() {
mpu6050_setup();
calibrate_mpu6050();
Serial.begin(19200); //memulai komunikasi baudrate/s
R = 10;
Q = 0.1;
Pt_prev = 1;
// put your setup code here, to run once:
time = millis();
left_prop.attach(10);
right_prop.attach(3);
left_prop.writeMicroseconds(2000);
right_prop.writeMicroseconds(2000);
delay(2000);
left_prop.writeMicroseconds(1000);
right_prop.writeMicroseconds(1000);
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
calculate_angle();
kalmanR = kalman_filter(roll);
kalmanP = kalman_filter(pitch);
timePrev = time;
time = millis();
elapsedTimes = (time - timePrev) / 1000;
error = desired_angle - kalmanP;
pid_p = kp * error;
if (-3 < error < 3) {
pid_i = pid_i + (ki * error);
}
pid_d = kd * ((error - previous_error) / elapsedTimes);
PID = pid_p + pid_i + pid_d;
if (PID < -1000) {
PID = -1000;
}
if (PID > 1000) {
PID = 1000;
}
pwmLeft = throttleL + PID;
pwmRight = throttleR - PID;
if (pwmRight < 1150) {
pwmRight = 1150;
}
if (pwmRight > 1475) {
pwmRight = 1475;
}
//Left
if (pwmLeft < 1290) {
pwmLeft = 1290;
}
if (pwmLeft > 1500) {
pwmLeft = 1500;
}
left_prop.writeMicroseconds(pwmLeft);
right_prop.writeMicroseconds(pwmRight);
previous_error = error;
// Serial.print(" \tPitch : ");
// Serial.print(pitch);
// Serial.print(" \tkalmanP");
// Serial.print(kalmanP);
// Serial.println(" ");
Serial.print(" \terror : ");
Serial.print(error);
Serial.print(" \tkalmanP : ");
Serial.print(kalmanP);
Serial.print(" \tPWM RIGHT : ");
Serial.print(pwmRight);
Serial.print(" \tPWM LEFT : ");
Serial.print(pwmLeft);
Serial.print(" \tPID : ");
Serial.print(PID);
Serial.println(" ");
}
void mpu6050_setup() {
Wire.beginTransmission(MPU);
Wire.write(MPU);
Wire.write(0b00000111); //reset
Wire.endTransmission();
//mpu reset
Wire.beginTransmission(0x68);
Wire.write(MPU);
Wire.write(0b00000000); //aktif kembali
Wire.endTransmission();
// lpf 184Hz acc, 188Hz gyr
Wire.beginTransmission(0x68);
Wire.write(0x1A);
Wire.write(0b00000001);
Wire.endTransmission();
// powe management / reset
Wire.beginTransmission(MPU);
Wire.write(0x6B); //manggil dulu
Wire.write(0x00); //nge-0-in semuanya
Wire.endTransmission();
// acelero hpf
Wire.beginTransmission(MPU);
Wire.write(0x1C);
Wire.write(0b00000111); //akses dhpf
Wire.endTransmission();
// gyro
Wire.beginTransmission(MPU);
Wire.write(0x1B);
Wire.write(0x00);
Wire.endTransmission();
}
void read_mpu6050_accel() {
Wire.beginTransmission(MPU);
Wire.write(ACCEL);
Wire.endTransmission();
Wire.requestFrom(MPU, 8);
AccRawX1 = Wire.read() << 8 | Wire.read(); //high and low accel data x
AccRawY1 = Wire.read() << 8 | Wire.read(); //high and low accel data y
AccRawZ1 = Wire.read() << 8 | Wire.read(); //high and low accel data z
// temp = Wire.read() << 8 | Wire.read();
}
void read_mpu6050_gyro() {
Wire.beginTransmission(MPU);
Wire.write(GYRO);
Wire.endTransmission();
Wire.requestFrom(MPU, 6);
GyRawX1 = Wire.read() << 8 | Wire.read();
GyRawY1 = Wire.read() << 8 | Wire.read();
GyRawZ1 = Wire.read() << 8 | Wire.read();
}
void calibrate_mpu6050() {
Serial.print("calibrating");
for (int i = 0; i < 2000; i++) {
read_mpu6050_accel();
read_mpu6050_gyro();
AccRawXc += AccRawX1;
AccRawYc += AccRawY1;
AccRawZc += AccRawZ1;
GyRawXc += GyRawX1;
GyRawYc += GyRawY1;
GyRawZc += GyRawZ1;
if (i % 100 == 0) Serial.print(".");
}
AccRawXc /= 2000; //ratarata
AccRawYc /= 2000;
AccRawZc /= 2000;
GyRawXc /= 2000;
GyRawYc /= 2000;
GyRawZc /= 2000;
Serial.println(AccRawXc);
Serial.println(AccRawYc);
Serial.println(AccRawZc);
}
void calculate_angle() {
read_mpu6050_accel();
// AccRawX2 = (AccRawX1 + (-1 * AccRawXc)) / 8192.0;
// AccRawY2 = (AccRawY1 + (-1 * AccRawYc)) / 8192.0;
// AccRawZ2 = (AccRawZ1 + (8192 - AccRawZc)) / 8192.0;
AccRawX2 = (AccRawX1 + (-1 * AccRawXc)) / 16384.0;
AccRawY2 = (AccRawY1 + (-1 * AccRawYc)) / 16384.0;
AccRawZ2 = (AccRawZ1 + (16384 - AccRawZc)) / 16384.0;
AccAngleX = (atan2(AccRawX2, AccRawZ2)) * 57.2957795;
AccAngleY = (atan2(AccRawY2, AccRawZ2)) * 57.2957795;
for (int i = 0; i < 250; i++) { //dapet data stabil
mpuDTime = (millis() - mpuTimer) / 1000; //beda selisih utk dpt time state
read_mpu6050_gyro();
// GyRawX2 = GyRawX1 / 65.5;
// GyRawY2 = GyRawY1 / 65.5;
GyRawX2 = GyRawX1 / 131.0;
GyRawY2 = GyRawY1 / 131.0;
// GyRawZ2 = (GyRawZ1 + (-1 * GyRawXc)) / 65.5;
GyAngleX += GyRawX2 * mpuDTime; //dpt nilai sudut gyro
GyAngleY += GyRawY2 * mpuDTime;
}
GyAngleX = (GyAngleX + (-1 * GyRawXc)) / 250;
GyAngleY = (GyAngleY + (-1 * GyRawYc)) / 250;
roll = 0.9998 * AccAngleX + 0.0002 * GyAngleX;
pitch = 0.9998 * AccAngleY + 0.0002 * GyAngleY;
}
float kalman_filter(float data) {
Xt_update = Xt_prev;
Pt_update = Pt_prev + Q;
Kt = Pt_update / (Pt_update + R);
Xt = Xt_update + (Kt * (data - Xt_update));\
Pt = (1 - Kt) * Pt_update;
Xt_prev = Xt;
Pt_prev = Pt;
return Xt;
}