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.

Download Report

Transcript 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.

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.
7/1/09
Harry Howard, CPST 410, Tulane University
2
Math in NXC
Simple arithmetic
 Just what you would expect:
+
*
/
7/1/09
Harry Howard, CPST 410, Tulane University
4
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).
7/1/09
Harry Howard, CPST 410, Tulane University
5
movemeter.nxc
int
int
int
int
WHEEL_DIAM = 56;
METER2MM = 1000;
DEG2ROT = 360;
degrees;
task main()
{
degrees = METER2MM*DEG2ROT/(PI*WHEEL_DIAM);
RotateMotor(OUT_AC, 75, degrees);
RotateMotor(OUT_AC, 75, -degrees);
Coast(OUT_AC);
}
7/1/09
Harry Howard, CPST 410, Tulane University
6
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.
7/1/09
Harry Howard, CPST 410, Tulane University
8
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
7/1/09
Harry Howard, CPST 410, Tulane University
9
Challenge
 Tribot,
1. 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.
2. Do the same thing again, reading C from the
file to be the new A;
 do this 3 times.
7/1/09
Harry Howard, CPST 410, Tulane University
10
FileAccessLoop.rbt, step 1
7/1/09
Harry Howard, CPST 410, Tulane University
11
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.
7/1/09
Harry Howard, CPST 410, Tulane University
12
FileAccessLoop1b.rbt
7/1/09
Harry Howard, CPST 410, Tulane University
13
FileAccessLoop1c.rbt
7/1/09
Harry Howard, CPST 410, Tulane University
14
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.
7/1/09
Harry Howard, CPST 410, Tulane University
16
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.
7/1/09
Harry Howard, CPST 410, Tulane University
17
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.
7/1/09
Harry Howard, CPST 410, Tulane University
18
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).
7/1/09
Harry Howard, CPST 410, Tulane University
19
Challenges
 SPOT, send a message to someone!
 SPOT, receive a message from someone!
7/1/09
Harry Howard, CPST 410, Tulane University
20
Send.rbt & Receive.rbt
7/1/09
Harry Howard, CPST 410, Tulane University
21
Next time
 Anything we didn’t finish.
 Tasks, routines, subroutines: Kelly 26.
7/1/09
Harry Howard, CPST 410, Tulane University
22