Oregon Robotics Tournament and Outreach Program Programming Techniques Workshop for Mindstorms NXT 2012 Opening doors to the worlds of science and technology for Oregon’s youth 1 Instructor Contacts Ken Cone ken_cone@ous.edu (503) 725-2918 Terry Hamm terry.hamm@gmail.com (503) 720-5157 Dale Jordan Dale_A_Jordan@msn.com Jim Ryan james.r.ryan@intel.com 971-215-6087 Roger Swanson swanson@hevanet.com 503-297-1824 2 ORTOP Project Administrator Cathy Swider Cathy_Swider@ous.edu (503) 725-2920 3 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 4 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 5 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!” 6 NXT Block Review Review the NXT G Software to look at all programming blocks that are available 7 NXT Block Summary High Utility Blocks Move Wait Loop Switch Sensor Blocks Stop Data Blocks (variables) My Blocks Low Utility Blocks Useful for Debugging Record/Play Sound Motor Display Send Number to Message Text Lamps Text Mini Blocks You should know Random already Advanced Will cover tonight 8 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. 9 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 10 One Note about Calibration With an uncalibrated light sensor With a calibrated 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 White is 100 Black is 0 Calibration spreads values over a wider range View function is not useful once you have calibrated your robot. 11 Simple Light Sensor Program 12 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? 13 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. 14 Use a Variable Variables hold values to be used elsewhere Use Edit Menu/Define Variables to define the variable Use the Variable Blocks (Data Block section) to: Click on Create Give the variable a name Set its type: logic, number, or text Read the variable Write the variable Manipulate the variable with other Data Blocks Demonstrate in the software 15 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. 16 Data Hubs Look at Data Hubs in the software 17 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 18 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 19 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 20 Solution? Can we use variables and data hubs to solve this problem? 21 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 22 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 23 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 24 Different Forms of Loop Block Look at Loop Blocks in the software Notice the “Data Hub” on the Loop on Logic Value block 25 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. 26 Wait For Using Light Sensor Block Use a Light Sensor block, which has a data hub, instead of a Wait For block 27 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. 28 Stop on Black with Trigger Value in Variable 29 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? 30 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 31 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. 32 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 33 Use a Switch Block How will we check the touch and light sensors? Look at Switch Blocks in the software 34 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. 35 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? 36 Use a Loop Controlled by a Logic Variable 37 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 38 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) 39 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 40 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. 41 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 42 Build a My Block to Display a Number Display Block – displays text only Convert number to text 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. You try it 43 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 44 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? 45 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 46 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. 47 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. 48 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 49 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 50 Contact Us Web site: http://www.ortop.org Email: questions@ortop.org Phone: 503-821-1136 51 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? 52 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 53 Stop on Green – One Answer Note the additions I made that were not in our steps, which I discovered in the debugging process. 54