540 likes | 623 Views
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 ken_cone@ous.edu (503) 725-2918. Jim Ryan james.r.ryan@intel.com
E N D
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 ken_cone@ous.edu (503) 725-2918 Jim Ryan james.r.ryan@intel.com 971-215-6087 Terry Hamm terry.hamm@gmail.com (503) 720-5157 Roger Swanson swanson@hevanet.com 503-297-1824 Dale Jordan Dale_A_Jordan@msn.com
ORTOP Project Administrator Cathy Swider Cathy_Swider@ous.edu (503) 725-2920
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
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
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!”
Review the NXT G Software to look at all programming blocks that are available NXT Block Review
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 NXT Block Summary • Useful for Debugging • Sound • Display • Number to Text • Text You should know already Will cover tonight
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.
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
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.
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?
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.
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
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.
Data Hubs • Look at Data Hubs in the software
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
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
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
Solution? • Can we use variables and data hubs to solve this problem?
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
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
Light Sensor Block Important Notes on Data Hubs • An input port with a wire overrides the parameter block • Parameter block value is used if no wire on an input port
Different Forms of Loop Block • Look at Loop Blocks in the software • Notice the “Data Hub” on the Loop on Logic Value block
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.
Wait For Using Light Sensor Block • Use a Light Sensor block, which has a data hub, instead of a Wait For block
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.
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?
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
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.
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
Use a Switch Block • How will we check the touch and light sensors? • Look at Switch Blocks in the software
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.
One Answer Note 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?
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
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)
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
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.
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
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.
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
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?
Using the Display Number MyBlock • A Light Sensor block to provide the value the light sensor is reading • The Display Number Myblock Note that two blocks have been added at each point where we want to see the light sensor value:
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.
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.
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
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