Lab 6A: aMazing Solution/ WhichWay

Lab 6 is worth 30 points. Shortest Path is worth 10 points, Your Path is worth 10 points, In Forest (Lab 6B) is 5 points, and Count Bees (Lab6B) is 5 points.

The WhichWay subroutine that was written for Lab 3 tells the bear which way to go by setting switch positions SW1 and SW0 for the direction and SW3 and SW2 for which way to turn. We will now be updating it to navigate through the maze using the logic developed in Prelab 1.

In this lab, you will first create an assembly version of the shortest path solution called ShortWhichWay with. Next, you will implement the flowchart you wrote in PreLab 1 and translated into assembly code as part of PreLab 6.

When you are done teaching the bear how to get out of the maze by himself, you will no longer need any of the eight switches as inputs. No more switches!

Table of Contents

What is New?

Here are some of the new concepts you will be exploring in this lab.

  • Translating a flow chart and high-level code into assembly.

No new assembly directives or instructions are introduced in this lab.

Create the Lab 6 Project

If you are using a lab computer, I would recommend saving all of your files to a flash drive. Do not forget to save often. At the end of the lab, do not forget to save the latest version and take your flash drive with you.

  1. Open AVR Studio and create a new Project named Lab06 (see Lab 1 for detailed instructions)
  2. Copy over maze.inc, pseudo_instr.inc, spi_shield.inc, testbench.inc, and upload.bat from Lab 5.
  3. Open the upload.bat text file in notepad (right-click edit) and change the filename from Lab05.hex to Lab06.hex
  4. Copy the text in Lab05.asm into Lab06.asm
  5. Update the information in the title block.
  6. Build your project and verify you are starting with zero errors and zero warnings.

Before You Start

  Verify that SRAM variable dir is initialized so the bear is facing North (dir = 0x03).
  Verify that SRAM variable room is initialized so the bear is in an empty room (room = 0x00).
  Verify that SRAM variable next_state is initialized to S0 (next_state = 0x00).
  Verify that SRAM variable row is initialized to the starting location just outside of the maze. (row = 0x14).
  Verify that SRAM variable col is initialized to the starting location (col = 0x00).
  Verify that SRAM variable walk is initialized so the bear does not automatically take a step. (walk = 0x00).

A Little House Cleaning

