lejos.hardware.sensor.DexterPressureSensor500 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;
import lejos.robotics.SampleProvider;
/**
* Dexter Industries DPressure500
* Pressure sensor for LEGO® MINDSTORMS® EV3 and NXT.
*
*
* 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
*
*
* Pressure
* Measures the pressure
* Pascal
* {@link #getPressureMode() }
*
*
*
*
*
*
* See Sensor datasheet
* See Sensor Product page
* See The
* leJOS sensor framework
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*
*
*
*
* @author Lawrie Griffiths
*
*/public class DexterPressureSensor500 extends AnalogSensor implements SensorConstants {
/*
*
* Formula from DPRESS-driver.h:
* vRef = 4.85
* vOut = rawValue * vRef / 1023
* result = (vOut / vRef - CAL1) / CAL2
*/
private static final double CAL1 = 0.04;
private static final double CAL2 = 0.0018;
/*
* Optimized:
* result = rawValue * DPRESS_MULT - DPRESS_OFFSET;
*/
private static final float DPRESS_MULT = (float)(1.0 / CAL2 );
private static final float DPRESS_OFFSET = (float)(CAL1 / CAL2);
public DexterPressureSensor500(AnalogPort port) {
super(port);
init();
}
public DexterPressureSensor500(Port port) {
super(port);
init();
}
protected void init() {
setModes(new SensorMode[]{ new PressureMode() });
port.setTypeAndMode(TYPE_CUSTOM, MODE_RAW);
}
/**
* Dexter Industries DPressure500, Pressure mode
* Measures the pressure
*
*
* Size and content of the sample
* The sample contains one element containing the pressure (in PA) measured by the sensor.
*
*
*
* @return A sampleProvider
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
* See Sensor datasheet
*/
public SampleProvider getPressureMode() {
return getMode(0);
}
private class PressureMode implements SensorMode {
@Override
public int sampleSize() {
return 1;
}
@Override
public void fetchSample(float[] sample, int offset) {
sample[offset] = (NXTRawValue(normalize(port.getPin1())) * DPRESS_MULT - DPRESS_OFFSET)* 1000f; // in pascals
}
@Override
public String getName() {
return "Pressure";
}
}
}