lejos.hardware.sensor.HiTechnicMagneticSensor 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;
/**
*
* The sensor detects magnetic fields that are present around the front of the sensor in a vertical orientation.
*
*
* 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
*
*
* Magnetic
* Measures the strength of a vertical magnetic field
* N/A, normalized
* {@link #getMagneticMode() }
*
*
*
*
*
*
* See Sensor Product page
* See The
* leJOS sensor framework
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*
*
*
*
* @author Lawrie Griffiths
*
*/public class HiTechnicMagneticSensor extends AnalogSensor implements SensorConstants {;
/**
* Create a magnetic sensor on an analog port
*
* @param port the analog port
*/
public HiTechnicMagneticSensor(AnalogPort port) {
super(port);
init();
}
/**
* Create a magnetic sensor on a sensor port
*
* @param port the analog port
*/
public HiTechnicMagneticSensor(Port port) {
super(port);
init();
}
protected void init() {
setModes(new SensorMode[]{ new MagneticMode() });
port.setTypeAndMode(TYPE_CUSTOM, MODE_RAW);
}
/**
* HiTechnic Magnetic sensor, Magnetic mode
* Measures the strength og the vertical magnetic field
*
*
* Size and content of the sample
* The sample contains one element containing the strength of the magnetic field around the sensor.
* The value is normalized (0-1) where 1 corresponds with the maximum field strength.
*/
public SampleProvider getMagneticMode() {
return getMode(0);
}
private class MagneticMode implements SensorMode {
@Override
public int sampleSize() {
return 1;
}
@Override
public void fetchSample(float[] sample, int offset) {
sample[offset] = normalize(port.getPin1());
}
@Override
public String getName() {
return "Magnetic";
}
}
}