-
Notifications
You must be signed in to change notification settings - Fork 2
/
MapMaze.c
276 lines (233 loc) · 6.02 KB
/
MapMaze.c
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
//Contains mapping algorithm function, function to decide if a wall is present based on ADC and function to rotate robot (if required) before moving to next cell
#include <signal.h>
#include "MapMaze.h"
#include "MazeManipulation.h"
#include "Config.h"
#include "led.h"
#include "DeadEndBlocking.h"
#include "Setup.h"
#include "Alignment.h"
#include "Motor2.h"
#include "softDelay.h"
#include "CellReservation.h"
#include "RfPacket.h"
#include "nRF24L01.h"
#include "initPorts.h"
static unsigned int CellCount = 1;
extern unsigned char TxPacket[32];
volatile extern unsigned char CTS;
void SetCellCount(unsigned int Ccount)
{
CellCount = Ccount;
}
unsigned int GetCellCount( void )
{
return CellCount;
}
void IncrementCellVisits (void)
{
unsigned char MouseX = GetMouseX();
unsigned char MouseY = GetMouseY();
unsigned char Visits = GetVisits(MouseX,MouseY);
SetVisits(MouseX,MouseY,Visits+1);
}
void IncrementCellCount (void)
{
unsigned char MouseX = GetMouseX();
unsigned char MouseY = GetMouseY();
unsigned char Visits = GetVisits(MouseX,MouseY);
if (!Visits) SetCellCount( GetCellCount() + 1 );
}
void AnalyseCellLR (void)
{
unsigned char MouseHeading = GetMouseHeading();
unsigned char MouseX = GetMouseX();
unsigned char MouseY = GetMouseY();
unsigned char Visits = GetVisits(MouseX,MouseY);
if (!Visits ) //if no visits to cell
{
if (MouseHeading == NORTH)
{
if (IsWallLeft()) AddWall(WEST,MouseX,MouseY); // LED12ON;
if (IsWallRight()) AddWall(EAST,MouseX,MouseY); // LED10ON;
}
if (MouseHeading == EAST)
{
if (IsWallLeft()) AddWall(NORTH,MouseX,MouseY);
if (IsWallRight()) AddWall(SOUTH,MouseX,MouseY);
}
if (MouseHeading == SOUTH)
{
if (IsWallLeft()) AddWall(EAST,MouseX,MouseY);
if (IsWallRight()) AddWall(WEST,MouseX,MouseY);
}
if (MouseHeading == WEST)
{
if (IsWallLeft()) AddWall(SOUTH,MouseX,MouseY);
if (IsWallRight()) AddWall(NORTH,MouseX,MouseY);
}
}
}
void AnalyseCellFB (void)
{
unsigned char MouseHeading = GetMouseHeading();
unsigned char MouseX = GetMouseX();
unsigned char MouseY = GetMouseY();
unsigned char Visits = GetVisits(MouseX,MouseY);
if (!Visits ) //if no visits to cell
{
SetThisCellMappedByThisMouse();
if (MouseHeading == NORTH)
{
if (IsWallFront()) AddWall(NORTH,MouseX,MouseY); // LED9ON;
if (IsWallBack()) AddWall(SOUTH,MouseX,MouseY); // LED11ON;
}
if (MouseHeading == EAST)
{
if (IsWallFront()) AddWall(EAST,MouseX,MouseY);
if (IsWallBack()) AddWall(WEST,MouseX,MouseY);
}
if (MouseHeading == SOUTH)
{
if (IsWallFront()) AddWall(SOUTH,MouseX,MouseY);
if (IsWallBack()) AddWall(NORTH,MouseX,MouseY);
}
if (MouseHeading == WEST)
{
if (IsWallFront()) AddWall(WEST,MouseX,MouseY);
if (IsWallBack()) AddWall(EAST,MouseX,MouseY);
}
}
}
unsigned char GetExitDir (void)
{
unsigned char MouseY = GetMouseY();
unsigned char MouseX = GetMouseX();
unsigned char n=0xFF;
unsigned char s=0xFF;
unsigned char e=0xFF;
unsigned char w=0xFF;
unsigned char Walls=GetWalls(MouseX,MouseY);
unsigned char Exits=( (~Walls)&(0xF) );
unsigned char ne=0; //smallest between n&e
unsigned char sw=0;//smallest between s&w
unsigned char nesw=0;//smallest between ne&sw
unsigned char move = 0; //dir to move
unsigned char rn = MOUSENUMBER & GetCellReserved(MouseX, MouseY-1); // reserved north
unsigned char rs = MOUSENUMBER & GetCellReserved(MouseX, MouseY+1);
unsigned char re = MOUSENUMBER & GetCellReserved(MouseX+1, MouseY);
unsigned char rw = MOUSENUMBER & GetCellReserved(MouseX-1, MouseY); // reserved west
if (Exits & NORTH)
{
if (rn)
{
n= (GetVisits(MouseX,MouseY-1)<<4) | NORTH;
}
}
if (Exits & EAST)
{
if (re)
{
e= (GetVisits(MouseX+1,MouseY)<<4) | EAST;
}
}
if (Exits & SOUTH)
{
if (rs)
{
s= (GetVisits(MouseX,MouseY+1)<<4) | SOUTH;
}
}
if (Exits & WEST)
{
if (rw)
{
w= (GetVisits(MouseX-1,MouseY)<<4) | WEST;
}
}
if ( (n==0xFF) && (e==0xFF) && (s==0xFF) && (w==0xFF) )
{
//do something like wait and re-do GED()
LED8ON;
softDelay(65535);
LED8OFF;
softDelay(65535);
return 0;
}
ne = (n <= e ) ? n : e ;
sw = (s <= w ) ? s : w ;
nesw = (ne <= sw) ? ne : sw ;
move = (nesw & 0xF);
if (move == NORTH) SetCellReserved(MouseX, MouseY-1);
if (move == EAST) SetCellReserved(MouseX+1, MouseY);
if (move == SOUTH) SetCellReserved(MouseX, MouseY+1);
if (move == WEST) SetCellReserved(MouseX-1, MouseY);
return move;
}
void Rotate (unsigned char dir) // does the mouse need to rotate to exit in the required dir?
{
unsigned char Heading = GetMouseHeading();
if (Heading == dir) return;
else if (Heading == (ShiftLeftWrapNibble(dir)) ) // need to turn 90deg Right
{
DriveSpotRotateLeftHalf(DEGREES90);
}
else if (Heading == (ShiftRightWrapNibble(dir)) ) // need to turn 90deg Left
{
DriveSpotRotateRightHalf(DEGREES90);
}
else if (Heading == (ShiftRightWrapNibble(ShiftRightWrapNibble(dir))) ) // need to turn 180 Deg
{
DriveSpotRotateLeftHalf(DEGREES180);
}
SetMouseHeading(dir);
}
void MoveToNewCellLogical (unsigned char dir)
{
unsigned char MouseX = GetMouseX();
unsigned char MouseY = GetMouseY();
if ( dir == NORTH ) MouseY--;//North
if ( dir == EAST ) MouseX++;//East
if ( dir == SOUTH ) MouseY++;//South
if ( dir == WEST ) MouseX--;//West
SetMouseX(MouseX);
SetMouseY(MouseY);
}
unsigned char IsMazeMapped (void)
{
unsigned int CurrentCellCount = GetCellCount();
unsigned int MaxCells = (MAZEX) * (MAZEY);
//SetLedsLower( ((unsigned char)CurrentCellCount) | (((unsigned char)MaxCells) << 4 ) );
if (CurrentCellCount >= MaxCells)
{
return 1;
}
else
{
return 0;
}
}
void DriveToEndOfCurrentCellCorrecting (void)
{
DriveForwardsHalf(5,1);
}
void DriveToStartOfNextCellNOTCorrecting (void)
{
DriveForwardsHalf(12,0);
}
void DriveToMiddleOfNewCellCorrecting (void)
{
DriveForwardsHalf(25,1);
}
void TxCellInfo (void)
{
unsigned char MouseX = GetMouseX();
unsigned char MouseY = GetMouseY();
while(!CTS);
dint();
AssembleMazePacket (MouseX,MouseY);
rfStandbyMode();
rfSendPacket2Module(&TxPacket[0],32);
rfTxMode();
eint();
}