C++ Robot Lab 7 — Navigating the Maze

Table of Contents

Introduction

For the first time in Lab 7, our real world robot meets the virtual world. In the early labs the real world robot learned how to takeAStep, turnRight, turnLeft, and turnAround. Lab 6 created a virtual representation of the real world maze that the robot would need to navigate. The first use of this virtual  maze was to map a step taken in the real world (takeAStep) to a corresponding step in the virtual world (enterRoom). The lab also had our robot study the room; answering questions concerning the number of bees (countBees) and whether or not it was in the forest (inForest). In this lab our robot will ask more questions about the virtual room in order to take action in the real world (takeAStep, turnRight, turnLeft, and turnAround) – and the circle is complete.

Build the Framework

Open Lab6 in the Arduino IDE. Save As Lab 7.

Create a new tab in the Arduino IDE, by clicking on the down-arrow located on the right-hand side of the tab bar. Name the tab VirtualWorld.

Figure 1. Tab Menu

If you have not done so already, add the three (3) look-up table you wrote in the VirtualWorld prelab, here or in 3DoTConfig.h.

/* Look around */
// look-up tables use direction the robot is facing as index
//                            SOUTH WEST EAST NORTH Direction robot is facing
const uint8_t look_right[] = {WEST,NORTH,SOUTH,EAST};
const uint8_t look_left[]  = {____,_____,_____,____};
const uint8_t look_back[]  = {____,_____,_____,____};

In order for the robot to navigate through the maze, decisions will have to be made. These decisions include: continue straight, turn left, turn right and turn around.  However, these actions depend on the type of room in which the robot is located. Currently you can keep track of the robot’s position throughout the maze virtually. Functions hitWall, leftSensor, and rightSensor, written in the prelab, now give you the ability to answer questions about this virtual room. If you have not done so already, place these three functions into the VirtualWorld tab.

/* check if the path in front of the robot is clear */
bool hitWall(robot_t robot) {

} 

/* check if the path to the left of the robot is clear */
bool leftSensor(robot_t robot) {

} 

/* check if the path to the right of the robot is clear */
bool rightSensor(robot_t robot) {

}

Which Way?

The last piece of the puzzle is translating your prelab 6 whichWay flowchart into a C++ program. The simplest way to help you translate your flowchart into code is by example. The flowchart in Figure 1.0 teaches your robot how to follow the shortest path through the maze.

Figure 2. Shortest Path

Add a call to the whichWay function at the end of loop(), followed by the shortest path solution.

void loop() { 
                                                        // reality    point of view
  // takeAStep();                                       // real world robot
  robot.room = enterRoom(robot.dir);                    // virtual    maze 
  if (robot.room == forest) {                           // virtual    robot
    inForest(robot.notepad);                            // real world robot
  }
  robot.notepad = countBees(robot.notepad, robot.room); // virtual    robot
  robot.dir = whichWay(robot.room, robot.dir);          // both      robot
}

/* Real and Virtual World */
uint8_t whichWay(uint8_t room, uint8_t dir) {
  room &= 0x0F; // clear the most significan nibble
  if (!rightSensor(room, dir)){
    dir = look_right[dir];  // update virtual world
    turnRight();            // turn robot in the real world
  } else if (hitWall(room, dir)){
    dir = look_left[dir];  // update virtual world
    turnLeft();            // turn robot in the real world
  }
  return dir;
}

The whichWay function takes as inputs robot properties dir and room. and returns the new direction the robot is facing.

The flowchart conditional blocks (diamonds) are implemented using if-else statements. The conditional expressions query the nature of the room by calling three functions, which answer the following questions:

Function  Question
rightSensor Does my right “virtual” sensor detect a wall?
leftSensor Does my left “virtual” sensor detect a wall?
hitWall Is there a wall in front of me?

Note: The rightSensor is not required for the robot to follow the shortest path.

Based on the type of room the robot finds itself, action blocks  “Turn Right” and “Turn Left” are called. The objective of the first statement in each block is to mimic in the virtual world the robot turning in the real world, which is done in the second line.

Now that all the pieces of the puzzle are in place, your robot should be able to navigate the shortest path through the maze.

Test whichWay

Apply what you learned in lab 6 to debug your code. For example, you can add this function at the end of your whichWay routine to see which direction your virtual robot is facing.

write_dir("I am looking ",dir,10);

Lab 7 Deliverable(s)

All labs should represent your own work – DO NOT COPY.

Submit all of the files in your sketch folder. Make sure that the code compiles without any errors.

Checklist

Remember before you turn in your lab…

  1. Completed shortest path whichWay.
  2. Your whichWay sketch with comments.
  3. Demonstration of your robot running the shortest path and hopefully your path through the maze.