-
Notifications
You must be signed in to change notification settings - Fork 0
/
Github_Code.ino
298 lines (259 loc) · 8.24 KB
/
Github_Code.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Define sensor pins
#define PIN_S1 A0
#define PIN_S2 A1
#define PIN_S3 A2
#define PIN_S4 A3
#define PIN_S5 A4
#define PIN_S6 A5
#define PIN_S7 A6
#define PIN_S8 A7
// Define motor driver pins
const int IN1 = 9;
const int IN2 = 8;
const int IN3 = 2;
const int IN4 = 3;
const int EN_A = 5;
const int EN_B = 6 ;
int count_KananKiri,count_Kanan,count_Kiri,troli_status=0,count_step=0,t;
// Variables for line following and PID control
const int trigger = 400;
int SpeedR ;
int SpeedL ; //selisih 10 sama SpeedR=
int sRRR, sRR, sR, sCR, sCL, sL, sLL, sLLL ;
int lineData, NilaiPosisi;
double Error = 0, SumError = 0, LastError = 0;
double BasePWM = 200;
double MaxSpeed = 200;
double MinSpeed = -200;
double Kp = 40;
double Ki = 0;
double Kd = 140 ;
double Ts = 0.5;
double outPID;
void setup()
{
Serial.begin(9600);
pinMode(IN1, OUTPUT); //IN1 A
pinMode(IN2, OUTPUT); //IN2 A
pinMode(IN3, OUTPUT); //IN1 B
pinMode(IN4, OUTPUT); //IN2 B
pinMode(EN_A, OUTPUT); //EN A
pinMode(EN_B, OUTPUT); //EN B
}
// Function to read sensor values
void sensor() {
sRRR = analogRead(PIN_S1);
sRR = analogRead(PIN_S2);
sR = analogRead(PIN_S3);
sCR = analogRead(PIN_S4);
sCL = analogRead(PIN_S5);
sL = analogRead(PIN_S6);
sLL = analogRead(PIN_S7);
sLLL = analogRead(PIN_S8);
// Convert sensor values to binary (1 or 0)
// based on a defined trigger value
if (sRRR <= trigger) {
sRRR = 1;
} else {
sRRR = 0;
}
if (sRR <= trigger) {
sRR = 1;
} else {
sRR = 0;
}
if (sR <= trigger) {
sR = 1;
} else {
sR = 0;
}
if (sCR <= trigger) {
sCR = 1;
} else {
sCR = 0;
}
if (sLLL <= trigger) {
sLLL = 1;
} else {
sLLL = 0;
}
if (sLL <= trigger) {
sLL = 1;
} else {
sLL = 0;
}
if (sL <= trigger) {
sL = 1;
} else {
sL = 0;
}
if (sCL <= trigger) {
sCL = 1;
} else {
sCL = 0;
}
}
// Function to control motors
void motor(int speedL, int speedR) {
if (speedL < 0)
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(EN_A, -speedL);
}
else if (speedL > 0)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EN_A, speedL);
}
else
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
if (speedR < 0)
{
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EN_B, -speedR);
}
else if (speedR > 0)
{
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(EN_B, speedR);
}
else
{
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
}
// Function to determine the current position error
void error() {
// Determine position error based on sensor values
if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 0 && sCR == 0 && sR == 0 && sRR == 0 && sRRR == 1) {
NilaiPosisi = -10;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 0 && sCR == 0 && sR == 0 && sRR == 1 && sRRR == 1) {
NilaiPosisi = -9;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 0 && sCR == 0 && sR == 1 && sRR == 1 && sRRR == 1) {
NilaiPosisi = -8;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 0 && sCR == 0 && sR == 0 && sRR == 1 && sRRR == 0) {
NilaiPosisi = -7;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 0 && sCR == 0 && sR == 1 && sRR == 1 && sRRR == 0) {
NilaiPosisi = -6;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 0 && sCR == 1 && sR == 1 && sRR == 1 && sRRR == 0) {
NilaiPosisi = -5;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 0 && sCR == 0 && sR == 1 && sRR == 0 && sRRR == 0) {
NilaiPosisi = -4;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 0 && sCR == 1 && sR == 1 && sRR == 0 && sRRR == 0) {
NilaiPosisi = -3;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 1 && sCR == 1 && sR == 1 && sRR == 0 && sRRR == 0) {
NilaiPosisi = -2;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 0 && sCR == 1 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = -1;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 1 && sCR == 1 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 0;
} else if (sLLL == 1 && sLL == 1 && sL == 1 && sCL == 1 && sCR == 1 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 0;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 1 && sCR == 1 && sR == 1 && sRR == 1 && sRRR == 1) {
NilaiPosisi = 0;
} else if (sLLL == 1 && sLL == 1 && sL == 1 && sCL == 1 && sCR == 1 && sR == 1 && sRR == 1 && sRRR == 1) {
NilaiPosisi = 0;
} else if (sLLL == 0 && sLL == 0 && sL == 0 && sCL == 1 && sCR == 0 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 1;
} else if (sLLL == 0 && sLL == 0 && sL == 1 && sCL == 1 && sCR == 1 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 2;
} else if (sLLL == 0 && sLL == 0 && sL == 1 && sCL == 1 && sCR == 0 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 3;
} else if (sLLL == 0 && sLL == 0 && sL == 1 && sCL == 0 && sCR == 0 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 4;
} else if (sLLL == 0 && sLL == 1 && sL == 1 && sCL == 1 && sCR == 0 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 5;
} else if (sLLL == 0 && sLL == 1 && sL == 1 && sCL == 0 && sCR == 0 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 6;
} else if (sLLL == 0 && sLL == 1 && sL == 0 && sCL == 0 && sCR == 0 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 7;
} else if (sLLL == 1 && sLL == 1 && sL == 1 && sCL == 0 && sCR == 0 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 8;
} else if (sLLL == 1 && sLL == 1 && sL == 0 && sCL == 0 && sCR == 0 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 9;
} else if (sLLL == 1 && sLL == 0 && sL == 0 && sCL == 0 && sCR == 0 && sR == 0 && sRR == 0 && sRRR == 0) {
NilaiPosisi = 10;
}
}
// PID control function
void PID() {
error();
int SetPoint = 0;
Error = SetPoint - NilaiPosisi ;
double DeltaError = Error - LastError ;
SumError += LastError ;
double P = Kp * Error ;
double I = Ki * SumError * Ts ;
double D = ((Kd / Ts) * DeltaError);
LastError = Error;
outPID = P + I + D ;
double motorKi = BasePWM - outPID ;
double motorKa = BasePWM + outPID ;
if (motorKi > MaxSpeed)motorKi = MaxSpeed;
if (motorKi < MinSpeed)motorKi = MinSpeed;
if (motorKa > MaxSpeed)motorKa = MaxSpeed;
if (motorKa < MinSpeed)motorKa = MinSpeed;
motor(motorKi + 10, motorKa);
}
// Function to display sensor values for debugging
void Display()
{
Serial.print("sLLL"); Serial.print("-"); //Most Left Sensor
Serial.print("sLL"); Serial.print("-");
Serial.print("sL"); Serial.print("-");
Serial.print("sCL"); Serial.print("-"); //Middle Sensor
Serial.print("sCR"); Serial.print("-"); //Middle Sensor
Serial.print("sR"); Serial.print("-");
Serial.print("sRR"); Serial.print("-");
Serial.print("sRRR"); Serial.print("-"); //Most Right Sensor
Serial.println(" ");
Serial.print(sLLL); Serial.print("-"); //Most Left Sensor
Serial.print(sLL); Serial.print("-");
Serial.print(sL); Serial.print("-");
Serial.print(sCL); Serial.print("-"); //Middle Sensor
Serial.print(sCR); Serial.print("-"); //Middle Sensor
Serial.print(sR); Serial.print("-");
Serial.print(sRR); Serial.print("-");
Serial.print(sRRR); Serial.print("-"); //Most Right Sensor
Serial.println(" ");
}
void loop()
{
if(sLLL == 0 && sLL == 0 && sL == 0 && sCL == 1 && sCR == 1 && sR == 1 && sRR == 1 && sRRR == 1 && count_step==0 ){
motor(-100,100);
delay(600);
for(t=2250 ; t>=0 ; t--){
sensor();
PID();
}
count_step++;
}
if(sLLL == 1 && sLL == 1 && sL == 1 && sCL == 1 && sCR == 1 && sR == 1 && sRR == 1 && sRRR == 1 && count_step==1){
motor(100,-100);
delay(700);
for(t=200 ; t>=0 ; t--){
sensor();
PID();
}
count_step++;
}
if(count_step==2){
motor(-100,-120);
delay(2000);
motor(0,0);
delay(2000);
//servo
count_step++;
}
sensor();
PID();
Serial.println(count_Kanan);
}