Having a robot be aware of it's position in a maze
Posted: 23 Nov 2010, 00:03
Hello,
I am currently building a robot that can solve a maze. So far my robot can successfully navigate the maze, but I am having issues with letting the robot know where it is currently positioned in the maze, and being able to check if the position it wants to move to has already been explored (this eliminates the robot from doing things like going down a dead end it has already explored). However I'm getting a myriad of compiler errors and I'm assuming my syntax is horribly wrong or I'm approaching the problem the wrong way.
Here is the relevant code:
Obviously a lot of the irrelevant code (such as movement or checking which paths even exist) is missing - if anybody wants me to post the entire source I am more than happy to.
and here is the compiler errors I am receiving:
(I apologize for the screenshot, I couldn't figure out how to copy/paste the compiler errors)
Much of this code is based heavily from Building Robots with Lego Mindstorms NXT by Dave Astolfo, Mario Ferrari and Giulio Ferrari, although the language they use is RobotC, not NXC. This book is fantastic and I can't recommend it enough.
I'm assuming I'm doing something wrong with my Directions enumerator or the #define macros I'm using. I've never really used either of these in any fashion before - am I just doing a bonehead error? As far as I can tell the logic behind looking at the correct array element is correct and what's going wrong is how I'm attempting to do it, as evidenced by the huge amount of compiler errors. Any help is greatly appreciated.
I am currently building a robot that can solve a maze. So far my robot can successfully navigate the maze, but I am having issues with letting the robot know where it is currently positioned in the maze, and being able to check if the position it wants to move to has already been explored (this eliminates the robot from doing things like going down a dead end it has already explored). However I'm getting a myriad of compiler errors and I'm assuming my syntax is horribly wrong or I'm approaching the problem the wrong way.
Here is the relevant code:
Code: Select all
enum Direction {
North = 0,
West = 1,
South = 2,
East = 3
};
int MyPosX = 0, MyPosY = 0; //starting position at (0,0)
//Finds which direction is to the left and right of current direction
#define LeftOf(Dir) (Direction)( (Dir+1)/4)
#define RightOf(Dir) (Dir == North ? East : (Direction)(Dir-1))
//converts directions into their relative position in the Map array
#define RelPosX(Dir) (Dir == West ? -1 : (Dir == East ? 1 : 0))
#define RelPosY(Dir) (Dir == North? 1 : (Dir == South ? -1 : 0))
//decrease the value of array position by 1 to indicate one less new path
#define DecreaseOne(Var) { Var -= 1; Var = ((Var) < 1 ? 1 : (Var); }
//Definition of Map array values"
// 0 - haven't visited this position
// 1 - been here, no unvisited paths are available
// 2, 3 - there are more ways to go from here
int Map[5][4];
bool Paths[3]; //checks which direction is available - (0=left, 1=front, 2=right)
void chooseDirections()
{
Direction myDir = North;
if(Paths[0])
{
//have we visited this position to our right?
if(Map[MyPosX+RelPosX(RightOf(MyDir))][MyPosY+RelPosY(RightOf(MyDir))] == 0)
{
//No, we have not. Visit that space now.
//Movement code goes here
}
else //Yes, we have already visited that path
{
DecreaseOne(Map[MyPosX][MyPosY]);
DecreaseOne(Map[MyPosX+RelPosX(RightOf(MyDir))][MyPosY+RelPosY(RightOf(MyDir))]);
}
}
else if(Paths[1])
{
//same code goes here to check if forward path is unvisited
}
else if(Paths[2])
{
//same code goes here to check if left path is unvisited
}
else
{
//code goes here to turn the robot around - we have reached a dead end
}
}
task main()
{
chooseDirection();
}
and here is the compiler errors I am receiving:
(I apologize for the screenshot, I couldn't figure out how to copy/paste the compiler errors)
Much of this code is based heavily from Building Robots with Lego Mindstorms NXT by Dave Astolfo, Mario Ferrari and Giulio Ferrari, although the language they use is RobotC, not NXC. This book is fantastic and I can't recommend it enough.
I'm assuming I'm doing something wrong with my Directions enumerator or the #define macros I'm using. I've never really used either of these in any fashion before - am I just doing a bonehead error? As far as I can tell the logic behind looking at the correct array element is correct and what's going wrong is how I'm attempting to do it, as evidenced by the huge amount of compiler errors. Any help is greatly appreciated.