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 .
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 *
getCommand()
function so it
B03b10f05r45
.)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.
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:
robot.beep(duration, freq1, freq2)
Use variable val
for the duration. Good values for freq1
and freq2
are 880 and 660, but you can use any values you want. (Note that B is for beep and b is for backward. Make sure B does not make the robot go backward like it did before.) executeInstructions(instr)
, which you will place in move.py
. The definition of the getCommand()
function stays in lab7.py
. executeInstructions(instr)
should take the string given by the user as its only argument. The function returns nothing. So, your final main code should be just this, when you are done:init("COM40")
getCommand()
, storing the result in a variable called instr
.executeInstructions(instr)
lab7.py
that isn't relevant anymore (code that is commented out, etc.) Make sure your code is well-documented, indented, hospitable, etc. Make sure you have the correct information in it about you, the author.To submit your final version of lab7.py
and move.py
, you’ll need to use Windows Explorer.
lab7.py
and move.py
files down in your workspace\CS104\lab7
.lab7.py
and move.py
.T:\CS104\current\<yourid>\
.lab7
. (Do NOT call it Lab7
or lab 7
or Lab 7
or IGonnaBeAEngneer
. Call it lab7
.)lab7
? If not, grab your ears and flap them while pretending to fly around the room, and then rename the folder lab7
.lab7.py
and move.py
) into that folder.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.