-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlane balancing system with 2 DOF.ino
144 lines (103 loc) · 2.31 KB
/
Plane balancing system with 2 DOF.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
#include <Servo.h>
#include <PID_v1.h>
#include "MegunoLink.h"
#include "RunningAverage.h"
//Touch panel filter (Running Average or Moving Average filter)
RunningAverage myRAX(6);
RunningAverage myRAY(6);
int plotCounter = 0;
int Xnew, Ynew;
int xServoPin = 3;
int yServoPin = 5;
//Reference positions or desired positions
double xRef = 500;
double yRef = 500;
//Parameters of PID controller
43
double kpx = 0.04;
double kix = 0.018;
double kdx = 0.018;
double kpy = 0.03;
double kiy = 0.022;
double kdy = 0.018;
double xInput, yInput;
double xOutput, yOutput;
//Sampling time*******
int Ts = 50;
XYPlot MyPlot;
TimePlot MyYTimePlot;
TimePlot MyXTimePlot;
PID xPID(&xInput, &xOutput, &xRef, kpx, kix, kdx, DIRECT);
PID yPID(&yInput, &yOutput, &yRef, kpy, kiy, kdy, DIRECT);
Servo xServo, yServo;
44
void setup() {
xServo.attach(xServoPin);
yServo.attach(yServoPin);
xOutput = 90;
yOutput = 90;
Serial.begin(9600);
xPID.SetMode(AUTOMATIC);
xPID.SetOutputLimits(60, 120);
xPID.SetSampleTime(Ts);
yPID.SetMode(AUTOMATIC);
yPID.SetOutputLimits(60, 120);
yPID.SetSampleTime(Ts);
xServo.write(xOutput);
yServo.write(yOutput);
delay(5000);
}
45
void loop() {
Xnew = getX();
myRAX.addValue(Xnew);
xInput = myRAX.getAverage();
xPID.Compute();
xServo.write(180 - xOutput);
Ynew = getY();
myRAY.addValue(Ynew);
yInput = myRAY.getAverage();
yPID.Compute();
yServo.write(180 - yOutput);
//Plot values
if(plotCounter == 100){
plotCounter = 0;
//MyPlot.SendData("SetXY", xRef, yRef);
MyPlot.SendData("Filter", xInput, yInput);
MyPlot.SendData("Original", Xnew, Ynew);
//MyTimePlot.SendData(F("Ori"), Ynew);
//MyYTimePlot.SendData(F("FilY"), yInput);
//MyYTimePlot.SendData(F("SetY"), yRef);
46
//MyXTimePlot.SendData(F("FilX"), xInput);
//MyXTimePlot.SendData(F("SetX"), xRef);
}
plotCounter++;
}
int getX(){
int X;
pinMode(A0, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, INPUT);
pinMode(A1, INPUT);
digitalWrite(A0, HIGH);
digitalWrite(A2, LOW);
delay(1);
X = analogRead(A3);
delay(1);
return X;
47
}
int getY(){
int Y;
pinMode(A0, INPUT);
pinMode(A2, INPUT);
pinMode(A3, OUTPUT);
pinMode(A1, OUTPUT);
digitalWrite(A3, HIGH);
digitalWrite(A1, LOW);
delay(1);
Y = analogRead(A0);
delay(1);
return Y;
}