Week 7 Lab: Go, Robot, Go!

The goal of this project is to get you to use strings much more: defining strings, reading them in, parsing them, indexing into them, etc.

This lab is very similar to lab 6 in which your scribbler reacted to the commands you gave it interactively. In that lab, your code repeatedly asked for an instruction, and then executed that instruction. This time when your program starts it will ask once for a single string that contains a series of commands for the robot to execute. When your loop finishes executing that series of commands, the program is done.

The format of the string is pretty simple:

<command><val><command><val><command><val>...

where <command> is a single letter, and <val> is a two digit number. The acceptable <command>s are:

f - forward
b - backward
l - turn left
r - turn right
B - beep

Here is a good sample input string:

f03l45f10r90b13B03f19

This will tell the robot to go forward for 3 cms, turn left 45 degrees, go forward for 10 cms, turn right 90 degrees, go backward for 13 cms, beep for 3 seconds, and go forward for 19 cms. Notice that all numbers are 2 digits -- if you need a single digit number, you make it two digits by putting a 0 before it. E.g., 03 .

Step 1: Make the lab7 folder

Open up Eclipse, switch to your S:\workspace workspace, and create a new Folder called lab7. Next, Copy the move.py file over from your lab6 folder, or, copy it from moodle where I have made my version available. Then, in the lab7 folder, create a new file called lab7.py.

Next, open lab6.py (your own or the version I have available on moodle) and copy the entire contents of the file to your lab7.py file. A lot of this project is going to be similar. At this point I highly recommend you close any old files you have open, so that you don't accidentally edit them.

Make sure the top of your lab7.py file has:

from move import *
from myro import *

Step 2: Read in an instruction string from the user

Copy and paste this code into your ****main**** code in lab7.py:

# ----------------- main code ------------------------

# init("COM40") # Note that this is commented out, for now.
instr = getCommand() # get the instruction string
done = False
currIdx = 0 # index into instr string
while not done: # code here to get first character (at index currIdx) into a variable cmd
# code here to get the second and third characters (offset from currIdx) into an
# integer
called val
# code here to print out the cmd and val
# code here to increment currIdx by 3 (thus preparing for the next time through the loop)
# code to test if currIdx is greater than or equal to the length of instr.
# If so, set done to True.

The old ***main*** code you copied from lab6.py has the init() call, etc., and while not done code, etc. Remove that code, but keep the big if-elif code. But, then comment out that if-elif code for now. We'll use it later, but for now, let's just comment it out.

Now, replace the comments in the while loop with actual code.. Then, test your code. The robot won't do anything at this point, but you should be able to test it by adding print statements that show you have gotten the instruction correctly, have broken it into the cmd and val correctly, etc.

Step 3: Implement the code to make the robot move

Start uncommenting out your big if-elif-elif-else statement, like in lab6, so that the robot actually performs the commands it has been given. Hints:

Step 4: Modularize your Code

Step 5: Submit your code

To submit your final version of lab7.py and move.py, you’ll need to use Windows Explorer.


Grading Rubric: 15 points total

10 points: program that runs correctly. 
1 points: correct comments at the top of the file.
4 points: code is clean and neat and uses good comments and variable names. I.e., the code is hospitable.