Error: no such theme PatternSkinTheme
You are here: Foswiki>Main Web>CSAndRobotsExco>ExcoLab4 (2013-04-03, 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
We will do the first half of lab just using the python shell. When we start to use the robots, you can connect to your robot as follows:
  1. Turn on your robot, and import the myro library in the python shell with from myro import *
  2. Connect to the robot with the command init("/dev/rfcomm0")

Pseudo Lecture: Arrays and Loops (20 mins)

Introduction to Arrays

An array is data structure (type of memory layout) that stores an ordered collection (i.e., a list) of values.

Introduction to Loops

For loop: _For_ loops are traditionally used when you have a piece of code which you want to repeat _n_ number of times
for item in array []

While: As an alternative, there is the [[][WhileLoop]], however, _while_ is used when a condition is to be met, or if you want a piece of code to repeat forever

 =while True:=

Writing an Array (5-10 Min)

This is how to declare an empty array:


In these examples, 'array', 'also_an_array', and 'bunnies' are the names of the arrays, and [] is the collection of things in the array (in this case, nothing).
If you want the array to hold values, you list them between the brackets:

bunnies['Lop', 'hare', 'chocolate']

Creating & Editing Arrays

For each command in this section, predict the code will do. Then, c/p the command into the terminal and observe the output. What happens? Can you explain what each command does?
>>> a = ['cat', 'dog', 'mouse']
>>> a
>>> a[0]
>>> a[1]
>>> a[2]

>>> a[0] = 'moose'
>>> a[1] = 4
>>> a[2] = "monkey"
>>> a[0] = 3*5

>>> a.append("cat")
>>> a.append("dog")
>>> a.append("cat")

>>> a.count("cat")
>>> a.count("dog")

>>> a.pop(0)
>>> a.pop(0)
>>> a.pop(0)
>>> a.pop(0)

>>> range(5)
>>> range(30)
>>> range(-1)
>>> range(0)

As you've hopefully seen, the elements within an array are stored in order, and you say "give me the second thing in array a" by writing "a[2]". For historical reasons, computer languages often use zero-indexing (meaning that they start counting with 0, not 1) So, in the array a['Kate', 'June', 'Ji Soo'], a[1] is 'June', not 'Kate'.

You can change a value in an array by direct assignment with '='. The command a[0] = 4 will turn the above array into [4, 'June', 'Ji Soo']. You can only use '=' to update an existing value, not to add new values. a[3] = 5 will return an error, because there's no value in space a[3]. If you want to add a new value, you will need to use 'append', which adds an element to the end of an array.

As you've noticed, arrays can be used to store all sorts of values; a single array can store strings, numbers, and other types of data. This is not true in all programming languages; in some, arrays must always store the same type (all strings, all integers, etc).

pop will remove the last thing on an array, and return it to the user.

range is a shorthand way to make an array of integers, counting up. It cannot be used with strings or other data types. As you shall see in the next section, ranges can be useful when writing loops in python.

Writing Loops (5-10 Min)

For each example in this section, predict what each for loop will do. Then, c/p the definition into the terminal and observe the output. What happened? What is the behavior of the loop?

For Loop example:
the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# this first kind of for-loop goes through a list
for number in the_count:
    print "This is count %d" % number

# same as above
for fruit in fruits:
    print "A fruit of type: %s" % fruit

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
    print "I got %r" % i

for i in range(14):
    print "I just wanted to do something 14 times!"

As you've hopefully seen, for-loops can be a useful way to do the same action for every element in an array. The syntax for a for-loop is:



A for-loop will assign the variable VARIABLE to the first element in the array, execute the commands in the body of the for-loop, assign the variable to the second element in the array, and so on, until it's gone through the entire loop. For example, the loop

for x in [1, 2, 3]

print x

Is equivalent to:

x = 1

print x

x = 2

print x

x = 3

print x

The for-loop is just a shorter way of writing these commands.

While Example:

Run the following code:
temperature = 115  
while temperature > 112: 
    temperature = temperature - 1

print('The tea is cool enough.')

While-loops are similar to for-loops, except that, instead of iterating through an array, a while loop tests a condition to see whether it should loop again. So, in the example above, the while-loop is understood by the computer as follows:

"Is 115 > 112?" Yes. So, print temperature, and subtract 1 from temperature.

"Is 114 > 112?" Yes. So, print temperature, and subtract 1 from temperature.

"Is 113 > 112?" Yes. So, print temperature, and subtract 1 from temperature.

"Is 112 > 112?" No, so we're done.

Before going on to work with the robots, write your own arrays, for-loops, and while-loops to make sure you understand how they work. Some options:

Write an array with integers. Then try to get the number of a particular index.

Write an array with strings, then try to get the string of a particular index.

Using your integer array, write a 'for' loop.

Using your string array, write a 'while' loop.

More commands for arrays, if you're interested:

ROBOTS and loops (20 Min)

Possible exercises:

0.a) Write a loop to have the the robot play the following frequencies in order: [400, 500, 600, 700]

0.b) Write a loop to have the robot play the last loop three times.

0.c) Adapt the your code from parts 0.a-0.b so that the robot beeps the frequencies [400, 500, 600, 700] at 1 sec each, then all the freq. again at 2 sec each, then again at 3 sec each.

1) Using the robot, try to loop through several movements, possibly from code you've already written for class.For example, if you wrote a "draw a square" function, write a loop to draw 5 squares, or to draw a square with the side-lengths given in the array [2, 4, 6, 8], for example. You can use 'While' to dance forever, or use 'For' to dance for a limited amount of times.

EXTREME EXERCISE: If you would like to, define several FUNctions and have the robot loop through these different dance functions to have a much longer complicated dance.

Looping through a song (20 Min)

For this exercise, try to accomplish the following:

Using a song template, have the robot 'sing'.

Use a loop within a loop to repeat certain parts of the song

Use a loop to repeat the entire song twice/thrice.

Tables of frequency/pitch: and

Link to You're Beautiful by James Blunt

These are the notes you may need for thsong and the corresponding hertz values

c: 277.18

c/b 554.37

Am: 440.00

F: 369.99

G: 392.00

Eb: 311.13

Bb: 466.16

Cm: 277.18

Ab: 415.30

-- AdrianaHurley - 2013-04-03
Topic revision: r6 - 2013-04-03, 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