Open the pseudo_instr.inc include file in AVR Studio. You need to Build your project (step 6 in “Create the Lab 6 Project” for the include files to be shown in the Project window.

Move the subroutines TakeAStep, EnterRoom, and IsHallway created in Lab 5 from the Lab6.asm main application to the pseudo_instr include file. Optionally, you may delete TestIsHallway or also move it to the pseudo_instr include file. You may notice that in the figure it shows Lab6A not Lab6.asm. I created a new project for each part of a lab. This allows me to easily move from one to the other. You have the option of simply making a copy of your labs so that you can step back in time.

At this time, the main application Lab6.asm should only have the initialization (reset), main loop (loop), the interrupt service routine (INT0_ISR) and the subroutines Delay and Pulse. All other subroutines should be located in include files or deleted. Any subroutines created for design challenges should also be moved to the include files.

Part I – Shortest Path

Before you implement your solution, let’s verify that your nearly complete program is working by implementing the shortest path (fewest bees) solution to the maze.

Step-by-Step Solution

  1. Remove the code that updates the variable dir based on switch positions from the main program. In the main program, you read the switches and set variable dir. You no longer use the switches on the shield to set dir, or anything else for that matter, so delete this section of your program.
  2. Add a call to a to-be-written ShortWhichWay subroutine. Place the subroutine call after the bear takes a step and enters a room (State S2). As shown here, the direction variable dir is updated after each call to ShortWhichWay.
  3. Copy and Paste my ShortWhichWay subroutine which implements the shortest path solution. I have superimposed the Arduino version of the WhichWay subroutine from Lab06A.ino so you can compare the two implementations.

; — Which Way Do I Go? —
; Called from main program
; Input: r24 = direction, r22 = room
; Output: r24 = direction
; No registers or flags are modified by this subroutine
ShortWhichWay:

mov r17, r24 // save to temporary register
// if(!rightPaw(dir, room))
rcall RightPaw // r22 = room, r24 = dir
tst r24
brne else_if_hitwall
// dir = turnRight(dir);
mov r24, r17 // move direction to calling argument
rcall TurnRight // new direction now in r24
rjmp which_end
// else if (hitWall(dir, room))

else_if_hitwall:

mov r24, r17
rcall HitWall // r22 = room, r24 = dir
tst r24
breq else
// dir = turnLeft(dir);
mov r24, r17
rcall TurnLeft // new direction now in r24
rjmp which_end

else:

mov r24, r17 // do not change direction

which_end:

ret

Assemble and upload your new program. If everything is working, your bear should now be able to navigate his way out of the maze, encountering the fewest number of bees. You should only need to push the step button when a decision needs to be made (corner, intersection, or encounters bees). And the really good news is you no longer need to toggle any switches!

Test Your Program

You may upload your program to quickly check to see if it works. An updated owner’s manual is provided in the next section to help you step through the maze. I would be surprised if your program works the first time in which case you will need to simulate the steps defined in the owner’s manual. See “Simulation and Debugging” for help here.

Updated Owner’s Manual

Follow these steps to navigate a maze.

  1. Download and run the program. The bear should be shown at the entrance of the maze – an empty room. The north facing direction 7-segment LED should be blinking instructing the bear to walk north. Toggling the switches should have no effect.
  2. Push the button to provide the bear with the directions. You should now see the room you entered displayed along with the direction in which the bear should now proceed.
  3. Repeat step 2 until the bear makes it to the forest.
  4. Push the button telling the bear to take a step. The bear should now be shown in the first room – a room with a west facing wall. You should also see the direction in which the bear should now proceed as defined by your WhichWay subroutine.
  5. Push the button to provide the bear with the direction. You should now see the bear walk (hallway blinks on and then off) until a decision needs to be made (a corner, fork in the road, or bees). The room the bear is in along with the new direction (blinking) in which the bear is facing, as defined by your WhichWay subroutine, should now be displayed.
  6. Repeat steps 5 until the bear makes it to the forest. What should be displayed on the seven segment display when the bear enters the forest is for now, undefined, so anything displayed is okay.

Simulation and Debugging

If your program does not work, then of course you will need to use AVR Studio’s simulator/debugger.

To faithfully simulate your program you will need to manually set the timer flag and trigger an external (button) interrupt as your bear travels the maze. If your bear gets confused fairly deep into the maze then set the row and column a few steps before the problem area and then set the timer flag and trigger an external (button) interrupt for the few steps needed for him to encounter the problem.

Don’t forget these helpful tips. You can right-click any variable and add it to the watch window. If the watch window does not already show values in hexadecimal, right click inside the window and select “Display all Values as Hex.”

Part II – My aMazing Solution

Replace the shortest path solution to the maze with the path defined by your flowchart from PreLab 1 as implemented by your WhichWay assembly subroutine written in PreLab 6. Rename that subroutine to be MyWhichWay in order to avoid any confusion over which version of WhichWay you are calling (from lab 3, the shortest path, from prelab 6).

This portion of the lab is not optional!

A Bug Story

Like you, I downloaded my program hoping it would work immediately and of course it did not. The direction segment stayed pointing North and a closed room appeared on the second step. My first course of action was to simplify, so I commented out most (I thought I had commented out all) of the new code and replaced it with the original WhichWay code. I then uploaded my simplified program and the problem did not go away. I suspected I had somehow broken the original version; but how? After simulating my call to WhichWay I rediscovered that the part of the code that called WhichWay saved the new direction returned in r24 to variable dir. I thought the new WhichWay did this step. Going back to WhichWay I discovered that I had not commented out a new section where I added push and pop r24 instructions to make my subroutine more transparent. Instead it broke the program. I removed the push/pop and my bear was on his way again.