DISPLAY and MOVE Day 2 Computer Programming through Robotics CPST 410 Summer 2009 Course organization  Course home page (http://robolab.tulane.edu/CPST410/)  Hand out syllabi.  Notice that Aug.

Download Report

Transcript DISPLAY and MOVE Day 2 Computer Programming through Robotics CPST 410 Summer 2009 Course organization  Course home page (http://robolab.tulane.edu/CPST410/)  Hand out syllabi.  Notice that Aug.

DISPLAY and MOVE
Day 2
Computer Programming
through Robotics
CPST 410
Summer 2009
Course organization
 Course home page
(http://robolab.tulane.edu/CPST410/)
 Hand out syllabi.
 Notice that Aug. 10 is the final day to turn
in the final project.
 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
P
h
o
t
o
s
7/1/09
Harry Howard, CPST 410, Tulane University
3
Preliminaries
Update on robots
 Are they all assembled?
 Who has a laptop?
 Start installing software.
7/1/09
Harry Howard, CPST 410, Tulane University
5
Upgrade firmware on robot
 Launch the Mindstorms NXT application on your laptop.
 Turn on the NXT brick.
 Connect the next brick to your laptop via the USB cable.
 From the Tools menu (at the top), select Update NXT
Firmware.
 In the Update NXT Firmware window, click 1.05 version
to highlight it.
 Click Download.
 Don't unplug it!
7/1/09
Harry Howard, CPST 410, Tulane University
6
The first screen
7/1/09
Harry Howard, CPST 410, Tulane University
7
Hello World!
Kelly §3
The Tradition
 The first program anyone writes displays
'Hello World!' on the screen.
 Who are we to break with tradition?
 So let's get started …
 ... with pseudo-code:
SPOT, I'd like you to display the words "Hello
World!" on your LCD screen.
7/1/09
Harry Howard, CPST 410, Tulane University
9
Start your first program
In the text box under Start New Program,
type 'HelloWorld' -- the name for our first program -and click on ‘Go >>’.
7/1/09
Harry Howard, CPST 410, Tulane University
10
The palette
7/1/09
Harry Howard, CPST 410, Tulane University
11
The DISPLAY block
Drag a DISPLAY block
from the common column
into the 'Start' space
7/1/09
Harry Howard, CPST 410, Tulane University
12
The
DISPLAY
block’s
settings
7/1/09
Harry Howard, CPST 410, Tulane University
13
The settings in detail
 Action
 Image
 Text
 Drawing
 Reset
 Position
 x (length)
 y (height)
 Display
 Clear
 File
7/1/09
Harry Howard, CPST 410, Tulane University
14
Enter the text
 Change your block’s action to Text and enter "Hello,
world!" in the box.
 Note the x and y values, plus the appearance of the line number.
 File > Save.
 Connect the robot to your laptop with the USB cable, if
you haven’t already done so.
7/1/09
Harry Howard, CPST 410, Tulane University
15
The controller
 Five buttons that turn
orange when the
mouse rolls over them:
 NXT window
 Download and run
selected
 Stop
 Download
 Download and run
7/1/09
Harry Howard, CPST 410, Tulane University
16
The NXT window: Comm tab
 Look at the battery charge
 Tell me whenever it goes down under 25%, so I can recharge the
batteries.
 If you are authorized: change the name to the name you
choose (max 8 characters) and push the return key.
7/1/09
Harry Howard, CPST 410, Tulane University
17
The NXT window: Memory tab
 You can manually upload (and download) files
from this screen, as well as delete them to free up
memory.
7/1/09
Harry Howard, CPST 410, Tulane University
18
Download and run the
program
 In the Controller, click
the right arrow in the
center (Download and
run).
 On the NXT brick,
press the orange
button.
7/1/09
Harry Howard, CPST 410, Tulane University
19
What happened?
 The text was displayed so quickly that you
may not have seen it.
 But that is easy to fix.
 New pseudo-code:
SPOT, I'd like you to display the words "Hello
World!" on your LCD screen for 10 seconds.
7/1/09
Harry Howard, CPST 410, Tulane University
20
The WAIT blocks
 There are several ways to implement this
pseudo-code, but the one we shall use here
involves a TIME WAIT block.
 Go to the application, click on the hourglass
icon in the Common palette, and look at the
WAIT blocks that spring out from it.
7/1/09
Harry Howard, CPST 410, Tulane University
21
DISPLAY + WAIT
Drag out a TIME WAIT block (timer icon) from the hourglass,
and drop it right after the DISPLAY block
7/1/09
Harry Howard, CPST 410, Tulane University
22
The TIME WAIT block
 Take a look at its settings,
 and enter the number of seconds to wait.
 Notice how this number appears on the block.
 Save and download to the NXT brick.
7/1/09
Harry Howard, CPST 410, Tulane University
23
Play time
 Take about 10 minutes to vary the settings
of the DISPLAY and TIME WAIT blocks
and see what happens.
 Obviously, you want to predict what will
happen before the program runs.
7/1/09
Harry Howard, CPST 410, Tulane University
24
Data hubs - skip!
 The settings of the DISPLAY block can be
changed from other parts of the program by
revealing its data hub.
 Put your cursor at the bottom of the
DISPLAY block and try to pull down the
indented line.
 Running the cursor over each icon reveals
the kind of information that it accepts.
7/1/09
Harry Howard, CPST 410, Tulane University
25
The VARIABLE block - skip!
 What Kelly describes does not work!
 To illustrate the usefulness of the data hub,
change to the Complete palette (the 3
interlocking squares at the bottom left).
 Select the big orange plus sign (Data),
pull out a suitcase (Variable), and
drop it in front of the DISPLAY block.
Change its setting from Logic 1 to Number 1.
7/1/09
Harry Howard, CPST 410, Tulane University
26
The text-based language NXC
Hansen
PC users
 Bricx Command Center 3.3
 http://bricxcc.sourceforge.net/
7/1/09
Harry Howard, CPST 410, Tulane University
28
First look at BricxCC
 Open BricxCC and move the Brick
Command Center window to the left so that
the Templates window is visible (see next
slide).
7/1/09
Harry Howard, CPST 410, Tulane University
29
Now go to the File menu
and start a new program.
7/1/09
Harry Howard, CPST 410, Tulane University
30
A basic program (p. 148)
 A program must always have at least one task,
followed by braces that contain the code that the
task executes:
task name()
{
// the task's code is placed here
}
 From the Templates window, click on the
Programs box and then click Task…(){…}. [next]
7/1/09
Harry Howard, CPST 410, Tulane University
31
Replace “name” with main,
which is used for a task
that has no obvious name.
7/1/09
Harry Howard, CPST 410, Tulane University
32
Hello, world!
In NXC
NXC HelloWorld
 Instead of blocks, NXC has functions.
 We need two, one to display the text and the
other to keep the text on the screen for a
while.
7/1/09
Harry Howard, CPST 410, Tulane University
34
TextOut
 NXC supplies a function specifically to display
text, TextOut.
 This function asks you for 3 pieces of information
 How many pixels from the left the text begins,
 What line the text begins on,
 And the text itself.
 This information is put between parentheses, with
each piece separated by a comma. The text is also
quoted:
 TextOut(0, LCD_LINE1, “Hello, world!”)
7/1/09
Harry Howard, CPST 410, Tulane University
35
TextOut in BricxCC
 In BricxCC, select “body” in the task, and
 In the Templates window, open the Drawing
box and click on the topmost TextOut
function. [see next]
7/1/09
Harry Howard, CPST 410, Tulane University
36
7/1/09
Harry Howard, CPST 410, Tulane University
37
Filling in TextOut in BricxCC
7/1/09
Harry Howard, CPST 410, Tulane University
38
Wait
 The function for keeping the text on the
screen is Wait.
 It measures time in milliseconds:
Wait(10000)
 In BricxCC, open the Timing box in the
Templates window, and click on the Wait
function. [see next]
7/1/09
Harry Howard, CPST 410, Tulane University
39
Now replace “time(1 ms)”
with 10000.
7/1/09
Harry Howard, CPST 410, Tulane University
40
Wait(10000)
7/1/09
Harry Howard, CPST 410, Tulane University
41
Compiling
 Now go to the Compile menu and select Compile.
 If you did everything right, nothing much happens.
 However, if you made a mistake, the compiler
might catch it.
 It will find typos;
 It will find syntax errors; for instance, it expects that
every line of code must be terminated by a semi-colon.
7/1/09
Harry Howard, CPST 410, Tulane University
42
Test the compiler
 As an example,
change LCD_LINE1 > LCD_LIN1
leave off the semicolon after Wait(10000)
 Compile and see what happens.
 Try some more.
7/1/09
Harry Howard, CPST 410, Tulane University
43
Summary
DISPLAY vs. TextOut
TextOut(0,LCD_LINE8,”test”)
7/1/09
Harry Howard, CPST 410, Tulane University
45
TIME WAIT vs. Wait
Wait(1)
7/1/09
Harry Howard, CPST 410, Tulane University
46
Get Movin'
Kelly §4
The MOVE block
 Start a new program and name it 'Move'.
 Drag a MOVE block (interlocking gears) onto the
working area.
 Look at its settings.
7/1/09
Harry Howard, CPST 410, Tulane University
48
MOVE block settings
 Ports: A, B, C (at top of NXT block).
 Direction: forwards (up), reverse (down), stop.
 Steering: if two motors are connected, dragging the control away from
center makes the robot go in a circle.
 Power: changes speed of a motor.
 Duration: unlimited, degrees, rotations, seconds
 Degrees & rotations must be positive, but you can make them negative by
changing Direction to reverse.
 Rotations also allows fractions.
 Next Action: brake, coast
 Brake applies the brakes for a quick, accurate stop that uses power.
 Coast disengages the motors. If the robot has inertia, it will keep moving.
7/1/09
Harry Howard, CPST 410, Tulane University
49
Play time
 Take about 10 minutes varying the settings
of the MOVE block and see what happens.
 Obviously, you want to predict what will
happen before the program runs.
7/1/09
Harry Howard, CPST 410, Tulane University
50
Get reset
Kelly §24
Execute this program
 SPOT, move forward 360 degrees (and
coast) and wait 3 seconds.
 SPOT, move forward 270 degrees (and
coast) and wait 3 seconds.
 Now, move forward 90 degrees and stop.
7/1/09
Harry Howard, CPST 410, Tulane University
52
Results
 How far should SPOT have moved?
360° + 270° + 90° = 720° or 2 rotations
 How far did SPOT actually move?
 Is that what you would expect from
watching SPOT execute the program?
No, because SPOT coasted a little after the first
two movements, so it should be off.
7/1/09
Harry Howard, CPST 410, Tulane University
53
Error correction
 The NXT brick pairs motors and watches
them to make sure that they spin at the same
rate.
 It also corrects for coasting errors, as we
just saw.
7/1/09
Harry Howard, CPST 410, Tulane University
54
The RESET MOTOR block
 If you want to turn error correction off, drop
a RESET MOTOR block after a MOVE
block.
 It resets the motors to 0, turning off the
error correction.
 Add the necessary RESET MOTOR blocks
to the program, and see what happens.
7/1/09
Harry Howard, CPST 410, Tulane University
55
Outputs in NXC
Hansen pp. 153-4 (§7)
236ff (§10)
407ff (Appendix A)
Normal motor control
 OnFwd(ports, power) - forward
 OnRev(ports, power) - reverse
 Coast(ports) - turn off and coast
also called Float
 Off(ports) - turn off and brake
reset Block & Tacho, but not Rotation counters
7/1/09
Harry Howard, CPST 410, Tulane University
57
Motor port outputs
 Each motor port has an output constant
 OUT_A
 OUT_B
 OUT_C
 They can be combined to run together:
 OUT_AB
 OUT_AC
 OUT_BC
 OUT_ABC
7/1/09
Harry Howard, CPST 410, Tulane University
58
Challenge
 SPOT, move forward for 5 seconds at 75%
power, then do the same in reverse, and then
stop.
7/1/09
Harry Howard, CPST 410, Tulane University
59
movenorm.nxc
task main()
{
OnFwd(OUT_AC, 75); // move forward at 75%
Wait(5000); // wait 5 secs
OnRev(OUT_AC, 75); // move backwards at 75%
Wait(5000); // wait 5 secs
Off(OUT_AC); // stop
}
7/1/09
Harry Howard, CPST 410, Tulane University
60
Regulated motor control
 The firmware can automatically adjust the power
level if it is not being achieved due to friction or
some other external influence (like trying to slow
one wheel with your fingers)
 OnFwdReg(ports, power, regmode)
 OnRevReg(ports, power, regmode)
 regmode
 OUT_REGMODE_SPEED: turns on regulation
 OUT_REGMODE_IDLE: turns off regulation
 same as OnFwd/OnRev
7/1/09
Harry Howard, CPST 410, Tulane University
61
Challenge
 SPOT, move forward for 5 seconds at 40%
regulated power, then do the same in
reverse, and coast to a stop.
 While SPOT is moving, slow him down
with your hand and see if the motors try to
speed up.
7/1/09
Harry Howard, CPST 410, Tulane University
62
movereg.nxc
task main()
{
OnFwdReg(OUT_AC, 40, OUT_REGMODE_SPEED);
Wait(5000); // wait 5 secs
OnRevReg (OUT_AC, 40, OUT_REGMODE_SPEED);
Wait(5000); // wait 5 secs
Coast(OUT_AC); // roll to a stop
}
7/1/09
Harry Howard, CPST 410, Tulane University
63
Synchronized motor control
 The firmware can automatically adjust the power
level to keep two motors running at the same
speed
 OnFwdSync(ports, power, turnpct)
 OnRevSync(ports, power, turnpct)
 turnpct
 the percentage that the motors turn to one side or the
either, -100 or 100
7/1/09
Harry Howard, CPST 410, Tulane University
64
Challenge
 SPOT, move forward for 5 seconds at 40%
synchronized power, then do the same in
reverse but turning to the right, and then
coast to a stop.
7/1/09
Harry Howard, CPST 410, Tulane University
65
movesync.nxc
task main()
{
OnFwdSync(OUT_AC, 40, 0);
Wait(5000); // wait 5 secs
OnRevSync(OUT_AC, 40, 50);
Wait(5000); // wait 5 secs
Coast(OUT_AC); // roll to a stop
}
7/1/09
Harry Howard, CPST 410, Tulane University
66
Tachometer motor control
 The rotation sensors built into the motors tell the
firmware how fast and how far the motors have
turned, information which can be used to control
them.
 RotateMotor(ports, power, angle)
 angle
 in degrees, positive or negative
7/1/09
Harry Howard, CPST 410, Tulane University
67
Challenge
 SPOT, move forward at 40% power for two
rotations, then do the same in reverse, and
coast to a stop.
7/1/09
Harry Howard, CPST 410, Tulane University
68
moverot.nxc
task main()
{
RotateMotor(OUT_AC, 40, 720);
// the rotations take the place of waiting
RotateMotor(OUT_AC, 40, -720);
Coast(OUT_AC);
}
7/1/09
Harry Howard, CPST 410, Tulane University
69
Next time
 Whatever we didn’t finish for today.
 A project?
 Logic, sensors, calibration; Waiting, loops
Kelly §8-9, 23; §10-11
7/1/09
Harry Howard, CPST 410, Tulane University
70