-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPID_V3_FAROOS_EDITION.ino
216 lines (212 loc) · 4.79 KB
/
PID_V3_FAROOS_EDITION.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
//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#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 3 //ULTRASONIC SENSOR SIGNAL TRANSFER PIN
#define USS_ECHO 2 //ULTRASONIC SENSOR SIGNAL RECIVE PIN
#define LED_BRAKE 7 //RED LED
int IR[5] = { A0, A1, A2, A3, A4 };
int IR_READ[5],speed,currentPos,setPos=12,currentError,prevError,I,D,Flag=1;
String serial_input= "";
int POS=0;
float USS_READ;
float kp,kd,ki,PIDOut;
Servo SERVO_1;
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)
{
getpidvalue();
}
}
void getpidvalue()
{
if(serial_input.substring(0,2)=="kp")
{serial_input.remove(0,2);
kp=serial_input.toFloat();
serial_input= "";
}
if(serial_input.substring(0,2)=="ki")
{serial_input.remove(0,2);
ki=serial_input.toFloat();
serial_input= "";
}
if(serial_input.substring(0,2)=="kd")
{serial_input.remove(0,2);
kd=serial_input.toFloat();
serial_input= "";
}
if(serial_input.substring(0,2)=="sp")
{serial_input.remove(0,2);
speed=serial_input.toInt();
serial_input= "";
}
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);
}
void readIR()
{
for (int i = 0; i < 5; i++)
IR_READ[i] = digitalRead(IR[i]); //STORE IR READINGS IN ARRAY
}
void STOP() {
analogWrite(FORWARD_RM, LOW);
analogWrite(FORWARD_LM, LOW);
}
//OBSTACLE DISTANCE DETECTION
int USS() {
float DURATION, DISTANCE;
digitalWrite(USS_TRIG, LOW);
delayMicroseconds(1);
digitalWrite(USS_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(USS_TRIG, LOW);
DURATION = pulseIn(USS_ECHO, HIGH);
DISTANCE = (DURATION * 0.0343) / 1;
//delay(100);
return DISTANCE;
}
//OBSTACLE REMOVAL
int SERVO_M() {
for (POS = 0; POS <= 180; POS += 1) {
SERVO_1.write(POS);
delayMicroseconds(1000);
}
for (POS = 180; POS >= 0; POS -= 1) {
SERVO_1.write(POS);
delayMicroseconds(1000);
}
}
void FollowLine(){
readIR();
currentPos=IR_READ[0]*1+IR_READ[1]*2+IR_READ[2]*3+IR_READ[3]*4+IR_READ[4]*5;
if(currentPos==0)
{
STOP();
Flag=1;
}
else if(currentPos==15)
{
//asm(NOP);
}
else
{
currentError= setPos-currentPos;
I+=currentError;
D=currentError-prevError;
prevError=currentError;
PIDOut=kp*currentError+ki*I+kd*D;
analogWrite(FORWARD_RM,constrain(speed-PIDOut,0,255));
analogWrite(FORWARD_LM,constrain(speed+PIDOut,0,255));
}
}
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);
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("55555555");
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("55555555");
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();
SERVO_1.write(POS);
if(digitalRead(BUTTON)==LOW)
Flag=0;
if(Flag==0){
FollowLine();
}
I=0;
}