LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > IR SumoBot WX Opponent Detection and Tracking

IR SumoBot WX Opponent Detection and Tracking

What it’s about

In a robot sumo match, your SumoBot WX needs a way to find, face, and charge at an opponent SumoBot.  Three common sensors that help a SumoBot detect an opponent at a distance are infrared, ultrasonic, and laser.
Infrared (IR) sensors, like the ones in this activity set, shine IR light and detect reflections off nearby objects.

Ultrasonic sensors like the Ping))) Ultrasonic Distance Sensor typically measure time of flight – the time it takes for a reflected ultrasonic chirp to return.  The microcontroller measures that return time and uses the speed of sound to calculate the distance.  Laser sensors like the LaserPING 2m Rangefinder also measure time of flight, but since light travels much faster than sound, the time measurement is normally built into the device.

In this activity, you will make IR distance sensors from the infrared (IR) LEDs.  The infrared LEDs and you will use are similar to the ones found inside infrared TV remotes.  Likewise, the infrared receivers you will use are similar to the ones found inside certain TVs, VCRs, projectors, to name a few.  Although IR light is invisible to us, the SumoBot will be able to use its IR LEDs like invisible flashlights and its IR receivers like eyes that can see the reflection when an IR beam shines on an opponent.

In this section’s activities, you will:

  • Enclose infrared LEDs in light shields
  • Mount the infrared LEDs and receivers on your SumoBot
  • Load, run and test programs that:
    • Display the relative distance of an object when it’s in the IR beam’s path
    • Make the SumoBot maneuver to follow an object.

Before you start

In addition to items like a computer and SumoBot robot, you will also need experience (practice with earlier activities) before continuing here.

Items

  • A SumoBot WX, built up through the activities in these pages:
    • Build your SumoBot WX – all pages in the activity set
    • Add Cables for Front QTI Sensors
    • Install the QTI Line Sensors
    • Test QTI Measurement Values with Terminal
  • A computer with a USB 2.0 compatible port and Chrome browser
  • USB A to micro-B cable
  • 4 AA batteries in the SumoBot WX robot’s battery pack
    • 1.2V Tenergy NiMH rechargeable batteries, recommended for the longest run time|
      — or —
    • 1.5 V Alkaline batteries, about 30 minutes run time

Experience

  • Basic BlocklyProp coding skills, at least through the BlocklyProp for your SumoBot WX pages
  • Practice with navigation, through the SumoBot WX Movement Basics pages
  • Practice with avoiding the edge of the ring, through the Install the QTI Line Sensors pages.

After you finish

You will have your first working program that can make your SumoBot “see” and charge an opponent.

 

Install the Front IR LEDs and Receivers

Parts:

  • (1) SumoBot, built up at least through the end of the Install the QTI Sensors page
  • (2) IR LEDs
  • (2) IR LED standoffs
  • (2) IR LED light shields
  • (2) IR Receivers

 

Prep:

  • Insert each IR LED into a standoff so that its longer leg passes through the hole with the + mark by it.  The shorter leg should pass through the hole with the – mark by it.
  • Press each IR LED into the standoff so that it snaps into place.
  • Snap each IR shield onto a standoff.

 

Installation:

  • Set the SumoBot’s POWER switch to 0.
  • If red LEDs are in the P5 and P6 sockets from previous activities, remove them now.
  • Insert the first LED into the P5 pair of sockets. Make sure the longer + pin goes into the socket labeled P5.  Also, make sure both pins are firmly seated in their sockets.  It’s okay to bend the longer pin a little to get the shorter pin to sink all the way into its socket.
  • With the dome on the IR receiver facing forward, insert it into the 5V…P1 sockets.  Make sure all three pins are firmly seated as well.
  • Repeat with a second IR LED into the P6 pair of sockets and a second IR receiver into the 5V…P2 sockets.

 

  • Bend the IR LEDs down so that they rest on the SumoBot board.  They should be level and pointing forward.  They should also be pointing slightly outward to the left and right.

