lejos.hardware.sensor.MindsensorsLineLeader Maven / Gradle / Ivy
Show all versions of lejos-ev3-api Show documentation
package lejos.hardware.sensor;
import lejos.hardware.port.I2CPort;
import lejos.hardware.port.Port;
/**
* MindSensors Line Follower Sensor
* This sensor an array of 8 sensors with controlled light source, returning you values of the sensor readings.
*
*
* 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
*
*
* Red
* Measures the light value when illuminated with a red light source.
* N/A, normalized
* {@link #getRedMode() }
*
*
*
*
*
* Sensor configuration
* The sensor can be calibrated for black and white using the calibrateWhite and calibrateBlack methods.
* The sensor can be put in and out of sleep mode (lights off) using the sleep method and wakeUp methods.
*
*
*
* See Sensor datasheet
* See Sensor Product page
* See The
* leJOS sensor framework
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*
*
*
*
* @author Juan Antonio Brenha Moral, Eric Pascual (EP)
*
*/
public class MindsensorsLineLeader extends I2CSensor {
private byte[] buf = new byte[8];
private final static byte COMMAND = 0x41;
/**
* Constructor
*
* @param port
* @param address I2C address for the device
*/
public MindsensorsLineLeader(I2CPort port, int address) {
super(port, address);
init();
}
/**
* Constructor
*
* @param port
*/
public MindsensorsLineLeader(I2CPort port) {
this(port, DEFAULT_I2C_ADDRESS);
init();
}
/**
* Constructor
*
* @param port
* @param address I2C address for the device
*/
public MindsensorsLineLeader(Port port, int address) {
super(port, address, TYPE_LOWSPEED_9V);
init();
}
/**
* Constructor
*
* @param port
*/
public MindsensorsLineLeader(Port port) {
this(port, DEFAULT_I2C_ADDRESS);
init();
}
protected void init() {
setModes(new SensorMode[]{ new RedMode() });
}
/**
* Send a single byte command represented by a letter
*
* @param cmd the command to be sent
*/
public void sendCommand(char cmd) {
sendData(COMMAND, (byte) cmd);
}
/**
* Sleep the sensor
*/
public void sleep() {
sendCommand('D');
}
/**
* Wake up the sensor
*
*/
public void wakeUp() {
sendCommand('P');
}
public void calibrateWhite() {
sendCommand('W');
}
public void calibrateBlack() {
sendCommand('B');
}
/**
* Return a sample provider in that measures the light reflection of a surface illuminated with a red led light. The sensor houses 8 light sensors. Each element in the sample represents one light sensor.
*/
public SensorMode getRedMode() {
return getMode(0);
}
private class RedMode implements SensorMode {
@Override
public int sampleSize() {
return 8;
}
@Override
public void fetchSample(float[] sample, int offset) {
getData(0x49,buf,8);
for(int i=0;i<8;i++) {
sample[offset+i] = buf[i]/100;
}
}
@Override
public String getName() {
return "Red";
}
}
}