240 likes | 331 Views
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.
E N D
The LOGIC and MATH blocksDay 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. Harry Howard, CPST 410, Tulane University
Yes? No? Maybe? Kelly §17
The challenge Tribot, • If the Light sensor detects a level above 30, • and if the Sound sensor detects a level above 20, • move forward two rotations. Harry Howard, CPST 410, Tulane University
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. Harry Howard, CPST 410, Tulane University
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!! Harry Howard, CPST 410, Tulane University
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 Harry Howard, CPST 410, Tulane University
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. Harry Howard, CPST 410, Tulane University
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. Harry Howard, CPST 410, Tulane University
LOGIC.rbt, final Harry Howard, CPST 410, Tulane University
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] Harry Howard, CPST 410, Tulane University
Repeat the challenge Tribot, • If the Light sensor detects a level above 30, • and if the Sound sensor detects a level above 20, • move forward two rotations. Harry Howard, CPST 410, Tulane University
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?) Harry Howard, CPST 410, Tulane University
Logic.nxc int light, sound; task main() { light = Sensor(S3); sound = Sensor(S2); if (light > 30 && sound > 20) RotateMotor(OUT_BC, 75, 720); } Harry Howard, CPST 410, Tulane University
LOGIC outputs Harry Howard, CPST 410, Tulane University
Basic math Kelly §20
The MATH block • Among the Data blocks • Does basic arithmetic: • addition • subtraction • multiplication • division Harry Howard, CPST 410, Tulane University
First challenge • Tribot, display the sum of two random numbers between 0 and 50. Harry Howard, CPST 410, Tulane University
Math1.rbt Harry Howard, CPST 410, Tulane University
Second challenge • Tribot, display two random numbers between 0 and 50 and their sum, in the format: A + B = C. Harry Howard, CPST 410, Tulane University
Math2a.rbtuses 3 DISPLAYs to put text on 3 lines Harry Howard, CPST 410, Tulane University
Math2a.rbtconnects TEXT to TEXT to put entire text on 1 line Harry Howard, CPST 410, Tulane University
Next time • Math in NXC • Files and Bluetooth • Tasks, routines, subroutines: Kelly 26. Harry Howard, CPST 410, Tulane University