220 likes | 325 Views
Math in NXC Day 10. 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. Math in NXC.
E N D
Math in NXCDay 10 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
Simple arithmetic • Just what you would expect: + - * / Harry Howard, CPST 410, Tulane University
Challenge • Tribot, move forward one meter, and then back up one meter and roll to a stop. • Assume that the wheels are 56 mm in diameter. • Convert diameter to circumference by multiplying it by π (pi in NXC). Harry Howard, CPST 410, Tulane University
movemeter.nxc int WHEEL_DIAM = 56; int METER2MM = 1000; int DEG2ROT = 360; int degrees; task main() { degrees = METER2MM*DEG2ROT/(PI*WHEEL_DIAM); RotateMotor(OUT_AC, 75, degrees); RotateMotor(OUT_AC, 75, -degrees); Coast(OUT_AC); } Harry Howard, CPST 410, Tulane University
Your own filing cabinet Kelly §22
Data loss • If your robot turns off or loses power, all of the data in your program is lost. • But you can save your data to a file on the NXT brick, using the FILE ACCESS block. Harry Howard, CPST 410, Tulane University
The FILE ACCESS block • Among the Advanced blocks • Actions: read, write, close, delete • A FILE ACCESS block can only perform one of these at a time. • Type: text, number (no logic!) • Previous values are not erased • New values are simply added to the end of the file • To erase, the file must be deleted and then re-created with the same file name Harry Howard, CPST 410, Tulane University
Challenge • Tribot, • Step 1 • The number '10' will be called A; • add a random number between 0 and 50 to it; • display the results in the format A + B = C; • save C in a file. • Do the same thing again, reading C from the file to be the new A; • do this 3 times. Harry Howard, CPST 410, Tulane University
FileAccessLoop.rbt, step 1 Harry Howard, CPST 410, Tulane University
Preparation to step 2 • You need to put the preceding material into a loop. • How does the loop begin? • If it is the first repetition of the loop, A = 10; • otherwise, A = C. • So there must be a SWITCH. • And it must be triggered by the count of the loop. Harry Howard, CPST 410, Tulane University
FileAccessLoop1b.rbt Harry Howard, CPST 410, Tulane University
FileAccessLoop1c.rbt Harry Howard, CPST 410, Tulane University
Messages Kelly §25
Pairing • Before wireless messages can be sent or received, the participating NXTs must be set up for wireless communication (paired). • Choose one to be the 'master'. • On its NXT brick, • navigate to the Bluetooth category, • choose Search, • when the search is complete, choose which connection number (1, 2, or 3) you want the second (slave) NXT to be known as. Harry Howard, CPST 410, Tulane University
Pairing, cont. • The first time a connection is established between two NXTs, a prompt to enter the Passkey will appear on both NXTs; • this needs to be accepted by both. • Now when you navigate to the Connection category on the master NXT, the slave NXT should be shown by its name occupying the selected connection number. • On the slave NXT, the master NXT should be shown occupying connection number 0. • At this point, the master NXT is ready to initiate communication with the slave NXT. Harry Howard, CPST 410, Tulane University
Multiple pairing • If the master NXT needs to communicate with multiple NXTs (up to a maximum of three), you should repeat the above process. • Note! If you have multiple NXT devices talking to each other, and your master NXT is also talking to more than one NXT device, you will need to have at least a one second delay built in to the program to allow the Bluetooth radio to change channels before sending the messages. • It’s only necessary to go through this process on one NXT. The receiving NXT will automatically update its connection when the master NXT is setting up its connection number. Harry Howard, CPST 410, Tulane University
The SEND & RECEIVE MESSAGE blocks • Two blocks enable communication between robots, SEND and RECEIVE MESSAGE. • Each NXT brick has 10 mailboxes. • Each mailbox can hold up to 5 values (of the same type). Harry Howard, CPST 410, Tulane University
Challenges • SPOT, send a message to someone! • SPOT, receive a message from someone! Harry Howard, CPST 410, Tulane University
Send.rbt & Receive.rbt Harry Howard, CPST 410, Tulane University
Next time • Anything we didn’t finish. • Tasks, routines, subroutines: Kelly 26. Harry Howard, CPST 410, Tulane University