lejos.robotics.objectdetection.FusorDetector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lejos-ev3-api Show documentation
Show all versions of lejos-ev3-api Show documentation
leJOS (pronounced like the Spanish word "lejos" for "far") is a tiny Java Virtual Machine. In 2013 it was ported to the LEGO EV3 brick.
The newest version!
package lejos.robotics.objectdetection;
import java.util.ArrayList;
import lejos.robotics.RangeReading;
import lejos.robotics.RangeReadings;
/**
* If you have a robot with multiple sensors (touch and range) and would like them to report to one
* listener, or if you want to control them at the same time (such as disabling them all at once) you can
* use this class.
*
* This class maintains its own thread for checking the FeatureDetectors.
*
* @author BB
*
*/
public class FusorDetector implements FeatureDetector, FeatureListener {
// TODO: Make inner FeatureListener
private ArrayList detectors = null;
private ArrayList readings = null;
/**
* The delay (in ms) between notifying listeners of detected features (if any are detected).
*/
private int delay;
private boolean enabled = true;
private ArrayList listeners = null;
public FusorDetector() {
detectors = new ArrayList();
readings = new ArrayList();
Thread x = new NotifyThread();
x.setDaemon(true);
x.start();
}
/**
* This method adds another FeatureDetector to the FusorDetector. This method will set the delay
* for this class to the largest delay if comes across from all the FeatureDetector objects added
* to it.
* @param detector
*/
public void addDetector(FeatureDetector detector) {
// Add this class as a FeatureListener:
detector.addListener(this);
// TODO: Does ArrayList already check if redundant objects are added?
if(!detectors.contains(detector)) detectors.add(detector);
// Set delay to the largest delay it comes across:
if(detector.getDelay() > this.delay)
this.delay = detector.getDelay();
readings.add(new RangeReadings(0)); // Add dummy object to expand size of RangeReadings
}
/**
* This method scans all the sensors added to this object and returns the amalgamated results.
* NOTE: This method is not called by the thread code.
*/
public Feature scan() {
RangeReadings rr = new RangeReadings(0);
for(FeatureDetector d : detectors) {
Feature df = d.scan();
if(df != null) {
RangeReadings temp = df.getRangeReadings();
for(int i=0;i 0)
notifyListeners(new RangeFeature(rrs));
// 4. Now clear out the detectors so they aren't resent next loop.
readings.clear();
// 5. And add dummies to it to make it proper size.
for(int i=0;i();
this.listeners.add(listener);
}
/**
* This method enables/disables automatic scanning and listener reporting for this object and
* all FeatureDetectors used in this FusorDetector object.
*/
public void enableDetection(boolean on) {
enabled = on;
for(FeatureDetector fd : detectors) {
fd.enableDetection(on);
}
}
public int getDelay() {
return delay;
}
public boolean isEnabled() {
return enabled;
}
public void setDelay(int delay) {
this.delay = delay;
}
}