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:
In addition to items like a computer and SumoBot robot, you will also need experience (practice with earlier activities) before continuing here.
You will have your first working program that can make your SumoBot “see” and charge an opponent.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.