All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lejos.hardware.sensor.HiTechnicBarometer Maven / Gradle / Ivy

Go to download

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.hardware.sensor;

import lejos.hardware.port.I2CPort;
import lejos.hardware.port.Port;
import lejos.utility.EndianTools;

/**
 * Hitechnic Barometric sensor
* The sensor measures both atmospheric pressure and temperature. * *

* The code for this sensor has not been tested. Please report test results to * the leJOS forum. *

* *

*

* * * * * * * * * * * * * * * * * * * * * *
Supported modes
Mode nameDescriptionunit(s)Getter
PressureMeasures atmospheric pressurePascal {@link #getPressureMode() }
TemperatureMeasures temperatureDegree Celcius {@link #getTemperatureMode() }
* * *

* Sensor configuration
* The sensor can be calibrated for pressure using the calibrate method. *

* * See * Sensor Product page * See The * leJOS sensor framework * See {@link lejos.robotics.SampleProvider leJOS conventions for * SampleProviders} * *

* * * @author Matthias Paul Scholz * */ public class HiTechnicBarometer extends I2CSensor { private static final int REG_TEMPERATURE = 0x42; private static final int REG_PRESSURE = 0x44; private static final int REG_PRESSURE_CALIBRATION = 0x46; // Wikipedia: 1 inHg at 0 °C = 3386.389 Pa private static final float MINHG_TO_PA = 3.38638866667f; // Wikipedia: Standard Atmosphere is 101325 Pa private static final float STANDARD_ATMOSPHERIC_PRESSURE = 101325f; private final byte[] buffer = new byte[2]; /** * Constructor. * * @param port * the {@link I2CPort} the sensor is connected to. */ public HiTechnicBarometer(final I2CPort port) { super(port, DEFAULT_I2C_ADDRESS); } /** * Constructor. * * @param port * the {@link I2CPort} the sensor is connected to. * @param address * the address */ public HiTechnicBarometer(final I2CPort port, final int address) { super(port, address); init(); } public HiTechnicBarometer(final Port port, final int address) { super(port, address, TYPE_LOWSPEED); init(); } public HiTechnicBarometer(final Port port) { this(port, DEFAULT_I2C_ADDRESS); init(); } protected void init() { setModes(new SensorMode[] { new PressureMode(), new TemperatureMode() }); if (getCalibrationMetric() == 0) calibrate(STANDARD_ATMOSPHERIC_PRESSURE); } /** * Re-calibrates the sensor. * * @param pascals * the calibration value in pascals */ public void calibrate(float pascals) { // compute calibration value in 1/1000 inHg int calibrationImperial = (int)(0.5f + pascals / MINHG_TO_PA); EndianTools.encodeShortBE(calibrationImperial, buffer, 0); sendData(REG_PRESSURE_CALIBRATION, buffer, 2); } /** * @return the present calibration value in pascals. Will be 0 in case no * explicit calibration has been performed. */ public float getCalibrationMetric() { // get calibration value in 1/1000 inHg getData(REG_PRESSURE_CALIBRATION, buffer, 2); int result = EndianTools.decodeUShortBE(buffer, 0); return result * MINHG_TO_PA; } /** * HiTechnic Barometer, Pressure mode
* Measures the atmospheric pressure of the air. * *

* Size and content of the sample
* The sample contains one element containing the atmospheric pressure (in Pascal) of the air. */ public SensorMode getPressureMode() { return getMode(0); } private class PressureMode implements SensorMode { @Override public int sampleSize() { return 1; } @Override public String getName() { return "Pressure"; } @Override public void fetchSample(float[] sample, int offset) { // get pressure in 1/1000 inHg getData(REG_PRESSURE, buffer, 2); int result = EndianTools.decodeUShortBE(buffer, 0); sample[0] = result * MINHG_TO_PA; } } /** * HiTechnic Barometer, Temperature mode
* Measures the temperature of the air. * *

* Size and content of the sample
* The sample contains one element containing the air temperature (in degree celcius). */ public SensorMode getTemperatureMode() { return getMode(1); } private class TemperatureMode implements SensorMode { @Override public int sampleSize() { return 1; } @Override public void fetchSample(float[] sample, int offset) { // get temperature in 1/10 °C getData(REG_TEMPERATURE, buffer, 2); int result = EndianTools.decodeShortBE(buffer, 0); sample[offset] = result / 10f + 273.15f; } @Override public String getName() { return "Temperature"; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy