Object detection allows a robot to detect objects in its path and take some action, such as avoiding the object. We chose the term feature to describe an object rather than obstacle, because sometimes the object is something the robot wants to seek, rather than avoid (such as a soccer ball). We didn't use the word "object" because it was a potentially confusing class name with object oriented Java programming.
A FeatureDetector detects objects using a sensor, such as a touch sensor or ultrasonic sensor. It is the main interface in the object detection package from which all data originates. There are many benefits of using a FeatureDetector:
There are currently two implementations:
The RangeFeatureDetector allows you to specify some parameters for the sensor, such as the maximum range you want it to report findings for, and the time between performing scans. Construct a simple RangeFeatureDetector as follows:
Once you have a FeatureDetector instantiated, you can perform a scan on it to retrieve data:
Warning: Make sure to check for a null object before trying to read data from the returned Feature object, otherwise your code will throw a null pointer exception.
The main benefit of a FeatureDeteector is the ability to automatically notify other classes when an object is detected via a listener interface. The next section discusses this in more detail.
Once you have a FeatureDetector instantiated, you can add a FeatureListener to it. The FeatureListener code will be notified when an object is detected via the FetureListener.featureDetected() method. There you can make your robot react to the detected object by performing some sort of action. The following code shows how to use a FeatureListener:
The most basic class to implement the Feature interface is the RangeFeature class. This class is a data container, and the data is retrieved by these methods:
Some scanners are capable of returning multiple object detections, such as the LEGO ultrasonic sensor. Other sensors are only capable of producing a single object detection. In either case, they are both capable of producing data to fulfill both methods, as described below.
When the getRangeReading() method is called, it returns the closest object that was detected by a scanner, even if it is capable of returning more than one hit.
When the getRangeReadings() method is called, it returns the all objects it detected. If the scanner is only capable of returning one hit, the RangeReadings object will contain only one RangeReading.
Multiple Feature Detectors
It is also possible to combine several different FeatureDetectors into one FeatureDetector using the FusorDetector class. This is useful for robots that have a number of detector sensors located on the robot (several bumpers at different locations and range sensors). The code to fuse many FeatureDetectors looks like this: