1 oregon robotics tournament and outreach program programming techniques workshop for mindstorms ...
TRANSCRIPT
1
Oregon Robotics Tournament and Outreach Program
Programming Techniques Programming Techniques Workshop for Workshop for
MindstormsMindstorms NXT NXT
20122012
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
2
Instructor Contacts
Roger SwansonRoger Swanson
[email protected]@hevanet.com
503-297-1824503-297-1824
Jim RyanJim Ryan
[email protected]@intel.com
971-215-6087971-215-6087
Ken ConeKen Cone
[email protected][email protected]
(503) 725-2918(503) 725-2918
Dale JordanDale Jordan
[email protected][email protected]
Terry HammTerry Hamm
[email protected]@gmail.com
(503) 720-5157(503) 720-5157
3
ORTOP Project Administrator
Cathy SwiderCathy Swider
[email protected][email protected](503) 725-2920
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
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
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!”
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
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.
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
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.
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?
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.
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
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.
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
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
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
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
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
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
25
Different Forms of Loop Block Look at Loop Blocks in the software
Notice the “Data Hub” on the Loop on Logic Value block
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.
27
Wait For Using Light Sensor Block Use a Light Sensor block, which has a
data hub, instead of a Wait For block
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.
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?
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
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.
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
34
Use a Switch Block How will we check the touch and light sensors?
Look at Switch Blocks in the software
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.
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?
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
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)
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
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.
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
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.
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
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?
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 reading
2. The Display Number Myblock
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.
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.
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
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
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?
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