Syntax-07-08-29 - Tulane University

Download Report

Transcript Syntax-07-08-29 - Tulane University

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.
7/1/09
Harry Howard, CPST 410, Tulane University
2
Yes? No? Maybe?
Kelly §17
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.
7/1/09
Harry Howard, CPST 410, Tulane University
4
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.
7/1/09
Harry Howard, CPST 410, Tulane University
5
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
numbers
Note that p. 127 mistakenly uses COMPARE
for LOGIC several times!!
7/1/09
Harry Howard, CPST 410, Tulane University
6
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 LOGIC
 connect the Light logic plug to the A plug of LOGIC
7/1/09
Harry Howard, CPST 410, Tulane University
7
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.
7/1/09
Harry Howard, CPST 410, Tulane University
8
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.
7/1/09
Harry Howard, CPST 410, Tulane University
9
LOGIC.rbt, final
7/1/09
Harry Howard, CPST 410, Tulane University
10
Logic in NXC
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]
7/1/09
Harry Howard, CPST 410, Tulane University
12
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.
7/1/09
Harry Howard, CPST 410, Tulane University
13
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?)
7/1/09
Harry Howard, CPST 410, Tulane University
14
Logic.nxc
int light, sound;
task main()
{
light = Sensor(S3);
sound = Sensor(S2);
if (light > 30 && sound > 20) RotateMotor(OUT_BC, 75, 720);
}
7/1/09
Harry Howard, CPST 410, Tulane University
15
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)
7/1/09
on plug A: T > F; F > T
Harry Howard, CPST 410, Tulane University
16
Basic math
Kelly §20
The MATH block
 Among the Data blocks
 Does basic arithmetic:
addition
subtraction
multiplication
division
7/1/09
Harry Howard, CPST 410, Tulane University
18
First challenge
 Tribot, display the sum of two random
numbers between 0 and 50.
7/1/09
Harry Howard, CPST 410, Tulane University
19
Math1.rbt
7/1/09
Harry Howard, CPST 410, Tulane University
20
Second challenge
 Tribot, display two random numbers
between 0 and 50 and their sum, in the
format: A + B = C.
7/1/09
Harry Howard, CPST 410, Tulane University
21
Math2a.rbt
uses 3 DISPLAYs to put text on 3 lines
7/1/09
Harry Howard, CPST 410, Tulane University
22
Math2a.rbt
connects TEXT to TEXT to put entire text on 1
line
7/1/09
Harry Howard, CPST 410, Tulane University
23
Next time
 Math in NXC
 Files and Bluetooth
 Tasks, routines, subroutines: Kelly 26.
7/1/09
Harry Howard, CPST 410, Tulane University
24