display and move day 2

Post on 12-Jan-2016

24 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

DISPLAY and MOVEDay 2

Computer Programming through Robotics

CPST 410

Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University 2

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 3

Phot

os

Preliminaries

7/1/09 Harry Howard, CPST 410, Tulane University 5

Update on robots

Are they all assembled?Who has a laptop?Start installing software.

7/1/09 Harry Howard, CPST 410, Tulane University 6

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 7

The first screen

Hello World!

Kelly §3

7/1/09 Harry Howard, CPST 410, Tulane University 9

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 10

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 11

The palette

7/1/09 Harry Howard, CPST 410, Tulane University 12

The DISPLAY block

Drag a DISPLAY block from the common column

into the 'Start' space

7/1/09 Harry Howard, CPST 410, Tulane University 13

The DISPLAY block’s settings

7/1/09 Harry Howard, CPST 410, Tulane University 14

The settings in detail

ActionImageTextDrawingReset

DisplayClear

File

Positionx (length)y (height)

7/1/09 Harry Howard, CPST 410, Tulane University 15

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 16

The controller

Five buttons that turn orange when the mouse rolls over them:NXT windowDownload and run

selectedStopDownloadDownload and run

7/1/09 Harry Howard, CPST 410, Tulane University 17

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 18

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 19

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 20

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 21

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 22

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 23

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 24

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 25

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 26

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), anddrop it in front of the DISPLAY block.Change its setting from Logic 1 to Number 1.

The text-based language NXC

Hansen

7/1/09 Harry Howard, CPST 410, Tulane University 28

PC users

Bricx Command Center 3.3http://bricxcc.sourceforge.net/

7/1/09 Harry Howard, CPST 410, Tulane University 29

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 30

Now go to the File menuand start a new program.

7/1/09 Harry Howard, CPST 410, Tulane University 31

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 32

Replace “name” with main, which is used for a task

that has no obvious name.

Hello, world!

In NXC

7/1/09 Harry Howard, CPST 410, Tulane University 34

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 35

TextOut

NXC supplies a function specifically to display text, TextOut.

This function asks you for 3 pieces of informationHow 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 36

TextOut in BricxCC

In BricxCC, select “body” in the task, andIn 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 37

7/1/09 Harry Howard, CPST 410, Tulane University 38

Filling in TextOut in BricxCC

7/1/09 Harry Howard, CPST 410, Tulane University 39

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 40

Now replace “time(1 ms)”with 10000.

7/1/09 Harry Howard, CPST 410, Tulane University 41

Wait(10000)

7/1/09 Harry Howard, CPST 410, Tulane University 42

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 43

Test the compiler

As an example,change LCD_LINE1 > LCD_LIN1leave off the semicolon after Wait(10000)

Compile and see what happens.Try some more.

Summary

7/1/09 Harry Howard, CPST 410, Tulane University 45

DISPLAY vs. TextOut

TextOut(0,LCD_LINE8,”test”)

7/1/09 Harry Howard, CPST 410, Tulane University 46

TIME WAIT vs. Wait

Wait(1)

Get Movin'

Kelly §4

7/1/09 Harry Howard, CPST 410, Tulane University 48

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 49

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 50

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.

Get reset

Kelly §24

7/1/09 Harry Howard, CPST 410, Tulane University 52

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 53

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 54

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 55

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.

Outputs in NXC

Hansen pp. 153-4 (§7)

236ff (§10)

407ff (Appendix A)

7/1/09 Harry Howard, CPST 410, Tulane University 57

Normal motor control

OnFwd(ports, power) - forwardOnRev(ports, power) - reverseCoast(ports) - turn off and coast

also called Float

Off(ports) - turn off and brakereset Block & Tacho, but not Rotation counters

7/1/09 Harry Howard, CPST 410, Tulane University 58

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 59

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 60

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 61

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)

regmodeOUT_REGMODE_SPEED: turns on regulationOUT_REGMODE_IDLE: turns off regulation

same as OnFwd/OnRev

7/1/09 Harry Howard, CPST 410, Tulane University 62

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 63

movereg.nxc

task main(){

OnFwdReg(OUT_AC, 40, OUT_REGMODE_SPEED);Wait(5000); // wait 5 secsOnRevReg (OUT_AC, 40, OUT_REGMODE_SPEED);Wait(5000); // wait 5 secsCoast(OUT_AC); // roll to a stop

}

7/1/09 Harry Howard, CPST 410, Tulane University 64

Synchronized motor control

The firmware can automatically adjust the power level to keep two motors running at the same speedOnFwdSync(ports, power, turnpct)OnRevSync(ports, power, turnpct)

turnpctthe percentage that the motors turn to one side or the

either, -100 or 100

7/1/09 Harry Howard, CPST 410, Tulane University 65

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 66

movesync.nxc

task main(){

OnFwdSync(OUT_AC, 40, 0); Wait(5000); // wait 5 secsOnRevSync(OUT_AC, 40, 50);Wait(5000); // wait 5 secsCoast(OUT_AC); // roll to a stop

}

7/1/09 Harry Howard, CPST 410, Tulane University 67

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)

anglein degrees, positive or negative

7/1/09 Harry Howard, CPST 410, Tulane University 68

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 69

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 70

Next time

Whatever we didn’t finish for today.A project?Logic, sensors, calibration; Waiting, loops

Kelly §8-9, 23; §10-11

top related