How To Program Object Avoidance and Detection Algorithm


This tutorial shows you various types of Object Avoidance and detection algorithm that can be used in object detection and avoidance robots, which is very useful to robot builders.

light Detection Algorithm for robot

Object Avoidance and Detection – Photovore Algorithm

Light Detection Algorithm comes in many forms, one of the simplest is Photovore, which means a robot that chases light. For beginners, this should be the first algorithm to learn.

For this to work, your robot needs at least two light detecting sensors, typicall photoresistors or IR emitter/detectors, out in front and spaced apart from each other. One on left side of your robot, the other located on the right side.

In your code, have the microcontroller read the analog value from both sensors. Then do a comparison – the sensor that reads more light is the direction your robot should turn. For example, if the left photoresistor reads more light than the right photoresistor, your robot should turn or tend towards the left. If both sensors read about the same value, meaning the both get the same amount of light, then your robot should drive straight.

light Detection Algorithm for robot

pseudo-code:

read left_photoresistor 
read right_photoresistor
if left_photoresistor detects more light than right_photoresistor 
then turn robot left
if right_photoresistor detects more light than left_photoresistor 
then turn robot right
if right_photoresistor detects about the same as left_photoresistor 
then robot goes straight
loop

If you havent discoverd this yet, the photovore algorithm works great with the differential drive algorithm. =)

Object Avoidance – Photovore Modified

Using the same exact code, and same exact robot, you can do a small modification to your robot to give your photovore the ability to avoid objects.

Bend your photoresistors so that they point downwards, and are close to the ground. Depending on the lighting, objects will all cast shadows onto the ground. Avoiding darkness, your photovore robot is naturally an object avoider.

Of course if the lighting shines directly onto an object, or if you have dark floors with white walls, it might not work so well . . . But its easy and it will work.

If you are making a line following robot, make a Photovore, point the photo-resistors towards the ground, and space the photo-resistors so that the distance is less than the width of the white line. You can use the exact same algorithm.

Improved Photovore Algorithm

This algorithm does the same as the original, but instead of case-based it works under a more advanced Fuzzy Logic control algorithm. Your robot will no longer just have the three modes of turn left, turn right, and go forward. Instead will have commands like ‘turn left by 10 degrees’ or ‘turn right really fast’, and with no additional lines of code!

pseudocode:

read left_photoresistor 
read right_photoresistor
left_motor = (left_photoresistor - right_photoresistor) * arbitrary_constant 
right_motor = (right_photoresistor - left_photoresistor) * arbitrary_constant
loop

Photovore – Split Brain Approach

This algorithm works without comparison of photoresistor values. Instead, just command the right motor based on light from the left sensor, and the left motor with only data from the right sensor.

light Detection Algorithm for robot

You can also get interesting variations by reversing the sensors for a cross-brain algorithm.

pseudocode:

read left_photoresistor 
read right_photoresistor
move left_wheel_speed = right_photoresistor * arbitrary_constant 
move right_wheel_speed = left_photoresistor * arbitrary_constant
loop

The human brain to an extent takes the split brain approach. Both halves of the brain, naturally connected by a nerve called the corpus callosum, share information between each other. But if you sever this nerve (as does happen although rarely for medical reasons), the brain mostly remains fully functional. People can lead normal lives, despite their brain being split in half! (seriously, Im not joking) This means the brain operates using the split brain approach, although on many occasions there are advantages to share information (hence the reason for the nerve).

light Detection Algorithm for robot

Photophobe

The photophobe robot is a robot that runs away from light instead of chases light. There are two ways you can do this. The first is simply to reverse the left and right photoresistors, so that the left sensor is on the right side, and the right sensor is on the left side. With no changes of code, it will avoid light!

light Detection Algorithm for robot

light Detection Algorithm for robot

But if you want to do the code method, make this slight change:

pseudocode:

read left_photoresistor 
read right_photoresistor
if left_photoresistor detects more light than right_photoresistor 
then turn robot right
if right_photoresistor detects more light than left_photoresistor 
then turn robot left
if right_photoresistor detects about the same as left_photoresistor 
then robot goes straight
loop
Share Button

Leave a Reply