Detecting Objects
These custom functions use the mechanical bumpers or ultrasonic sensor:
checkBumpers()
— detect bumper collision with object on left or right sidemeasureDistance()
— measure distance ahead to nearest object in pathavoidCollision()
— avoid collision with nearby object in pathfindClosestObject()
— find the closest object (360° scan) and drive towards it
checkBumpers()
A custom function named checkBumpers()
uses the mechanical bumpers to detect collisions with obstacles.
The left and right mechanical bumpers each have a "whisker" that extends out to one side. The "whisker" is a flexible metal wire that will bend during a collision. If the wire bends far enough, it will make electrical contact with a metal screw on the bumper board. It is similar to how a switch or button works.
In order to work, the checkBumpers()
function must be continuously called by the loop()
function (or continuously called by a loop within another function).
The checkBumpers()
function requires these objects as part of your global variables before the setup()
function:
Add the checkBumpers()
custom function after the loop()
function:
ADD CODE TO FUNCTION: You need to add code within the checkBumpers()
function to perform actions (brake, back up, turn, etc.) for each bumper collision.
NOTE: The checkBumpers()
function does not check for a simultaneous collision with both bumpers. If a simultaneous bumper collision did occur, the function will treat it as a left bumper collision (because that's the first check performed in the if
statement). However, you could modify the function to check for all three possibilities: both bumpers, left bumper only, or right bumper only.
measureDistance()
A custom function named measureDistance()
uses an ultrasonic sensor to measure the distance ahead to the nearest object in the robot's path.
The ultrasonic sensor has a transmitter (i.e., a speaker) that can produce high-frequency sound, which cannot be heard by the human ear. The sensor also has a receiver (i.e., a microphone) that detects the echo of the high-frequency sound when it is reflected back from a nearby object. By measuring how much time it takes for the echo to arrive, you can calculate the distance between the sensor and the object.
When the measureDistance()
function is called, it will return the distance measurement as a float
value (decimal number). Your app will typically store this value in a local variable, and then do something with the value.
For example, this code statement declares a local variable named sensorDist
to store the float
value returned by calling the measureDistance()
function:
The measureDistance()
function requires these global variables before the setup()
function:
You need to set the pin modes for the ultrasonic sensor's transmitter (Trig) and receiver (Echo). In addition, you want to be sure the transmitter is turned off (LOW
) when the app first starts. Add this code within the setup()
function:
Add the measureDistance()
custom function after the loop()
function:
GO METRIC: The measureDistance()
function returns the distance in inches, but the function can be modified to return the distance in centimeters.
avoidCollision()
A custom function named avoidCollision()
uses an ultrasonic sensor to avoid collisions with a nearby object in the robot's path.
The avoidCollision()
function requires the measureDistance()
function, so be sure to add that function after the loop()
function.
In order to work, the avoidCollision()
function must be continuously called by the loop()
function (or continuously called by a loop within another function).
The avoidColliion()
function requires this object as part of your global variables before the setup()
function:
Add the avoidCollision()
custom function after the loop()
function:
ADD CODE TO FUNCTION: You need to add code within the avoidCollision()
function to perform actions (brake, turn, etc.) when an obstacle is too close.
You'll need to decide what actions the robot should perform when an obstacle is too close. Depending on the purpose and context of your robot, your solution will be different:
Maybe the robot should turn around and drive in the opposite direction.
Maybe the robot should turn 90° right (or left) and then continue driving.
Maybe the robot should scan left and right to check for a clear path.
Maybe the robot should navigate around the obstacle to maintain its original direction.
etc.
AVOID COLLISION WHILE FOLLOWING LINE
Avoiding collisions while following a line is possible, but it presents a challenge:
When the robot detects that an obstacle is nearby in the path ahead, the robot has to leave the line, detour around the obstacle, and then find the line again.
This diagram shows a possible solution to detour around an obstacle while following a line.
Notice in step 5 of the diagram that the robot makes a 45° turn to approach the line at an angle (instead of making a 90° turn). The reason for this is to ensure that one IR line sensor will detect the line first, so the followLine()
function can steer the robot to center itself on the line again. (Otherwise, if the robot approaches the line head-on at a 90° angle, the robot will simply drive over the line.)
The code for the loop()
function could be as simple as:
The code for detouring around an obstacle would be placed within the avoidCollision()
custom function, which might look like this:
This version of the avoidCollision()
function has a local variable named detourDist
set to a value of 12.0
inches. This represents how far the robot will detour around an obstacle, so this value needs to be larger than the width or depth (whichever is larger) of the actual obstacle. As needed, adjust the value of detourDist
based on the size of your obstacles. Just be sure to include a decimal point, since it is a float
value.
The diagram and sample code shown above represent just one possible solution for avoiding obstacles while following a line. You might need to create a different solution that works better for your particular task scenarios.
findClosestObject()
A custom function named findClosestObject()
uses an ultrasonic sensor and the wheel encoders to perform a 360° scan of the environment to find the closest object and then drive towards it.
To accomplish this, the robot perform two pivots:
The first pivot turns the robot 360° to find the direction and distance to the closest object. The ultrasonic sensor is used to measure the distance to the object, while the wheel encoder count is used to indicate the "direction" of the object. Variables are used to compare the measurements and keep track of which object is the closest.
The second pivot turns the robot to face the direction of the closest object found in the first pivot. Then the robot drives towards the object based the distance to the object.
The findClosetObject()
function requires two other custom functions, in order to work. Be sure to add these two functions after the loop()
function:
measureDistance()
function — used to measure distance to nearby objectsdriveDistance()
function — used to drive towards the closest object
The findClosestObject()
function requires these objects as part of your global variables before the setup()
function:
The findClosestObject()
function produces an alert sound after completing each of its two pivots. It assumes the speaker's pin number is stored in a global variable named speaker
. If necessary, modify this variable name (or remove the sounds).
Add the findClosetObject()
custom function after the loop()
function: