Difference between revisions of "RBI Chapter 7"

From IPRE Wiki
Jump to: navigation, search
 
Line 18: Line 18:
 
[[Image:slinky.jpg.jpg]]
 
[[Image:slinky.jpg.jpg]]
  
<pre>
+
Sound waves work similarly. When your friend talks to you, the sound travels in your direction in the form of waves to deliver the message to you. The number of cycles that a sound wave completes in a second is known as its frequency.
if person == girl:
+
speak(“I am a girl”)
+
else:
+
speak(“I am a boy”)
+
</pre>
+
  
In order to add these, we can simply say:  
+
Now, let’s put sound and frequency together: A sound wave with a frequency of 300 Hz completes 300 cycles in a second. While humans can hear frequencies ranging from 20 Hz to 20,000 Hz, your robots only function well between a range of 200 Hz to 9000 Hz.
 
+
<pre>
+
>>> x + y
+
11
+
</pre>
+
  
Notice the first line of the code: there is a double “equal to” sign. Whenever we want to compare a variable to something, we use the double “equal to” sign. In this case we were checking if the person is a girl. Hence, we wrote: if person == girl. We use “=” to assign values to variables, as you have been doing.
+
'''Let's Beep!''' <br>
  
Whenever there is an “if” in your code, you must have an “else:” or “elif:” (which stands for else if) following the “if.” Let us look at an example where we use “elif.” Suppose we want to separate the blondes, brunettes, red-haired and people with any other hair color in the classroom and we want each person to say which group they fall into. How will the code for it look?
+
To make your robot beep a random note, type in the following:
  
 
<pre>
 
<pre>
if person == blonde:
+
>>> beep()
speak (“I am blonde”)
+
elif person == brunette:
+
speak (“I am brunette”)
+
elif person == redhaired:
+
speak (“I am red-haired”)
+
else:
+
speak (“I am not any of those”)
+
 
</pre>
 
</pre>
  
Then you decide that your robot is going too slow and you want to increase the speed to 0.8. In order to do this, you will have to go through all 5 steps and change 0.5 to 0.8. A better way would've been to write it this way:
+
This note played only for half a second. If you want to play a longer note, you can give the function the number of seconds you want it to play for inside the parentheses.  
  
 
<pre>
 
<pre>
x = 0.5
+
>>> beep(time)
forward(x, 1)
+
turnLeft(x, 2)
+
turnRight(x, 5)
+
forward(x, 10)
+
turnRight(x, 7)
+
 
</pre>
 
</pre>
  
Now can you see the difference between else and elif? In the first example, we used else instead of elif because there were only two options. Look at the third line of the first example again. It says:
+
To make your robot play a note with a certain frequency, you can put this value inside the parentheses as well in the following way:
  
 
<pre>
 
<pre>
else:
+
>>> beep(time, frequency)
speak(“I am a boy”)
+
 
</pre>
 
</pre>
  
instead of
+
For example, to make your robot beep the first note in “Do-Re-Me…”, type in the following:
  
 
<pre>
 
<pre>
if person == boy:
+
>>> beep(1, 260)
speak(“I am a boy”)
+
 
</pre>
 
</pre>
  
This means that if the person is not a girl, then that person has to be a boy. There are no other conditions. So writing only “else” is enough. However, in our second example, if a person is not blonde, he/she can have black hair or red hair or hair of some other color. It has more than two conditions. So we need to have the “elif” statements to take those other options into consideration. <br>
+
Therefore, what you just typed in made your robot beep at a frequency of 260 Hz for 1 second.  
 +
 
 +
<b>
 +
Remember that frequency values can range from 200Hz to 9000 Hz. Time values should range from 1 second to 5 seconds.</b>
 +
<br>
 
<br>  
 
<br>  
  
'''Loops: Redraw the Square''' <br>
+
'''Make Tunes!''' <br>
ARemember what you learned about loops last class? Let's apply the idea to an activity you have done before. After doing this, you will understand the importance of loops!
+
  
We have already taught you how to put your commands in a file so that they will run in a sequence. Remember how you made your robot walk like a guard in the second class? You wrote something like this:
+
When you typed in any of the commands shown above, you only heard one beep. What if you wanted a bunch of notes together? That’s right, you should use loops.
  
 
<pre>
 
<pre>
from myro import *
+
>>> loop(beep, 5)  
 
+
</pre>
forward(4, 0.5)
+
turnRight(1, 0.6)
+
  
forward(4, 0.5)
+
This command will beep five notes with random frequencies.  
turnRight(1, 0.6)
+
  
forward(4, 0.5)
+
Note: In the loop function, you will not be able to put in values for time or frequency.
turnRight(1, 0.6)
+
  
forward(4, 0.5)
+
If you want to be able to give the beep function different frequency values to play in a sequence, you must create a list. Here’s how you would do that: First, you would give your list a name – this can be anything, just like you learnt how to store variables. We will call our list “tune.
turnRight(1, 0.6)
+
  
forward(4, 0.5)
+
<pre>
turnRight(1, 0.6)
+
>>> tune = [200, 500, 300, 500, 600]
 
</pre>
 
</pre>
  
Repeating commands like this is very inefficient. What if you could write one command which would tell it to repeat a command, or a number of commands, 5 times? Fortunately, there is a way to do that – using loops. It's very simple. First we need to decide what we are repeating. In this example, it is repeating the following two lines of commands:
+
Your list can contain numerous frequency values separated by a comma. Values of each list should be contained within square brackets as shown above.  
 +
 
 +
You can now have the beep function play your list of frequencies by typing:
  
 
<pre>
 
<pre>
forward(4, 0.5)
+
>>> beep(0.5, tune)
turnRight(1, 0.6)
+
 
</pre>
 
</pre>
  
We can give this sequence a name: let’s call it step. This means that you have defined the sequence as step. To do this, you must start with def step(), so it will look like this:  
+
So the format is:
  
 
<pre>
 
<pre>
def step():
+
>>> beep(time, list)
forward(4, 0.5)
+
turnRight(1, 0.6)
+
 
</pre>
 
</pre>
  
You have just defined a function! A function defines a sequence of commands that you want your computer or robot to follow.
+
Each note will beep for the same amount of time. If you are putting you’re frequencies in a list, you cannot play the notes for different amounts of time.
  
Note: While in this example, the function is called “step”, you can call it anything you want.
+
Note: Do not use the word “list” to name your variables or lists. This is a reserved word.
  
Next, you want to tell your robot to repeat step() 5 times. To do this, you can use loop() in the following way:
+
What if you want your robot to play “Do-Re-Me…”? The first step is to make your own list of frequencies. Let’s try playing “Do-Re-Me…” Table 1 shows the frequencies needed to play this tune.
  
 
<pre>
 
<pre>
>>> loop(function, number of repetitions)
+
Note Frequency (Hz)
 +
C 260 Hz
 +
D 290 Hz
 +
E 330 Hz
 +
F 350 Hz
 +
G 390 Hz
 +
A 440 Hz
 +
B 490 Hz
 +
C` 520 Hz
 +
Table 1
 
</pre>
 
</pre>
  
In this case, you would write:
+
Create a list called doReMe that will contain all of these frequencies. To do this, we must type:
  
 
<pre>
 
<pre>
>>> loop(step, 5)
+
>>> doReMe = [260, 290, 330, 350, 390, 440, 490, 520]
 
</pre>
 
</pre>
  
Easy? How would you make your robot draw a square in this manner? You want to figure out what you need to repeat in order to make a complete square (remember what you did in Robot Review!).
+
Then, we must use the beep function:
  
How would you make a triangle?
+
<pre>
<br>
+
>>> beep(doReMe)
<br>
+
</pre>
  
'''Activity: Maze''' <br>
+
You can use a list to play a scale going up or down according to the frequencies you put in your list, the way you just did when trying to play the “Do-Re-Me…” song. However, it does take a while to a put in all of those frequencies. What if you want your robot to play 30 increasing notes? You would have to enter 30 values in a list and then play it. Instead, you can use the scaleUp and scaleDown functions. Type in the following to hear an increasing scale:
Now you will use your knowledge of if-else statements to write a program of your own to steer your robot through a maze set up for you in your classroom. You will have to use if-else statements to find your way through the maze to the end.
+
  
So what code do you think you will need to write to make this work and how will you write it? Think about what are the main challenges are in moving through a maze. When your robot is moving forward and it faces an obstacle, you can ask it to turnLeft and move forward again. Or you can ask it to turnRight instead. It can be done in many different ways. For example, you can define a function called walk in the following way:
+
<pre>
 +
>>> loop(scaleUp, 15)
 +
</pre>
 +
 
 +
and the following to hear a decreasing scale:
  
 
<pre>
 
<pre>
def walk():
+
>>> loop(scaleDown, 15)
if wall():
+
if heads():
+
turnLeft(0.7, 0.2)
+
else:
+
turnRight(0.7, 0.2)
+
else:
+
forward(0.7)
+
 
</pre>
 
</pre>
  
Is that too complicated? Don’t worry; you know enough to understand it!
+
These functions will play 15 notes going up or coming down according to the function you type in.
  
def walk() is the definition of this function that we have named walk. Remember, you can name it whatever you want. heads() is a function which models the flipping of a coin. When you flip a coin, 50% of the time, you will get heads and the other 50% of the time you will get tails. Thus, heads() returns True 50% of the time and False the other 50% of the time. In the above example, if heads() is True, then the robot will turnLeft, else it will turnRight. This is just to randomize your robot’s movement, so that it will turnLeft half the time and turnRight half the time. If your robot does not see a wall, it will continue going forward. Easy, right? You can change the above code to other similar commands and values that you have learned.  
+
Note: You will not be able to specify any frequency or time values for the scaleUp and scaleDown methods.  
  
So what happens when you run this code? Your robot will move forward if there is no wall, or stop and turnLeft or turnRight when it sees a wall, and then it stops. What should we do to make it repeat itself? That’s right, we should use loops! You can repeat the walk function 100 times by typing:
 
  
<pre>
+
<br>
>>> loop (walk, 100)
+
<br>
</pre>
+
  
Good luck!
+
'''Activity: Compose!''' <br>
 +
Your robot can make more music! Use what you have learnt today to play the rhyme Twinkle Twinkle Little Star.
  
 
<br>
 
<br>

Revision as of 18:19, 14 May 2008

Chapter : Beep, Beep, Beep

Things You Will Learn

  • Basics of Frequency & Sound
  • Making your robot beep


Introduction
When you used your gamepads, you must have noticed that your robot can beep. The gamepad only allowed the robot to beep in three different tones. What do you think determines these tones? Frequency is what allows the robot to play different tones. Let’s learn more about frequency.

Frequency & Sound
What is your favorite sport? Basketball, swimming, bicycling? Do you sometimes measure the amount of time it takes for you to shoot 10 baskets, the amount of time it takes for you to complete 2 laps when you go bicycling or swimming? Well, frequency is the opposite measure – it measures how many baskets you can shoot in one second or how many or much of a lap you can finish in one second. Like time is measured in seconds, the units for frequency are Hertz (Hz). Thus, 1 Hz means one lap or basket or cycle per second.

A wave is made up of many cycles. One such cycle in a wave is shown in Figure 1. You can make similar waves using a slinky. If you move one end of the slinky back and forth repeatedly while the other end is held steady, you will produce such waves.

Slinky.jpg.jpg

Sound waves work similarly. When your friend talks to you, the sound travels in your direction in the form of waves to deliver the message to you. The number of cycles that a sound wave completes in a second is known as its frequency.

Now, let’s put sound and frequency together: A sound wave with a frequency of 300 Hz completes 300 cycles in a second. While humans can hear frequencies ranging from 20 Hz to 20,000 Hz, your robots only function well between a range of 200 Hz to 9000 Hz.

Let's Beep!

To make your robot beep a random note, type in the following:

>>> beep()

This note played only for half a second. If you want to play a longer note, you can give the function the number of seconds you want it to play for inside the parentheses.

>>> beep(time)

To make your robot play a note with a certain frequency, you can put this value inside the parentheses as well in the following way:

>>> beep(time, frequency)

For example, to make your robot beep the first note in “Do-Re-Me…”, type in the following:

>>> beep(1, 260)

Therefore, what you just typed in made your robot beep at a frequency of 260 Hz for 1 second.

Remember that frequency values can range from 200Hz to 9000 Hz. Time values should range from 1 second to 5 seconds.

Make Tunes!

When you typed in any of the commands shown above, you only heard one beep. What if you wanted a bunch of notes together? That’s right, you should use loops.

>>> loop(beep, 5) 

This command will beep five notes with random frequencies.

Note: In the loop function, you will not be able to put in values for time or frequency.

If you want to be able to give the beep function different frequency values to play in a sequence, you must create a list. Here’s how you would do that: First, you would give your list a name – this can be anything, just like you learnt how to store variables. We will call our list “tune.”

>>> tune = [200, 500, 300, 500, 600]

Your list can contain numerous frequency values separated by a comma. Values of each list should be contained within square brackets as shown above.

You can now have the beep function play your list of frequencies by typing:

>>> beep(0.5, tune)

So the format is:

>>> beep(time, list)

Each note will beep for the same amount of time. If you are putting you’re frequencies in a list, you cannot play the notes for different amounts of time.

Note: Do not use the word “list” to name your variables or lists. This is a reserved word.

What if you want your robot to play “Do-Re-Me…”? The first step is to make your own list of frequencies. Let’s try playing “Do-Re-Me…” Table 1 shows the frequencies needed to play this tune.

Note	Frequency (Hz)
C	260 Hz
D	290 Hz
E	330 Hz
F	350 Hz
G	390 Hz
A	440 Hz
B	490 Hz
C`	520 Hz
Table 1

Create a list called doReMe that will contain all of these frequencies. To do this, we must type:

>>> doReMe = [260, 290, 330, 350, 390, 440, 490, 520]

Then, we must use the beep function:

>>> beep(doReMe)

You can use a list to play a scale going up or down according to the frequencies you put in your list, the way you just did when trying to play the “Do-Re-Me…” song. However, it does take a while to a put in all of those frequencies. What if you want your robot to play 30 increasing notes? You would have to enter 30 values in a list and then play it. Instead, you can use the scaleUp and scaleDown functions. Type in the following to hear an increasing scale:

>>> loop(scaleUp, 15)

and the following to hear a decreasing scale:

>>> loop(scaleDown, 15)

These functions will play 15 notes going up or coming down according to the function you type in.

Note: You will not be able to specify any frequency or time values for the scaleUp and scaleDown methods.




Activity: Compose!
Your robot can make more music! Use what you have learnt today to play the rhyme Twinkle Twinkle Little Star.



Chapter 1.
Chapter 2.
Chapter 3.
Chapter 4.
Chapter 5.

CREU Project Home.