oregon robotics tournament and outreach program

54
1 Oregon Robotics Tournament and Outreach Program Programming Programming Techniques Workshop Techniques Workshop for Mindstorms for Mindstorms NXT NXT 2013 2013 Opening doors to the worlds of science Opening doors to the worlds of science and technology for Oregon’s youth and technology for Oregon’s youth

Upload: zuri

Post on 08-Feb-2016

17 views

Category:

Documents


0 download

DESCRIPTION

Oregon Robotics Tournament and Outreach Program. Programming Techniques Workshop for Mindstorms  NXT. 2013. Opening doors to the worlds of science and technology for Oregon’s youth. Instructor Contacts. Ken Cone [email protected] (503) 725-2918. Jim Ryan [email protected] - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Oregon Robotics Tournament and Outreach Program

1

Oregon Robotics Tournament and Outreach Program

Programming Techniques Programming Techniques Workshop for Workshop for

MindstormsMindstorms NXT NXT20132013

Opening doors to the worlds of Opening doors to the worlds of science and technology for science and technology for

Oregon’s youthOregon’s youth

Page 2: Oregon Robotics Tournament and Outreach Program

2

Instructor Contacts

Roger SwansonRoger [email protected]@hevanet.com503-297-1824503-297-1824

Jim RyanJim [email protected]@intel.com971-215-6087971-215-6087

Ken ConeKen [email protected][email protected](503) 725-2918(503) 725-2918

Dale JordanDale [email protected][email protected]

Terry HammTerry [email protected]@gmail.com(503) 720-5157(503) 720-5157

Page 3: Oregon Robotics Tournament and Outreach Program

3

ORTOP Project Administrator

Cathy SwiderCathy [email protected][email protected]

(503) 725-2920

Page 4: Oregon Robotics Tournament and Outreach Program

4

Workshop Goals In Workshops 1 and 2 we have

introduced very basic programming to get you started with your robots

In this Workshop we delve more deeply into just the programming Introduce techniques that we think you

are likely to use sometime in your coaching careers

Discuss the Why as well as the How

Page 5: Oregon Robotics Tournament and Outreach Program

5

Agenda Tour the NXT software to review all

the available NXT blocks Distinguish those with higher utility

from those with lower utility Hands on exercises to try out the

new, higher utility blocks

Page 6: Oregon Robotics Tournament and Outreach Program

6

Caveat about this Workshop

Don’t expect to go right back to your teams and teach them all this new stuff you have learned.

Look for opportunities when the youngsters are ready to pick up a new fundamental technique

Our goal is to prepare you better so you are ready when a new teaching opportunity arises

And remember: “The kids do the work!”

Page 7: Oregon Robotics Tournament and Outreach Program

7

NXT Block Review Review the NXT G Software to look

at all programming blocks that are available

Page 8: Oregon Robotics Tournament and Outreach Program

8

NXT Block Summary High Utility

Blocks Move Wait Loop Switch Sensor

Blocks Stop Data Blocks

(variables) My Blocks

Low Utility Blocks Record/Play Motor Send

Message Lamps Mini Blocks Random Advanced

Useful for Debugging Sound Display Number to

Text Text

You should know already Will cover tonight

Page 9: Oregon Robotics Tournament and Outreach Program

9

Simple Light Sensor Program Do you remember the light sensor

exercises in Workshop 1? Stop on black or green Stop only on black

Difference in the two programs was simply the trigger value used on the light sensor.

Trigger values can be influenced by ambient light.

Page 10: Oregon Robotics Tournament and Outreach Program

10

Light Sensor Calibration Let’s make sure each of our robots is

“uncalibrated” Calibrating the light sensor adds more

confusion than value at this point in a team’s development

Use the NXT’s built-in View function Best way to figure out how to set trigger

values for light sensors at this point. Robot must be uncalibrated for this to work

properly

Page 11: Oregon Robotics Tournament and Outreach Program

11

One Note about Calibration With an uncalibrated light sensor

Values read by the light sensor are 0-100 White is typically in the 60’s Black is typically in the 20’s or 30’s

With a calibrated light sensor White is 100 Black is 0

Calibration spreads values over a wider range

View function is not useful once you have calibrated your robot.

Page 12: Oregon Robotics Tournament and Outreach Program

12

Simple Light Sensor Program

Page 13: Oregon Robotics Tournament and Outreach Program

13

Enhanced Light Sensor Program

Suppose that I want to use that code sequence in lots of places in my program.

When light conditions change, I have to find each Wait For block and change the trigger value.

Any ideas on what we might do to make that easier?

Page 14: Oregon Robotics Tournament and Outreach Program

14

An Answer Is in the Slides

Throughout this workshop an answer is given in the slide set so

you have something to review when you get home. Don’t look ahead until you have tried the exercise

yourself.

Page 15: Oregon Robotics Tournament and Outreach Program