Trimming the leads — The pictures show the leads of these parts untrimmed.  It’s best to leave them that way for now.  Before an actual match, the leads can be trimmed shorter to prevent bending if your robot gets toppled.  After trimming the LED leads, the LED won’t have a long and short pin, so you will need to use the + marker on the plastic standoff to figure out which lead goes into the +P5 and +P6 sockets.  Keep in mind, if you trim them too short, they won’t be usable.  So as a first step, it’s best to trim them so that the leads are still about twice as long as you think they should be.  Then, remove a little at a time until you get the best fit.  

 

Terminal IR Distance Measurements Tests

Now, your SumoBot’s IR distance sensors are ready to test.  These tests are important, and the SumoBot has to pass all of them before it’ll be ready to chase objects in the sumo ring.

Program

This test will verify that each IR distance sensor reports 8 when no object is detected, or something in the 0 to 7 range when an object is detected.  The value 0 indicates the object is very close, while a value of 7 is almost out of range.  The actual distances will depend on how reflective the object is.  For example, a large white shoebox might get a 5 report at 2 or 3 times the distance of a stealthy, black, and minimally reflective SumoBot.

  • Download SumoBot-Front-IR-Terminal.svg.

SumoBot-Front-IR-Terminal.svg

  • In BlocklyProp Solo, click the Open button, and open the SumoBot-Front-IR-Terminal.svg file you just downloaded.
  • Set POWER to 1.
  • Click the Load & Run (Save code to EEPROM)  button to load the program into your SumoBot.
  • Leave the USB cable connected.

Tests

The animation below shows how to position a box at various distances in front of each IR sensor.  Keep in mind that the distances will depend on how well the box reflects infrared light.  In other words, don’t worry if your measured distances don’t match the ones in the animation.   

A white cardboard box (not glossy) or the backside of a sheet of white poster board will be highly reflective.  A book will be somewhat less reflective, and a black box might absorb most of the infrared and so the reflection will be faint, and the object will pass out of range (distance of 8) at some point much closer to the front of the SumoBot.

Uneven measurements are also okay.  For example, one side might report a distance of 5 while the other reports 3 or 4.  The main goal is to get measurements of 8 when objects are out of range, and “some” indication of distance as the object is moved closer to the front of the SumoBot.  

  • With USB still connected, place the SumoBot on the sumo ring with about 4 ft or 1.2 m of open space in front of the SumoBot.  
  • Place an object like a cardboard box with one face very close to the left IR LED -approximately 1” or 2.5 cm away.  
  • Verify that the distance under L is in the 0 to 2 range, and the distance under R is 8.
  • Move the box further away, and verify that the values increase, up to a point where the box is out of range and the distance is 8.  The distance under R should be 8 the entire time.
  • Repeat for the right sensor.  This time, the left sensor should report 8 while the distance values for the right sensor increase with object distance.  
  • If you were able to get distances from 0..2 close up, and up to 8 out of range for each side, skip to How the Tests Work on the next page.  If not, check the Troubleshooting section below first.

 

Troubleshooting

If one or both of the IR sensors seem to be “stuck” at a particular measurement, it might indicate a loose or incorrect connection.  In that case, make sure that all the pins are properly connected to the right sockets.  

If you are not able to get 8 regardless of how clear the path in front of the SumoBot is, try these tips.

  • Try tipping your SumoBot slightly upwards.  
    • If that fixes the problem and allows the distance reported to be 8 when nothing is detected, it means the infrared light from the IR LEDs is reflecting off the table surface in front of the SumoBot.  
    • If the SumoBot is not on the sumo ring for these tests, find a way to bring the ring to the SumoBot or bring the test to the ring.  
    • If this test was already on the ring, AND if tipping the SumoBot slightly upward prevented the unwanted detections:
      • Point the IR LED slightly upward so that less IR light shines on the surface in front of it.  It will still need to detect a SumoBot in front of it, so try to minimize how far upward you point the IR LEDs.
  • If it doesn’t seem like tipping the SumoBot upward results in distance values of 8, try turning off any fluorescent lights in the room.  If that allows the measurement to go to 8 for no objects, it means the fluorescent lights are actually triggering the IR receivers.  Not all fluorescent lights do this, but if one near your robotics area does, consider moving your area somewhere away from that particular light.  Another option would be to make a sun visor to prevent light from above from reaching the IR receivers.  
  • If the distance tops out at 5 or 6 when it’s on the ring, make a note of the “no objects detected” distance.  You will need it in the Detect and Charge at an Opponent activity’s Program section.

 

