conditions and loops day 4

48
Conditions and loops Day 4 Computer Programming through Robotics CPST 410 Summer 2009

Upload: aubrianna-carrillo

Post on 30-Dec-2015

31 views

Category:

Documents


1 download

DESCRIPTION

Conditions and loops Day 4. Computer Programming through Robotics CPST 410 Summer 2009. Course organization. Course home page (http://robolab.tulane.edu/CPST410/) Lab (Newcomb 442) will be open for practice with 3-4 Macs, but you can bring your own laptop and all robots. Sound in NXT-G. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Conditions and loops Day 4

Conditions and loopsDay 4

Computer Programming through Robotics

CPST 410

Summer 2009

Page 2: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 2

Course organization

Course home page (http://robolab.tulane.edu/CPST410/)

Lab (Newcomb 442) will be open for practice with 3-4 Macs, but you can bring your own laptop and all robots.

Page 3: Conditions and loops Day 4

Sound in NXT-G

Kelly §6 Make some noise!

Page 4: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 4

The SOUND block

1. This icon shows whether the block will play a sound file or a tone.

2. This icon shows whether the block will start or stop playing a sound.

3. This icon shows the block’s volume. An icon with four orange bars is set to the loudest volume.

4. You can drag data wires from other blocks to this block’s data hub that will affect the Sound block’s properties.

Page 5: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 5

Kelly's example, reversed

Spot, move forward 1 rotations at 50% power coasting to a stop and then play me a C note for 2 seconds at 75% volume.

Page 6: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 6

MoveSound.rbt

Page 7: Conditions and loops Day 4

Sound in NXC

Page 8: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 8

Two useful sound functions

DefinitionsPlayTone(frequency, duration)PlayFile(filename)

ExamplesPlayTone(440, 500); Wait(500);PlayFile(“Startup.rso”);

Note that PlayTone executes immediately, so that all by itself, it does not play for 500 ms; it relies on Wait to actually extend the duration to the one specified.

Page 9: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 9

Do Kelly's example in NXC

TRIBOT, move forward 1 rotation at 50% power coasting to a stop and then play me a C note for 2 seconds at 75% volume.

Page 10: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 10

MoveSound.nxc

task main()

{

RotateMotor(OUT_AC, 75, 360);

PlayTone(1047,2000);

Wait(2000);

}

Page 11: Conditions and loops Day 4

Waiting

Kelly §10 Wait for it!

Page 12: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 12

Waiting

The robot is almost always waiting to do something, even when it is already doing something: TRIBOT is moving towards a black line, waiting for the Light

sensor to detect it. TRIBOT is preparing to throw a ball at a target, waiting for the

Touch sensor to be pressed and released. TRIBOT is rolling towards the wall, waiting for the Ultrasonic

sensor to detect it. TRIBOT is sitting at the start line, waiting for the Sound sensor to

hear me yell “Go!”.

Page 13: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 13

Conditions

The WAIT block will stop waiting when specific conditions are met.

The conditions are usually given by some kind of sensor feedback.

The blocks for each condition can be found in two ways:Common palette > Wait.By changing the setting from Time to Sensor on the

Time WAIT block.

Page 14: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 14

The conditions

Time waitTouch sensor waitLight sensor waitUltrasonic sensor waitNXT Buttons waitRotation sensor waitReceive Message wait

Page 15: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 15

A simple test of a condition

TRIBOT, move forward for an unlimited duration at a power of 50 until the Touch sensor is pressed.

Page 16: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 16

MoveTilTouch.rbt

Page 17: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 17

A more complex example

SPOT, move forward for 1 rotation at 50% power, brake, and beep. If your Light sensor detects a light level greater than 30, move backward 1/2 rotation at 50% power, coasting to a stop.

Page 18: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 18

MoveTilLight.rbt

Page 19: Conditions and loops Day 4

Conditions in NXC

Page 20: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 20

A complication

The sensor functions in NXC do not include this ability to wait; all they do is read sensor values.

So you have to add the condition that the sensor value is supposed to meet, yourself.

Page 21: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 21

If

There are two optionsif (condition) consequence

if (condition) consequence else alternative

Examplesif (x == 1) y = 2;

if (x == 1) { y = 2; z = 3; }

if (x == 1) y = 3; else y = 4;

Page 22: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 22

We can’t do the simple test

TRIBOT, move forward for an unlimited duration at a power of 50 until the Touch sensor is pressed.

PROBLEM: there is no ‘unlimited’ parameter for any of the NXC movement functions, so we skip this task, though the next slide shows what we would want the program to look like.

Page 23: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 23

MoveTilTouch.nxc

task main()

{

OnFwd(OUT_AC, 50);

if (Sensor(S1) == 1)

{

Off(OUT_AC);

}

}

Page 24: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 24

The more complex example

TRIBOT, move forward for 1 rotation at 50% power, brake, and beep. If your Light sensor detects a light level greater than 30, move backward 1/2 rotation at 50% power, coasting to a stop.

Page 25: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 25

MoveTilLight.rbt

task main(){RotateMotor(OUT_BC, 50, 360);PlayTone(1047, 500);Wait(500);if (Sensor(S3) > 30)

{ RotateMotor(OUT_BC, 50, -180);}

}

Page 26: Conditions and loops Day 4

Loops

Kelly §11 Round and Round

Page 27: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 27

How would you do this?

TRIBOT, display the output of the Light sensor briefly (500ms) 10 times.

Page 28: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 28

The LOOP block

The LOOP block repeats what is inside it until a condition is met.The loop break conditions are the same as the

WAIT conditions.

You drop a block inside a loop by grabbing the block, holding down the mouse button, and dropping it inside when the loop expands.

Page 29: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 29

Go ahead and do it

TRIBOT, display the output of the Light sensor briefly (1s) 10 times.

Page 30: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 30

DisplaySensorLoop.rbt

Page 31: Conditions and loops Day 4

Loops in NXC

Page 32: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 32

REPEAT

The repeat command, repeat(t) {}, repeats what is in the curly brackets for the number of times given by t.

Page 33: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 33

Convert the example to NXC

TRIBOT, display the raw output of the Light sensor briefly (1s) 10 times.

Page 34: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 34

DisplaySensorLoop.nxcint light;

string text_light;

task main()

{

repeat(10)

{

light = Sensor(S3);

txt_light = NumToStr(light);

TextOut(0, LCD_LINE4, txt_light);

Wait(1000);

}

}

Page 35: Conditions and loops Day 4

More practiceProgram each bit of pseudocode in NXT-G

and then in NXC.

My suggestion follows each bit of pseudocode, so don’t look ahead until you

are finished with your program!

Page 36: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 36

Follow up

TRIBOT, display the output of the Light sensor briefly (1s) 10 times and beep after each display. Laugh when you are finished.

Page 37: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 37

SensorLoopSound.rbt

Page 38: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 38

SensorLoopSound.nxcint light;string text_light;task main(){

repeat(10){

light = Sensor(S3);txt_light = NumToStr(light);TextOut(0, LCD_LINE4, txt_light);Wait(1000);PlayTone(1047,500);Wait(500);

}PlayFile(“Laughing 02.rso”);Wait(1000);

}

Page 39: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 39

Epilog on scope

Note the difference being in the loop versus being out of the loop makes. PlayTone is played 10 times, because it is in the loop. PlayFile is only played once, because it is not in the loop.

The effect of an expression like repeat is known as its scope, and in NXC the scope of an expression is stated explicitly by means of the curly brackets.

Page 40: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 40

Thought experiment

How would you make TRIBOT move forward and then turn right -- four times?By dropping in 8 MOVE blocks, each with the

same settings (one for each forward move, and one for each turn).

Does this sound like a good usage of your time?No!

Page 41: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 41

The pseudocode

TRIBOT, move forward for 0.5 s at 50% power and then make a 90° turn for 0.65 s also at 50% power. Do this 4 times.

Page 42: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 42

RepeatTurn.rbt

Page 43: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 43

Turning in NXC

You may have noticed that the NXC movement functions do not have the nice ‘steering wheel’ ability that NXT-G has.

The trick to turning in NXC is to reverse a motor. I’ll leave it up to you to figure out which one.

Page 44: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 44

RepeatTurn.nxc

task main()

{

repeat(4)

{

OnFwd(OUT_BC, 50);

Wait(500);

OnRev(OUT_C, 50);

Wait(650);

}

}

Page 45: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 45

Mix them up

TRIBOT, move forward for 1 rotation at 50% power and coast. If your Light sensor detects a level greater than 20, beep and move backward 1/2 rotation at 50% power, coasting to a stop. Do this 3 times, then say the number ‘three’.

Page 46: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 46

MoveLightLoop.rbt

Page 47: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 47

MoveLightLoop.nxctask main(){

repeat(3){

RotateMotor(OUT_BC, 50, 360);if (Sensor(S3) > 20) {

PlayTone(1047,500);Wait(500);RotateMotor(OUT_BC, 50, -180);

}}PlayFile(“03.rso”);Wait(1000);

}

Page 48: Conditions and loops Day 4

7/1/09 Harry Howard, CPST 410, Tulane University 48

Next time

Quiz 1This will just be a quiz on the blocks.

More loops, switching.Random numbers.'Line Follower' - project done in class