15

Use a Variable Variables hold values to be used elsewhere Use Edit Menu/Define Variables to define the

variable Click on Create Give the variable a name Set its type: logic, number, or text

Use the Variable Blocks (Data Block section) to: Read the variable Write the variable Manipulate the variable with other Data Blocks

Demonstrate in the software

Page 16: Oregon Robotics Tournament and Outreach Program

16

Introducing Data Hubs Many of the program blocks have a

capability called a “Data Hub” These are used to set parameters

of a program block during the execution of the program instead of during the writing of the program.

We need Data Hubs in addition to Variables to solve our problem.

Page 17: Oregon Robotics Tournament and Outreach Program

17

Data Hubs Look at Data Hubs in the software

Page 18: Oregon Robotics Tournament and Outreach Program

18

Data Wire Tips Easiest way to connect a data

wire: click on the port at the left end and then click on the port on the right end

To delete a data wire: click on the right end of the wire

Page 19: Oregon Robotics Tournament and Outreach Program

19

Data Wire Tips (cont.) You cannot connect a wire from an

output port on a data hub unless there is a wire going into the corresponding input port

Broken wire

Add a new variable block in the read state

Page 20: Oregon Robotics Tournament and Outreach Program

20

Data Wire Tips (cont.) Don’t run long data wires from a

variable block to a data hub. Instead, insert a variable block for the variable you want to use near where you want to use it so that the data wire is short.

Not so good Better

Page 21: Oregon Robotics Tournament and Outreach Program

21

Solution? Can we use variables and data

hubs to solve this problem?

Page 22: Oregon Robotics Tournament and Outreach Program

22

How About Trying… Create a variable to hold the

trigger value. Use that variable with a data hub

on the Wait For Block to set the trigger value.

Let’s look at the NXT G software

Page 23: Oregon Robotics Tournament and Outreach Program

23

There’s a Problem Wait For blocks don’t have data hubs. Can we simulate a Wait For block

with another block that does have a data hub?

Let’s look at the Light Sensor block in the NXT G software

We also need to look at other kinds of Loop blocks

Page 24: Oregon Robotics Tournament and Outreach Program

24

Light Sensor Block

• An input port with a wire overrides the parameter block

• Parameter block value is used if no wire on an input port

Important Notes on Data Hubs

Page 25: Oregon Robotics Tournament and Outreach Program

25

Different Forms of Loop Block Look at Loop Blocks in the software

Notice the “Data Hub” on the Loop on Logic Value block

Page 26: Oregon Robotics Tournament and Outreach Program

26

It’s Your Turn Write a program to stop on a black

line, but use the Light Sensor block instead of the Wait For block.

Page 27: Oregon Robotics Tournament and Outreach Program

27

Wait For Using Light Sensor Block Use a Light Sensor block, which has a data

hub, instead of a Wait For block

Page 28: Oregon Robotics Tournament and Outreach Program

28

Further Enhance Your Program Use a variable to hold the trigger

value. Set it once so that the program will

stop on green or black. Set it differently so that the program

skips green and stops on black. Your turn to try it.

Page 29: Oregon Robotics Tournament and Outreach Program

29

Stop on Black with Trigger Value in Variable

Page 30: Oregon Robotics Tournament and Outreach Program

30

Let’s Try Something New What if I wanted the robot to

stop if the touch sensor is pressed OR if the light sensor has detected a black line?

Page 31: Oregon Robotics Tournament and Outreach Program

31

Steps to Develop a Program Understand what is to be accomplished Write down a list of tasks describing how

you would solve the problem Break the tasks up into simpler subtasks Program the subtasks, try them, and

debug them Put the subtasks together for the final

program

Page 32: Oregon Robotics Tournament and Outreach Program

32

How Would You Approach This? Write down a list of steps you

would take to get your robot to stop when the touch sensor is pressed OR the light sensor detects a black line.

Do this thought process without thinking about the programming ramifications.

Page 33: Oregon Robotics Tournament and Outreach Program

33

One List of Steps Start the motors to get the robot moving. Check if the touch sensor has been

pressed. If it has, stop the robot. If not, check if a black line has been

detected. If so, stop the robot. Otherwise go back and repeat the above

steps

Page 34: Oregon Robotics Tournament and Outreach Program

34

Use a Switch Block How will we check the touch and light sensors?

Look at Switch Blocks in the software

Page 35: Oregon Robotics Tournament and Outreach Program

35

Stop on Touch or Light Sensor You try it. Write a program so that your robot

will stop if the light sensor detects a black line OR if the touch sensor is pressed.

Page 36: Oregon Robotics Tournament and Outreach Program

36

One AnswerNote that the robot will stop if either the touch sensor is pressed or a black line is detected, but the loop is still going. What might we do to continue with something else after the robot stops?

Page 37: Oregon Robotics Tournament and Outreach Program

37

