-
Notifications
You must be signed in to change notification settings - Fork 2
/
PID_V10.ino
421 lines (405 loc) · 8.19 KB
/
PID_V10.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include<Wire.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#define FORWARD_RM 11 // RIGHT MOTORS FORWARD MOVEMENT
#define FORWARD_LM 6 // LEFT MOTORS FORWARD MOVEMENT
#define BACKWARD_RM 10 // RIGHT MOTORS BACKWARD MOVEMENT
#define BACKWARD_LM 9 // LEFT MOTORS BACKWARD MOVEMENT
#define BUTTON 4 // ON/OFF BUTTON
#define BUZZER 12 // BACKGROUND MUSIC
#define SERVO 5 //SERVO ARM OBSTECALE REMOVE
#define USS_TRIG 13 //ULTRASONIC SENSOR SIGNAL TRANSFER PIN
#define USS_ECHO 8 //ULTRASONIC SENSOR SIGNAL RECIVE PIN
#define encoderpin_1 2 //ENCODER "INTTERUPT" PIN LEFT
#define encoderpin_2 3 //ENCODER "INTTERUPT" PIN RIGHT
volatile int encoderCount = 0;
volatile int encoderCount1 = 0;
int IR[5] = { A0, A1, A2, A3, 7 };
int IR_READ[5],speed=125,currentError,prevError,Flag=1,mspeed=150,Mode,setdistance=10,POS=0,holes=20;
String serial_input= "";
float kp=110,kd=1000,ki=10,distance,prevTime=0;
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
byte HEART[8] ={
0b00001010,
0b00010101,
0b00010001,
0b00001010,
0b00000100,
0b00000000,
0b00000000,
0b00000000
}; //heart shape character
void getSerial()
{
while (Serial.available())
{
char x=Serial.read();
serial_input +=x;
delay(2);
}
if (serial_input.length()>0)
{
movement();
getpidvalue();
serial_input= "";
}
}
void movement()
{
switch(serial_input[0]){
case'a':
Mode=1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Line Follower");
break;
case'm':
Mode=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Manual");
break;
case'q':
Flag=0;
break;
case'f':
Forward();
break;
case'l':
Left();
break;
case'r':
Right();
break;
case'b':
Backward();
break;
case's':
STOP();
break;
case'v':
STOP();
Flag=1;
break;
case'o':
serial_input.remove(0,1);
mspeed=serial_input.toInt();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("speed=");
lcd.print(mspeed);
break;
case'h':
digitalWrite(BUZZER,HIGH);
break;
case'j':
digitalWrite(BUZZER,LOW);
break;
}
}
void getpidvalue()
{
if(serial_input.substring(0,1)=="k")
{
if(serial_input.substring(0,2)=="kp")
{serial_input.remove(0,2);
kp=serial_input.toFloat();
}
if(serial_input.substring(0,2)=="ki")
{serial_input.remove(0,2);
ki=serial_input.toFloat();
}
if(serial_input.substring(0,2)=="kd")
{serial_input.remove(0,2);
kd=serial_input.toFloat();
}
if(serial_input.substring(0,2)=="ks")
{serial_input.remove(0,2);
speed=serial_input.toInt();
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("kp=");
lcd.print(kp,4);
lcd.setCursor(0,1);
lcd.print("ki=");
lcd.print(ki,4);
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("kd=");
lcd.print(kd);
lcd.setCursor(0,1);
lcd.print("speed=");
lcd.print(speed);
delay(1000);
lcd.clear();
}
}
void Forward()
{
analogWrite(FORWARD_RM, mspeed);
analogWrite(FORWARD_LM, mspeed);
}
void Backward()
{
analogWrite(BACKWARD_RM, mspeed);
analogWrite(BACKWARD_LM, mspeed);
}
void Right()
{
analogWrite(BACKWARD_RM, mspeed);
analogWrite(FORWARD_LM, mspeed);
}
void Left()
{
analogWrite(FORWARD_RM, mspeed);
analogWrite(BACKWARD_LM, mspeed);
}
void STOP()
{
analogWrite(FORWARD_RM, LOW);
analogWrite(FORWARD_LM, LOW);
analogWrite(BACKWARD_RM, LOW);
analogWrite(BACKWARD_LM, LOW);
}
//OBSTACLE DISTANCE DETECTION
void USS()
{
float DURATION, DISTANCE;
digitalWrite(USS_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(USS_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(USS_TRIG, LOW);
DURATION = pulseIn(USS_ECHO, HIGH,5000);
if (DURATION>0)
distance = (DURATION * 0.0343) / 2;
else
distance = 60;
//delay(100);
}
void FollowLine()
{int rspeed,lspeed,I,D,PIDOut;
readIR();
error();
if(currentError==9)
{
STOP();
Flag=1;
}
else{
I=currentError+prevError;
D=currentError-prevError;
prevError=currentError;
PIDOut=kp*currentError+ki*I+kd*D;
rspeed=speed-PIDOut;
lspeed=speed+PIDOut;
if(rspeed>=0)
{
digitalWrite(BACKWARD_RM,LOW);
analogWrite(FORWARD_RM,constrain(rspeed,0,180));
}
else
{
digitalWrite(FORWARD_RM,LOW);
analogWrite(BACKWARD_RM,constrain(-rspeed,0,180));
}
if(lspeed>=0)
{
digitalWrite(BACKWARD_LM,LOW);
analogWrite(FORWARD_LM,constrain(lspeed,0,180));
}
else
{
digitalWrite(FORWARD_LM,LOW);
analogWrite(BACKWARD_LM,constrain(-lspeed,0,180));
}
}
}
void readIR()
{
for (int i = 0; i < 5; i++)
IR_READ[i] = digitalRead(IR[i]); //STORE IR READINGS IN ARRAY
}
void error()
{
switch(IR_READ[0]*16 + IR_READ[1]*8 + IR_READ[2]*4 + IR_READ[3]*2 + IR_READ[4]*1){
case 16:
currentError=6;
break;
case 24:
currentError=5;
break;
case 30:
currentError=4;
break;
case 28:
currentError=3;
break;
case 29:
currentError=2;
break;
case 25:
currentError=1;
break;
case 27:
currentError=0;
break;
case 19:
currentError=-1;
break;
case 23:
currentError=-2;
break;
case 7:
currentError=-3;
break;
case 15:
currentError=-4;
break;
case 3:
currentError=-5;
break;
case 1:
currentError=-6;
break;
}
}
void servocnt()
{
for(int i=0;i<50;i++)
{
digitalWrite(SERVO,HIGH);
delayMicroseconds(1000);
digitalWrite(SERVO,LOW);
delayMicroseconds(19500);
}
for(int i=0;i<50;i++)
{
digitalWrite(SERVO,HIGH);
delayMicroseconds(2500);
digitalWrite(SERVO,LOW);
delayMicroseconds(17500);
}
}
void ENCODER_RPM()
{
// Calculate RPM every second
float currentTime = millis();
if (currentTime - prevTime >= 1000)
{
// update the previous time
prevTime = currentTime;
// Calculate RPM
float rpm1 = (float)encoderCount * (60.0 / (float)holes);
float rpm2 = (float)encoderCount1 * (60.0 / (float)holes);
// Display RPM on LCD
lcd.setCursor(5, 1);
lcd.print(" "); // Clear previous value
lcd.setCursor(0, 1);
lcd.print("RPM:");
lcd.setCursor(5, 1);
lcd.print(rpm1, 1);
lcd.setCursor(11, 1);
lcd.print(" "); // Clear previous value
lcd.setCursor(11, 1);
lcd.print(rpm2, 1);
// Reset the encoder count
encoderCount1 = 0;
encoderCount = 0;
}
}
void encoderISR_1()
{
encoderCount ++;
}
void encoderISR_2()
{
encoderCount1 ++;
}
void setup()
{ Serial.begin(9600);
pinMode(FORWARD_RM, OUTPUT);
pinMode(FORWARD_LM, OUTPUT);
pinMode(BACKWARD_RM, OUTPUT);
pinMode(BACKWARD_LM, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
pinMode(USS_TRIG, OUTPUT);
pinMode(USS_ECHO, INPUT);
pinMode(SERVO, OUTPUT);
pinMode(encoderpin_1, INPUT);
pinMode(encoderpin_2, INPUT);
attachInterrupt(digitalPinToInterrupt(encoderpin_1), encoderISR_1, RISING);
attachInterrupt(digitalPinToInterrupt(encoderpin_2), encoderISR_2, RISING);
lcd.init(); // initialize the lcd
lcd.backlight();
//LCD CODE START
lcd.begin(16, 2);
lcd.createChar(0,HEART);
lcd.setCursor(3, 0);
lcd.print("FARES SHERIF");
lcd.setCursor(4,1);
lcd.print("20106051");
delay(500);
for (int i = 0; i < 16; i++) {
lcd.scrollDisplayLeft();
delay(300);
}
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("SEIF ELDIN");
lcd.setCursor(4,1);
lcd.print("20106729");
delay(500);
for (int i = 0; i < 16; i++) {
lcd.scrollDisplayLeft();
delay(300);
}
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("YOUSSEF EHAB");
lcd.setCursor(4,1);
lcd.print("20104535");
delay(500);
for (int i = 0; i < 16; i++) {
lcd.scrollDisplayLeft();
delay(300);
}
lcd.clear();
delay(500);
lcd.setCursor(2, 0);
lcd.print("ENG.MOHAMED G.");
lcd.setCursor(1,1);
lcd.print("DR.MOSTAFA FOUZ");
delay(2000);
for(int i=0;i<16;i++)
{
delay(500);
lcd.setCursor(i,0);
lcd.write((byte)0);
lcd.setCursor(i,1);
lcd.write((byte)0);
}
delay(1000);
lcd.clear();
//LCD CODE END
}
void loop()
{
getSerial();
ENCODER_RPM();
USS();
if(digitalRead(BUTTON)==LOW)
Flag=0;
if(Flag==0)
{
FollowLine();
while(distance<setdistance && distance>0)
{
USS();
STOP();
servocnt();
}
}
}