lejos.hardware.sensor.DexterLaserSensor 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;
/**
*
* This class represents a Dexter Industries Laser Sensor. The sensor contains a laser and a photodiode to read ambient light values. This sensor can be calibrated to low and high values.
*
*
*
* The Dexter Industries laser can turn on and off very rapidly, with the following characteristics:
*
* it takes about 8-10 ms to turn on and reach full power it takes about 5 ms to turn off
*
*/
/**
* Dexter laser sensor
* The sensor contains a laser and a photodiode to read ambient light values.
* The Dexter Industries laser can turn on and off very rapidly, with the following characteristics:
*
* it takes about 8-10 ms to turn on and reach full power it takes about 5 ms to turn off
*
*
*
* The code for this sensor has not been tested. Please report test results to
* the leJOS forum.
*
*
*
*
*
* Supported modes
*
*
* Mode name
* Description
* unit(s)
* Getter
*
*
* Ambient
* Measures light level with the laser off
* N/A
* {@link #getAmbientMode() }
*
*
* Laser
* Measures light level with the laser on
* N/A
* {@link #getLaserMode() }
*
*
*
*
*
*
*
* See The
* leJOS sensor framework
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*
*
*
*
* @author ?
*
*/public class DexterLaserSensor extends AnalogSensor implements SensorConstants {
protected static final long SWITCH_DELAY = 10;
private boolean laser = false;
/**
* Create a laser sensor object attached to the specified port, and sets the laser on or off.
*
* @param port
* an already open analog port
*/
public DexterLaserSensor(AnalogPort port) {
super(port);
init();
}
/**
* Create a laser sensor object attached to the specified port, and sets the laser on or off.
*
* @param port
* port, e.g. Port.S1
*/
public DexterLaserSensor(Port port) {
super(port);
init();
}
protected void init() {
setLaser(laser);
setModes(new SensorMode[] { new Laser(false,"Ambient"), new Laser(true, "laser") });
}
public void setLaser(boolean laserState) {
switchType(laserState ? TYPE_LIGHT_ACTIVE : TYPE_LIGHT_INACTIVE, SWITCH_DELAY);
this.laser = laserState;
}
/**
* Get a sample provider that returns samples with the laser turned off.
*
* @return the sensor mode
*/
/**
* Dexter laser sensor, Ambient mode
* Measures light level with the laser off
*
*
* Size and content of the sample
* The sample contains one elements representing normalised (range 0-1) light level.
*
*
*
* @return A sampleProvider
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*/
public SensorMode getAmbientMode() {
return getMode(0);
}
/**
* Dexter laser sensor, Laser mode
* Measures light level with the laser on
*
*
* Size and content of the sample
* The sample contains one elements representing normalised (range 0-1) light level.
*
*
*
* @return A sampleProvider
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*/ public SensorMode getLaserMode() {
return getMode(1);
}
private class Laser implements SensorMode {
private boolean state;
private String name;
private Laser(boolean state, String name) {
this.state = state;
this.name = name;
}
@Override
public int sampleSize() {
return 1;
}
@Override
public void fetchSample(float[] sample, int offset) {
setLaser(state);
sample[offset] = 1.0f - normalize(port.getPin1());
}
@Override
public String getName() {
return name;
}
}
}