17 #include "path_planning.h"
29 #define NO_PARENT 0xff
54 uint8_t pathList[MAX_PATH_LIST_SIZE];
55 uint8_t pathListIndex = 0;
59 static uint8_t goalListSize = 0;
61 static inline uint8_t uniformCostSearch( uint8_t startNode, uint8_t goalNode );
63 static inline void recostructPath( uint8_t startNode, uint8_t goalNode );
64 static inline uint8_t updatePathTo( uint8_t nodeNum );
69 uint8_t updatePath(
void)
86 for (i = 0; i < goalListSize; i++)
88 startCost[i] = uniformCostSearch(botNode, goalList[i]);
89 stopCost[i] = uniformCostSearch(goalList[i],
STOP_NODE);
93 for (i = 0; i < goalListSize; i++)
95 for (j = 0; j <= i; j++)
101 tempCost = uniformCostSearch(goalList[i], goalList[j]);
102 goalCost[i][j] = tempCost;
103 goalCost[j][i] = tempCost;
109 for (i = 0; i < goalListSize; i++)
117 if (goalListSize == 0)
126 tempCost = startCost[curPerm[0]];
127 for (i = 0; i < goalListSize - 1; i++)
128 tempCost += goalCost[curPerm[i]][curPerm[i + 1]];
129 tempCost += stopCost[curPerm[i]];
132 if (tempCost < bestCost)
135 memcpy(bestPerm, curPerm, goalListSize *
sizeof(*bestPerm));
138 }
while (generateNextPermutation(curPerm, curPerm + goalListSize));
141 memcpy(tempGoalList, goalList, goalListSize *
sizeof(*tempGoalList));
142 for (i = 0; i < goalListSize; i++)
143 goalList[i] = tempGoalList[bestPerm[i]];
146 pathListIndex = MAX_PATH_LIST_SIZE - 1;
147 uniformCostSearch(goalList[goalListSize - 1],
STOP_NODE);
148 recostructPath(goalList[goalListSize - 1],
STOP_NODE);
149 for (i = goalListSize - 1; i > 0; i--)
151 uniformCostSearch(goalList[i - 1], goalList[i]);
152 recostructPath(goalList[i - 1], goalList[i]);
154 uniformCostSearch(botNode, goalList[0]);
155 recostructPath(botNode, goalList[0]);
162 inline void addToGoalList(uint8_t nodeNum)
164 if (!isInGoalList(nodeNum))
166 goalList[goalListSize++] = nodeNum;
171 bool removeFromGoalList(uint8_t nodeNum)
173 int8_t i = findValue(goalList, goalListSize, nodeNum);
181 for (; i < goalListSize - 1; i++)
183 goalList[i] = goalList[i + 1];
191 inline void adjustNumKnownGoals( int8_t adjustment )
193 numKnownGoals += adjustment;
196 inline bool allGoalsFound(
void)
201 inline bool isInGoalList(uint8_t nodeNum)
203 return findValue(goalList, goalListSize, nodeNum) != -1;
206 inline void printGoalList(
void)
211 lcdWriteStr(
" ", 0, 0);
213 if (goalListSize == 0)
215 lcdWriteStr(
"(empty)", 0, 0);
218 for (i = 0; i < goalListSize; i++)
220 lcdPrintDecU08(goalList[i], 0, 3 * i);
227 uint8_t getNextBallNodeNum(
void)
230 uint8_t nodeNum = pathList[pathListIndex + i];
231 while (isBallNode(nodeNum))
233 if (isInGoalList(nodeNum))
238 nodeNum = pathList[pathListIndex + i];
249 inline int8_t getNextHeading(
void)
252 int8_t nextNodeIndex;
256 getNode(botNode, &nextNode);
257 nextNodeIndex = findValue(nextNode.
adjNodes,
259 pathList[pathListIndex + 1]);
262 if (nextNodeIndex == -1)
264 fatalError(
"pathList error",
"");
273 uint8_t updatePathTo(uint8_t nodeNum)
275 uint8_t tempCost = uniformCostSearch(botNode, nodeNum);
276 pathListIndex = MAX_PATH_LIST_SIZE - 1;
277 recostructPath(botNode, nodeNum);
283 inline uint8_t uniformCostSearch(uint8_t startNode, uint8_t goalNode)
289 uint8_t curNodeNum = startNode;
296 searchSpace[i].parent = NO_PARENT;
297 searchSpace[i].pathCost = MAX_COST;
298 searchSpace[i].visited =
false;
301 searchSpace[startNode].pathCost = 0;
302 searchSpace[startNode].visited =
true;
303 while (curNodeNum != goalNode)
306 getNode(curNodeNum, &nodeData);
311 if (!searchSpace[n].visited && searchSpace[n].pathCost == MAX_COST)
313 searchSpace[n].parent = curNodeNum;
314 searchSpace[n].pathCost = curCost + nodeData.
adjCosts[i];
315 visitList = addNodeByCost(visitList, n );
320 if (visitList == NULL )
322 fatalError(
"uniformCostSearc",
"error ");
328 curNodeNum = visitList->nodeNum;
329 curCost = searchSpace[curNodeNum].pathCost;
330 searchSpace[curNodeNum].visited =
true;
331 visitList = freeNode(visitList);
337 return searchSpace[goalNode].pathCost;
348 newNode->nodeNum = newNodeNum;
349 newNode->nextNode = NULL;
358 if (searchSpace[head->nodeNum].pathCost > searchSpace[newNodeNum].pathCost)
362 head->nextNode = next;
369 next = head->nextNode;
371 while (next != NULL )
373 if (searchSpace[next->nodeNum].pathCost >= searchSpace[newNodeNum].pathCost)
376 current = current->nextNode;
377 next = next->nextNode;
380 newNode->nextNode = next;
381 current->nextNode = newNode;
393 inline void recostructPath(uint8_t startNode, uint8_t goalNode)
395 pathList[pathListIndex] = goalNode;
396 while (goalNode != startNode)
398 goalNode = searchSpace[goalNode].parent;
400 pathList[pathListIndex] = goalNode;
409 head = freeNode(head);
418 head = head->nextNode;