How the Tests Work

If humans could see infrared as red, it might look like the image below.  The SumoBot on the left shines both of its IR lights.  The lower beam misses the opponent SumoBot, and just keeps going.  In this case, the IR receiver sends a high signal to the Propeller microcontroller’s P6 I/O pin because it does not detect any infrared.  The upper beam bounces off the corner of the opponent SumoBot, and some of the reflected infrared light reaches the IR receiver connected to P5.  That IR receiver sends P5 a low signal indicating it detected infrared light.
 

Let’s take a look at the SumoBot-Front-IR-Terminal code blocks.

The main loop just repeatedly calls two functions with run function blocks.  First, it calls “IR Front” to measure the distances, and then “IR Front Terminal” to display those distances.

The program contains all the functions you have created up until now as well as a few new ones.  For example, QTIs front Turn from Border is new, and if you look inside, you’ll see the switch…case statement you created to avoid the border in Navigate with QTIs -Avoid the Border.  The IR Front function is also new, and it does the IR measurement work.  

  • Right-click the function IR Front block, and select Expand block.

When you expand the IR front block, it should look like this.  

The IR front block uses frequency PIN 5 duration (ms) 1 frequency (Hz) 38000 to make the IR LED flicker on/off at 38,000 Hz (times per second).  This is what the IR receiver requires to detect the reflected infrared.  Other sources of infrared, like lamps and sunlight, do not tend to flicker at 38,000 Hz—they’re usually emitting infrared at some constant level instead of flickering.  This consistent, rapid on/off behavior makes it possible for the IR receivers in devices to detect the signal from an IR remote by only detecting infrared that flickers at 38,000 Hz.

Immediately after making the IR LED flicker for 1 ms, the program uses irLF = irLF + check PIN 1 to check the IR receiver’s output and keep a tally of how many times the object was not detected.  In the case of not detected, the IR receiver’s output stays high, and check PIN returns a binary 1.  If reflected IR is detected, the IR receiver’s output sends a low signal, and check PIN returns a binary 0.  

The process for both IR LEDs and receivers is repeated in a loop that counts from 0 to 140 in steps of 20.  The user defined code uses the da variable to set the voltage at all the IR LED’s cathodes.  By increasing this voltage, it makes the IR LED dimmer each time through the loop, which in turn makes the IR sensor more nearsighted.  By counting the number of times check PIN returns 1 (did not detect an object), it arrives at a number that indicates the relative distance.  

When the object is up close, every time through the loop, check PIN returns 0 because no matter how nearsighted it gets, the reflection is still detected.  When the object is out of range, check PIN returns 1 each time since it did not detect the flickering infrared reflected off any objects.  All those 1 values are added up with the irLF = irLF + check PIN… operation, for a result of 8.  

 

Detect and Charge at an Opponent

Now that the IR receivers have passed the tests, it’s time to use them in a program that makes the SumoBot charge at an opponent it detects with its IR.

Program

This program charges straight ahead if an opponent is detected by both IR sensors.  If the opponent is only seen by the left sensor, the SumoBot pivots left.  Likewise, if the opponent is only seen by the right sensor, the SumoBot pivots right.

  • Download SumoBot-Front-IR-Nav.

SumoBot-Front-IR-Nav.svg

  • In BlocklyProp Solo, click the Open button, and open the SumoBot-Front-IR-Nav.svg file you just downloaded.

 

  • If your Terminal IR Distance Measurements had a max distance of 8 for both sensors, skip to Set POWER to 1 step below.  Otherwise, continue here:
    • If one or both of your IR receivers would not report anything above some value like 5 or 6 with no objects in its path, first make sure you have tried everything in the Troubleshooting section.  If you did and the IR still reports a max distance below 8, then:
      • Right-click the IR Front Navigator function block and select Expand Block.  (There’s an example of this in the How It Works section.)
      • Change the 7 values to one less than the no objects detected distance.  For example, if the no objects detected distance was 6, change the 7s to 5s.
  • Set POWER to 1.
  • Click the Load & Run (Save code to EEPROM)  button to load the program into your SumoBot.
  • Set POWER to 0.

