Week 4 Lab: Moving Scribblers by centimeters and degrees


In this lab, you will practice writing code ...

Step 1: Make the lab4 project

Open up Eclipse. Make sure you are using your workspace on the S: drive -- should be S:\workspace. Once your workspace is correct, create a new Folder in your CS104 project, called lab4. In this folder, create a new file called lab4.py.

Put this code into your file:


# top comment block here

# imports
from myro import *

# constants defined here

# functions defined here

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

# connect to the robot
init("COM40")


This code is a basic template for a python program, defining the areas where you should put code. It is the python programming convention that your code goes in this order. As you fill in these sections, you may take the comments out, although it is nice to have the # ----- main code ----- left in.

Step 2: Compute distance per second

The myro library for interfacing with the Scribbler is not so great, because what we often want to do is to tell the robot to go forward a certain distance -- not go forward for a certain amount of time. But, with a little work, we can create our own function that makes the robot go forward for a certain distance.

Write code in your file (in the "main" section) so that your Scribbler goes forward at full speed for 1 second and then backward for 1 second (recall that full speed is the value 1.0). Recall that the command to make the scribbler go forward for 1 second is:

forward(1.0, 1)

(The first parameter is the speed -- 1.0 means full speed. The second parameter is the time -- 1 second in this case.)

Now, add the code below the forward() call to make it go backward at full speed for 1 second.

Put your Scribbler down on a whiteboard on the floor, insert a pen, and try it. (Your Scribbler might not end up exactly at the same place. Don't worry about it. Sometimes if you run the program again, your results will be a little better.) Get out a ruler or tape measure and figure out how many centimeters the scribbler goes, per unit time. (Rulers are in the drawer with the pens, erasers, etc.

Create a variable in your code, CENTS_PER_SEC,that holds the number of centimeters your Scribbler travels per second, according to your calculations. Put this variable in the constants section of the code. Add a comment on the line above this variable declaration that describes what CENTS_PER_SEC means. (Note that this "variable" is not going to be varied, so we'll treat it like a constant, and give the name in all caps, as is the python convention.)

Step 3: Create a function travelForw()

In the function definitions section of your file, add this code, replacing the <your code here> lines with your code:

def travelForw(dist):
    """Make scribbler go forward for dist centimeters.
    """
    # replace <your code here> with a line of code to compute the time needed to 
    # go dist centimeters, using the variable CENTS_PER_SEC in your calculation.
    # You'll want to make a new variable to refer to that computed value.  I used
    # the variable name secsPerCent in my implementation.
    <your code here>
    # Replace next line with a call to forward() with proper values.
    <your code here>

Put the following code in your "main" part of the file.


print("Going forward 10 centimeters")
travelForw(10)

Run your code and see if your line is 10 centimeters long. Note that it does not have to be exact. Anything between 9 and 11 centimeters is really close enough, as far as I'm concerned. (Remember this is a CS class, not an engineering class. :-)

Create a second function travelBack(dist) that makes your Scribbler go backwards by the given distance. Add a line of code in your main code area to run travelBack() to see if it works correctly.

Step 4: Create a function turnLeftDegrees()

We want to create a function that takes as a parameter a value in degrees and then turns left that number of degrees. But, remember that the turnLeft() function provided for us in the myro library takes a number of seconds to turn -- not a very convenient method, IMO. So, to create our function that turns by degrees, we will (and by "we", I mean "you") have to figure out how far the Scribbler turns in a second.

How are you going to calculate the number of seconds it takes to turn a certain number of degrees? Hint: what if you make your scribbler go forward for a few centimeters, turn 180 degrees, and then go forward again for a few centimeters? With some trial and error, you should be able to figure out how long it takes to turn 180 degrees. After that, you can compute how many degrees it turns per second.

Before we make a funciton to turn left by a certain number of degrees, let's figure out the code that will go in the function. So: replace the code in the main area of your file with this code:

travelForw(20)
turnLeft(1.0, <somevalhere>) # replace <somevalhere> with a value in seconds to turn
travelForw(20)

Experiement with adjusting how long you turn left until you have your scribbler turning 180 degrees (plus-or-minus a few %). While you are doing this, you might find it useful to learn the keyboard shortcut to run your program: Ctrl-F11.

When you have figured out how long it takes to turn 180 degrees, calculate how many degrees the Scribbler turns in one second. Then, define a variable DEGS_PER_SEC underneath your CENTS_PER_SEC definition, that is set to the number of degrees that scribbler turns per 1 second. Add a comment above this declaration that explains what DEGS_PER_SEC means.

Now, create a function definition like the code given below.

def turnLeftDegrees(degs):
    """Make Scribbler turn left the given number of degrees"""
    <your code here using myro's turnLeft() function and your DEGS_PER_SEC variable>

Fill in the code in the above function. Then, test this function by replacing your code in your main area to use turnLeftDegrees() instead of calling turnLeft() directly.

Finally, create a function turnRightDegrees() and make sure it is correct, by calling it from your main code.

Step 5: Draw a n-sided polygon

Create a function using this code, verbatim:

def drawNgon(numSides, sideLen):
    """Draw a n-sided regular polygon, with side of length 
sideLen centimeters.
""" for i in range(numSides): # do the following numSides times. <your code here. It may use either turnLeftDegrees() or turnRightDegrees().>

Replace the <your code here...> line with a few lines of code to compute the angle you need to turn to make a regular polygon with numSides, and then a call to travelForw() and a call to either turnLeftDegrees() or turnRightDegrees(), whichever you choose.

Replace all your "main" code you have in place already with this main code to call your function:


init("COM40")

nSides = int(input("Enter number of sides for polygon: "))
sideLength = float(input("Enter length of each side, in cm: "))
drawNgon(nSides, sideLength)

Your file should only have this in it when you are done:

Step 6: Submit your code

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


This lab is worth 15 points. 
8 points: program that runs correctly: 2 pts for correct travelForw() and travelBack(), 2 pts for correct turnLeftDegrees() and turnRightDegrees() and 2 pts for a correct drawNgon(). 2 pts for correct main code.
1 point: correct comments at the top of the file.
6 points: code is hospitable. Good variable names are used. Good comments used. Good spacing between operators. Good use of blank lines to show related blocks of code. No old extraneous comments left behind...