forked from karimelghazouly/MazeSolver
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
307 lines (265 loc) · 8.12 KB
/
main.cpp
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
#include <unistd.h>
#include <iostream>
#include <vector>
#include <string>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
#define speed 14.5
#define pexilsPerCm 28.5
#define scale 8
#define endl '\n'
using namespace cv;
using std::cin;
using std::cout;
using std::vector;
using std::pair;
using std::make_pair;
using std::string;
using std::queue;
Mat image; //Declaring the image globally to make it easier to use with functions.
struct cord
{
//making a struct for coordinates
short x;
short y;
};
cord parent[3000][3000];
bool visit[3000][3000];
vector <pair <int, int> > v, inputToSecond; //to parse cordinates to 2nd proram
vector <pair <string, int> > path;
cord findFirst();
void draw(int pr, int pc, int firstr, int firstc);
/* 2nd program starts here */
enum{RIGHT,LEFT,UP,DOWN,INVALID}; //Directions Flags
string s[4] = {"R" , "L", "U", "D"}; //to print
int dir[1000000];
int dir_det(int y0, int x0, int y1, int x1);
void set_counts();
string toString(int num);
void serial_transmissions();
/* 2nd program ends here */
int main()
{
//Entering the name/path of the file that contains the map.
cout << "Enter the name/relative path of the file that contains the map : ";
string mazeName;
cin >> mazeName;
image = imread(mazeName,1);
//Entering the rf communication channel that the system is connected with the Arduino on.
string channel;
cout << "Enter the Arduino channel number: "; cin >> channel;
string toFile = "\" >> /dev/rfcomm" + channel;
cout << "The device is connected at " << toFile << "\"" << endl;
bool done = false;
int startRow, startCol, currentr, currentc, dr, dc;
startCol = findFirst().x; //First coloumn of black pixels to avoid white borders
startRow = findFirst().y; //First row of black pixels to avoid white borders
cout << startCol << ' ' << startRow << '\n';
for(int i=startCol+2; i<image.cols; i++)
{
if(image.at<Vec3b>(startRow,i)[0] >= 225 && image.at<Vec3b>(startRow,i)[1] && image.at<Vec3b>(startRow,i)[2] >= 225)//searching for the maze entrance
{
startCol=i+1;
break;
}
}
cout << startCol << ' ' << startRow << '\n';
queue < pair<int,int> > q;
q.push(make_pair(startRow,startCol));
//BFS + intest Path
while(!q.empty())
{
currentr=q.front().first;//taking info from queue
currentc=q.front().second;//taking info from queue
cout<<"current r = "<<currentr<<" current c = "<<currentc<<endl;
if(currentr == image.rows-1)// if we are at the ending line line of the maze
{
dr=currentr;
dc=currentc;
done=true;
}
if(currentr-1 >= 0 && currentr-1 >= startRow && image.at<Vec3b>(currentr-1,currentc)[0] >= 225 &&
image.at<Vec3b>(currentr-1,currentc)[1] >= 225 && image.at<Vec3b>(currentr-1,currentc)[2] >= 225 &&
visit[currentr-1][currentc]==0)//checking if pixel is white before adding it to the queue
{
q.push(make_pair(currentr-1,currentc));
parent[currentr-1][currentc].x=currentr;//saving parents of pixel x
parent[currentr-1][currentc].y=currentc;//saving parents of pixel y
visit[currentr-1][currentc]=1;//mark pixel as visited
}
if(currentr+1<image.rows&&image.at<Vec3b>(currentr+1,currentc)[0] >= 225 && image.at<Vec3b>(currentr+1,currentc)[1] >= 225 &&
image.at<Vec3b>(currentr+1,currentc)[2] >= 225 && visit[currentr+1][currentc]==0)//checking if pixel is white before adding it to the queue
{
q.push(make_pair(currentr+1,currentc));
parent[currentr+1][currentc].x=currentr;//saving parents of pixel x
parent[currentr+1][currentc].y=currentc;//saving parents of pixel y
visit[currentr+1][currentc]=1;//mark pixel as visited
}
if(currentc-1 >= 0 && image.at<Vec3b>(currentr,currentc-1)[0] >= 225 && image.at<Vec3b>(currentr,currentc-1)[1] >= 225 &&
image.at<Vec3b>(currentr,currentc-1)[2] >= 225 && visit[currentr][currentc-1]==0)//checking if pixel is white before adding it to the queue
{
q.push(make_pair(currentr,currentc-1));
parent[currentr][currentc-1].x=currentr;//saving parents of pixel
parent[currentr][currentc-1].y=currentc;//saving parents of pixel
visit[currentr][currentc-1]=1;//mark pixel as visited
}
if(currentr<image.cols && image.at<Vec3b>(currentr,currentc+1)[0] >= 225 &&
image.at<Vec3b>(currentr,currentc+1)[1] >= 225 && image.at<Vec3b>(currentr,currentc+1)[2] >= 225 &&
visit[currentr][currentc+1]==0)//checking if pixel is white before adding it to the queue
{
q.push(make_pair(currentr,currentc+1));
parent[currentr][currentc+1].x=currentr;//saving parents of pixel
parent[currentr][currentc+1].y=currentc;//saving parents of pixel
visit[currentr][currentc+1]=1;//mark pixel as visited
}
q.pop();
}
draw(dr, dc, startRow, startCol);//drawing the path found
/*2nd program main statrs here*/
int py,px,y,x;
for(int i = 0; i < inputToSecond.size(); i++)
{
y = inputToSecond[i].first , x = inputToSecond[i].second;
if(i)
dir[i-1] = dir_det(py, px, y, x);
py = y, px = x;
}
set_counts();
serial_transmissions();
/*2nd program main ends here*/
namedWindow("image");//making window to show the solved maze
imshow("image",image);
waitKey(0);
for(int i = 0; i < path.size(); i++)
{
int val;
string next = (string)(path[i].first) + " " + toString(path[i].second);
cout << "The command in queue is: " << next << endl;
cout << "Would you like to send it? (Y/N): \nPress S to skip it" << endl;
char yes; cin >> yes;
val = path[i].second;
if (yes == 'S' || yes == 's')
{
continue;
}
else if(yes != 'y' && yes != 'Y')
{
i--;
do
{
cout << "Direction and time: ";
cin >> next;
}while(next != "F" && next != "R" && next != "L" && next!= "B");
cin >> val;
next += (" "+toString(val));
}
string command = "echo \"" + next + toFile;
system(("echo \"" + next + toFile + "\n").c_str());
sleep((val+1000)/1000);
}
return 0;
}
cord findFirst()
{
cord f;
for(int i=0; i<image.rows; i++)
{
for(int j=0; j<image.cols; j++)
{
if(image.at<Vec3b>(i, j)[0] <= 25 && image.at<Vec3b>(i, j)[1] <= 25 && image.at<Vec3b>(i, j)[2] <= 25)//searching for the first column of black pixel and first row of black pixel
{
f.x=j;
f.y=i;
return f;
}
}
}
}
void draw(int pr, int pc, int firstr, int firstc)
{
int r, c;
while(true)
{
inputToSecond.push_back(make_pair(pr, pc));
image.at<Vec3b>(pr,pc)[0] = 0;
image.at<Vec3b>(pr,pc)[1] = 255;
image.at<Vec3b>(pr,pc)[2] = 0;
image.at<Vec3b>(pr,pc+1)[0] = 0;
image.at<Vec3b>(pr,pc+1)[1] = 255;
image.at<Vec3b>(pr,pc+1)[2] = 0;
image.at<Vec3b>(pr,pc+2)[0] = 0;
image.at<Vec3b>(pr,pc+2)[1] = 255;
image.at<Vec3b>(pr,pc+2)[2] = 0;
if(pr == firstr && pc == firstc) break;
r = pr;
c = pc;
pr = parent[r][c].x;
pc = parent[r][c].y;
}
}
int dir_det(int y0, int x0, int y1, int x1)
{
/* 0 is previous, 1 is current
*
* Right
* y1 == y0 , x1 == x0 + 1
*
* Left
* y1 == y0 , x1 == x0 - 1
*
* Up
* y1 == y0 + 1 , x1 == x0
*
* Down
* y1 == y0 - 1 , x1 == x0
*/
if(y0 == y1){
if(x1 == x0 + 1)
return RIGHT;
else return LEFT;
}
if(x0 == x1){
if(y1 == y0 + 1)
return DOWN;
else return UP;
}
return INVALID; //will not pass here
}
void set_counts()
{
int cur = dir[0], cnt = 1;
for(int i = 1; i < inputToSecond.size() - 1; i++)
{
if(dir[i] != cur)
{
v.push_back(make_pair(cur, cnt));
cnt = 0;
}
cnt++;
cur = dir[i];
}
}
string toString(int num)
{
string ret = "";
while(num){
ret += (char)('0'+num%10);
num /= 10;
}
reverse(ret.begin(), ret.end());
return ret;
}
void serial_transmissions()
{
path.push_back(make_pair("F", v[0].second / (pexilsPerCm * speed) * 1000 * scale));
for(int i = 1; i < v.size(); i++)
{
string corner = s[ v[i-1].first ] + s[ v[i].first ];
if(corner == "RU" || corner == "LD" || corner == "DR" || corner == "UL")
path.push_back(make_pair("L", 620));
if(corner == "RD" || corner == "LU" || corner == "DL" || corner == "UR")
path.push_back(make_pair("R", 620));
path.push_back(make_pair("F", v[i].second / (pexilsPerCm * speed) * 1000 * scale));
}
}