One interesting point about this code is that there are some sequences that should never be
encountered by a left-turning robot, like ‘RBR’, which would be replaced by ‘S’ according to this code.
In a more advanced program, you might want to keep track of inconsistencies like this, since they
indicate some kind of a problem that could cause the robot to get lost.
Now let’s step through a slightly more complicated maze, showing how we can simplify the path as we
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
// Path simplification.
The strategy is that whenever we encounter a
// sequence xBx, we can simplify it by cutting out the dead end.
For
// example, LBL -> S, because a single S bypasses the dead end
// represented by LBL.
void
simplify_path()
{
// only simplify the path if the second-to-last turn was a 'B'
if
(path_length < 3 || path[path_length-2] !=
'B'
)
return
;
int
total_angle = 0;
int
i;
for
(i=1;i<=3;i++)
{
switch
(path[path_length-i])
{
case
'R'
:
total= 90;
break
;
case
'L'
:
total= 270;
break
;
case
'B'
:
total= 180;
break
;
}
}
// Get the angle as a number between 0 and 360 degrees.
total_angle = total_angle % 360;
// Replace all of those turns with a single one.
switch
(total_angle)
{
case
0:
path[path_length - 3] =
'S'
;
break
;
case
90:
path[path_length - 3] =
'R'
;
break
;
case
180:
path[path_length - 3] =
'B'
;
break
;
case
270:
path[path_length - 3] =
'L'
;
break
;
}
// The path is now two steps shorter.
path_length -= 2;
}
Pololu 3pi Robot User’s Guide
© 2001–2019 Pololu Corporation
8. Example Project #2: Maze Solving
Page 46 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...