8.d. The Main Loop(s)
The strategy of our program is expressed in the file
maze-solve.c
. Most importantly, we want to keep
track of the path that we have followed, so we define an array storing up to 100; these will be the same
characters used in the
turn()
function. We also need to keep track of the current path length so that we
know where to put the characters in the array.
Our “main loop” is found in the function
maze_solve()
, which is called after calibration, from
main.c
.
This function actually includes two main loops – a first one that handles solving the maze, and a
second that replays the solution for the fastest possible time. In fact, the second loop is actually a loop
within a loop, since we want to be able to replay the solution many times. Here’s an outline of the code:
The first main loop needs to drive down a segment of the course, decide how to turn, and record the
turn in the
path
variable. To pass the correct arguments to
select_turn()
, we need to carefully examine
the intersection as we cross it. Note that there is a special exception for finding the end of the maze.
The following code works pretty well, at least at the slow speeds that we’re using:
1
2
char
path[100] =
""
;
unsigned
char
path_length = 0;
// the length of the path
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
// This function is called once, from main.c.
void
maze_solve()
{
while
(1)
{
// FIRST MAIN LOOP BODY
// (when we find the goal, we use break; to get out of this)
}
// Now enter an infinite loop - we can re-run the maze as many
// times as we want to.
while
(1)
{
// Beep to show that we finished the maze.
// Wait for the user to press a button...
int
i;
for
(i=0;i<path_length;i++)
{
// SECOND MAIN LOOP BODY
}
// Follow the last segment up to the finish.
follow_segment();
// Now we should be at the finish!
Restart the loop.
}
}
Pololu 3pi Robot User’s Guide
© 2001–2019 Pololu Corporation
8. Example Project #2: Maze Solving
Page 42 of 85
Содержание 0J5840
Страница 24: ...Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 5 How Your 3pi Works Page 24 of 85...
Страница 67: ...Source code Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 10 Expansion Information Page 67 of 85...
Страница 77: ...Source code Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 10 Expansion Information Page 77 of 85...