Week 3 Labs

 

Part 1: Sine Wave Lab

Step 1: Find the lab and do the lab.

Go to the online textbook that we use. In the Python Modules section, click on "The random module". About half way down, you'll see a Lab called Sine Wave. Click on that and do that lab. Work with a partner.

NOTE: sometimes when you run code in the browser, it seems like nothing is happening. Be a little patient. If the code has to print out a bunch of lines of output, it takes 5 or 10 seconds in this environment.

NOTE 2: in the next step you have to copy your code from the browser environment into Eclipse and then submit from there. So, you could just work in Eclipse from the beginning if you want. Also, some students have seen some problems with drawing the sine wave in the textbook environment, and then it worked fine from Eclipse.

Step 2: Make the lab3 folder

Note that you do not submit your code in the online textbook. Our graders cannot see any code submitted there. Instead you need to copy your final code out of the browser there, and submit it like we normally do. Follow these steps:

Open up Eclipse. Make sure you are using your workspace on the S: drive -- should be S:\workspace. Once your workspace is correct, select CS104, right-click and create a new folder called lab3. Then, right-click on that folder and create a new file called sine.py.

Copy your code out of your browser workspace and into sine.py. Make sure you save the file there!

Before you submit your code, make sure that this information is in a comment block at the top of the file sine.py:

#
# CS104 Lab 3: <something here to describe this lab>
# <Your Name and your partner's name, if you have a partner>
# <Date>
#

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

This lab is worth 5 points. 
3 points: program that runs correctly. 
1 points: correct comments at the top of the file.
1 points: code is clean and neat and perfectly indented, with appropriate comments.

 

Part 2: Drawing Shapes


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

Step 1: Look at myro library command reference page

Take a quick look at this page: http://wiki.roboteducation.org/Myro_Reference_Manual. Pay special attention to the Movement Functions. You probably want to bookmark this page so that you can find it and refer to it in the future.

Step 2: Make the lab3 project

Open up Eclipse, right-click on the lab3 folder and create a new file called lab3.py. Paste the following code into lab3.py.



from myro import *    # pull everything from myro library into this program

# connect to the robot
init("COM40")

# draw a square
forward(1, 1)    # go forward at full speed for 1 second
turnLeft(1, .7)  # turn left for .7 seconds
forward(1, 1)
turnLeft(1, .7)
forward(1, 1)
turnLeft(1, .7)
forward(1, 1)
turnLeft(1, .7)

Try running this code and see if you get a square. (Don't forget that first you have to use bluetooth to connect to your robot and get a COM port allocated. Anyone who connects to someone else's robot gets to do penance by standing up in front of the class and singing the Albanian national anthem.)

Your Scribbler will probably not draw a perfect square. In fact, it might not even be close. We'll fix that coming up.

Step 3: Use some variables

Change your code so that instead of a line like this:

forward(1, 1)

your have a line like this:

forward(speed, numSeconds)

Before this line you'll need to create the two variables, speed and numSeconds, and assign the value 1 to each of them. Use these variables in all calls to forward(). Create another variable, leftSeconds, with the value 0.7, and use that variable in all calls to turnLeft(). (You should use speed, which refers the value 1, in the calls to turnLeft(), too.)

Change the value of leftSeconds so that you get the Scribbler to draw something that is close to a square. Change only the value stored in leftSeconds to refine your drawing. Don't take more than 5 minutes doing this.

Step 3.5: Use a for loop

The code we have does the two steps (forward, turn) 4 times. We should really just put that into a for loop. So, after the init() call, add a for loop line that will execute its body 4 times. Remember that the pattern of a for loop is:

for <var> in <list>:
<statements>

You'll want to use range() to make the list. Then, indent the forward() call and turnLeft() call so that they are the body of the loop. Delete all the other calls to forward() and turnLeft().

Step 4: Draw a spiral

Create a variable numTimes, and set its value to 10. Put this code just before your for loop.

Then, change your for loop so that it runs the body numTimes times, not 4 times.

Now, test your code: it should draw a "square" 2.5 times.

Next, add code inside the for loop body, after the call to turnLeft(), that updates the values of numSeconds and/or leftSeconds. For example, the code could add 1 to numSeconds each time, or could divide leftSeconds in half each time.

Try out your code and see what kind of spiral or shape your robot draws. DON'T LET YOUR ROBOT DRAW ON THE CARPET!

Play around, and see if you can create some fun spirals that spiral in or spiral out, etc. Perhaps you could use the golden mean as a factor in your code?

You could even duplicate the for loop code in the file and change values so that you'd get a spiral that spirals out and then back in again...

Step 5: Submit your code

You may submit your code for full credit when your code has a for loop that repeatedly goes forward and then turns left, and then updates leftSeconds and/or numSeconds. Put comments in your code above the lines that update leftSeconds/numSeconds, describing how those values are changed. E.g., the comment might say you are going to turn left 5% more each time.

Before you submit your code, make sure that this information is in a comment block at the top of the file lab3.py:

#
# CS104 Lab 3: <something here to describe this lab>
# <Your Name and your partner's name, if you have a partner>
# <Date>
#

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

This lab is worth 10 points. 
5 points: program that runs correctly. 
1 points: correct comments at the top of the file.
4 points: code is clean and neat and perfectly indented, with appropriate comments.