24
The LOGIC and MATH blocks Day 9 Computer Programming through Robotics CPST 410 Summer 2009

The LOGIC and MATH blocks Day 9

Embed Size (px)

DESCRIPTION

The LOGIC and MATH blocks Day 9. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: The LOGIC and MATH blocks Day 9

The LOGIC and MATH blocksDay 9

Computer Programming through Robotics

CPST 410

Summer 2009

Page 2: The LOGIC and MATH blocks Day 9

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: The LOGIC and MATH blocks Day 9

Yes? No? Maybe?

Kelly §17

Page 4: The LOGIC and MATH blocks Day 9

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

4

The challenge

Tribot, 1. If the Light sensor detects a level above 30,

2. and if the Sound sensor detects a level above 20,

3. move forward two rotations.

Page 5: The LOGIC and MATH blocks Day 9

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

5

Brain-storming

Can we do this with what we know so far?No, we can only use one sensor at a time as a

condition for a robot to do something.

Page 6: The LOGIC and MATH blocks Day 9

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

6

The LOGIC block

Like the COMPARE block, the LOGIC block performs a comparison.

For the LOGIC block, it is between two logical values (True/False), not two numbersNote that p. 127 mistakenly uses COMPARE

for LOGIC several times!!

Page 7: The LOGIC and MATH blocks Day 9

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

7

Program the conditions

Drop in a Sound and a Light sensor block and configure them. Sound < 20 Light < 30

Drop in a LOGIC block from the Data icons Operation = And Pull down the data hubs of each sensor

connect the Sound logic plug to the B plug of LOGICconnect the Light logic plug to the A plug of LOGIC

Page 8: The LOGIC and MATH blocks Day 9

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

8

LOGIC.rbt, first step

Note how wiring the first block to the second (B) hub prevents the wires from crossing, so you can understand what is going on better.

Page 9: The LOGIC and MATH blocks Day 9

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

9

Program the response

SPOT must move forward 3 rotations if the output of LOGIC is True.

How would we do that?Use a SWITCH with the True compartment

containing a MOVE.

Page 10: The LOGIC and MATH blocks Day 9

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

10

LOGIC.rbt, final

Page 11: The LOGIC and MATH blocks Day 9

Logic in NXC

Page 12: The LOGIC and MATH blocks Day 9

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

12

LOGIC in NXC

There is no such operation as LOGIC in NXC,

so you have to break it down into its components,

using the logical operators:&& [logical AND]|| [logical OR]! [logical negation]

Page 13: The LOGIC and MATH blocks Day 9

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

13

Repeat the challenge

Tribot, 1. If the Light sensor detects a level above 30,

2. and if the Sound sensor detects a level above 20,

3. move forward two rotations.

Page 14: The LOGIC and MATH blocks Day 9

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

14

Brainstorming

Collect the reading of the light sensor into a variable.

Collect the reading of the sound sensor into a different variable.

Compare both to the stipulated values.Combine the comparison with ‘and’.Rotate the motor twice (how many

degrees?)

Page 15: The LOGIC and MATH blocks Day 9

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

15

Logic.nxc

int light, sound;task main(){

light = Sensor(S3);sound = Sensor(S2);if (light > 30 && sound > 20) RotateMotor(OUT_BC, 75, 720);

}

Page 16: The LOGIC and MATH blocks Day 9

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

16

LOGIC outputs

T, T F, T T, F F, F

&& (and)

T F F F

|| (or) T T T F

XOR F T T F

! (not) on plug A: T > F; F > T

Page 17: The LOGIC and MATH blocks Day 9

Basic math

Kelly §20

Page 18: The LOGIC and MATH blocks Day 9

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

18

The MATH block

Among the Data blocksDoes basic arithmetic:

additionsubtractionmultiplicationdivision

Page 19: The LOGIC and MATH blocks Day 9

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

19

First challenge

Tribot, display the sum of two random numbers between 0 and 50.

Page 20: The LOGIC and MATH blocks Day 9

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

20

Math1.rbt

Page 21: The LOGIC and MATH blocks Day 9

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

21

Second challenge

Tribot, display two random numbers between 0 and 50 and their sum, in the format: A + B = C.

Page 22: The LOGIC and MATH blocks Day 9

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

22

Math2a.rbtuses 3 DISPLAYs to put text on 3

lines

Page 23: The LOGIC and MATH blocks Day 9

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

23

Math2a.rbtconnects TEXT to TEXT to put entire

text on 1 line

Page 24: The LOGIC and MATH blocks Day 9

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

24

Next time

Math in NXCFiles and BluetoothTasks, routines, subroutines: Kelly 26.