-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_solver.ino
172 lines (144 loc) · 3.2 KB
/
maze_solver.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
#include <PID_v1.h>
#include<NewPing.h>
//Settings
int max_dist = 4000;
double Kp = 0.7, Ki = 0.1, Kd = 0.5;
int baseSpeed = 150;
int lExtSpeed = 30; //Error
int turnTime = 350;
int uTurnTime = 750;
int goFowdTime = 250;
int thershold = 40;
int fThershold = 20;
int outLowLim = 100;
int outHigLim = 225;
//Functions
void readSensors();
void lTurn();
void rTurn();
void stopAll();
void stabilize();
void goFowd();
void decide();
void uTurn();
void Print();
int lTrig = A0;
int fTrig = A1;
int rTrig = A2;
int lEcho = A3;
int fEcho = A4;
int rEcho = A5;
const int lFowd = 6; //Left Forward pwm
const int lBack = 4;
const int rBack = 7;
const int rFowd = 5; //Right Forward pwm
double lDist, rDist, fDist;
double mean;
double lms = baseSpeed, rms = baseSpeed;
NewPing lSensor(lTrig, lEcho, max_dist);
NewPing fSensor(fTrig, fEcho, max_dist);
NewPing rSensor(rTrig, rEcho, max_dist);
PID lPid(&lDist, &lms, &mean, Kp, Ki, Kd, DIRECT);
PID rPid(&rDist, &rms, &mean, Kp, Ki, Kd, DIRECT);
void setup() {
Serial.begin(9600);
pinMode(rBack, OUTPUT);
pinMode(rFowd, OUTPUT);
pinMode(lBack, OUTPUT);
pinMode(lFowd, OUTPUT);
lPid.SetMode(AUTOMATIC);
rPid.SetMode(AUTOMATIC);
lPid.SetOutputLimits(outLowLim, outHigLim);
rPid.SetOutputLimits(outLowLim, outHigLim);
}
void loop() {
readSensors();
stabilize();
decide();
stopAll();
}
void readSensors() {
int iterations = 5;
int lDura = lSensor.ping_median(iterations);
delay(50);
int fDura = fSensor.ping_median(iterations);
delay(50);
int rDura = rSensor.ping_median(iterations);
delay(50);
lDist = lSensor.convert_cm(lDura);
fDist = fSensor.convert_cm(fDura);
rDist = rSensor.convert_cm(rDura);
}
void lTurn(){
digitalWrite(lFowd, LOW);
digitalWrite(lBack, HIGH);
digitalWrite(rFowd, HIGH);
digitalWrite(rFowd, LOW);
delay(turnTime);
}
void rTurn(){
digitalWrite(rFowd, LOW);
digitalWrite(rBack, HIGH);
digitalWrite(lFowd, HIGH);
digitalWrite(lBack, LOW);
delay(turnTime);
}
void stopAll() {
digitalWrite(rBack, LOW);
digitalWrite(rFowd, LOW);
digitalWrite(lBack, LOW);
digitalWrite(lFowd, LOW);
}
void stabilize(){
mean = (lDist+rDist)/2;
lPid.Compute();
rPid.Compute();
}
void goFowd() {
analogWrite(lFowd, lms + lExtSpeed);
digitalWrite(lBack, LOW);
analogWrite(rFowd, rms);
digitalWrite(rBack, LOW);
delay(goFowdTime);
}
void uTurn() {
digitalWrite(lFowd, LOW);
digitalWrite(lBack, HIGH);
digitalWrite(rFowd, HIGH);
digitalWrite(rFowd, LOW);
delay(uTurnTime);
}
void decide() {
if (lDist > thershold)
{
lTurn();
}
else if (fDist > fThershold)
{
goFowd();
}
else if (rDist > thershold)
{
rTurn();
}
else
{
uTurn();
}
}
void Print(){
Serial.print("Leftdistance = ");
Serial.println(lDist);
Serial.print("Rightdistance = ");
Serial.println(rDist);
Serial.print("Frontdistance = ");
Serial.println(fDist);
Serial.print("Mean = ");
Serial.println(mean);
Serial.print("Lms = ");
Serial.println(lms);
Serial.print("Rms = ");
Serial.println(rms);
Serial.print("\n");
delay(3000);
}