Object Detection
Object Detection

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.

Feature Detectors

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:

  • Automatic scanning and reporting of data
  • A listener interface to segregate the "action-response" code
  • Allows one code segment to respond to data from multiple sensors

There are currently two implementations:

  • RangeFeatureDetector - uses RangeFinder classes, such as the LEGO UltrasonicSensor.
  • TouchFeatureDetector - uses Touch classes, such as the LEGO TouchSensor.

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:

int MAX_DISTANCE = 50; // In centimeters
int PERIOD = 500; // In milliseconds
UltrasonicSensor us = new UltrasonicSensor(SensorPort.S4);
FeatureDetector fd = new RangeFeatureDetector(us, MAX_DISTANCE, PERIOD);

Once you have a FeatureDetector instantiated, you can perform a scan on it to retrieve data:

Feature result = fd.scan();
if(result != null)
	System.out.println("Range: " + result.getRangeReading().getRange());

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.

Feature Listeners

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:

import lejos.nxt.*;
import lejos.robotics.objectdetection.*;

public class ObjectDetect implements FeatureListener {

	public static int MAX_DETECT = 80;
	
	public static void main(String[] args) throws Exception {
		
		ObjectDetect listener = new ObjectDetect();
		UltrasonicSensor us = new UltrasonicSensor(SensorPort.S4);
		RangeFeatureDetector fd = new RangeFeatureDetector(us, MAX_DETECT, 500);
		fd.addListener(listener);
		Button.ENTER.waitForPressAndRelease();
	}
	
	public void featureDetected(Feature feature, FeatureDetector detector) {
		int range = (int)feature.getRangeReading().getRange();
		Sound.playTone(1200 - (range * 10), 100);
		System.out.println("Range:" + range);
	}
}

Feature data

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:

  • getRangeReading() - returns a RangeReading object
  • getRangeReadings() - returns a RangeReadings collection
  • getTimeStamp() - returns the system time (in ms) when this data was collected

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.

Single Reading

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.

Multiple Readings

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:

UltrasonicSensor us = new UltrasonicSensor(SensorPort.S4);
FeatureDetector detector1 = new RangeFeatureDetector(us, MAX_DETECT,RANGE_READING_DELAY);

Touch leftBump = new TouchSensor(SensorPort.S2);
FeatureDetector detector2 = new TouchFeatureDetector(leftBump, 10, TOUCH_Y_OFFSET); 

Touch rightBump = new TouchSensor(SensorPort.S3);
FeatureDetector detector3 = new TouchFeatureDetector(rightBump, -10, TOUCH_Y_OFFSET); 

FusorDetector fusion = new FusorDetector();
fusion.addDetector(detector1);
fusion.addDetector(detector2);
fusion.addDetector(detector3);

fusion.addListener(myFeatureListener);

Back to top