College of Engineering and Mineral Resources Computer Engineering 313 Spring 2010 Laboratory #4-A (Micromouse Algorithms)
Goals This lab introduces the modified flood fill algorithm and teaches how to implement the algorithm using the C Programming Language. Background Maze Solving Algorithms As introduced in the first experiment this semester, a micromouse is a small robotic mouse whose goal is to solve a maze. The mouse is to be completely autonomous and must find its way from the starting position to the central area of the maze without any intervention. To solve the maze, the mice can implement one of many different searching algorithms such as the flood fill, modified flood fill, A*, etc. In the first lab this semester, you learned how to initialize the two matrices used in the modified flood fill algorithm that is used to solve the maze: wall map and maze distance values. Distance values are the number of moves that the mouse must take in order to reach its destination (one of the center six units of the maze). Flood Fill Algorithm To first introduce the modified flood fill algorithm, let s first take a look at the flood fill algorithm. The flood fill is a simple algorithm that determines the area connected to a given node in a multi-dimensional array. It sets the goal values (destination cells) to zero and floods the surrounding cells with radiating, increasing values. Figure 1 provides an example. Figure 1: Flood Fill Algorithm [1] 2
Examining Figure 1, notice that the center value is zero and all other cells are filled with values corresponding to their distance from the goal cell. After flooding the maze with values, the algorithm then searches the adjacent nodes for the smallest value to determine which cell to travel. It then continues to follow the values in descending order until it has reached the center. This algorithm is rather simple and provides a reliable method to finding the center of the maze; however, the modified flood fill algorithm can find the center more quickly so we will introduce this algorithm. Modified Flood Fill Algorithm The modified flood fill algorithm is similar to the flood fill algorithm for it also uses distance values to navigate the maze. The primary difference is that the modified flood fill algorithm does not flood the entire maze with values. It modifies only the values that need to be changed. For example, if a wall is encountered and the robot is not in the destination cell, it updates the value of that cell to 1 + the minimum value of its open neighbors. Figure 2 shows an example of this process. Figure 2: Modified Flood Fill Algorithm [1] Examining Figure 2, when the robot encounters a wall to the east and can only move north or south. The north and south cells (open neighbors) are checked and we find that the current cell s new value is 1 + the minimum value of its open neighbors or 1+3=4. 3
Once the robot has found the destination cell, it can return to the beginning of the maze using the distance values. On return, it is often a good practice to double check the wall positions. Once the micromouse has returned to the beginning, the maze is solved and the mouse can scamper off for a speed run to the center of the maze. So, the modified flood fill process for updating the distance values is: Figure 3: Updating Distance Values with Modified Flood Fill Algorithm [1] Using the algorithm from Figure 3, the modified flood fill algorithm now becomes the following and should be executed every time the mouse enters a new cell. 1. Update the wall map Activity #1 2. Update the distance values (only if necessary) (modified flood fill) Activity #2 3. Determine which neighbor cell has the lowest distance value Activity #3 4. Move to the neighboring cell with the lowest distance value Activity #4 4
Procedure Using your CodeWarrior Project and in particular your LCD functions from the previous labs, complete the following activities. Activity 1: Write a function that updates the wall map as the mouse travels through the maze keeping into consideration the direction the mouse is facing. For example, the mouse will start from position (0, 0) facing North using the directional notation shown below. To test your function, traverse the highlighted path in the figure below where the mouse starts in the bottom left cell of the maze facing the north direction. Reminder: If you chose to use the same directional notation shown above, your initialize wall function that was written in lab #1 should have been initialized as follows. Example: void UpdateWallMap (byte direction) // Based on direction of the mouse // Read each sensor value that the mouse can see. In this case, examine each wall that the mouse can see in its current orientation (Left, Right, Forward) 5
// Update the walls array You can use the following array if necessary to test your function. This wall map corresponds to the maze shown in the figure above. byte testwalls[13][6] = {{11, 0, 0, 11, 0, 0},{10, 0, 0, 10, 0, 0},{10, 0, 0, 10, 0, 0}, {2, 5, 5, 14, 0, 0}, {10, 0, 0, 3, 9, 0}, {10, 0, 0, 14, 10, 0}, {2, 9, 0, 0, 10, 0}, {14, 10, 0, 0, 10, 0}, {0, 10, 0, 0, 10, 0}, {0, 10, 0, 0, 10, 0}, {0, 6, 5, 5, 14, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}}; Activity 2: Write a function that implements the modified flood fill algorithm from Figure 3. This function updates the maze's distance values as the micromouse traverses the maze. Reminder: The maze distance value initialization function should have initialized the distance values as shown in Figure 4. Originally, it may be easier to debug your function using the Instruction Set Simulator and Real-time debugger. Figure 4: Initialized Distance Values Activity 3: Write a function that checks all the potential next moves to determine which neighboring cell has the lowest distance value (being careful on the outer walls for example there is no row 1 position when at position (0, 0) in the maze. Essentially, you will want to check the following cell s distance values to determine where to move: (row 1, row + 1, column + 1, and column 1). One suggestion in how to store this information is to store the row value in the upper nibble of a byte and the column value in the lower value of a byte. 6
Activity 4: Update your mouse s new position (x, y) move to the appropriate cell. Test your functions using the maze in Figure 5. Display the position of the mouse and distance value on the LCD as you use your algorithm to traverse the maze. Figure 5: Example Maze with wall values Lab Notebook 1. Include a brief description of what was accomplished in this lab. Be sure to include all functions with comments. (40 points) 2. Explain the role(s) of the proximity sensors in the micromouse algorithm (5 points). 3. If you were building the micromouse, how many sensors would you use? Why? (5 points). 4. Identify at least two other potential micromouse algorithms? Are the advantages or disadvantages to these algorithms over the modified flood fill algorithm? Explain (5 points) 5. List any problems that you encountered in the lab and suggestions for improvement of the lab. If no problems were encountered or you have no suggestions, please state NONE. (5 points) References 1. http://www.micromouseinfo.com/introduction/mfloodfill.html 7
8