Applied Robotics with the SumoBot Student Guide VERSION 1.
WARRANTY Parallax Inc. warrants its products against defects in materials and workmanship for a period of 90 days from receipt of product. If you discover a defect, Parallax Inc. will, at its option, repair or replace the merchandise, or refund the purchase price. Before returning the product to Parallax, call for a Return Merchandise Authorization (RMA) number. Write the RMA number on the outside of the box used to return the merchandise to Parallax.
DISCLAIMER OF LIABILITY Parallax Inc. is not responsible for special, incidental, or consequential damages resulting from any breach of warranty, or under any legal theory, including lost profits, downtime, goodwill, damage to or replacement of equipment or property, or any costs of recovering, reprogramming, or reproducing any data stored in or used with Parallax products. Parallax Inc.
Table of Contents · Page i Table of Contents Preface........................................................................................................................iii Introduction................................................................................................................ iii Educator Resources.................................................................................................. iv The Stamps In Class Educational Series ...............................................
Page ii · Applied Robotics with the SumoBot ACTIVITY #3: Using Peripheral Vision...................................................................164 ACTIVITY #4: Introduction to State Machines and Diagrams ................................170 ACTIVITY #5: Search Pattern and Tawara Avoidance ..........................................176 ACTIVITY #6: Fully Functional Sumo Example Programs.....................................186 Summary.........................................................................
Preface · Page iii Preface INTRODUCTION Robotics is currently enjoying ever increasing popularity with students. Especially when it involves a contest or competition, enthusiasm runs high as participants put everything they've got into their robots in hopes of winning top honors.
Page iv · Applied Robotics with the SumoBot EDUCATOR RESOURCES While the SumoBot Competition kit is designed for the classroom, it really provides an excellent starting point for the robotics enthusiast who wants to have a first taste of robot sumo wrestling. This book is written for ages 14 and up, and it contains lessons that can be useful additions to a variety of courses, including robotics, physics, technology, and pre-engineering.
Preface · Page v “Applied Sensors”, Student Guide, Version 1.3, Parallax Inc., 2003 “Basic Analog and Digital”, Student Guide, Version 1.3, Parallax Inc., 2004 “Industrial Control”, Student Guide, Version 1.1, Parallax Inc.
Page vi · Applied Robotics with the SumoBot SPECIAL CONTRIBUTORS Parallax Inc. would like to recognize the Education Team members who made this book possible: Education and Project Manager Aristides Alvarez, Author and Engineer Andy Lindsay, Technical Illustrator Rich Allred, Graphic Designer Jen Jacobs, and Technical Editor Stephanie Lindsay.
Chapter 1: Mechanical Adjustments · Page 7 Chapter #1: Mechanical Adjustments BEFORE YOU GET STARTED To complete the activities in this book, you will need to build, program and test two complete SumoBot robots by following the activities in the SumoBot Manual. Also, since Applied Robotics with the SumoBot is an advanced robotics text that builds upon the concepts introduced in Robotics with the Boe-Bot, familiarity with that material is recommended.
Page 8· Applied Robotics with the SumoBot its opponent. The less friction, the more easily the SumoBot slips, which means it can no longer push as hard. There are two ways to increase the friction between the tire tread and sumo ring. First, increase the SumoBot's weight, and second, find the best possible tread material. The interesting thing about tire tread materials is that they have to be paired with the material the sumo ring is made out of.
Chapter 1: Mechanical Adjustments · Page 9 Setting up the SumoBot Competition Ring Poster The SumoBot robot and SumoBot Robot Competition Ring poster are for indoor use only. For best results, follow these setup instructions: √ √ √ √ √ Unfold the SumoBot Competition Ring poster, and re-fold it the opposite way so the creases will lie flat, then unfold it again.
Page 10· Applied Robotics with the SumoBot Testing Plow Adjustments A simple program to make each SumoBot go forward should be used to test the plows. This eliminates the possibility of IR object detectors interfering with the SumoBot's forward motion. For example, if one of the SumoBot's IR detectors briefly misses its opponent, the SumoBot will hesitate and might not be going full speed when the two SumoBots collide.
Chapter 1: Mechanical Adjustments · Page 11 √ √ √ √ Repeat five to ten times to be sure which SumoBot's plow adjustment has the advantage. Adjust the plow of the SumoBot that appeared to lose more often, and repeat the test. When you are confident that one of your SumoBots has a winning plow adjustment, try lots of different adjustments on the other SumoBot to find out if there is any better adjustment that can make it the winner.
Page 12· Applied Robotics with the SumoBot DEBUG "Main program running...", CR ' Display program status ' -----[ Main Routine ]------------------------------------------------------FOR counter = 1 TO 100 PULSOUT ServoLeft, 850 PULSOUT ServoRight, 650 PAUSE 20 NEXT ' Deliver 100 forward pulses DEBUG "Done!", CR, "Press/release reset", CR, "twice to restart...", CR END ' User instructions Understanding Forward100Pulses.
Chapter 1: Mechanical Adjustments · Page 13 an even number of times, including zero, the Reset_Button routine just displays the message prompting you to press the Reset button, then it ends the program ends. If you have already completed Robotics with the Boe-Bot, the forward motion code in Forward100Pulses.bs2's Main Routine should be very familiar.
Page 14· Applied Robotics with the SumoBot Figure 1-4 SumoBot Servo Connections The instructions that make the SumoBot move forward starts with these PIN declarations: ServoLeft ServoRight PIN PIN 13 12 The SumoBot's left servo is connected to P13, so I/O pin P13 is given the name ServoLeft. Likewise, P12 is connected to the right servo, so it's named ServoRight. In order for the program to apply 100 pulses, a counter variable is declared: counter VAR Byte This FOR...
Chapter 1: Mechanical Adjustments · Page 15 Figure 1-5 Other Collision Paths One SumoBot can approach at an angle, or even with a curved path. Do not adjust your plows. The strategies presented in this book will use the head-on approach. You can modify your programs (and the plows) to optimize for different approach angles, but wait until after Chapter 5. √ Test a variety of approach angles and plow intersection points with the same full-speed-forward settings.
Page 16· Applied Robotics with the SumoBot A notebook for your observations - keep notes on the various results you observe for developing wrestling strategies. ACTIVITY #2: PREVENTING SERVO SLOWDOWN A SumoBot that executes certain maneuvers more quickly will have an edge over a slower opponent. One of the things that can slow your SumoBot down is trying to read too many sensors between servo pulses.
Chapter 1: Mechanical Adjustments · Page 17 Figure 1-6 Top Speed vs. Low Time 53 RPM 47 RPM As mentioned earlier, taking too much time between pulses to check sensors can cause the servos to slow down. IR object detectors don't take a very big bite out of the low time between servo pulses. Each one only takes a couple milliseconds to read. QTI line detectors, on the other hand, can take up to 20 milliseconds each.
Page 18· Applied Robotics with the SumoBot With a couple modifications to Forward100Pulses.bs2 from Activity #1, you'll be ready to go. √ √ Save Forward100Pulses.bs2 as ForwardLowTimeTest.bs2. Add a LowTime constant declaration: LowTime √ CON 20 Change the FOR...NEXT loop in the Main Routine to a DO...
Chapter 1: Mechanical Adjustments · Page 19 √ ' ' ' ' ' ' ' Compare the results of the two trials and determine which program gives the SumoBot better performance. -----[ Program Description ]-----------------------------------------------Applied Robotics with the SumoBot - ForwardLowTimeTest.bs2 SumoBot goes forward indefinitely. Use the Reset button to start and stop the forward motion. {$STAMP BS2} {$PBASIC 2.
Page 20· Applied Robotics with the SumoBot LOOP DEBUG "Press/release reset", CR, "twice to restart...", CR ' User instructions END Your Turn - More Pulses, Less Distance Chapter 4, Activity #3 in Robotics with the Boe-Bot demonstrates how the amount of time a servo turns translates to distance traveled. When there's less time between each pulse, the program will have to send the servos more pulses to make them turn for the same amount of time.
Chapter 1: Mechanical Adjustments · Page 21 The distance traveled will not be exactly the same because the servos don't turn as fast with 40 ms pauses. This means that you'll probably have to use a little less than twice as many pulses with 20 ms pauses to match the distance of the program with 40 ms pulses. √ Tune the EndValue in the FOR...NEXT loop with the 20 ms pauses to get as close as possible to the distance traveled with 40 ms pauses.
Page 22· Applied Robotics with the SumoBot √ √ √ √ Run the modified program. Repeat the 10 second measurement and record the servo speed. Keep increasing the LowTime constant and measuring servo speed until you note obvious speed decay. Record the maximum LowTime value you can use without slowing down your SumoBot in your notes for future reference. Your Turn - Wheel Rotation Speed Measurements You can also calculate the percent speed reduction for a given pause time.
Chapter 1: Mechanical Adjustments · Page 23 Scales that show both ounces and grams are not uncommon, likewise with scales that show both kilograms and pounds. They all appear to work because they are being used on the Earth's surface. When astronauts took equipment from the earth to the moon, the equipment weighed less because gravity on the moon isn't as strong. However, each piece of equipment still had the same mass (total protons neutrons and electrons).
Page 24· Applied Robotics with the SumoBot Since weight is a force, and gravity is a form of acceleration, an equation you will find useful in your calculations is: W = m× g 2 2 The acceleration due to gravity is 9.8 m/s in the SI system and 32 ft/s in the British Engineering system. Problem: An object has a mass of 0.75 kg, what's its weight in newtons? Solution: Start with the equation w = mg, and figure out which system to use and which pieces of information you already know.
Chapter 1: Mechanical Adjustments · Page 25 If you measure an object's mass to be a certain number of grams, converting to kilograms is a matter of using the conversion factor that will make the units you don't want cancel out. Problem: You measured your mass to be 280 g, and you want kg. Solution: Multiply by the conversion factor with the g in the denominator. 1= 1 kg 1000 g It will cancel out the g in the numerator of the term you are starting with (280 g), and the result will have units of kg.
Page 26· Applied Robotics with the SumoBot 8 oz = 8 oz × 1 × 1 × 1 1lb 453.6 g 1kg = 8 oz × × × 16 oz 1lb 1000 g = 0.2268 kg These unit conversions take some practice; here are some tips: √ √ √ √ √ Find the equality with your desired result units first. Make it a conversion factor with the desired units in the numerator. Find equalities with units that link your starting units to your result units.
Chapter 1: Mechanical Adjustments · Page 27 Figure 1-8 also shows a close-up of the contact surfaces, where little components of the force you apply to the block and the frictional forces are opposing each other. While some of the frictional forces actually do come from the surface's roughness, there is also interaction between the molecules in the two surfaces that govern frictional forces.
Page 28· Applied Robotics with the SumoBot ∑ Fy = 0 N− W = 0 N= W N = W indicates that the normal force, N, is pushing back just as hard as the object's weight is pushing down on the surface. This has to be true. If it wasn't, the block would sink through the table, or maybe the table would start sinking into the earth. Newton's Third Law can be summarized like this: if two bodies (such as the block and the surface it's on) interact, each body exerts an equal and opposite force on the other.
Chapter 1: Mechanical Adjustments · Page 29 To appreciate how different the coefficients of friction can be for different pairs of materials, take a look at Table 1-2. Table 1-2: Examples of Coefficients of Friction[3] Materials µS µK Rubber on Concrete 1.0 0.8 Copper on steel 0.53 0.36 Ice on ice 0.1 0.003 Strategy Consideration: The coefficient for each pair of materials is different.
Page 30· Applied Robotics with the SumoBot Figure 1-10 Friction vs. Applied Forces Another Strategy Consideration Figure 1-9 shows that the SumoBot can exert its highest force of friction just before losing traction. After it starts slipping, it can't push as hard against its opponent because its treads only have kinetic friction to rely on. Problem: You had to apply 5 lb of force to make an object that weighs 10 lb start to slide on a surface.
Chapter 1: Mechanical Adjustments · Page 31 Coefficients of friction do not have units. A friction force is always equal to the coefficient of friction, multiplied by the normal force. There are no units that need to cancel each other out. For example, if you start with a normal force that's in newtons, and multiply by a coefficient of friction, the result will be a frictional force in newtons.
Page 32· Applied Robotics with the SumoBot Figure 1-12 Individual Free Body Diagrams Since we already know that T = T1, we now know that T = W1. Next, calculate the sum of the forces of the block on the table in both the x and y axis directions. ∑ Fy = 0 N− W = 0 N= W ∑F X =0 T − fS = 0 W1 − fS = 0 W1 = fS fS = W1 So, our key for solving this puzzle is that the normal force N is equal to the object's weight, and the force of static friction fS is equal to the weight of the hanging object.
Chapter 1: Mechanical Adjustments · Page 33 Next, convert to the corresponding units of force (newtons): W = mg N = 2.1kg × 9.8 m / s 2 = 20.58 N fS = 0.7 kg × 9.8 m / s2 = 6.86 N Finally, plug these forces into the coefficient of kinetic friction equation and calculate the result: fK N 6.86 N = 20.58 N µS = = 0.333... Why couldn't I have just used the ratio of the masses? For this particular problem on the earth's surface, you can use this shortcut.
Page 34· Applied Robotics with the SumoBot Problem: The coefficient of static friction for two surfaces is 0.1 (pretty slippery). How much force will it take to get a 10 lb weight to start to slide if the contact surfaces are level? Solution: It takes 1 lb of force to start the object's slide: fS = µS × W = 0.
Chapter 1: Mechanical Adjustments · Page 35 Don't worry if you don't happen to have a gram scale or pulley and attachment. Thanks to the popularity of calorie counting, dietary scales can be purchased at most food markets and drug stores for under $10 (US). Pulleys and mounting hardware are also easy to get from hardware stores for $5 or less, just ask where to find the sliding door replacement parts. Figure 1-13 shows examples of both pieces of equipment.
Page 36· Applied Robotics with the SumoBot Figure 1-14 Friction Force Test When you have constructed this setup, follow these steps. √ √ √ √ √ √ √ √ √ √ Draw free body diagrams and write the equations for Figure 1-14. Use the problem and solution with Figure 1-11 and Figure 1-12 as your guide. Record the SumoBot's weight as W = _____. Add weight to the cup until the SumoBot starts to slide. Remove enough weight so that the SumoBot doesn't slide.
Chapter 1: Mechanical Adjustments · Page 37 √ √ √ √ √ Use your weights and the equations introduced in this activity to calculate the coefficients of static and kinetic friction for the SumoBot tire treads and the sumo ring surface. Put the SumoBot on the scale, and add mass until the scale indicates your SumoBot and payload is just under 500 g. Repeat these tests with your 500 g SumoBot. Compare the forces of friction the sumo ring exerts on the tire tread with and without payload.
Page 38· Applied Robotics with the SumoBot SUMMARY This chapter introduced a variety of mechanical adjustments you can make to the SumoBot. Adjusting the plow angle is one important consideration. Managing the pause time between servo pulses is another important consideration because it can result in slower servo motor operation. Frictional forces are yet another important consideration. There are two ways to increase the friction between the SumoBot's tire treads and the competition ring.
Chapter 1: Mechanical Adjustments · Page 39 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.
Page 40· Applied Robotics with the SumoBot 13. It takes 70 lbs of force to start a 100 lb object sliding along a level surface. Calculate the coefficient of static friction between the object and the surface. 14. It takes 28 lb of force to start a 37 lb object sliding along a level surface. Calculate the coefficient of static friction between the object and the surface. 15. It takes 4.9 N of force to start a 1 kg object sliding along a level surface.
Chapter 2: EEPROM Tricks and Program Tips · Page 41 Chapter #2: EEPROM Tricks and Program Tips The BASIC Stamp's EEPROM is a great tool for storing values that you don't want to get erased. After you've stored a value to EEPROM, it doesn't matter whether the power has been turned off or the Reset button has been pressed and released. The value will still be there (in EEPROM) when your program needs it.
Page 42· Applied Robotics with the SumoBot As the SumoBot programs get larger and more complex, both good EEPROM management and strict adherence to code conventions will make large and complex programs seem easy, or at least, not nearly as hard as they might look. ACTIVITY #1: A CLOSER LOOK AT THE EEPROM The optional Symbol names that you can use to precede DATA directives are powerful programming tools that make storing and accessing values much easier.
Chapter 2: EEPROM Tricks and Program Tips · Page 43 Figure 2-1 shows what the DATA directives will store in the EEPROM bytes with addresses 0 to 31. It also points out the first byte address of each DATA directive with Symbol names. Keep in mind that the BASIC Stamp Editor makes each DATA directive's Symbol name a constant equal to the address of the first byte in the DATA directive. That way, you can always use the Symbol name to reference the first DataItem in a given DATA directive.
Page 44· Applied Robotics with the SumoBot EEPROM stands for Electrically Erasable Reprogrammable Read Only Memory. EEPROM memory is nonvolatile. That means, you can store a value in EEPROM, disconnect power, then reconnect power, and the values you stored will still be there. Compile-time vs. Run-time - DATA directives are processed when the BASIC Stamp Editor compiles the program. In other words, DATA directives are processed at compile-time.
Chapter 2: EEPROM Tricks and Program Tips · Page 45 READ RunStatus, temp DEBUG DEC temp, CR DEBUG "Address of UndefData........", DEC UndefData, CR, CR DEBUG "Address of Alphabet........", DEC Alphabet, CR DEBUG "Values stored........." FOR counter = 0 TO 6 READ Alphabet + counter, temp DEBUG temp PAUSE 200 NEXT END Your Turn - Examining EEPROM Symbol name Values Figure 2-2 shows the Memory Map window for SymbolNamesVsAddressContents.bs2.
Page 46· Applied Robotics with the SumoBot the right by 161 = 16, then the third digit from the right by 162 = 256, and so on. Add up all the products, and you've got your conversion.
Chapter 2: EEPROM Tricks and Program Tips · Page 47 √ Modify the RunStatus DATA directive like this: RunStatus DATA @10, Word 500 √ Modify the command that reads the EEPROM byte at the RunStatus address like this: READ RunStatus, Word temp √ √ Test the program to make sure it can retrieve and display 500. Check the Memory Map and verify that the RunStatus DATA directive sets aside 2 bytes instead of 1.
Page 48· Applied Robotics with the SumoBot READ EepromCounter, temp To make the program count the number of consecutive resets, simply add 1 to the value of temp and write it back to the EEPROM byte at the EepromCounter address. Next time the program starts, the value the READ command fetches will be higher by 1. temp = temp + 1 WRITE EepromCounter, temp The program can then make decisions based on the value of the temp variable.
Chapter 2: EEPROM Tricks and Program Tips · Page 49 temp = temp + 1 WRITE EepromCounter, temp temp = temp - 1 IF temp = 0 THEN DEBUG "Since download, you have", CR, "pressed Reset ", DEC temp, " times.", CR ELSE DEBUG CRSRX, 11, "...", DEC temp, " times.", CR ENDIF END Your Turn - Distinguishing Odd from Even You can determine whether the Reset button has been pressed/released an odd or even number of times by examining the temp variable as a binary number.
Page 50· Applied Robotics with the SumoBot binary value from the right is accessed with .BIT1; the third binary value is accessed with .BIT2, and so on. Here is an IF...THEN statement you can use to determine whether the value of temp is odd or even in ResetButtonCounter.bs2: IF temp.BIT0 = 0 THEN DEBUG DEC temp, " is even.", CR ELSE DEBUG DEC temp, " is odd.", CR ENDIF √ √ √ Save ResetButtonCounter as ResetButtonCounterYourTurn.bs2 Insert the odd/even IF...
Chapter 2: EEPROM Tricks and Program Tips · Page 51 Figure 2-3 Debug Terminal Transmit and Receive Windowpanes Transmit Windowpane Type each value here, then press Enter. Receive Windowpane Prompts you to enter values and replies into the Transmit Windowpane. Illustrating Many Jobs with a Few Variables The temp and counter variables get used and re-used in the next example program. In later example programs they will be used and re-used by many different subroutines.
Page 52· Applied Robotics with the SumoBot DEBUG CR, "Enter up to 20 values", CR, "between 100 and 1000", CR, "Press 0 (zero) when done", CR, ">" counter = 0 DO UNTIL counter >= 40 OR temp = 0 DEBUGIN DEC temp WRITE Values + counter, Word temp counter = counter + 2 DEBUG ">" LOOP The temp variable is then used to get a character from the Debug Terminal's Transmit windowpane and use it to make a decision.
Chapter 2: EEPROM Tricks and Program Tips · Page 53 DEBUG CRSRX, 0, DEC5 temp, CRSRX, 12 IF temp > compare THEN DEBUG "greater than" ELSEIF temp < compare THEN DEBUG "less than" ELSE DEBUG "equal to" ENDIF DEBUG CR counter = counter + 2 LOOP ENDIF Example Program: ThreeVariablesManyJobs.bs2 √ √ √ Enter, save, and run ThreeVariablesManyJobs.bs2. Try feeding the program the same values shown in Figure 2-3 first. Then re-run the program and use your own values. Review the program.
Page 54· Applied Robotics with the SumoBot ' Get values and store them to EEPROM DEBUG CR, "Enter up to 20 values", CR, "between 100 and 1000", CR, "Press 0 (zero) when done", CR, ">" counter = 0 DO UNTIL counter >= 40 OR temp = 0 DEBUGIN DEC temp WRITE Values + counter, Word temp counter = counter + 2 DEBUG ">" LOOP ' Display & compare values? (y/n) DEBUG CR, "Compare values to ", CR, "threshold? (y/n)", CR, ">" DEBUGIN temp ' Display and compare values IF temp = "y" OR temp = "Y" THEN DEBUG CR, " Thresho
Chapter 2: EEPROM Tricks and Program Tips · Page 55 LOOP ENDIF DEBUG CR, "All done!" END ACTIVITY #3: PROGRAM ON/OFF WITH RESET It's really handy to be able to start and halt a sumo wrestling program by pressing and releasing the SumoBot's Reset button. This technique was first introduced in Chapter 3 of the SumoBot book. It makes it possible to press and release the Reset button to toggle between two separate program modes: wrestle, and wait for reset.
Page 56· Applied Robotics with the SumoBot temp is even, it displays "Program running..." and returns the program to initialization and onward from there. ' -----[ Subroutine - Reset ]------------------------------------------------Reset: READ RunStatus, temp temp = temp + 1 WRITE RunStatus, temp ' Byte @RunStatus -> temp ' Increment temp ' Store new value for next time IF (temp.BIT0 = 1) THEN DEBUG CLS, "Press/release Reset", CR, "button..." END ELSE DEBUG CR, "Program running...
Chapter 2: EEPROM Tricks and Program Tips · Page 57 continues to the rest of the program. As you keep pressing the Reset button, the odd/even pattern continues, as does the program's alternation between the "Press/release Reset..." and "Program running..." messages. Example Program: TestResetButton.bs2 √ √ √ √ √ √ √ Enter, save, and download TestResetButton.bs2. Verify that it displays the "Press/release reset button..." message and ends the program. Press/release the Reset button.
Page 58· Applied Robotics with the SumoBot END ' -----[ Subroutine - Reset ]------------------------------------------------Reset: READ RunStatus, temp temp = temp + 1 WRITE RunStatus, temp ' Byte @RunStatus -> temp ' Increment temp ' Store new value for next time IF (temp.BIT0 = 1) THEN DEBUG CLS, "Press/release Reset", CR, "button..." END ELSE DEBUG CR, "Program running..." ENDIF ' Examine temp.
Chapter 2: EEPROM Tricks and Program Tips · Page 59 Building and Testing the LED and Piezospeaker Circuits The LED and piezospeaker circuit shown in Figure 2-4 is an interesting combination of the individual circuits. Instead of connecting the LED and piezospeaker to separate I/O pins, both circuits are connected to the same I/O pin. This circuit can come in handy, especially if you are running low on available I/O pins.
Page 60· Applied Robotics with the SumoBot LedSpeaker PIN 5 After declaring this PIN directive, the name LedSpeaker can be used as either an input or an output. The BASIC Stamp Editor examines each command with LedSpeaker as an argument and decides whether to send an output to the pin or store the input value sensed by the pin. When used in the PIN argument of commands like HIGH, LOW, TOGGLE, or FREQOUT, the BASIC Stamp Editor treats the I/O pin as an output. Example Program: TestLedSpeaker.
Chapter 2: EEPROM Tricks and Program Tips · Page 61 FOR counter = 1 TO 30 TOGGLE LedSpeaker PAUSE 250 NEXT ' Flash LED on/off 15 times DEBUG CR, "Speaker playing tone" ' Prompt to listen for tone FREQOUT LedSpeaker, 2000, 3000 ' Play 3 kHz tone for 2 seconds DEBUG CR, "All done." ' Prompt user - program finished END Your Turn - Playing Musical Notes The LOOKUP command can be handy for storing brief sequences of musical notes. √ √ √ Save a copy of the program as TestLedSpeakerYourTurn.bs2.
Page 62· Applied Robotics with the SumoBot Parts List (1) Jumper wire (1) LED - red (1) 1 Resistor - 470 Ω (yellow-violet-brown) (1) 1 Resistor - 10 kΩ (brown-black-orange) Figure 2-5 Pushbutton Circuit added to LED and Piezospeaker A PIN directive can also be used for P6: pBSense PIN 6 Now the name pBSense can be used in DEBUG commands to display whichever pushbutton state the I/O pin senses. pBSense can also be used in IF...
Chapter 2: EEPROM Tricks and Program Tips · Page 63 Figure 2-6 Debug Terminal Displaying the State of pbSense Zeros repeat every 1/10 second while the button is not pressed. Ones repeat while the button is pressed. Example Program: TestPushbutton.bs2 √ √ √ Enter, save, and run TestPushButton.bs2. The Debug Terminal should display a message, and then start displaying a series of repeating zeros every 1/10 second. Watch the Debug Terminal as you press, hold, and release the pushbutton.
Page 64· Applied Robotics with the SumoBot pbSense PIN 6 ' Pushbutton connected to P6 ' -----[ Initialization ]----------------------------------------------------DEBUG CLS, "Press/hold/release" , CR, "pushbutton on", CR, "breadboard...", CR ' PROMPT press/release pushbutton ' -----[ Main Routine ]------------------------------------------------------DO ' DO...
Chapter 2: EEPROM Tricks and Program Tips · Page 65 ACTIVITY #5: PUSHBUTTON PROGRAM MODE SELECTION In some competitions, changing strategy between sumo rounds could make a big difference in your SumoBot's standing. This activity introduces a simple technique you can use to select the SumoBot's mode of operation based on cues from the speaker. Selecting the Mode In the next example program, you can enter a mode selection by pressing and holding the pushbutton while you press and release the Reset button.
Page 66· Applied Robotics with the SumoBot has still not been released, the outer loop repeats again, the inner loop beeps three times, and so on. ' -----[ Subroutine - Mode_Select ]-----------------------------------' Selects mode of operation Mode_Select: DEBUG CR, ' Display user instructions CR, "Release pushbutton after", CR, "number of beeps to select", CR, "mode (1 to 5).
Chapter 2: EEPROM Tricks and Program Tips · Page 67 CASE 1 DEBUG CASE 2 DEBUG CASE 3 DEBUG CASE 4 DEBUG CASE 5 DEBUG ENDSELECT ENDIF "one" "two" "three" "four" "five" END ' End program Strategy Tip Your SumoBot programs could use this mode argument any number of ways. For example, it might use SELECT...CASE to choose between different kinds of navigation routines that search for opponents.
Page 68· Applied Robotics with the SumoBot ' -----[ EEPROM Data ]-------------------------------------------------------ModeSelect DATA 0 ' Program ' -----[ Initialization ]----------------------------------------------------IF pbSense = 1 THEN GOSUB Mode_Select ' Call Mode_Select subroutine ' -----[ Main Routine ]------------------------------------------------------READ ModeSelect, temp ' Fetch mode from EEPROM IF temp = 0 THEN DEBUG CR, "No mode selected", CR, "Hold down pushbutton", CR, "then p
Chapter 2: EEPROM Tricks and Program Tips · Page 69 NEXT PAUSE 1000 IF pbSense = 0 THEN WRITE ModeSelect, temp EXIT ENDIF NEXT ' ' ' ' 1 second between beeps Button released? Record mode to EEPROM Exit FOR...NEXT loop RETURN Your Turn √ Try modifying PushbuttonMode.bs2 so that you can choose different forward distances to make the SumoBot travel in case you have to repeat plow adjustments or other tests.
Page 70· Applied Robotics with the SumoBot One of the most important ingredients of modular programs is keeping the same naming conventions for variables. For example, in this book, temp is a temporary variable that is used to receive and manipulate stored information and sensor measurements. After decisions are made based on temp in one subroutine, temp is always free to be used in the next subroutine. The same applies to the counter variable.
Chapter 2: EEPROM Tricks and Program Tips · Page 71 While all the steps up to this point were purely mechanical, the Initialization section will take some thought. There are several options. You can put the IF...THEN statement from PushbuttonMode.bs2's initialization as the first, second or third step. To make the decision, think about how the program would work if the IF...THEN is the first step. Then, consider how it would work differently if it was the second step, and so on.
Page 72· Applied Robotics with the SumoBot ' -----[ I/O Definitions ]---------------------------------------------------LedSpeaker PIN 5 ' P5 controls LED & speaker ' ---> Pasted from PushbuttonMode.bs2 <--pbSense PIN 6 ' Pushbutton connected to P6 ' -----[ Variables ]---------------------------------------------------------temp counter VAR VAR Word Byte ' Temporary variable ' Loop counting variable.
Chapter 2: EEPROM Tricks and Program Tips · Page 73 DEBUG "five" ENDSELECT ENDIF ResetTest: DEBUG CR, "Done!" END ' Verify we made it to main. ' -----[ Subroutine - Reset ]------------------------------------------------Reset: READ RunStatus, temp temp = temp + 1 WRITE RunStatus, temp ' Byte @RunStatus -> temp ' Increment temp ' Store new value for next time IF (temp.BIT0 = 1) THEN DEBUG CLS, "Press/release Reset", CR, "button..." END ELSE DEBUG CR, "Program running..." ENDIF ' Examine temp.
Page 74· Applied Robotics with the SumoBot PAUSE 100 FREQOUT LedSpeaker, 100, 4000 NEXT PAUSE 1000 IF pbSense = 0 THEN WRITE ModeSelect, temp EXIT ENDIF NEXT ' ' ' ' 1 second between beeps Button released? Record mode to EEPROM Exit FOR...NEXT loop RETURN Your Turn - Testing the New Program There should be two ways to set the program mode. The first is to hold down the pushbutton as you download the program. The other is to hold the pushbutton down as you press and release the Reset button.
Chapter 2: EEPROM Tricks and Program Tips · Page 75 SUMMARY This chapter introduced a variety of EEPROM and program management techniques. EEPROM management focused on declaring and naming DATA directives for all EEPROM bytes and groups of bytes used in the program. Techniques for counting resets were introduced for both toggling between different SumoBot program modes and for selecting from a list of program modes.
Page 76· Applied Robotics with the SumoBot 9. What is the decimal number for hexadecimal-E? 10. What PBASIC operator allows you to examine individual bits in larger variables? 11. What does the IF...THEN statement in the Reset subroutine do if the temp variable is even? 12. What variable stores the binary 1/0 values sensed by pbSense? 13. In PushbuttonMode.bs2, what how is temp used? 14. Why are PBASIC programs separated into sections? 15.
Chapter 3: EEPROM Tricks and Program Tips · Page 77 Chapter #3: Sensor Management The SumoBot's sensors are critical components to its performance in the Sumo ring. To get the most out of the sensors that come in the SumoBot Robot Competition Kit, it's important to have a better understanding of how the sensors work. Understanding each sensor's strengths, weaknesses and pitfalls will be important ingredients to your SumoBot strategies.
Page 78· Applied Robotics with the SumoBot ACTIVITY #1: TESTING AND TUNING INFRARED OBJECT DETECTORS Figure 3-1 shows the SumoBot and the locations of its front left and front right infrared (IR) object detectors. The IR LED and receivers were plugged into headers X8 and X9 and tested in the Chapter 4 of the SumoBot Manual. Testing and tuning these object detectors can make a huge difference in your SumoBot's performance in the competition ring.
Chapter 3: EEPROM Tricks and Program Tips · Page 79 to the header. The right column shows the components that you plugged into the header, the IR receiver, and shielded IR LED. Figure 3-2 IR Object Detection Circuits Testing the front left IR detector involves sending a 38500 kHz signal to P4, then immediately storing the value sensed at P11 in a bit variable. Here is an example of some PBASIC code that will do this: irLF VAR Bit . . .
Page 80· Applied Robotics with the SumoBot If the IR receiver is sending 5 V to P11, it means it doesn't see any reflected infrared. If this is the case, the BASIC Stamp stores the value 1 in the P11 input register bit IN11. If the IR receiver does detect infrared reflected off some object, it will send 0 V to P11. While P11 senses 0 V, IN11 stores a 0. The problem is, the IR receiver only sends that 0 V for a fraction of a millisecond after the FREQOUT command stops.
Chapter 3: EEPROM Tricks and Program Tips · Page 81 The IR receivers are active-low, meaning they send a low signal to signify the active condition (IR signal detected). When your SumoBot is dealing with lots of sensors, the programming and trouble shooting will all be easier if they are all active-high. The problem is this: irLF stores a 0 when an object is detected and a 1 when it is not. Instead, we want irLF to store the opposite, a 1 when an object is detected, and a 0 when it is not.
Page 82· Applied Robotics with the SumoBot Figure 3-3 Opponent Detected at Front-Right Example Program - TestFRontIrObjectDetectors.bs2 √ √ √ Enter, save, and run TestFrontIrObjectDetectors.bs2. Point your SumoBot away from nearby objects. It may actually detect walls as far as 1 to 2 m away. If your detectors are pointing slightly downward, they will also detect the floor, so point the IR LEDs level to the ground, or even slightly upward if needed.
Chapter 3: EEPROM Tricks and Program Tips · Page 83 ' ' ' ' -----[ Title ]-------------------------------------------------------------Applied Robotics with the SumoBot - TestFrontIrObjectDetectors.bs2 This program tests the IR object detectors mounted on the X8 and X9 headers on the front of the SumoBot. ' {$STAMP BS2} ' {$PBASIC 2.5} ' Target = BASIC Stamp 2 ' Language = PBASIC 2.
Page 84· Applied Robotics with the SumoBot Your Turn - Headlight Adjustments A small change in the direction the SumoBot's IR LEDs are pointing can make a huge difference in its performance. Remember this when testing the competition code at the end of chapter 4 and the datalogging code at the end of Chapter 5. You will probably find yourself adjusting the direction each IR LED points to get the most consistent detection of your opponent.
Chapter 3: EEPROM Tricks and Program Tips · Page 85 Testing for Sources of IR Interference Fluorescent lights have a part called a ballast built into them. The ballast is responsible for amplifying the AC outlet voltage to a level that is high enough to make the gas inside the glass tube fluoresce and emit light. Some of the electronic ballasts built into more recently manufactured fluorescent lights can be a problem for the infrared detectors.
Page 86· Applied Robotics with the SumoBot ' -----[ Main Routine ]-----------------------------------------------DO ' DO...LOOP repeats indefinitely irLF = ~IrSenseLF irRF = ~IrSenseRF ' Save IR receiver outputs ' Sound alarm if IR detected IF irLF = 1 OR irRF = 1 THEN DEBUG "IR interference detected!!!", CR FOR counter = 1 TO 7 FREQOUT LedSpeaker, 50, 4000 PAUSE 50 NEXT ENDIF LOOP ' Repeat DO...
Chapter 3: EEPROM Tricks and Program Tips · Page 87 Always test for and eliminate sources of IR interference near your SumoBot Competition Ring. ' ' ' ' -----[ Title ]-------------------------------------------------------------Applied Robotics with the SumoBot - IrInterferenceSniffer.bs2 This program tests for IR interference from other fluorescent lights, handheld remotes, video recorders and other SumoBot robots. ' {$STAMP BS2} ' {$PBASIC 2.5} ' Target = BASIC Stamp 2 ' Language = PBASIC 2.
Page 88· Applied Robotics with the SumoBot Testing for Electrical Continuity The leads on the IR LEDs and receivers tend to be thinner than jumper wires and other component leads. The X8 and X9 sockets on some SumoBot boards may also have sockets with slightly larger holes than the ones in the breadboard. A resulting loose fit could be a problem for some IR LEDs and detectors.
Chapter 3: EEPROM Tricks and Program Tips · Page 89 other parts can be clipped into small segments and inserted into very loose-fitting X8 and X9 sockets along with the component leads. Figure 3-4 shows examples. If the IR component leads already have a snug fit in the X8/X9 sockets, do not follow these steps. Instead, move on to testing and tuning effective range.
Page 90· Applied Robotics with the SumoBot Figure 3-5 Opponent vs. Distraction If your SumoBot has a tendency to get distracted by nearby objects, there are ways to make it more nearsighted. The sensitivity of the IR receivers can be adjusted by changing either the resistance in series with the IR LED or the FREQOUT command's frequency. A larger series resistor would make the IR LED headlights dimmer, and different frequencies would make the IR receivers less sensitive to reflected infrared.
Chapter 3: EEPROM Tricks and Program Tips · Page 91 and at each frequency, the Debug Terminal displays a "yes" if the IR receiver detected an object, or a "no" if it didn't. Notice how the Debug Terminal shows a lot more "yes" detections for the 0.6 m distance test than it does for the 0.85 m test. The 0.85 m test demonstrates that 39.5 kHz is the most sensitive frequency for the detectors being tested.
Page 92· Applied Robotics with the SumoBot The next example program is another modified version of TestIrFrontObjectDetectors.bs2. The IrFreq constant in the FREQOUT command's Freq1 argument is replaced with a Word variable named frequency. This variable is swept from 36000 to 42000 in steps of 500 by a FOR...NEXT loop. It's a quick and easy way to perform a frequency sweep with the IR LEDs and capture the IR receivers' frequency responses.
Chapter 3: EEPROM Tricks and Program Tips · Page 93 √ √ √ √ √ √ √ If zero "yes" readings appear, move the SumoBot 5 cm closer to the wall. If several "yes" readings appear, move the SumoBot 5 cm farther from the wall. Press and release the Reset button to refresh the Debug Terminal. Repeat until you find the distance threshold between no object detections and a few object detections at the most sensitive frequencies.
Page 94· Applied Robotics with the SumoBot ' -----[ Initialization ]----------------------------------------------------DEBUG CLS, "FRONT IR DETECTORS", CR, "Frequency Left Right", CR, "--------- --------", CR ' Display heading ' -----[ Main Routine ]------------------------------------------------------FOR frequency = 36000 TO 42000 STEP 500 FREQOUT IrLedLF, 1, frequency irLF = ~IrSenseLF ' Left IRLED shines IR light ' Save IR receiver output FREQOUT IrLedRF, 1, frequency irRF = ~IrSenseRF ' Repeat f
Chapter 3: EEPROM Tricks and Program Tips · Page 95 √ Modify TestIrFrontObjecDetectors.bs2 so that it has 3 or 4 useful versions of the IrFreq CON directive. ACTIVITY #2: A CLOSER LOOK AT THE QTI LINE SENSORS The SumoBot shown in Figure 3-7 has its QTI line sensors mounted under left and right sides of the plow. The QTIs are designed to detect the white tawara line, which is the border of the competition ring.
Page 96· Applied Robotics with the SumoBot Figure 3-8 Front Left and Right QTI Schematics Each QTI has four components mounted on its PCB, a 220 Ω resistor, a 470 Ω resistor, a 0.01 µF capacitor, and a QRD1114 reflective object sensor. The way these parts are connected to the QTI's 3-pin header make it so that you can turn its power on or off by sending either a high or low signal to its W pin. The actual sensor measurements are taken through its R pin.
Chapter 3: EEPROM Tricks and Program Tips · Page 97 The QRD1114 has an IRLED in it that shines IR light on the surface it's facing. Unlike the IR object detectors we just finished testing, this device does not need to flash the IR on/off at 38.5 kHz. That's because the QTI is designed to be held close enough to its target surface that IR from the window and the lamps in the room won't be as likely interfere. Figure 3-10 shows a simplified version of the QTI circuit with the power already turned on.
Page 98· Applied Robotics with the SumoBot uses RCTIME to measure and store the time it took for V0 to decay to 1.4 V in a variable named temp: HIGH 9 PAUSE 1 RCTIME 9, 1, temp Remember that P10 has to be set high to turn on the QTI, and low to turn it back off again when the measurement is done.
Chapter 3: EEPROM Tricks and Program Tips · Page 99 Figure 3-11 QTI RC-Decay for Different Surfaces If there is no surface to reflect IR from the QTI's IR LED, the only source of infrared for the IR transistor's base is the ambient light in the room. That might be a lot of infrared if sunlight is streaming through the windows, or a little if the blinds are closed and only a few fluorescent lights are on. That's the case with the 3500 RCTIME measurement.
Page 100· Applied Robotics with the SumoBot Figure 3-12 White Tawara vs. Black Sumo Ring Surface Example Program - TestFrontQtiLineSensors.bs2 Keep well away from direct sunlight and other sources of bright light. Your SumoBot and SumoBot Robot Competition Ring poster should be well away from any sources of direct sunlight and other bright lights. Close nearby blinds if necessary, and make sure the light sources in your work area are either fluorescent or indirect incandescent.
Chapter 3: EEPROM Tricks and Program Tips · Page 101 ' {$STAMP BS2} ' {$PBASIC 2.5} ' Target = BASIC Stamp 2 ' Language = PBASIC 2.
Page 102· Applied Robotics with the SumoBot Your Turn - Detecting Tilt What if the QTIs were to give you a unique signature when the SumoBot is tilted backward? √ Try tilting the SumoBot while it's sitting on the sumo ring. √ Are the readings different from black and white? √ How does the light level in the room effect tilt measurements? Strategy tip While it seems like this might be a great way to detect whether your SumoBot is at a disadvantage, it tends not to work in practice.
Chapter 3: EEPROM Tricks and Program Tips · Page 103 should measure the QTI readings for the black surface and use them as a basis for setting a threshold value to decide whether the QTIs are "seeing" black or white. As you may have noticed from Activity #2, white QTI measurements are very small in comparison. Your program can safely set the threshold at 1/4 of the average of the two QTI sensors' measurements of black.
Page 104· Applied Robotics with the SumoBot Figure 3-13 White Tawara vs. Black Sumo Ring Surface Example Program: QtiSelfCalibrate.bs2 √ Place the SumoBot on the black part of your practice ring. Lighting Reminder Your SumoBot and SumoBot Robot Competition Ring poster should be well away from any sources of direct sunlight and other bright lights. Close nearby blinds if necessary, and make sure the light sources in your work area are either fluorescent or indirect incandescent.
Chapter 3: EEPROM Tricks and Program Tips · Page 105 ' {$STAMP BS2} ' {$PBASIC 2.5} ' Target = BASIC Stamp 2 ' Language = PBASIC 2.
Page 106· Applied Robotics with the SumoBot DEBUG "Black" ENDIF PAUSE 100 ' Delay for slower PCs LOOP ' -----[ Subroutine - Read_Line_Sensors ]------------------------------------Read_Line_Sensors: HIGH qtiPwrLeft HIGH qtiSigLeft PAUSE 1 RCTIME qtiSigLeft, 1, qtiLeft LOW qtiPwrLeft ' Turn left QTI on ' Discharge capacitor HIGH qtiPwrRight HIGH qtiSigRight PAUSE 1 RCTIME qtiSigRight, 1, qtiRight ' Turn right QTI on ' Discharge capacitor ' Measure charge time ' Turn left QTI off ' Measure charge time
Chapter 3: EEPROM Tricks and Program Tips · Page 107 √ √ Save and then run the modified program, starting with both QTIs over a black portion of the practice ring. Repeat your tests with the tawara line, and verify that the SumoBot is making the correct navigation decisions. ACTIVITY #4: READING THE QTI SENSORS MORE QUICKLY How much time does it take to read QTIs? It depends a lot on the surface and the lighting conditions in the room.
Page 108· Applied Robotics with the SumoBot With some creative programming, you can actually check as many QTIs as you want, and it will only a fraction of the time that it takes to check just one QTI with the RCTIME command when it's detecting black. This activity will show you how. The Pulse-Decay Trick Figure 3-14 is a repeat of Figure 3-10, a schematic for what's happening inside the QTIs after Vdd has been applied to its on/off input, P10.
Chapter 3: EEPROM Tricks and Program Tips · Page 109 The code block below takes less time than an RCTIME command would over a black surface. It starts like it's going to take an RCTIME measurement, by setting P9 high and waiting 1 ms for the V0 to approach 5 V. Instead of taking an RCTIME measurement, the code bock changes P9 from output to input. That's the same thing the RCTIME command does internally, and it's what causes the voltage to start to decay.
Page 110· Applied Robotics with the SumoBot Figure 3-15 Pulse-Decay Trick Timing Accounting for Command Execution Times Figure 3-15 shows the ideal situation, ignoring the amount of time it takes for the BASIC Stamp 2 to transition from one command to the next. Subtracting 220 from the threshold variable will make your PULSOUT 6, threshold command delay for the correct amount of time.
Chapter 3: EEPROM Tricks and Program Tips · Page 111 Example Program: QtiPulseTrickLeft.bs2 The QtiPulseTrickLeft.bs2 program verifies that this technique works. Examine it carefully. Especially make sure you can match the commands that deal with P5 and P9 to the timing diagram in Figure 3-15. √ √ √ Make sure the left QTI is over the black surface in your sumo ring. Enter, save, and run QtiPulseTrickLeft.bs2.
Page 112· Applied Robotics with the SumoBot Your Turn - Incorporating the Right QTI Here's the interesting part, you can add another QTI measurement to the same block of code that performed the time measurement on the left QTI. The routine will take approximately the same amount of time, but you will be measuring two QTIs. √ Add a variable declaration for the state of the right QTI. qtiStateLeft √ VAR Bit Add this code block right after the code that sets the threshold based on P9 measurements.
Chapter 3: EEPROM Tricks and Program Tips · Page 113 Incorporating the Pulse-Decay Trick into Another Program Here is how to incorporate the test code from QtiPulseTrickLeft.bs2 into TestFrontQtiLineSensors.bs2 from Activity #2. The first step is to declare a dummy PIN name for P6. DummyPin PIN 6 Also declare a couple of extra bit variables to store the states of qtiSigLeft (P9) and qtiSigRight (P8).
Page 114· Applied Robotics with the SumoBot Read_Line_Sensors: HIGH qtiPwrLeft HIGH qtiPwrRight HIGH qtiSigLeft HIGH qtiSigRight PAUSE 1 ' Turn on QTIs INPUT qtiSigLeft INPUT qtiSigRight PULSOUT DummyPin, qtiThreshold ' Start the decays qtiStateLeft = qtiSigLeft qtiStateRight = qtiSigRight ' Snapshot of QTI signal states LOW qtiPwrLeft LOW qtiPwrRight ' Turn off QTIS ' Push signal voltages to 5 V ' Wait 1 ms for capacitors ' Wait for threshold time RETURN The last step is to go through the Main
Chapter 3: EEPROM Tricks and Program Tips · Page 115 ' -----[ I/O Definitions ]--------------------------------------------------qtiPwrLeft qtiSigLeft PIN PIN 10 9 ' Left QTI on/off pin ' Left QTI signal pin qtiPwrRight qtiSigRight PIN PIN 7 8 ' Right QTI on/off pin ' Right QTI signal pin DummyPin PIN 6 ' Unused I/O pin ' -----[ Variables ]---------------------------------------------------------qtiStateLeft qtiStateRight qtiLeft qtiRight qtiThreshold VAR VAR VAR VAR VAR Bit Bit Word Word Wor
Page 116· Applied Robotics with the SumoBot LOOP ' -----[ Subroutine - Calibrate_Qtis ]---------------------------------------Calibrate_Qtis: HIGH qtiPwrLeft HIGH qtiSigLeft PAUSE 1 RCTIME qtiSigLeft, 1, qtiLeft LOW qtiPwrLeft ' Turn left QTI on ' Discharge capacitor HIGH qtiPwrRight HIGH qtiSigRight PAUSE 1 RCTIME qtiSigRight, 1, qtiRight ' Turn right QTI on ' Discharge capacitor GOSUB Read_Line_Sensors qtiThreshold = (qtiLeft + qtiRight) / 2 qtiThreshold = qtiThreshold / 4 ' Get reflection values '
Chapter 3: EEPROM Tricks and Program Tips · Page 117 Your Turn - Active High vs. Active Low At present, the alert for the SumoBot encountering the white tawara line is when either qtiStateLeft or qtiStateRight store 0. You can use the invert bits operator (~) to change this so that the qtiState variables store 1 when they see they see white. √ Use the invert bits operator, the tilde ~, to invert the states of the qtiSig PIN names before storing them in the qtiState variables.
Page 118· Applied Robotics with the SumoBot Figure 3-16 Sensors and Indicators Built on the Breadboard IR Object Detector (Left Side) IR Object Detector (Right Side) Building and Testing the Side-Mounted IR Object Detector Circuits Figure 3-17 shows schematics of the side mounted IR LED and IR receiver pairs that comprise the left and right side object detectors. These detectors have the essentially the same components as the ones connected to the X8/X9 headers.
Chapter 3: EEPROM Tricks and Program Tips · Page 119 Figure 3-17 Side-mounted IR Object Detection Circuits Left Figure 3-18 Wiring Diagram Right
Page 120· Applied Robotics with the SumoBot You will see that TestSideIrObjectDetectors.bs2 is just a modified version of TestFrontIrObjectDetectors.bs2. Different PIN directives for the side mounted IR LEDs and receivers are declared, likewise for bit variables that store the IR receivers' output states. All of these different names are also updated in the FREQOUT commands, IF...THEN statements, and so on. Figure 3-19 shows the detection pattern for the side mounted IR object detectors.
Chapter 3: EEPROM Tricks and Program Tips · Page 121 Example Program: TestSideIrObjectDetectors.bs2 ' ' ' ' ' -----[ Title ]-------------------------------------------------------------Applied Robotics with the SumoBot - TestSideIrObjectDetectors.bs2 This program tests only IR object detectors on the SumoBot's breadboard that look to the left and right. It does not test the ones mounted in front. ' {$STAMP BS2} ' {$PBASIC 2.5} ' Target = BASIC Stamp 2 ' Language = PBASIC 2.
Page 122· Applied Robotics with the SumoBot Your Turn - Testing All Four Your SumoBot will eventually need to check and process information from all four IR object detectors each time through the Main Routine’s DO...LOOP. In this case, it's a matter of incorporating elements from the IR object detector programs from this activity and Activity #1 into a single program.
Chapter 3: EEPROM Tricks and Program Tips · Page 123 √ Start by opening all of the following programs: o o o o o √ √ √ TestLedSpeaker.bs2 TestPushButton.bs2 TestFrontIrObjectDetectors.bs2 QtiPulseDecayTrick.bs2 TestSideIrObjectDetectors.bs2 Save QtiPulseDecayTrick.bs2 as TestAllSensors.bs2 Copy and paste the I/O definitions and variables from the other programs into your new program.
Page 124· Applied Robotics with the SumoBot has to be changed back to an input first. So, the Main Routine of TestPushButton.bs2 will not serve our purpose, but this simple subroutine will. √ Add a Read_Pushbutton subroutine: Read_Pushbutton: INPUT pbSense pushbutton = pbSense ' Set I/O pin to input ' Store state of pbSense RETURN Some of the initialization routines will not be necessary. Others, like Calibrate_Qtis are crucial.
Chapter 3: EEPROM Tricks and Program Tips · Page 125 qtiPwrLeft qtiSigLeft PIN PIN 10 9 ' Left QTI on/off pin P10 ' Left QTI signal pin P9 qtiPwrRight qtiSigRight PIN PIN 7 8 ' Right QTI on/off pin P7 ' Right QTI signal pin P8 DummyPin PIN 6 ' I/O pin for pulse-decay P6 IrLedLF IrSenseLF PIN PIN 4 11 ' Left IR LED connected to P4 ' Left IR detector to P11 IrLedRF IrSenseRF PIN PIN 15 14 ' Right IR LED connected to P15 ' Right IR detector to P14 IrLedLS IrSenseLS PIN PIN 2 1 ' Left IR
Page 126· Applied Robotics with the SumoBot ' -----[ Main Routine ]------------------------------------------------------DO ' DO...
Chapter 3: EEPROM Tricks and Program Tips · Page 127 HIGH qtiPwrLeft HIGH qtiPwrRight HIGH qtiSigLeft HIGH qtiSigRight PAUSE 1 ' Turn on QTIs INPUT qtiSigLeft INPUT qtiSigRight PULSOUT DummyPin, qtiThreshold ' Start the decays qtiStateLeft = ~ qtiSigLeft qtiStateRight = ~ qtiSigRight ' Snapshot of QTI signal states LOW qtiPwrLeft LOW qtiPwrRight ' Turn off QTIS ' Push signal voltages to 5 V ' Wait 1 ms for capacitors ' Wait for threshold time RETURN ' -----[ Subroutine - Read_Object_Detectors ]--
Page 128· Applied Robotics with the SumoBot Your Turn - Cleaning up Names These variable declaration names have some room for improvement: irLS irLF irRF irRS VAR VAR VAR VAR Bit Bit Bit Bit ' ' ' ' State State State State of of of of Left Side IR Left Front IR Right Front IR Right Side IR qtiStateLeft qtiStateRight VAR VAR Bit Bit ' Stores snapshot of QtiSigLeft ' Stores snapshot of QtiSigRight The main problem is that they are not really consistent.
Chapter 3: EEPROM Tricks and Program Tips · Page 129 √ √ √ Repeat by using this feature to change qtiStateLeft to qtiLF. Some of the comments may now be out of alignment. Go through the program and insert spaces to line the side comments back up to column 46. Save your modified program; you will need it in the next couple activities.
Page 130· Applied Robotics with the SumoBot irLS irLF irRF irRS VAR VAR VAR VAR sensors.BIT3 sensors.BIT2 sensors.BIT1 sensors.BIT0 ' ' ' ' State State State State of of of of Left Side IR Right Side IR Right Front IR Right Side IR The best thing about this arrangement is that no other changes in the program have to be made. The program will still function normally, and you can display each bit value as before. √ √ √ Save TestAllSensorsYourTurn.bs2 as SensorsWithFlagsByte.bs2.
Chapter 3: EEPROM Tricks and Program Tips · Page 131 Figure 3-22 shows an example of how these modified routines display the contents of the sensors byte. But first let’s look at this line for a moment: DEBUG CRSRXY, 0, 4, BIN8 sensors DEBUG CRSRXY, 0, 4, places the Debug Terminal cursor at column 0, row 4 (the top row is row 0) right inside the display heading built by the Initialization DEBUG command.
Page 132· Applied Robotics with the SumoBot √ In order from BIT0 to BIT6, test the sensors in this order: infrared object detectors - right side, right front, left front, left side. QTI line sensors - right front, left front, pushbutton. ACTIVITY #8: VARIABLE MANAGEMENT FOR LARGE PROGRAMS When you have a project with a lot of sensors and programmed machine reactions, variable space can become pretty tight. For example, the program in the previous activity uses three words and one byte.
Chapter 3: EEPROM Tricks and Program Tips · Page 133 ' qtiThreshold temp multi VAR VAR VAR Word Word Word ' Stores black/white threshold ' <--- New temporary variable ' <--- New multipurpose variable As mentioned earlier, a variable doesn't have to store the QTI decay black/white threshold value because it doesn't change after it is set at the beginning of the program. This makes it a prime candidate for EEPROM storage.
Page 134· Applied Robotics with the SumoBot RCTIME qtiSigLeft, 1, temp ' <--- New measure charge time LOW qtiPwrLeft multi = temp ' Turn left QTI off ' <--- Add HIGH qtiPwrRight HIGH qtiSigRight PAUSE 1 ' RCTIME qtiSigRight, 1, qtiRight RCTIME qtiSigRight, 1, temp ' Turn right QTI on ' Discharge capacitor ' Measure charge time ' <--- New measure charge time ' qtiThreshold = (qtiLeft + qtiRight) / 2 ' Calculate average multi = (multi + temp) / 2 ' <--- New calculate average ' qtiThreshold = qtiThre
Chapter 3: EEPROM Tricks and Program Tips · Page 135 ' PULSOUT DummyPin, qtiThreshold PULSOUT DummyPin, temp ' Wait for threshold time ' <--- New wait threshold time qtiLF = ~qtiSigLeft qtiRF = ~qtiSigRight . . . ' Snapshot of QTI signal states Remember, the goal of all this work is to make the program more easily expandable. The other goal is to make it so that you can move parts of this program to other programs that follow the same conventions.
Page 136· Applied Robotics with the SumoBot IrLedRS IrSenseRS PIN PIN 3 0 ' Right IR LED connected to P3 ' Right IR detector to P0 pbSense PIN 6 ' Pushbutton connected to P6 LedSpeaker PIN 5 ' LED/speaker connected to P5 ' -----[ Constants ]---------------------------------------------------------IrFreq CON 38500 ' IR LED transmit frequency ' -----[ Variables ]---------------------------------------------------------' qtiLeft ' qtiRight ' qtiThreshold VAR VAR VAR Word Word Word ' Stores
Chapter 3: EEPROM Tricks and Program Tips · Page 137 DO ' DO...
Page 138· Applied Robotics with the SumoBot WRITE QtiThresh, Word multi ' <--- New threshold to EEPROM RETURN ' -----[ Subroutine - Read_Line_Sensors ]------------------------------------Read_Line_Sensors: ' HIGH qtiPwrLeft HIGH qtiPwrRight HIGH qtiSigLeft HIGH qtiSigRight PAUSE 1 ' Turn on QTIs READ QtiThresh, Word temp ' <-- Add get threshold time INPUT qtiSigLeft INPUT qtiSigRight ' Start the decays PULSOUT DummyPin, qtiThreshold PULSOUT DummyPin, temp ' Wait for threshold time ' <--- New wai
Chapter 3: EEPROM Tricks and Program Tips · Page 139 INPUT pbSense pushbutton = pbSense ' Store state of pbSense RETURN Your Turn - Program Cleanup √ √ √ Review the commented lines of code and their replacements. Remove the commented lines of code. Run the program and verify that it still works properly.
Page 140· Applied Robotics with the SumoBot SUMMARY This chapter reviewed how the SumoBot's IR detectors work, then introduced basic IR detector testing along with IR interference, electrical continuity, and IR receiver frequency response tests. Using these tests will help ensure that the SumoBot's IR detectors are reliable, and more likely to detect an opponent than lead the SumoBot on a snipe hunt. Side-mounted IR object detectors were added to the breadboard to give the SumoBot peripheral vision.
Chapter 3: EEPROM Tricks and Program Tips · Page 141 11. What factors should you keep in mind when selecting a frequency for a given SumoBot ring? 12. What label designates the header with the front-left QTI sensor? 13. What connections does the SumoBot board make to the header with the front-left QTI Sensor that you don't have to build on a breadboard? 14. How does the BASIC Stamp turn the power to a QTI on and off? 15. How many components are on the QTI sensor? 16.
Page 142· Applied Robotics with the SumoBot Projects 1. Write a program that beeps at different notes to tell you which sensor it detects. 2. Place an object on the SumoBot's left at a distance outside the SumoBot Competition Ring that the SumoBot can detect with only its most sensitive IR object detection frequencies. Place the other SumoBot on the right side.
Chapter 4: Navigation Tips · Page 143 Chapter #4: Navigation Tips Effectively using four object detectors and two QTI line sensors for SumoBot navigation can seem a little daunting at first, especially when you consider that there are 64 different possible combinations of detected and not detected that you can get from this array. It turns out that you can reduce all these possibilities to a very simple IF...ELSEIF...ELSE...ENDIF statement.
Page 144· Applied Robotics with the SumoBot ACTIVITY #1: SERVO CONTROL WITH LOOKUP COMMANDS Since all the other subroutines in this book have utilized some combination of the counter and temp variables, why not make it so for servo control subroutines? This activity demonstrates a way to do it with the LOOKUP command. Servo Control Review Figure 4-1 is a repeat of Figure 1-4 from Chapter 1, Activity #1. A quick review: The left servo is connected to header X7 on the SumoBot board.
Chapter 4: Navigation Tips · Page 145 Figure 4-2 The Servo Control Loop The FOR...NEXT loop's EndValue is the number of 1/41 second increments the servo will run. Every time through the loop takes about 24.6 ms, so 41 times through the 24.6 ms loop takes about 41 × 0.0246 ≈ 1 second. Increasing the FOR...NEXT loop's EndValue argument to 82 would make the servos run for twice the time, about 2 seconds. Half a second of servo rotation would be an EndValue of 20, and so on.
Page 146· Applied Robotics with the SumoBot With some experimentation, you can also control the speed of the servos too. Note from Figure 4-3 that the relationship between pulse width and rotational velocity is not simply linear. For example, a Duration of 700 is still pretty close to full speed clockwise, but as you increase to 710, 720, 730, and so on, there should be a noticeable reduction in speed. The closer Duration is to 750, the slower the servo will turn.
Chapter 4: Navigation Tips · Page 147 ' Applied Robotics with the SumoBot - ServoControlExample.bs2 ' {$STAMP BS2} ' {$PBASIC 2.5} counter VAR byte ' SumoBot goes forward 3 seconds. FOR counter = 1 TO 122 PULSOUT 13, 850 PULSOUT 12, 650 PAUSE 20 NEXT ' SumoBot goes backward 3 seconds. FOR counter = 1 TO 122 PULSOUT 13, 650 PULSOUT 12, 850 PAUSE 20 NEXT ' SumoBot rotates left .75 seconds. FOR counter = 1 TO 30 PULSOUT 13, 650 PULSOUT 12, 650 PAUSE 20 NEXT ' SumoBot rotates right .75 seconds.
Page 148· Applied Robotics with the SumoBot Your Turn - Using PIN Definitions and Constants Instead of the number 13 in the PULSOUT command's Pin argument, the example program can use the PIN directive ServoLeft. Likewise, the program can use ServoRight in place of the number 12. Since we know that 650 is the PULSOUT Duration for fullspeed-clockwise, a good constant name for this number would be FS_CW.
Chapter 4: Navigation Tips · Page 149 Let's take a second look at that LOOKUP command from Chapter 2, Activity #4. If counter is 0, the LOOKUP command will copy 1046 to the note variable. If counter is 1, LOOKUP will copy 1175 to the note variable. If counter is 2, 1319 will be copied to note, and so on. LOOKUP counter, [1046, 1175, 1319, 1397, 1580, 1760, 1976, 2093], note Example Program: LookupExample.bs2 √ √ Enter, save, and run LookupExample.bs2.
Page 150· Applied Robotics with the SumoBot √ √ Save the example program as LookupExampleYourTurn.bs2 Add these constant declarations with the CON directive: C_6 D_6 E_6 F_6 G_6 A_6 B_6 C_7 √ CON CON CON CON CON CON CON CON 1046 1175 1319 1397 1580 1760 1976 2093 Change the lookup table to this: LOOKUP counter, [C_6, D_6, E_6, F_6, G_6, A_6, B_6, C_7], note √ Run the program, and verify that it works the same as before.
Chapter 4: Navigation Tips · Page 151 The servo control subroutine will send pulse values to the servos. Constants for full speed counterclockwise (FS_CCW), full speed clockwise (FS_CW), and no rotation (NO_ROT) will make the subroutine easier to write. FS_CCW FS_CW NO_ROT CON CON CON 850 650 750 ' Full speed counterclockwise ' Full speed clockwise ' No rotation Here is the Pulse_Servos subroutine. Remember that Forward is a constant, set to 0 by a constant declaration (Forward CON 0).
Page 152· Applied Robotics with the SumoBot Example Program: ServoControlWithLookup.bs2 √ √ √ √ √ Enter and save ServoControlWithLookup.bs2 Move the 3-position switch on the SumoBot circuit board to position 1. Download the program to the SumoBot. Hold down the Reset button on the SumoBot circuit board, then move the 3position switch from 1 to 2. Place the SumoBot on the practice ring, let go of the Reset button, and watch it navigate.
Chapter 4: Navigation Tips · Page 153 NO_ROT CON 750 ' No rotation ' -----[ Variables ]---------------------------------------------------------temp counter VAR VAR Word Byte ' Temporary variable ' Loop counting variable.
Page 154· Applied Robotics with the SumoBot Your Turn Here is a wider variety of maneuver constants along with extra servo pulse width rotation constants you will need to implement all 10 maneuvers.
Chapter 4: Navigation Tips · Page 155 √ Modify the Pulse_Servos subroutine to accommodate the rest of the maneuvers in the SumoBot maneuver constants list. √ For the CurveLeft and CurveRight maneuvers, tune them so that the SumoBot can do a full circle in the ring without passing over the white tawara line (see Figure 4-4). Figure 4-4 Circling inside the Sumo Ring Spend some time refining CurveLeft and CurveRight. These two maneuvers are strategically important to your SumoBot.
Page 156· Applied Robotics with the SumoBot motion and the opponent collides with it at that moment, it will be at a disadvantage. If the SumoBots are already pushing each other and one of them does this maneuver, it will also give its opponent an opportunity to push it closer to the white tawara line. Figure 4-5 Nearby Opponent Seen by only one IR object detector Other approaches include pivoting on one wheel and curving while continuing forward.
Chapter 4: Navigation Tips · Page 157 Figure 4-6 Nearby Opponent Seen by one IR object detector, but not for long if curving is the only method employed. You can also program the SumoBot for more complex maneuvers, such as curving for a while, then rotating in place. With a limited amount of curve time, it gives the SumoBot a chance to get lined up with its opponent if it is close to making contact or has already done so.
Page 158· Applied Robotics with the SumoBot way to ensure this is to keep a subroutine that's in charge of both sending pulses to the servos and checking sensors. In the code below, every time the Main Routine calls Servos_And_Sensors, both the Pulse_Servos subroutine and the sensors subroutines (Read_Object_Detectors in this case) get checked.
Chapter 4: Navigation Tips · Page 159 You can test this next example program by positioning the SumoBot so that neither of its IR object detectors see an object. Then place your hand in front of one of the detectors. The SumoBot should rotate toward your hand until both detectors see it. Then it should lunge forward at your hand. If you remove your hand, the SumoBot will stop and wait. That's because the ELSE condition in the IF...THEN...ELSIF...ELSE statement. All it does is checks the IR detectors.
Page 160· Applied Robotics with the SumoBot ' ' ' ' -----[ Title ]-------------------------------------------------------------Applied Robotics with the SumoBot - FrontIrNavigation.bs2 When it sees an object with only one object detecting eye, it corrects it's heading until it sees the object with both eyes. ' {$STAMP BS2} ' {$PBASIC 2.5} ' Target = BASIC Stamp 2 ' Language = PBASIC 2.
Chapter 4: Navigation Tips · Page 161 temp counter VAR VAR Word Byte ' Temporary variable ' Loop counting variable. maneuver VAR Nib ' SumoBot travel maneuver sensors VAR Byte ' Sensor flags byte irLS irLF irRF irRS VAR VAR VAR VAR sensors.BIT3 sensors.BIT2 sensors.BIT1 sensors.
Page 162· Applied Robotics with the SumoBot RETURN ' -----[ Subroutine - Pulse_Servos ]-----------------------------------------Pulse_Servos: ' Pulse to left servo LOOKUP maneuver, [ FS_CCW, FS_CW, FS_CW, FS_CCW, NO_ROT, FS_CCW, LS_CCW, FS_CCW ], temp PULSOUT ServoLeft, temp ' Pulse to right servo LOOKUP maneuver, [ FS_CW, FS_CCW, FS_CW, FS_CCW, FS_CW, NO_ROT, FS_CW, LS_CW ], temp PULSOUT ServoRight, temp ' Pause between pulses (remove when using IR object detectors + QTIs).
Chapter 4: Navigation Tips · Page 163 ELSEIF irLF = 1 THEN ' Just left? DO UNTIL (irLF = 1 AND irRF = 1) OR counter > 15 maneuver = CurveLeft ' Curve left 15 GOSUB Servos_And_Sensors counter = counter + 1 LOOP DO UNTIL (irLF = 1 AND irRF = 1) OR counter > 30 maneuver = RotateLeft ' Rotate left 30 GOSUB Servos_And_Sensors counter = counter + 1 LOOP The Main Routine below incorporates this code for both sides, and is a drop-in replacement for the main routine in FrontIrNavigation.bs2.
Page 164· Applied Robotics with the SumoBot GOSUB Servos_And_Sensors counter = counter + 1 LOOP ELSE GOSUB Read_Object_Detectors ENDIF ' No objects detected? ' State = search pattern LOOP ACTIVITY #3: USING PERIPHERAL VISION When autonomous robots start a sumo match facing each other, the match tends to be more a battle of brawn than brains. When a match starts with the SumoBot robots sideways to each other, things get a little more interesting.
Chapter 4: Navigation Tips · Page 165 Figure 4-8 Peripheral Vision for Opponent Detection Responding to an Object Detection on the Side This code for side IR object detection works about the same as the examples from the previous activity. They can be added to the IF...THEN statements in the previous example program's main routine. They will make the SumoBot respond by rotating in place to turn toward objects it detects on either side.
Page 166· Applied Robotics with the SumoBot Example Program: FrontAndSideIrNavigation.bs2 The beauty in terminating the rotate loop as soon as one of the front IR detectors sees the object is that one of the other code blocks will then take over to get the SumoBot to face its opponent. Let's see how it performs: √ √ √ √ √ √ √ √ √ √ Save FrontIrNavigiation.bs2 as FrontAndSideIrNavigation.bs2 Insert the peripheral vision ELSEIF...THEN...
Chapter 4: Navigation Tips · Page 167 IrLedLF IrSenseLF PIN PIN 4 11 ' Left IR LED connected to P4 ' Left IR detector to P11 IrLedRF IrSenseRF PIN PIN 15 14 ' Right IR LED connected to P15 ' Right IR detector to P14 IrLedRS IrSenseRS PIN PIN 3 0 ' Right IR LED connected to P3 ' Right IR detector to P0 ' -----[ Constants ]---------------------------------------------------------' SumoBot maneuvers Forward Backward RotateLeft RotateRight PivotLeft PivotRight CurveLeft CurveRight CON CON CON CON
Page 168· Applied Robotics with the SumoBot IF irLF = 1 AND irRF = 1 THEN maneuver = Forward GOSUB Servos_And_Sensors ELSEIF irLF = 1 THEN counter = 0 DO UNTIL (irLF = 1 AND irRF maneuver = CurveLeft GOSUB Servos_And_Sensors counter = counter + 1 LOOP DO UNTIL (irLF = 1 AND irRF maneuver = RotateLeft GOSUB Servos_And_Sensors counter = counter + 1 LOOP ELSEIF irRF = 1 THEN counter = 0 DO UNTIL (irLF = 1 AND irRF maneuver = CurveRight GOSUB Servos_And_Sensors counter = counter + 1 LOOP DO UNTIL (irLF = 1 AND
Chapter 4: Navigation Tips · Page 169 sensors = 0 ' Clear previous sensor values GOSUB Read_Object_Detectors ' Call Read_Object_Detectors RETURN ' -----[ Subroutine - Pulse_Servos ]-----------------------------------------Pulse_Servos: ' Pulse to left servo LOOKUP maneuver, [ FS_CCW, FS_CW, FS_CW, FS_CCW, NO_ROT, FS_CCW, LS_CCW, FS_CCW ], temp PULSOUT ServoLeft, temp ' Pulse to right servo LOOKUP maneuver, [ FS_CW, FS_CCW, FS_CW, FS_CCW, FS_CW, NO_ROT, FS_CW, LS_CW ], temp PULSOUT ServoRight, temp ' Pa
Page 170· Applied Robotics with the SumoBot Your Turn - Limiting Rotation Unlike the front IR detectors, the loops for the side IR detectors do not limit the amount of rotation in place the SumoBot will perform. √ Add conditions to the DO UNTIL...LOOP code blocks that allow your SumoBot to turn and face objects detected to the side, but that prevent the SumoBot from rotating more than 180°. Is it worth sweeping back in case the SumoBot might have missed something on its first rotation? Maybe.
Chapter 4: Navigation Tips · Page 171 state machines". However, the term "state machine" is commonly used to refer to finite state machines. Your SumoBot is without a doubt, a state machine. The last couple of example programs have used terms like "State = search pattern" and "State = track front left object" in the comments. The IR object detectors sense conditions, and the SumoBot's embedded BASIC Stamp executes a program that interprets each new condition and makes the transition to the correct state.
Page 172· Applied Robotics with the SumoBot that state while pbSense = 1, and the other that transitions to LED off when pbSesnse = 0. State diagrams can be visual aids for describing the different states in certain programs along with the conditions the programs use to transition from one state to the next. For example, PushbuttonLed.bs2 can be thought of as the instructions to make the SumoBot a state machine implementation of Figure 4-9. Example Program: PushbuttonLed.
Chapter 4: Navigation Tips · Page 173 Your Turn Figure 4-10 shows a modified version of the state machine diagram that involves a variable named counter. Because of the 100 ms pause between repeats of the DO...LOOP in PushubttonLed.bs2, the counter variable keeps the LED blink-time less than or equal to 2 seconds (assuming the counter starts at 1). It also ensures a 1 second delay before the LED starts blinking again, even if you keep pressing the button.
Page 174· Applied Robotics with the SumoBot LOOP ELSEIF irRF = 1 THEN ' Just right? counter = 0 ' State=track front right object DO UNTIL (irLF = AND irRF = 1) OR counter > 15 maneuver = PivotRight ' Pivot right 15 GOSUB Servos_And_Sensors counter = counter + 1 LOOP ELSE ' No objects detected? GOSUB Read_Object_Detectors ' State = search pattern ENDIF LOOP Software that is used for designing and modeling state machines that go into automated machinery and/or integrated circuits have various ways of both s
Chapter 4: Navigation Tips · Page 175 Figure 4-11 Hybrid State Machine Diagram √ Compare Figure 4-11 to the Main Routine from FrontIrNavigation.bs2 and verify that they describe the same state transitions. State diagrams are not a replacement for flowcharts. While state diagrams introduced in this activity provide an overview of states in a state machine design, flow charts provide a detailed visual description of what the code in a program does.
Page 176· Applied Robotics with the SumoBot Your Turn - Drawing a State Machine Diagram √ Try drawing a hybrid state machine diagram for the Main Routine in FrontAndSideIrNavigation.bs2 from this chapter's Activity #3. ACTIVITY #5: SEARCH PATTERN AND TAWARA AVOIDANCE Before your SumoBot will be ready for a match, it's got to be able to stay inside that white tawara line.
Chapter 4: Navigation Tips · Page 177 ' -----[ Main Routine ]------------------------------------------------------DO IF qtiLF = 1 THEN FOR counter = 1 TO 15 maneuver = Backward GOSUB Servos_And_Sensors NEXT FOR counter = 1 TO 15 maneuver = RotateRight GOSUB Servos_And_Sensors NEXT ELSEIF qtiRF = 1 THEN FOR counter = 1 TO 15 maneuver = Backward GOSUB Servos_And_Sensors NEXT FOR counter = 1 TO 15 maneuver = RotateLeft GOSUB Servos_And_Sensors NEXT . . .
Page 178· Applied Robotics with the SumoBot FOR counter = 1 TO 12 maneuver = RotateRight GOSUB Servos_And_Sensors IF sensors <> 0 THEN GOTO Next_State NEXT FOR counter = 1 TO 24 maneuver = RotateLeft GOSUB Servos_And_Sensors IF sensors <> 0 THEN GOTO Next_State NEXT FOR counter = 1 TO 12 maneuver = RotateRight GOSUB Servos_And_Sensors IF sensors <> 0 THEN GOTO Next_State NEXT Next_State: ENDIF ' Look right ' Look left ' Re-align to forward ' Exit point of search pattern LOOP Figure 4-12 shows the sea
Chapter 4: Navigation Tips · Page 179 √ √ Press and hold the Reset button until you place it on the ring as shown in Figure 4-12. Let go of the Reset button, and compare your search pattern to the one shown in the figure. Does your SumoBot mistake creases in the poster for white tawara lines? If your SumoBot backs up and then executes a turn before it gets to the white tawara line, it may be detecting one or more of the creases in the SumoBot Competition Ring poster.
Page 180· Applied Robotics with the SumoBot DummyPin PIN 6 ' I/O pin for pulse-decay P6 ' -----[ Constants ]---------------------------------------------------------' SumoBot maneuvers Forward Backward RotateLeft RotateRight PivotLeft PivotRight CurveLeft CurveRight CON CON CON CON CON CON CON CON 0 1 2 3 4 5 6 7 ' ' ' ' ' ' ' ' Forward Backward RotateLeft RotateRight Pivot to the Pivot to the Curve to the Curve to the ' ' ' ' ' Full speed counterclockwise Full speed clockwise No rotation Low spe
Chapter 4: Navigation Tips · Page 181 maneuver = Backward GOSUB Servos_And_Sensors NEXT FOR counter = 1 TO 15 maneuver = RotateRight GOSUB Servos_And_Sensors NEXT ELSEIF qtiRF = 1 THEN FOR counter = 1 TO 15 maneuver = Backward GOSUB Servos_And_Sensors NEXT FOR counter = 1 TO 15 maneuver = RotateLeft GOSUB Servos_And_Sensors NEXT ELSE FOR counter = 1 TO 35 maneuver = Forward GOSUB Servos_And_Sensors IF sensors <> 0 THEN GOTO NEXT FOR counter = 1 TO 12 maneuver = RotateRight GOSUB Servos_And_Sensors IF senso
Page 182· Applied Robotics with the SumoBot LOW qtiPwrLeft multi = temp ' Turn left QTI off ' Free temp for another RCTIME HIGH qtiPwrRight HIGH qtiSigRight PAUSE 1 RCTIME qtiSigRight, 1, temp ' Turn right QTI on ' Discharge capacitor multi = (multi + temp) / 2 ' Calculate average multi = multi / 4 ' Take 1/4 average IF multi > 220 THEN multi = multi - 220 ELSE multi = 0 ENDIF ' Account for code overhead WRITE QtiThresh, Word multi ' Threshold to EEPROM ' Measure charge time RETURN ' -----[ Su
Chapter 4: Navigation Tips · Page 183 ' Pause between pulses (remove when using IR object detectors + QTIs).
Page 184· Applied Robotics with the SumoBot Figure 4-13 Sample SumoBot Search Pattern This one is different because the SumoBot looks around before it starts going forward. There are a couple of different ways to get the SumoBot to do this maneuver. The first is to write a custom Initialization routine that performs this maneuver before moving on to the Main Routine. Here's another way. √ √ Save SearchPatternAndAvoidTawara.bs2 as a new file with YourTurn.
Chapter 4: Navigation Tips · Page 185 √ Add the GOTO Look_About command to the end of the Initialization routine. ' -----[ Initialization ]-----------------------------------------GOSUB Calibrate_Qtis GOTO Look_About √ ' Determine b/w threshold ' <--- Add Start mid search pattern Run the modified program and verify that it makes the SumoBot look around before moving forward. There are lots of questions to consider and test when designing a search pattern.
Page 186· Applied Robotics with the SumoBot ACTIVITY #6: FULLY FUNCTIONAL SUMO EXAMPLE PROGRAMS This activity features two fully functional SumoBot example programs. While they both do the same things; they are organized a little differently. The first is simply a combination of the example programs, mainly from this chapter. The second has the code for each state moved to subroutines.
Chapter 4: Navigation Tips · Page 187 ' ' ' ' -----[ Title ]-------------------------------------------------------------Applied Robotics with the SumoBot - TestSumoWrestler.bs2 Fully functional state machine based sumo wrestling program with peripheral vision. ' {$STAMP BS2} ' {$PBASIC 2.5} ' Target = BASIC Stamp 2 ' Language = PBASIC 2.
Page 188· Applied Robotics with the SumoBot FS_CW NO_ROT LS_CCW LS_CW CON CON CON CON 650 750 770 730 ' ' ' ' Full speed clockwise No rotation Low speed counterclockwise Low speed clockwise 38500 ' IR LED frequency ' IR object detectors IrFreq CON ' -----[ Variables ]---------------------------------------------------------temp multi counter VAR VAR VAR Word Word Byte ' Temporary variable ' Multipurpose variable ' Loop counting variable.
Chapter 4: Navigation Tips · Page 189 NEXT ELSEIF qtiRF = 1 THEN FOR counter = 1 TO 15 maneuver = Backward GOSUB Servos_And_Sensors NEXT FOR counter = 1 TO 15 maneuver = RotateLeft GOSUB Servos_And_Sensors NEXT ELSEIF irLF = 1 AND irRF = 1 THEN maneuver = Forward GOSUB Servos_And_Sensors ELSEIF irLF = 1 THEN counter = 0 DO UNTIL (irLF = 1 AND irRF = 1) maneuver = CurveLeft GOSUB Servos_And_Sensors counter = counter + 1 LOOP DO UNTIL (irLF = 1 AND irRF = 1) maneuver = RotateLeft GOSUB Servos_And_Sensors cou
Page 190· Applied Robotics with the SumoBot IF sensors <> 0 THEN GOTO NEXT Look_About: FOR counter = 1 TO 12 maneuver = RotateRight GOSUB Servos_And_Sensors IF sensors <> 0 THEN GOTO NEXT FOR counter = 1 TO 24 maneuver = RotateLeft GOSUB Servos_And_Sensors IF sensors <> 0 THEN GOTO NEXT FOR counter = 1 TO 12 maneuver = RotateRight GOSUB Servos_And_Sensors IF sensors <> 0 THEN GOTO NEXT Next_State: ENDIF Next_State ' Starting point label ' Look right Next_State ' Look left Next_State ' Re-align to forwar
Chapter 4: Navigation Tips · Page 191 ' -----[ Subroutine - Calibrate_Qtis ]---------------------------------------Calibrate_Qtis: HIGH qtiPwrLeft HIGH qtiSigLeft PAUSE 1 ' Turn left QTI on ' Discharge capacitor RCTIME qtiSigLeft, 1, temp ' Measure charge time LOW qtiPwrLeft multi = temp ' Turn left QTI off ' Free temp for another RCTIME HIGH qtiPwrRight HIGH qtiSigRight PAUSE 1 RCTIME qtiSigRight, 1, temp ' Turn right QTI on ' Discharge capacitor multi = (multi + temp) / 2 ' Calculate average mu
Page 192· Applied Robotics with the SumoBot ' Pulse to left servo LOOKUP maneuver, [ FS_CCW, FS_CW, FS_CW, FS_CCW, NO_ROT, FS_CCW, LS_CCW, FS_CCW ], temp PULSOUT ServoLeft, temp ' Pulse to right servo LOOKUP maneuver, [ FS_CW, FS_CCW, FS_CW, FS_CCW, FS_CW, NO_ROT, FS_CW, LS_CW ], temp PULSOUT ServoRight, temp ' Pause between pulses (remove when using IR object detectors + QTIs).
Chapter 4: Navigation Tips · Page 193 qtiLF = ~qtiSigLeft qtiRF = ~qtiSigRight ' Snapshot of QTI signal states LOW qtiPwrLeft LOW qtiPwrRight ' Turn off QTIS RETURN Moving the State Routines to Subroutines Here is the Main Routine from the upcoming example program SumoWrestler.bs2, a revision of TestSumoWrestler.bs2 in which all of the state routines have been moved to subroutines.
Page 194· Applied Robotics with the SumoBot maneuver = Backward GOSUB Servos_And_Sensors NEXT FOR counter = 1 TO 15 maneuver = RotateRight GOSUB Servos_And_Sensors NEXT ' Turn right RETURN There was one small problem that had to be corrected before the program would run properly.
Chapter 4: Navigation Tips · Page 195 √ √ √ √ √ √ ' ' ' ' closely matches the conditions for your competition ring, taking into account nearby objects, whether or not the floor reflects infrared, etc. Also, make sure to adjust your QTI threshold calculation if you encountered problems with detecting the crease marks in the SumoBot Robot Competition Ring poster. See Activity #5 in this chapter. Download your updated SumoWrestler.bs2 into SumoBot A. Save a copy of the program as MySumoWithSubrtoutines.bs2.
Page 196· Applied Robotics with the SumoBot IrSenseRS PIN 0 ' Right IR detector to P0 ' -----[ Constants ]---------------------------------------------------------' SumoBot maneuvers Forward Backward RotateLeft RotateRight PivotLeft PivotRight CurveLeft CurveRight CON CON CON CON CON CON CON CON 0 1 2 3 4 5 6 7 ' ' ' ' ' ' ' ' Forward Backward RotateLeft RotateRight Pivot to the Pivot to the Curve to the Curve to the 850 650 750 770 730 ' ' ' ' ' Full speed counterclockwise Full speed clockwise
Chapter 4: Navigation Tips · Page 197 GOSUB GOSUB GOSUB GOSUB Reset Start_Delay Calibrate_Qtis Look_About ' ' ' ' Wait for Reset press/release 5 Second delay Determine b/w threshold Was Goto Look_About ' -----[ Main Routine ]------------------------------------------------------DO IF qtiLF = 1 THEN GOSUB Avoid_Tawara_Left ELSEIF qtiRF = 1 THEN GOSUB Avoid_Tawara_Right ELSEIF irLF = 1 AND irRF = 1 THEN GOSUB Go_Forward ELSEIF irLF = 1 THEN GOSUB Track_Front_Left_Object ELSEIF irRF = 1 THEN GOSUB Track_F
Page 198· Applied Robotics with the SumoBot FOR counter = 1 TO 5 PAUSE 900 FREQOUT LedSpeaker, 100, 3000 NEXT ' 5 beeps, 1/second RETURN ' -----[ Subroutine - Calibrate_Qtis ]---------------------------------------Calibrate_Qtis: HIGH qtiPwrLeft HIGH qtiSigLeft PAUSE 1 ' Turn left QTI on ' Discharge capacitor RCTIME qtiSigLeft, 1, temp ' Measure charge time LOW qtiPwrLeft multi = temp ' Turn left QTI off ' Free temp for another RCTIME HIGH qtiPwrRight HIGH qtiSigRight PAUSE 1 RCTIME qtiSigRight, 1,
Chapter 4: Navigation Tips · Page 199 GOSUB Read_Line_Sensors ' Look for lines RETURN ' -----[ Subroutine - Pulse_Servos ]-----------------------------------------Pulse_Servos: ' Pulse to left servo LOOKUP maneuver, [ FS_CCW, FS_CW, FS_CW, FS_CCW, NO_ROT, FS_CCW, LS_CCW, FS_CCW ], temp PULSOUT ServoLeft, temp ' Pulse to right servo LOOKUP maneuver, [ FS_CW, FS_CCW, FS_CW, FS_CCW, FS_CW, NO_ROT, FS_CW, LS_CW ], temp PULSOUT ServoRight, temp ' Pause between pulses (remove when using IR object detectors + Q
Page 200· Applied Robotics with the SumoBot READ QtiThresh, Word temp ' Get threshold time INPUT qtiSigLeft INPUT qtiSigRight ' Start the decays PULSOUT DummyPin, temp ' Wait threshold time qtiLF = ~qtiSigLeft qtiRF = ~qtiSigRight ' Snapshot of QTI signal states LOW qtiPwrLeft LOW qtiPwrRight ' Turn off QTIS RETURN ' -----[ Subroutine - Avoid_Tawara_Left ]------------------------------------Avoid_Tawara_Left: FOR counter = 1 TO 15 maneuver = Backward GOSUB Servos_And_Sensors NEXT FOR counter = 1
Chapter 4: Navigation Tips · Page 201 RETURN ' -----[ Subroutine - Track_Front_Left_Object ]------------------------------Track_Front_Left_Object: counter = 0 DO UNTIL (irLF = 1 AND irRF = 1) OR counter > 15 maneuver = CurveLeft ' Curve left 15 GOSUB Servos_And_Sensors counter = counter + 1 LOOP DO UNTIL (irLF = 1 AND irRF = 1) OR counter > 30 maneuver = RotateLeft ' Rotate left 30 GOSUB Servos_And_Sensors counter = counter + 1 LOOP RETURN ' -----[ Subroutine - Track_Front_Right_Object ]-------------------
Page 202· Applied Robotics with the SumoBot Track_Side_Right_Object: DO UNTIL irRF = 1 OR irLF = 1 maneuver = RotateRight GOSUB Servos_And_Sensors LOOP ' Rotate right RETURN ' -----[ Subroutine - Search_Pattern ]---------------------------------------Search_Pattern: FOR counter = 1 TO 35 maneuver = Forward GOSUB Servos_And_Sensors IF sensors <> 0 THEN GOTO NEXT Look_About: FOR counter = 1 TO 12 maneuver = RotateRight GOSUB Servos_And_Sensors IF sensors <> 0 THEN GOTO NEXT FOR counter = 1 TO 24 maneuver =
Chapter 4: Navigation Tips · Page 203 SUMMARY This chapter introduced a technique that uses the LOOKUP command for adapting servo control to use with the temp and counter variables. It also introduced a subroutine that makes checking the sensors between each servo pulse automatic. DO UNTIL...LOOP code blocks were used to respond to a sensor with an action until another sensor event occurred or a counter exceeded a certain number of repetitions.
Page 204· Applied Robotics with the SumoBot 9. What combination of PULSOUT commands does it take to make the SumoBot execute a pivot-right turn? 10. In the lookup command review, what value will the counter variable have to store to make the LOOKUP command copy 1580 to the note variable? 11. What constant values were chosen for Forward, Backward, RotateLeft, and RotateRight? 12. What variable has to be set to a value before the Pulse_Servos subroutine is called? Pulse_Servos 13.
Chapter 4: Navigation Tips · Page 205 Projects 1. Hold some sumo matches, and make sure to draw cards from the list in Figure 47 between each round. 2. Modify the two-eyed Basic_Competition_Program from the SumoBot Manual text to improve its chances against the modified four-eyed SumoBot when starting from any of the positions in Figure 4-7.
Page 206· Applied Robotics with the SumoBot
Chapter 5: Debugging and Datalogging · Page 207 Chapter #5: Debugging and Datalogging If the SumoBot exhibits a problem behavior in the ring, figuring out the problem's actual cause can be rather confounding. Things happen pretty fast in the sumo ring, and it's often hard to discern what the SumoBot might have "seen" when you're watching a round. Especially if you extensively modify SumoWrestler.
Page 208· Applied Robotics with the SumoBot Slow motion debugging leaves out one crucial element: the actual conditions the SumoBot experiences during a round. This is where datalogging comes in. The SumoBot can be programmed to save all the Debug Terminal information it displayed to EEPROM. After the round, you can play it all back, and match what the SumoBot saw and did to its problem behavior. The Scientific Method The five steps in the scientific method are: 1. State the problem 2. Make observations 3.
Chapter 5: Debugging and Datalogging · Page 209 Before testing each of these questions, it's important to make a disposable copy of the SumoWrestler.bs2. √ Save SumoWrestler.bs2 as CompCodeLedTest.bs2 Testing for a Cleared Bit The first question was "...did the irRf bit get accidentally cleared somewhere in the program?" Some LED indicator code can certainly answer this question: IF irRS = 1 THEN HIGH LedSpeaker ELSE LOW LedSpeaker Where you put this line of code can make a big difference in your test.
Page 210· Applied Robotics with the SumoBot √ √ √ Try it in the practice ring with the 3-position power switch set to 2. Start your test SumoBot with its opponent on the right and watch the LED to see if you can discern when it detects its opponent. After you are done with this test, make sure to either comment the IF...THEN statement by placing an apostrophe to the left of it, or just delete it.
Chapter 5: Debugging and Datalogging · Page 211 GOSUB Servos_And_Sensors LOOP LOW LedSpeaker ' <--- Add RETURN √ √ √ √ √ √ √ √ Run the modified program with the SumoBot's 3-position power switch set to 1. Make sure no objects are in view of the SumoBot's other three object detectors. Wave your hand in front of the right object detector. Verify that the LED blinks and the speaker clicks rapidly until you wave your hand in front of one of the front IR detectors.
Page 212· Applied Robotics with the SumoBot √ Modify the main routine so that this bug is incorporated. ELSEIF irLS = 1 THEN GOSUB Track_Side_Left_Object ELSEIF irRS = 1 THEN GOSUB Track_Side_Left_Object ' ' ' ' Left side? State = track side left obj. Right side? State = track side right obj. With this particular bug, the LED would not turn on in the previous test indicating that the code just wasn't making it there.
Chapter 5: Debugging and Datalogging · Page 213 itself that you have declared two symbols, LED_MODE, which has been set equal to one, and DEBUG_MODE, which has been set equal to two. ' -----[ Compiler Definitions ]---------------------------------------#DEFINE LED_MODE = 1 #DEFINE DEBUG_MODE = 2 Most compiler directives begin with #. Examples include #DEFINE, #IF, #THEN, #ELSE, #SELECT, and #CASE. The others begin with the dollar sign, such as $STAMP and $PBASIC. The #IF...
Page 214· Applied Robotics with the SumoBot #SELECT DEBUG_MODE #CASE 0 DEBUG "DEBUG_MODE is zero." #CASE 1 DEBUG "DEBUG_MODE is one." #CASE 2 DEBUG "DEBUG_MODE is two." #ENDSELECT Because LED_MODE is 1, the code inside the commands nested in the #IF LED_MODE = 1 #THEN code block will become part of the program. The code inside the #ELSE block will be ignored, unless of course, you change the #DEFINE LED_MODE directive to zero.
Chapter 5: Debugging and Datalogging · Page 215 √ √ √ √ √ Enter, save, and run Compiler directives.bs2 and check the program's behavior against your predictions. Change #DEFINE LED_MODE = 1 to #DEFINE LED_MODE = 0. Predict how the program will behave the next time it is downloaded to the BASIC Stamp. Run the program, and again test your predictions. Repeat for the other four combinations of values that you can set LED_MODE and DEBUG_MODE equal to.
Page 216· Applied Robotics with the SumoBot DO DEBUG CRSRX, 0, ? counter counter = counter + 1 #IF LED_MODE = 1 #THEN DEBUG "LED state = ", BIN1 counter.BIT0, CRSRUP TOGGLE LedSpeaker PAUSE 200 #ELSE DEBUG "LED_MODE disabled", CRSRUP PAUSE 100 #ENDIF LOOP Your Turn - Conditionally Compiling LED Commands In Activity #1, you inserted four different PBASIC commands to make the LED notify you of certain events.
Chapter 5: Debugging and Datalogging · Page 217 performs. This kind of tool can be really helpful for isolating both program and circuit bugs. One important feature of this diagnostic tool, is that it all appears in conditional compiler directives. Because of this, you will be able to change one value in your program, and change it from a diagnostic tool back to competition code. Incorporation Diagnostic Display Code in the SumoWrestler.
Page 218· Applied Robotics with the SumoBot ' -----[ Variables ]---------------------------------------------------------. . . #IF DEBUG_MODE = 1 #THEN state VAR Nib ' State machine value #ENDIF All the sensor, state, and maneuver values are displayed in a table, so a table heading is added to the Initialization section. ' -----[ Initialization ]----------------------------------------------------. . .
Chapter 5: Debugging and Datalogging · Page 219 . . ' -----[ Subroutine - Avoid_Tawara_Right ]-----------------------------------Avoid_Tawara_Right: #IF DEBUG_MODE = 1 #THEN state = ATR #ENDIF . . . ' -----[ Subroutine - Go_Forward ]-------------------------------------------Go_Forward: #IF DEBUG_MODE = 1 #THEN state = GF #ENDIF . . . The Display_All subroutine displays the current sensors variable’s value, navigation state, and maneuver.
Page 220· Applied Robotics with the SumoBot ENDSELECT DEBUG CRSRX, 20 ' Cursor to column 20 SELECT maneuver CASE Forward DEBUG "Fwd" CASE Backward DEBUG "Bkwd" CASE RotateLeft DEBUG "RotLft" CASE RotateRight DEBUG "RotRt" CASE CurveLeft DEBUG "CrvLft" CASE CurveRight DEBUG "CrvRt" ENDSELECT ' Display maneuver DEBUG CR ' Carriage return for next line PAUSE 200 ' Pause 0.2 seconds for reading. RETURN #ENDIF Example Program: SumoWrestlerWithDebugMode.
Chapter 5: Debugging and Datalogging · Page 221 right sides (irLS and irRS) do not detect objects, because they store 0s. The same applies to the left and right front QTI object detectors (qtiLF and qtiRF), which both see the black sumo ring surface. Figure 5-2 shows an example of what the Debug Terminal displays. Figure 5-2 Debug Terminal DEBUG_MODE = 1.
Page 222· Applied Robotics with the SumoBot In the first row, none of the sensors see anything, the state is search pattern (SP), and the SumoBot is starting by rotating right. On the second row, the SumoBot has detected an object on its left, and the state has transitioned to track side left object (TSLO). The maneuver is rotate left (RotLft). The object is then moved in front of the front-left IR object detector.
Chapter 5: Debugging and Datalogging · Page 223 ServoLeft ServoRight PIN PIN 13 12 ' Left servo connected to P13 ' Right servo connected to P12 qtiPwrLeft qtiSigLeft PIN PIN 10 9 ' Left QTI on/off pin P10 ' Left QTI signal pin P9 qtiPwrRight qtiSigRight PIN PIN 7 8 ' Right QTI on/off pin P7 ' Right QTI signal pin P8 DummyPin PIN 6 ' I/O pin for pulse-decay P6 LedSpeaker PIN 5 ' LED & speaker connected to P5 IrLedLS IrSenseLS PIN PIN 2 1 ' Left IR LED connected to P2 ' Left IR detecto
Page 224· Applied Robotics with the SumoBot ' State constants.
Chapter 5: Debugging and Datalogging · Page 225 ' -----[ Main Routine ]------------------------------------------------------DO IF qtiLF = 1 THEN GOSUB Avoid_Tawara_Left ELSEIF qtiRF = 1 THEN GOSUB Avoid_Tawara_Right ELSEIF irLF = 1 AND irRF = 1 THEN GOSUB Go_Forward ELSEIF irLF = 1 THEN GOSUB Track_Front_Left_Object ELSEIF irRF = 1 THEN GOSUB Track_Front_Right_Object ELSEIF irLS = 1 THEN GOSUB Track_Side_Left_Object ELSEIF irRS = 1 THEN GOSUB Track_Side_Right_Object ELSE GOSUB Search_Pattern ENDIF ' ' '
Page 226· Applied Robotics with the SumoBot RETURN ' -----[ Subroutine - Calibrate_Qtis ]---------------------------------------Calibrate_Qtis: HIGH qtiPwrLeft HIGH qtiSigLeft PAUSE 1 ' Turn left QTI on ' Discharge capacitor RCTIME qtiSigLeft, 1, temp ' Measure charge time LOW qtiPwrLeft multi = temp ' Turn left QTI off ' Free temp for another RCTIME HIGH qtiPwrRight HIGH qtiSigRight PAUSE 1 RCTIME qtiSigRight, 1, temp ' Turn right QTI on ' Discharge capacitor multi = (multi + temp) / 2 ' Calculat
Chapter 5: Debugging and Datalogging · Page 227 RETURN ' -----[ Subroutine - Pulse_Servos ]-----------------------------------------Pulse_Servos: ' Pulse to left servo LOOKUP maneuver, [ FS_CCW, FS_CW, FS_CW, FS_CCW, NO_ROT, FS_CCW, LS_CCW, FS_CCW ], temp PULSOUT ServoLeft, temp ' Pulse to right servo LOOKUP maneuver, [ FS_CW, FS_CCW, FS_CW, FS_CCW, FS_CW, NO_ROT, FS_CW, LS_CW ], temp PULSOUT ServoRight, temp ' Pause between pulses (remove when using IR object detectors + QTIs).
Page 228· Applied Robotics with the SumoBot INPUT qtiSigLeft INPUT qtiSigRight ' Start the decays PULSOUT DummyPin, temp ' Wait threshold time qtiLF = ~qtiSigLeft qtiRF = ~qtiSigRight ' Snapshot of QTI signal states LOW qtiPwrLeft LOW qtiPwrRight ' Turn off QTIS RETURN ' -----[ Subroutine - Avoid_Tawara_Left ]------------------------------------Avoid_Tawara_Left: #IF DEBUG_MODE = 1 #THEN state = ATL #ENDIF FOR counter = 1 TO 15 maneuver = Backward GOSUB Servos_And_Sensors NEXT FOR counter = 1 TO 15
Chapter 5: Debugging and Datalogging · Page 229 #IF DEBUG_MODE = 1 #THEN state = GF #ENDIF maneuver = Forward GOSUB Servos_And_Sensors ' 1 forward pulse RETURN ' -----[ Subroutine - Track_Front_Left_Object ]------------------------------Track_Front_Left_Object: #IF DEBUG_MODE = 1 #THEN state = TFLO #ENDIF counter = 0 DO UNTIL (irLF = 1 AND irRF = 1) OR counter > 15 maneuver = CurveLeft ' Curve left 15 GOSUB Servos_And_Sensors counter = counter + 1 LOOP DO UNTIL (irLF = 1 AND irRF = 1) OR counter > 30 man
Page 230· Applied Robotics with the SumoBot #IF DEBUG_MODE = 1 #THEN state = TSLO #ENDIF DO UNTIL irRF = 1 OR irLF = 1 maneuver = RotateLeft GOSUB Servos_And_Sensors LOOP ' Rotate left RETURN ' -----[ Subroutine - Track_Side_Right_Object ]------------------------------Track_Side_Right_Object: #IF DEBUG_MODE = 1 #THEN state = TSRO #ENDIF DO UNTIL irRF = 1 OR irLF = 1 maneuver = RotateRight GOSUB Servos_And_Sensors LOOP ' Rotate right RETURN ' -----[ Subroutine - Search_Pattern ]-------------------------
Chapter 5: Debugging and Datalogging · Page 231 GOSUB Servos_And_Sensors IF sensors <> 0 THEN GOTO Next_State NEXT Next_State: ' Exit point of search pattern RETURN ' -----[ Subroutine - Display_All ]------------------------------------------#IF DEBUG_MODE = 1 #THEN Display_All: DEBUG BIN8 sensors, CRSRX, 10 ' Display sensors byte as bits ' Cursor to column 10 SELECT state CASE ATL DEBUG "ATL" CASE ATR DEBUG "ATR" CASE GF DEBUG "GF" CASE TFLO DEBUG "TFLO" CASE TFRO DEBUG "TFRO" CASE TSLO DEBUG "TSLO" C
Page 232· Applied Robotics with the SumoBot ENDSELECT DEBUG CR ' Carriage return for next line PAUSE 200 ' Pause 0.2 seconds for reading. RETURN #ENDIF Your Turn The bulk of the commands that are added to the program when DEBUG_MODE is set to 1 are DEBUG commands with strings of characters. The strings of characters take up a considerable amounts of memory. You can compare how much program memory is consumed by the diagnostic code as follows: √ √ Set DEBUG_MODE = 0.
Chapter 5: Debugging and Datalogging · Page 233 √ Click Run -> Memory map again and compare how much EEPROM is used by the code that was added for debugging. ACTIVITY #4: DATALOGGING A COMPETITION ROUND Displaying values in slow motion while the SumoBot is sitting still won't necessarily expose or solve problems the SumoBot has when it's up against another SumoBot in the competition ring. It's also not convenient to try to have a round while the SumoBot is tethered to the serial cable.
Page 234· Applied Robotics with the SumoBot #IF DEBUG_MODE = 1 OR DATALOG_MODE > 0 #THEN ' State constants. ATL CON 0 ' Avoid_Tawara_Left ATR CON 1 ' Avoid_Tawara_Right GF CON 2 ' Go-Forward . . . #ENDIF A constant named MaxBytes is declared to make it convenient to set the number of bytes that can be used for storing data logged during a round. In this example, MaxBytes is $150. The $ makes a number hexadecimal. A hexadecimal value is used because the Memory Map uses hexadecimal values.
Chapter 5: Debugging and Datalogging · Page 235 Figure 5-4 Memory Map The highest EEPROM address not occupied by PBASIC program code is $257. An extra variable is necessary to keep track of which EEPROM byte gets the next record. This logIndex variable is used with the WRITE command for saving the records to EEPROM and with READ command to retrieve them.
Page 236· Applied Robotics with the SumoBot because we do not want to overwrite all the recorded records from the round the SumoBot just finished before they get displayed. DATALOG_MODE = 2 is for playing back the recorded values from EEPROM. This playback will use all the features that DEBUG_MODE = 1 used in the previous activity's example program. This #IF statement used to just have the DEBUG_MODE = 1 condition. Now, it also has the DATALOG_MODE = 2 condition.
Chapter 5: Debugging and Datalogging · Page 237 ' -----[ Subroutine - Servos_And_Sensors ]-----------------------------------Servos_And_Sensors: #IF DEBUG_MODE = 1 #THEN GOSUB Display_All #ENDIF #IF DATALOG_MODE = 1 #THEN temp.HIGHBYTE = sensors temp.NIB1 = state temp.NIB0 = maneuver WRITE LogData + LogIndex, Word temp logIndex = logIndex + 2 IF logIndex >= (LogData + MaxBytes) THEN #ENDIF . . .
Page 238· Applied Robotics with the SumoBot #IF DATALOG_MODE = 2 #THEN Playback_Round: FOR logIndex = 0 TO MaxBytes STEP 2 READ LogData + logIndex, Word temp sensors = temp.HIGHBYTE state = temp.NIB1 maneuver = temp.NIB0 GOSUB Display_All ' Loop gets all records ' Get record ' NEXT END #ENDIF The same state variable that was updated by the navigation subroutines for the previous example program also has to be updated when DATALOG_MODE = 1. This ensures that correct state values are written to EEPROM.
Chapter 5: Debugging and Datalogging · Page 239 To display all the datalogged values when DATALOG_MODE = 2, the same Display_All subroutine from the previous activity's example program is used. So, its conditional compiler directive has to be adjusted as well. ' -----[ Subroutine - Display_All ]------------------------------------------#IF DEBUG_MODE = 1 OR DATALOG_MODE = 2 #THEN . . . Example Program: SumoWrestlerWithDataLogMode.
Page 240· Applied Robotics with the SumoBot √ √ √ √ √ √ √ The SumoBot will log data as it competes for just under 10 seconds, then it stops. After the SumoBot stops, connect it to the serial cable. Change DATALOG_MODE = 1 to DATALOG_MODE = 2. Run the program and leave the SumoBot connected to the serial cable. Press/release the Reset button. After the speaker beeps 5 times, it will display the logged data to the Debug Terminal in the same format as the previous activity.
Chapter 5: Debugging and Datalogging · Page 241 qtiPwrLeft qtiSigLeft PIN PIN 10 9 ' Left QTI on/off pin P10 ' Left QTI signal pin P9 qtiPwrRight qtiSigRight PIN PIN 7 8 ' Right QTI on/off pin P7 ' Right QTI signal pin P8 DummyPin PIN 6 ' I/O pin for pulse-decay P6 LedSpeaker PIN 5 ' LED & speaker connected to P5 IrLedLS IrSenseLS PIN PIN 2 1 ' Left IR LED connected to P2 ' Left IR detector to P1 IrLedLF IrSenseLF PIN PIN 4 11 ' Left IR LED connected to P4 ' Left IR detector to P11
Page 242· Applied Robotics with the SumoBot GF TFLO TFRO TSLO TSRO SP #ENDIF CON CON CON CON CON CON 2 3 4 5 6 7 ' ' ' ' ' ' Go-Forward Track_Front_Left_Object Track_Front_Right_Object Track_Side_Left_Object Track_Side_Right_Object Search_Pattern #IF DATALOG_MODE > 0 #THEN ' Datalogging constants MaxBytes CON $150 - $10 ' Maximum number of bytes stored #ENDIF ' -----[ Variables ]---------------------------------------------------------temp multi counter VAR VAR VAR Word Word Byte ' Temporary va
Chapter 5: Debugging and Datalogging · Page 243 #ENDIF ' -----[ Initialization ]----------------------------------------------------GOSUB Reset GOSUB Start_Delay GOSUB Calibrate_Qtis ' Wait for Reset press/release ' 5 Second delay ' Determine b/w threshold #IF DEBUG_MODE = 1 OR DATALOG_MODE = 2 #THEN DEBUG CLS, "Sensors State Maneuver", CR, ' Display table heading "-------- -------- --------", CR #ENDIF #IF DATALOG_MODE = 2 #THEN GOTO Playback_Round #ENDIF ' Alternate main routine GOSUB Look_About ' W
Page 244· Applied Robotics with the SumoBot sensors = temp.HIGHBYTE state = temp.NIB1 maneuver = temp.NIB0 GOSUB Display_All ' NEXT END #ENDIF ' -----[ Subroutine - Reset ]------------------------------------------------Reset: READ RunStatus, temp temp = temp + 1 WRITE RunStatus, temp ' Byte @RunStatus -> temp ' Increment temp ' Store new value for next time IF (temp.BIT0 = 1) THEN DEBUG CLS, "Press/release Reset", CR, "button..." END ELSE DEBUG CR, "Program running..." ENDIF ' Examine temp.
Chapter 5: Debugging and Datalogging · Page 245 multi = temp ' Free temp for another RCTIME HIGH qtiPwrRight HIGH qtiSigRight PAUSE 1 RCTIME qtiSigRight, 1, temp ' Turn right QTI on ' Discharge capacitor multi = (multi + temp) / 2 ' Calculate average multi = multi / 4 ' Take 1/4 average IF multi > 220 THEN multi = multi - 220 ELSE multi = 0 ENDIF ' Account for code overhead WRITE QtiThresh, Word multi ' Threshold to EEPROM ' Measure charge time RETURN ' -----[ Subroutine - Servos_And_Sensors ]
Page 246· Applied Robotics with the SumoBot Pulse_Servos: ' Pulse to left servo LOOKUP maneuver, [ FS_CCW, FS_CW, FS_CW, FS_CCW, NO_ROT, FS_CCW, LS_CCW, FS_CCW ], temp PULSOUT ServoLeft, temp ' Pulse to right servo LOOKUP maneuver, [ FS_CW, FS_CCW, FS_CW, FS_CCW, FS_CW, NO_ROT, FS_CW, LS_CW ], temp PULSOUT ServoRight, temp ' Pause between pulses (remove when using IR object detectors + QTIs).
Chapter 5: Debugging and Datalogging · Page 247 qtiLF = ~qtiSigLeft qtiRF = ~qtiSigRight ' Snapshot of QTI signal states LOW qtiPwrLeft LOW qtiPwrRight ' Turn off QTIS RETURN ' -----[ Subroutine - Avoid_Tawara_Left ]------------------------------------Avoid_Tawara_Left: #IF DEBUG_MODE = 1 OR DATALOG_MODE = 1 #THEN state = ATL #ENDIF FOR counter = 1 TO 15 maneuver = Backward GOSUB Servos_And_Sensors NEXT FOR counter = 1 TO 15 maneuver = RotateRight GOSUB Servos_And_Sensors NEXT ' Back up ' Turn right
Page 248· Applied Robotics with the SumoBot RETURN ' -----[ Subroutine - Track_Front_Left_Object ]------------------------------Track_Front_Left_Object: #IF DEBUG_MODE = 1 OR DATALOG_MODE = 1 #THEN state = TFLO #ENDIF counter = 0 DO UNTIL (irLF = 1 AND irRF = 1) OR counter > 15 maneuver = CurveLeft ' Curve left 15 GOSUB Servos_And_Sensors counter = counter + 1 LOOP DO UNTIL (irLF = 1 AND irRF = 1) OR counter > 30 maneuver = RotateLeft ' Rotate left 30 GOSUB Servos_And_Sensors counter = counter + 1 LOOP RET
Chapter 5: Debugging and Datalogging · Page 249 GOSUB Servos_And_Sensors LOOP RETURN ' -----[ Subroutine - Track_Side_Right_Object ]------------------------------Track_Side_Right_Object: #IF DEBUG_MODE = 1 OR DATALOG_MODE = 1 #THEN state = TSRO #ENDIF DO UNTIL irRF = 1 OR irLF = 1 maneuver = RotateRight GOSUB Servos_And_Sensors LOOP ' Rotate right RETURN ' -----[ Subroutine - Search_Pattern ]---------------------------------------Search_Pattern: #IF DEBUG_MODE = 1 OR DATALOG_MODE = 1 #THEN state = SP #EN
Page 250· Applied Robotics with the SumoBot RETURN ' -----[ Subroutine - Display_All ]------------------------------------------#IF DEBUG_MODE = 1 OR DATALOG_MODE = 2 #THEN Display_All: DEBUG BIN8 sensors, CRSRX, 10 ' Display sensors byte as bits ' Cursor to column 10 SELECT state CASE ATL DEBUG "ATL" CASE ATR DEBUG "ATR" CASE GF DEBUG "GF" CASE TFLO DEBUG "TFLO" CASE TFRO DEBUG "TFRO" CASE TSLO DEBUG "TSLO" CASE TSRO DEBUG "TSRO" CASE SP DEBUG "SP" ENDSELECT ' Display state DEBUG CRSRX, 20 ' Cursor t
Chapter 5: Debugging and Datalogging · Page 251 PAUSE 200 #ELSE PAUSE 50 #ENDIF ' Pause 0.2 seconds for reading RETURN #ENDIF Your Turn - Customizing Your SumoBot The possibilities are endless for increasing your SumoBot's abilities. In personal robotics club competitions, SumoBots have been sighted with extensive modifications. One winning competitor used only the SumoBot printed circuit board and made a custom chassis, plow, and motors and sensors.
Page 252· Applied Robotics with the SumoBot Displaying the sensors variable can help for quick trouble-shooting between rounds. The LCD Terminal AppMod (#29121) is designed to be attached directly to the SumoBot board’s AppMod Header for this purpose, but you will have to move some of your other sensors around to accommodate it. Another option is the Parallax Serial LCD (#27976), which only takes one I/O pin, and lots less code to control, and it can be used in place of the status LED and pushbutton.
Chapter 5: Debugging and Datalogging · Page 253 SUMMARY This chapter introduced three techniques to help find the cause(s) of SumoBot problem behaviors: (1) LED signals, (2) debugging routines, and (3) datalogging a sumo round. Conditional compiler directives were applied to all these techniques, so that you can change one or two values at the beginning of the program to either include or exclude your conditional LED/debugging/datalogging code from the program. Questions 1.
Page 254· Applied Robotics with the SumoBot 4. Modify SumoWrestlerWithDebugMode.bs2 so that it displays numbers next to each record. Projects 1. Determine the average number of samples per second the SumoWrestlerWithDatalogMode.bs2 takes. If you have not already done so, modify the program so that it displays numbers next to each record. Use a stopwatch to identify certain events, then look for them by calculating the approximate record number that should hold the event. 2.
Appendix A: System Requirements and Parts Listing · Page 255 Appendix A: System Requirements and Parts Listing System and Software Requirements • • • • PC running Windows® 2000/XP or higher operating system. Available serial port OR USB port with USB-to serial adapter (#800-00030) BASIC Stamp Editor for Windows v2.0 or higher Optional: BASIC Stamp source code for the experiments in this book, available for free download from www.parallax.com on the Applied Robotics with the SumoBot page (#27403).
Page 256· Applied Robotics with the SumoBot Table A-1: SumoBot Robot Competition Kit (#27402) Parts and quantities subject to change without notice Part # Description Quantity 27000 Parallax CD-ROM 1 27400 SumoBot Manual 1 27403 Applied Robotics with the SumoBot text 1 27404 SumoBot Competition Ring poster 1 150-02210 220 Ω resistors ¼ W 5% 6 150-04710 470 Ω resistors ¼ W 5% 8 150-01030 10 kΩ resistors ¼ W 5% 2 350-00003 LED-Infrared 10 350-00006 LED-Red 2 350-00014 IR Receiv
Appendix A: System Requirements and Parts Listing · Page 257 Table A-2: For SumoBot Robots purchased separately Parts and quantities subject to change without notice Part# Description Quantity 27400 SumoBot Robot Kit 2 27403 Applied Robotics with the SumoBot text 1 27404 SumoBot Competition Ring poster 1 150-02210 220 Ω resistors ¼ W 5% 4 150-04710 470 Ω resistors ¼ W 5% 6 150-01030 10 kΩ resistors ¼ W 5% 2 350-00003 LED-Infrared 4 350-00014 IR Receiver 4 350-90000 LED Standoff
Page 258· Applied Robotics with the SumoBot
Index -#- #. See conditional compile directives -$- $. See hexadecimal, See compiler directives -.- .BIT operator, 49 .HIGHBYTE, 236 .LOWBYTE, 236 .NIB, 237 -@- @Address operator, 46 -~- ~. See invert bits operator -A- acceleration, 24 Accelerometer, 251 -B- British Engineering units, 23 -C- cgs units, 23 coefficient of friction, 28, 31 coefficients of friction, 29 comment-out, 94 compiler directives, 212, 213 compile-time, 44 conditional compile directives #DEFINE, 212 #IF...#ENDIF, 213 #SELECT..
friction force, 31 friction forces, 26 frictional force diagram, 29 -G- gram, 23 gravity, 24 Guarantee, 2 -H- hardware requirements, 255 additional household items, 257 hexadecimal, 234 hexadecimal conversion, 45, 234 hybrid state machine diagram, 175 -I- IF...ELSEIF...ELSE...
programs TestLedSpeaker.bs2, 60 CompilerDirectives.bs2, 214 TestPushButton.bs2, 63 Forward100Pulses.bs2, 11 TestResetButton.bs2, 57 ForwardLowTimeTest.bs2, 18 TestSideIrObjectDetectors.bs2, 120 FrontAndSideIrNavigation.bs2, 166 TestSumoWrestler.bs2, 186 FrontIrNavigation.bs2, 159 ThreeVariablesManyJobs.bs2, 53 IrInterferenceSniffer.bs2, 86 LookupExample.bs2, 149 PushbuttonLed.bs2, 172 PushbuttonMode.bs2, 67 QtiPulseDecayTrick.bs2, 114 QtiPulseTrickLeft.bs2, 111 QtiSelfCalibrate.
Scientific Method, 208 SELECT...CASE, 66 self calibrating QTI sensors, 102 sensor flags, 143 servo connections, 13 servo control, 145 servo rotational velocity graph, 146 servo RPM calculation, 21 servo slow-down, 16, 21 sigma, 27 slug, 23 software requirements, 255 state machine, 170 reset state, 170 state machine diagram, hybrid, 175 state machine diagram, modified, 173 state machine.