looping forever reading/observing sensors (inputs) reacting to sensors obstacle sensors

32
Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

Upload: lesley-haynes

Post on 28-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

Looping foreverReading/observing sensors (inputs)

Reacting to sensorsObstacle sensors

Page 2: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

Learning objectives:

1. Introduce the forever loop.

2. Introduce ifs.

Page 3: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors
Page 4: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

Looping foreverRepeats

whatever is inside of the loop forever.

(It can also optionally repeat something a specific number of times.)

Page 5: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

Looping foreverWhat does this program do?

Page 6: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

Looping forever1. Remove the first pause and run it.

Q1: What do you observe?

2. Put the first pause back in and remove the second one. Run it.Q2: What do you observe?

3. Remove all pauses. Run it.Q3: What do you observe?

4. Using the Scribbler programming GUI, remove the start of the forever loop. Don’t run it!Q4: What happened (when you removed the start

of the forever loop)?

Page 7: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors
Page 8: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

Program P1: Artificial Life

Program P1 artificial life (?)

Write a Scribbler program that does the following:1. Move the Scribbler forward for about ¼ second.2. Pause for about one second.

It should repeat this sequence of operations forever.You should also cause one or more of the LEDs to

turn on and off (sort of like a heartbeat) as well.Send a screen shot and the .scb file to me.

Page 9: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors
Page 10: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

What can we read/observe?1. Line sensors

2.Obstacle sensors3. Crash (stall)4. Light sensors5. Coin toss

Page 11: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

What can we read/observe?Obstacle sensors:

1. Left2. Right

Page 12: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors
Page 13: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

IF - ELSE1. Observe a sensor.

2. Conditional tiles are used to create a fork in the road of your program's execution.

3. Depending on the outcome of the chosen condition, your program may continue in one of two different directions (but not both).

Page 14: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

IF - ELSEThe simplest conditional is the IF-

THEN-ELSE tile (yellow).This tile checks for the presence of an

obstruction on the left and none on the right.

If this condition is met the path indicated by the green check mark is taken.

If not, the path indicated by the red "X" is taken.

So, overall, what this particular program does is turn the left LED on if there's a left obstruction and not a right one, and turns it off otherwise.

Page 15: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

IF - ELSE

Conditionals are inserted into your program the same as other tiles. Just click on the conditional button and a cursor will appear as you mouse over the worksheet, showing you where the conditional can be inserted. When you click it into place, the conditional edit box will appear, as shown to the left.

Page 16: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

IF - ELSE

Here you can select what condition you want to test by clicking one of the five multiple-choice boxes until that condition appears.

Page 17: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

IF - ELSE

For most conditions, the slider at the bottom can be used to place further restrictions on when the condition is TRUE, such as a required number of consecutive observations of that condition.

Page 18: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

IF - ELSE

The remaining button is used to switch the TRUE and FALSE directions.Notice that the reversed directions are

highlighted in black to make them more obvious in your program.

Page 19: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

IF - ELSE

• When a conditional is placed, it creates several locations where additional tiles may be inserted.

Page 20: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

IF - ELSE

• When a conditional is placed, it creates several locations where additional tiles may be inserted.• One is immediately to the bottom of the tile

and is normally the program fork corresponding to a TRUE condition.

Page 21: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

IF - ELSE

• When a conditional is placed, it creates several locations where additional tiles may be inserted.• Another insertion point is the arrow to the

right of the tile. This is normally the path taken by the FALSE condition.

Page 22: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

IF - ELSE

• When a conditional is placed, it creates several locations where additional tiles may be inserted.• A third insertion point is the arrow below the

conditional tile. This occurs where the forking program paths rejoin. A tile place here will execute regardless of whether the condition was TRUE or FALSE.

Page 23: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors
Page 24: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

ELSE - IFThere is yet another insertion point available

only if you happen to be inserting another conditional tile. It's just to the right of the conditional tile and is indicated by a vertical cursor.

Page 25: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

ELSE - IF

By inserting additional conditionals in this fashion, you can create multi-way branching in your program, as the example illustrates. Here, all possible obstruction conditions are being tested with a motion response for each one.

Notice that the last condition (no obstruction) doesn't need an explicit test, since it will be the one that's TRUE if, and only if, the other three are FALSE.

Page 26: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors
Page 27: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

What can we read/observe?Obstacle sensors

Page 28: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

Obstacle sensorcondition• The obstacle sensor uses the two infrared LEDs on the

left- and right-front of the Scribbler, along with the IR detector in the middle-front to detect light reflected from obstacles in its path. Each of the four possible combinations of left and right obstacles represents a separate condition. You can also restrict how many consecutive times the condition must be observed for it to be TRUE.

• In the example above, the condition of “left obstacle present and right obstacle absent” must hold for four or more successive observations for the condition to be TRUE.

Page 29: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors
Page 30: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

Program P2: FearMake a copy of program P1, and use it as the basis for

P2.

Add the following functionality:Read the obstacle sensors.Program the robot to react by moving away from the

obstacle, if present (simulating fear).

Q5: Experiment and determine the range (closest and farthest) of the obstacle sensors. Describe your experiment in a paragraph and report your results.

Page 31: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors
Page 32: Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

Lab assignment deliverablesThe Subject of your single email must be:

CSC 120 IF lab <your name>(I will deduct points if you fail to do this.)

Include answers to questions Q1 through Q5.

Include the P1 .scb file and screen shot of P1.

Include the P2 .scb file and screen shot of P2.