-
Notifications
You must be signed in to change notification settings - Fork 0
/
Self_BalanceRobot_v1.ino
114 lines (93 loc) · 2.21 KB
/
Self_BalanceRobot_v1.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
#include <Arduino.h>
#include <TinyMPU6050.h>
#include <PID_v1.h>
//MPU var
MPU6050 mpu (Wire);
int Y;
//PID
#define PID_MIN_LIMIT -255
#define PID_MAX_LIMIT 255
#define PID_SAMPLE_TIME_IN_MILLI 10
double Setpoint, Input, Output;
double Kp=10, Ki=0, Kd=0.02;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
//Motor
unsigned const int EN1=3;
unsigned const int IN11=5;
unsigned const int IN12=6;
unsigned const int EN2=9;
unsigned const int IN21=8;
unsigned const int IN22=7;
int min_motorSpeed = 25;
void motorrotation(int motors/*,motors2*/){
if(motors < 0){
digitalWrite(IN11, HIGH);
digitalWrite(IN12, LOW);
digitalWrite(IN21, HIGH);
digitalWrite(IN22, LOW);
}
else if(motors >= 0){
digitalWrite(IN11, LOW);
digitalWrite(IN12, HIGH);
digitalWrite(IN21, LOW);
digitalWrite(IN22, HIGH);
}
/*if(motors2 < 0){
digitalWrite(IN21, HIGH);
digitalWrite(IN22, LOW);
}
else if(motors2 >= 0){
digitalWrite(IN21, LOW);
digitalWrite(IN22, HIGH);
}*/
motors = abs(motors) + min_motorSpeed;
//motors2 = abs(motors2) + min_motorSpeed;
motors = constrain(motors, min_motorSpeed, 255);
//motors2 = constrain(motors2, min_motorSpeed, 255);
analogWrite(EN1,motors);
analogWrite(EN2, motors);
}
void setup() {
//MPU
mpu.Initialize();
Serial.begin(115200);
mpu.Calibrate();
mpu.Execute();
Y = mpu.GetAngY();
// ءMotor setup
pinMode(IN11, OUTPUT);
pinMode(IN12, OUTPUT);
pinMode(IN21, OUTPUT);
pinMode(IN22, OUTPUT);
/* digitalWrite(IN11, HIGH);
digitalWrite(IN12, LOW);
digitalWrite(IN21, HIGH);
digitalWrite(IN22, LOW);
*/
pinMode(EN1, OUTPUT);
pinMode(EN1, OUTPUT);
motorrotation(0);
//PID
myPID.SetOutputLimits(PID_MIN_LIMIT, PID_MAX_LIMIT);
Input = Y;
Setpoint = 0;
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(PID_SAMPLE_TIME_IN_MILLI);
}
void loop() {
// put your main code here, to run repeatedly:
mpu.Execute();
Y = mpu.GetAngY();
Input = Y;
Setpoint = 0;
myPID.SetMode(AUTOMATIC);
myPID.Compute();
motorrotation(Output);
// Debugging prints
Serial.print("Angle: ");
Serial.print(Y);
Serial.print(" Output: ");
Serial.println(Output);
delay(10);
//delay(PID_SAMPLE_TIME_IN_MILLI);
}