lejos.hardware.sensor.HiTechnicEOPD Maven / Gradle / Ivy
Show all versions of lejos-ev3-api Show documentation
package lejos.hardware.sensor;
import lejos.hardware.port.AnalogPort;
import lejos.hardware.port.Port;
/**
* HiTechnic EOPD Sensor
* The EOPD or Electro Optical Proximity Detector uses an internal light source to detect the presence of a target or determine changes in distance to a target.
*
*
*
*
* Supported modes
*
*
* Mode name
* Description
* unit(s)
* Getter
*
*
* Long Distance
* Measures the relative distance to an object
* N/A, a normalized value that represents the relative distance to an object. 0 = minimum range, 1 = maximum range.
* {@link #getLongDistanceMode() }
*
*
* Short Distance
* Measures the relative distance to an object
* N/A, a normalized value that represents the relative distance to an object. 0 = minimum range, 1 = maximum range.
* {@link #getShortDistanceMode() }
*
*
*
*
*
* See Sensor Product page
* See The
* leJOS sensor framework
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*
*
*
*
* @author Michael Smith
*
*/
public class HiTechnicEOPD extends AnalogSensor implements SensorConstants {
protected static final long SWITCH_DELAY = 10;
/**
* @param port NXT sensor port 1-4
*/
public HiTechnicEOPD (AnalogPort port){
super(port);
init();
}
/**
* @param port NXT sensor port 1-4
*/
public HiTechnicEOPD (Port port){
super(port);
init();
}
protected void init() {
setModes(new SensorMode[]{ new LongDistanceMode(), new ShortDistanceMode() });
}
/**
* HiTechnic EOPD sensor, Long distance mode
* Measures the relative distance to a surface.
*
*
* Size and content of the sample
* The sample contains one element containing the relative distance to a surface in the range of 0 to 1.
* Where 0 corresponds to the minimum of the measurement range and 1 corresponds to the maximum of the measyurement range.
* The measurement range depends on the color and reflectivity of the measured surface. The measurement is more or less linear to the distance for a given surface.
*/
public SensorMode getLongDistanceMode() {
return getMode(0);
}
private class LongDistanceMode implements SensorMode {
@Override
public int sampleSize() {
return 1;
}
@Override
public void fetchSample(float[] sample, int offset) {
switchType(TYPE_LIGHT_INACTIVE, SWITCH_DELAY);
sample[offset] = (float) Math.sqrt((normalize(port.getPin1())));
}
@Override
public String getName() {
return "Long distance";
}
}
/**
* HiTechnic EOPD sensor, Short distance mode
* Measures the relative distance to a surface. This mode is suited for white objects at short distance.
*
*
* Size and content of the sample
* The sample contains one element containing the relative distance to a surface in the range of 0 to 1.
* Where 0 corresponds to the minimum of the measurement range and 1 corresponds to the maximum of the measyurement range.
* The measurement range depends on the color and reflectivity of the measured surface. The measurement is more or less linear to the distance for a given surface.
*/ public SensorMode getShortDistanceMode() {
return getMode(1);
}
public class ShortDistanceMode implements SensorMode {
@Override
public int sampleSize() {
return 1;
}
@Override
public void fetchSample(float[] sample, int offset) {
switchType(TYPE_LIGHT_ACTIVE, SWITCH_DELAY);
sample[offset] = (float) Math.sqrt((normalize(port.getPin1())));
}
@Override
public String getName() {
return "Short distance";
}
}
}