Skip to content

Commit 174ea81

Browse files
committed
Set initial heading based on obstacles, not map coordinates
1 parent a8bc76b commit 174ea81

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

src/spiral_stc.cpp

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,64 @@ std::list<gridNode_t> SpiralSTC::spiral(std::vector<std::vector<bool> > const& g
4545
// Copy incoming list to 'end'
4646
std::list<gridNode_t> pathNodes(init);
4747

48-
// set initial direction towards longest side
49-
int robot_dir = point;
50-
int pattern_dir = point;
51-
if (nRows >= nCols) {
52-
if (pathNodes.back().pos.y < nRows / 2) {
53-
robot_dir = north;
54-
dy = 1;
55-
}
56-
else {
57-
robot_dir = south;
58-
dy = -1;
59-
}
60-
} else {
61-
if (pathNodes.back().pos.x < nCols / 2) {
62-
robot_dir = east;
63-
dx = 1;
48+
// this array stores how far the robot can travel in a straight line for each direction
49+
int free_space_in_dir[5] = {0};
50+
// for each direction
51+
for (int i = 1; i < 5; i++) {
52+
// start from starting pos
53+
x2 = pathNodes.back().pos.x;
54+
y2 = pathNodes.back().pos.y;
55+
// loop until hits wall
56+
while (validMove(x2, y2, nCols, nRows, grid, visited)) {
57+
switch (i) {
58+
case east:
59+
x2++;
60+
break;
61+
case west:
62+
x2--;
63+
break;
64+
case north:
65+
y2++;
66+
break;
67+
case south:
68+
y2--;
69+
break;
70+
default:
71+
break;
72+
}
73+
free_space_in_dir[i]++;
6474
}
65-
else {
66-
robot_dir = west;
75+
}
76+
// set initial direction towards direction with most travel possible
77+
int robot_dir = 0;
78+
int pattern_dir = point;
79+
int indexValue = 0;
80+
for (int i = 1; i <= 4; i++) {
81+
if (free_space_in_dir[i] > indexValue) {
82+
robot_dir = i;
83+
indexValue = free_space_in_dir[i];
84+
}
85+
}
86+
// set dx and dy based on robot_dir
87+
switch(robot_dir) {
88+
case east: // 1
89+
dx = +1;
90+
dy = 0;
91+
break;
92+
case west: // 2
6793
dx = -1;
68-
}
94+
dy = 0;
95+
break;
96+
case north: // 3
97+
dx = 0;
98+
dy = +1;
99+
break;
100+
case south: // 4
101+
dx = 0;
102+
dy = -1;
103+
break;
104+
default:
105+
break;
69106
}
70107

71108
bool done = false;

0 commit comments

Comments
 (0)