Tests

  • Make an 8 ½ inch tall x 3.5 inch wide cylinder out of a sheet of printer or binder paper and some tape.
  • Disconnect USB, make sure batteries are loaded and take it to the sumo ring.
  • Set POWER to 2.
  • Allow the SumoBot to roam the sumo ring, and verify that it still avoids the border with its QTIs.
  • Next, try placing the paper cylinder 6 inches (or 150 cm) in front of one of the sensors.  The SumoBot should turn toward the object and then start pushing it out of the ring.

 

Understanding and Tuning your Code

Now that your tests are complete, you can fine tune the code to maximize your SumoBot’s chances in the competition ring. But to be able to tune the code, you need to understand how it works before you start experimenting with different tuning strategies.

How It Works

The main loop checks the “QTIs Front” function first.  After that, if QTIs store anything other than binary 11, it means that the SumoBot needs to forget about anything in front of it and instead avoid the border and go in a new direction.  So, the if qtis ≠ 11 binary, do input has a block that calls run function “QTIs front turn from Border”.  On the other hand, if QTIs front loads 11 binary into the qtis variable, it means the SumoBot is safely cruising over the black sumo ring surface, so it should look for an opponent.  Inside the else input, there are two enabled function calls.  The first is to “IR Front”, which checks for objects and stores their relative distance values in the irLF (IR Left Front) and irRF (IR Right Front) variables.  Then, “IR Front Navigator” sets the wheel speeds based on the distances.

 

  • Right-click the function IR Front Navigator… block and select Expand Block.

The IR Front Navigator function has a chain of if…else if…else if…else conditions.  The first is if irL and irRF are both less than 7.  It means they both see an opponent, so charge straight ahead at full speed.  If only one of the values was less than 7, it means the other sensor is 8, and the opponent was only sighted by one of the two IR sensors. 

The first else if detects the opponent on the left, but not on the right, so its do input has CR servo PIN…speed… blocks that make it pivot to the left.  The second else if is an inverse of the first, with the opponent detected on the right, and servo blocks for pivoting right.  The else condition is a catch all, and currently, it’s just moving forward.  

Tuning Strategies

There are several areas where code adjustments can improve your SumoBot’s chances of winning in the ring.  Since each robot and competition environment is slightly different, exact code is not given here. But these are three strategies you might consider for custom code adjustments.

How Close is Too Close

Probably the most important ingredient to a winning SumoBot strategy is tuning and customizing how motors respond to opponent detection (and not detection).  The example you just tried is the most basic version.  In terms of simple adjustments, those 7 values can be reduced if you need to make the SumoBot only respond when the opponent is at a closer range.  This is not necessarily a good thing, unless your SumoBot is having problems detecting infrared that reflects off the surface of the sumo ring or objects at a distance around the ring.

Side Eye

What happens if the opponent tips your SumoBot upwards?  The opponent disappears!  The function in this code example should still deal with it properly since it plows straight forward when an opponent is not visible as well as when it is visible.  It’s only when the opponent is detected on one side (with one eye) that the SumoBot responds by turning.  

One clever adjustment could be turning in place faster if the opponent is detected further away on a given side.  On the other hand, you might want the SumoBot to turn slower if it’s detected on a side but closer.  The trick to this kind of modification would be to multiply the irLF or irRF distance by some value to get a range from 100 to -100 on the wheel that currently pivots.

Look Both Ways

Another way to improve your chances of winning is to scan left and right for an opponent if none is detected for a certain amount of time.  It’s a challenging piece of code that can be added to the else condition.  If you can get it to work well, your SumoBot will be a much stronger competitor.

 

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024


Source URL:https://learn.parallax.com/courses/ir-sumobot-wx-opponent-detection-and-tracking/
Links