Error: no such theme PatternSkinTheme
You are here: Foswiki>Main Web>CSAndRobotsExco>ExcoLab3 (2013-02-27, KateIngersoll)EditAttach


Set up (2 mins)

This section includes directions on connecting to your robot, and should be review:
  1. Open the terminal
  2. Use cd and pwd to move into your exco directory
  3. Open a second terminal window (remember that you want one for the python shell and the other for regular terminal commands)
  4. In your new terminal, open the python shell by typing python
  5. Turn on your robot, and import the myro library in the python shell with from myro import *
  6. Connect to the robot with the command init("/dev/rfcomm0")

Continuation of Lab 2: Writing and Importing Functions (20 mins)

Make sure that you are comfortable writing and importing your own functions: work through the remaining exercises in Lab 2 through the section on 'inefficient functions'. By the end of this section you should:
  1. Be able to write your own functions, import them into the Python shell, and run them
  2. Know how to write functions both with and without parameters
  3. Know how to re-organize repetitive lists of commands into nested functions
If you have any questions about the lab exercises, please ask!

Syntax reminders:
  1. To open up emacs (the program you use to edit functions), you can type: emacs & into the regular terminal.
  2. Every file that wants to use myro commands (forward, turnLeft, etc) must start with the line from myro import *
  3. To import a file into the python shell, type from filename import *

Introduction to Variables & Basic Math in Python


Type the following expressions into the terminal. Discuss the results with your lab partner.
  1. my_age
  2. my_age = 21
  3. my_age
  4. your_age
  5. your_age = 20
  6. your_age
  7. my_age + your_age

Type the following expressions into the terminal. Can you describe what each operation does? Can you describe in what order operations are calculated? Can you also discover how to subtract in Python?
  1. 5 + 4
  2. 5+4
  3. 5+4+7
  4. 5 + 1 * 3
  5. (5 + 1) * 3
  6. 5 * 1 + 3
  7. 5 * (1 + 3)
  8. 20/4
  9. 25/20
  10. 20/20
  11. 19/20
  12. 21/20.
  13. 21./20
  14. 19/20.
  15. As a class, we will write the following helper functions, using the fact that turnLeft(.5, 6.26) turns the robots approx. 360 degrees.
    1. turnLeft90() - turn left 90 degrees
    2. turnLeftDegrees(degrees) - turns left the given # of degrees
    3. turn180() - turns 180 degrees
  16. To get more practice with variables, we will demo an example of a robot song!

Lecture Notes: Variables in Python

  • What are variables?
    • As we discussed briefly last class, variables are names that we assign to values (such as numbers)
    • Think of a table of name/value pairs => the computer stores the "value" of the variable name, and can look up this value
    • Think of them as shorthand, or a "human readable" name for a value
  • Why do we use variables?
    • To make our code easier to understand
    • DRY- Don't Repeat Yourself

Drawing on paper (28 mins)

  1. Write the following functions, using the functions we wrote as a class
    1. square(side_length) --draws a square of the given size
    2. eq_triangle(side_length) --draws an equilateral triangle of the given size

Preview to Next Week (5 mins)

  1. Use the functions you've written, and the functions we've written together, you will work next class to write a function that makes the robot draw interesting drawings, or play a song, or both simultaneously! do an interesting song & drawing. In the last few minutes of class, brainstorm with a partner the function you want to create, and make a sketch/draft of your ideas on paper.
    1. Ideas for interesting drawings:
      1. Draw circles
      2. Draw spirals
      3. Draw some geometric pattern- "nested" shapes, spiraling shapes, nested & spiraling shapes, etc. Possibly search for "geometric patterns" for inspiration.
      4. Draw something representational (a hat, a cat, a dog)
    2. Ideas for other functions:
      1. A robot that plays notes while moving (a singing & dancing robot)
      2. Find a favorite song, and teach the robot to sing it
      3. Anything else that uses the things you've learned so far!

Music Reference:

Code for Kate's Mystery Song: SongExample

Tables of frequency/pitch: and

Treble Staff Notes:

Simple Songs:

Notes for "you are my sunshine" chorus:

Very Simple Sheet Music for "ode to joy":
Topic revision: r4 - 2013-02-27, KateIngersoll

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback