RBI Chapter 7

From IPRE Wiki
Revision as of 18:05, 14 May 2008 by Mansi (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

if person == girl:
	speak(“I am a girl”)
else:
	speak(“I am a boy”)

In order to add these, we can simply say:

>>> x + y
11

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.

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?

if person == blonde:
	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”)

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:

x = 0.5
forward(x, 1) 
turnLeft(x, 2)
turnRight(x, 5)
forward(x, 10)
turnRight(x, 7)

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:

else:
	speak(“I am a boy”)

instead of

if person == boy:
	speak(“I am a boy”)

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.

Loops: Redraw the Square
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:

from myro import *

forward(4, 0.5)
turnRight(1, 0.6)

forward(4, 0.5)
turnRight(1, 0.6)

forward(4, 0.5)
turnRight(1, 0.6)

forward(4, 0.5)
turnRight(1, 0.6)

forward(4, 0.5)
turnRight(1, 0.6)

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:

forward(4, 0.5)
turnRight(1, 0.6)

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:

def step():
	forward(4, 0.5)
	turnRight(1, 0.6)

You have just defined a function! A function defines a sequence of commands that you want your computer or robot to follow.

Note: While in this example, the function is called “step”, you can call it anything you want.

Next, you want to tell your robot to repeat step() 5 times. To do this, you can use loop() in the following way:

>>> loop(function, number of repetitions)

In this case, you would write:

>>> loop(step, 5)

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!).

How would you make a triangle?

Activity: Maze
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:

def walk():
	if wall():
		if heads():
			turnLeft(0.7, 0.2)
		else:
			turnRight(0.7, 0.2)
	else:
		forward(0.7)

Is that too complicated? Don’t worry; you know enough to understand it!

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.

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:

>>> loop (walk, 100)

Good luck!



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

CREU Project Home.