-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridMap.cpp
More file actions
executable file
·210 lines (190 loc) · 3.72 KB
/
Copy pathGridMap.cpp
File metadata and controls
executable file
·210 lines (190 loc) · 3.72 KB
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
/* GridMap Class
*
* @author: Navid Fattahi <navid.fattahi (at) alumni.ubc.ca>
* Alireza Afshar
*
* August 2013
* this code is public domain, enjoy!
*
*/
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#if defined(__AVR__)
#include <avr/io.h>
#endif
#include "WProgram.h"
#endif
#include <GridMap.h>
#define DIR_NORTH 0
#define DIR_EAST 1
#define DIR_SOUTH 2
#define DIR_WEST 3
#define MOVFWD 0
#define MOVRGT 1
#define MOVBWD 2
#define MOVLFT 3
#define MOVARD 4
GridMap::GridMap()
{
direction = 0;
currposx = 7;
currposy = 7;
int i, j;
for (i = 0; i < 15; i++)
for (j = 0; j < 8; j++)
map[i][j] = 0;
// Robot's starting position: map[8][7]
map[currposx][currposy] = 1;
}
/* Takes in the nextStep and fills the
corresponding cell in the map according
to the facing direction of the robot
@param: nextStep (moving direction)
@return: 1 for success, 0 for failure
*/
int GridMap::updateMap (int nextStep)
{
int nextposx = currposx;
int nextposy = currposy;
if (direction == 0) // Facing North
{
if (nextStep == 0) // Moving Forward
nextposy--;
else if (nextStep == 1) // Turning Right
{
nextposx++;
direction = DIR_EAST;
}
else if (nextStep == 2) // Moving Backward
nextposy++;
else if (nextStep == 3) // Turning Left
{
nextposx--;
direction = DIR_WEST;
}
else if (nextStep == 4)
{
direction = DIR_SOUTH;
}
}
else if (direction == 1) // Facing East
{
if (nextStep == 0) // Moving Forward
nextposx++;
else if (nextStep == 1) // Turning Right
{
nextposy++;
direction = DIR_SOUTH;
}
else if (nextStep == 2) // Moving Backward
nextposx--;
else if (nextStep == 3) // Turning Left
{
nextposy--;
direction = DIR_NORTH;
}
else if (nextStep == 4)
{
direction = DIR_WEST;
}
}
else if (direction == 2) // Facing South
{
if (nextStep == 0) // Moving Forward
nextposy++;
else if (nextStep == 1) // Turning Right
{
nextposx--;
direction = DIR_WEST;
}
else if (nextStep == 2) // Moving Backward
nextposy--;
else if (nextStep == 3) // Turning Left
{
nextposx++;
direction = DIR_EAST;
}
else if (nextStep == 4)
{
direction = DIR_NORTH;
}
}
else if (direction == 3) // Facing West
{
if (nextStep == 0) // Moving Forward
nextposx--;
else if (nextStep == 1) // Turning Right
{
nextposy--;
direction = DIR_NORTH;
}
else if (nextStep == 2) // Moving Backward
nextposx++;
else if (nextStep == 3) // Turning Right
{
nextposy++;
direction = DIR_SOUTH;
}
else if (nextStep == 4)
{
direction = DIR_EAST;
}
}
// Validate Cell
if ((nextposx >= 0 && nextposx <= 14) && (nextposy >= 0 && nextposy <= 7))
{
currposx = nextposx;
currposy = nextposy;
map[currposx][currposy] = 1;
Serial.print("map[");
Serial.print(currposx);
Serial.print("][");
Serial.print(currposy);
Serial.println("] = 1");
Serial1.print("map[");
Serial1.print(currposx);
Serial1.print("][");
Serial1.print(currposy);
Serial1.println("] = 1");
return 1; // Success!
}
else
{
return 0; // Failed: Not a valid cell!
Serial.println("Failed to update map!");
Serial1.println("Failed to update map!");
}
}
void GridMap::updateDeadEnd()
{
map[currposx][currposy] = 2;
}
int GridMap::getCurrPosx()
{
return currposx;
}
int GridMap::getCurrPosy()
{
return currposy;
}
int GridMap::getRobotDir()
{
return direction;
}
void GridMap::printMap()
{
int i, j;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 15; j++)
{
Serial.print(map[j][i]);
Serial.print(" ");
Serial1.print(map[j][i]);
Serial1.print(" ");
}
Serial.println("");
Serial1.println("");
}
}