Use a Loop Controlled by a Logic Variable

Page 38: Oregon Robotics Tournament and Outreach Program

38

Subroutines or My Blocks What if you find that you are using

the same sequence of blocks in many places in your program?

Examples: 90 degree spin turn Go forward to stop on a black line

Define that sequence as a My Block and save memory in the process

Page 39: Oregon Robotics Tournament and Outreach Program

39

Building a My Block Select the blocks from the program Choose Make A New My Block in

the Edit menu Give the My Block a name, fill in

the description, and build an icon (optional)

Page 40: Oregon Robotics Tournament and Outreach Program

40

Move in a Square

A “primitive” way to move in a square How big is this program?

Use the NXT Window to manage memory Size is 3.7KB

Demonstrate memory management in the software

Page 41: Oregon Robotics Tournament and Outreach Program

41

Move in a Square with My Blocks

Same program with My Blocks is 2.8KB Note that if I double click on a My

Block, it opens the code for inspection or editing.

Page 42: Oregon Robotics Tournament and Outreach Program

42

Managing My Blocks Where does the NXT Software keep My

Blocks? Use Manage Custom Palette in the Edit Menu The My Blocks sub folder has your My Blocks

You can email a file from the My Blocks folder to share the My Block

If you receive a My Block, move it to the My Blocks directory to make it visible in the Custom Palette

Page 43: Oregon Robotics Tournament and Outreach Program

43

Build a My Block to Display a Number Display Block –

displays text only

Convert number to text

You try it

If the bounding box you use to select the blocks for the My Block crosses a data wire, then that value becomes an input or output parameter for the My Block.

Page 44: Oregon Robotics Tournament and Outreach Program

44

Display Number My Block Note the Wait For block to wait for

3 seconds to give time to read the result

Note the number as input parameter to the MyBlock

Page 45: Oregon Robotics Tournament and Outreach Program

45

Debugging Example Go back to your most simple Stop on

Black Program Use your display number My Block to

display the light value when the robot stops.

Add a Move Block to move ahead a small amount (20 degrees)

Now use your display number My Block to display the light value.

What did you notice?

Page 46: Oregon Robotics Tournament and Outreach Program

46

Using the Display Number MyBlock

Note that two blocks have been added at each point where we want to see the light sensor value:1. A Light Sensor block to provide the value the

light sensor is reading2. The Display Number Myblock

Page 47: Oregon Robotics Tournament and Outreach Program

47

What Did You Notice in this Last Exercise? The light sensor value when the robot

first stops will meet the trigger value constraint.

The value 20 degrees further, however, can be quite different.

If the light sensor senses right on the border between black and white, the value will be less than white but greater than black – it might look like green.

Page 48: Oregon Robotics Tournament and Outreach Program

48

Other Debugging Ideas I don’t know where the robot is in the

program. Use a Sound Block to just play a tone to see if the robot has gotten to a particular point.

What value does my variable have? “Print” the value to the NXT screen to see the value.

Take a subtask out of a more complex larger program and debug it separately rather than trying to debug it in the full more complex environment.

Page 49: Oregon Robotics Tournament and Outreach Program

49

Class Exercises -- Summary

Simulate a Wait For Block – Light Sensor Block, Loop variants, and Data Hubs.

Control Stop on Black with trigger value in a variable – creating, reading, and writing Variables.

Stop the robot if the touch sensor is pressed OR the light sensor detects a black line – Switch Block

Develop Display Number My Block – Number to Text, Display Block, My Block with input parameter, and memory management

Use the Display Number to understand why you might be surprised with what the robot is seeing when it stops on black – Debugging idea and using a My Block

Page 50: Oregon Robotics Tournament and Outreach Program

50

More Exercises for the High Achievers or To Do at Home Write a program that will stop only on

a green line. (An example is attached at the end of the slide set.)

Do a My Block that takes an integer as input and moves that many centimeters or inches

Use distance sensor in CanDo challenge

Use color sensor to do the stop on green only exercise

Page 51: Oregon Robotics Tournament and Outreach Program

51

Contact UsWeb site: http://www.ortop.orgEmail: [email protected]: 503-821-1136

Page 52: Oregon Robotics Tournament and Outreach Program

52

Program to Stop on Green We did the following light sensor

exercises in Workshop 1. Stop on black or green Stop only on black

Obvious follow-up is to stop only on green

What is the list of tasks that you would use?

Page 53: Oregon Robotics Tournament and Outreach Program

53

Program to Stop on Green One possible list of tasks

Go forward and stop on green or black (We did this program in Workshop 1. No need to reinvent the wheel.)

Decide if robot has stopped on green or black

If green, stop the program If black, continue the program to the

next green or black block

Page 54: Oregon Robotics Tournament and Outreach Program

54

Stop on Green – One Answer

Note the additions I made that were not in our steps, which I discovered in the debugging process.