lejos.hardware.sensor.HiTechnicGyro 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;
/**
* HiTechnic NXT Gyro Sensor
* The NXT Gyro Sensor contains a single axis gyroscopic sensor that detects rotation.
*
*
* 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
*
*
* Rate
* The Rate mode measures the angular speed of the sensor over a single axis
* Degrees/second
* {@link #getRateMode() }
*
*
*
*
*
*
* See Sensor Product page
* See The
* leJOS sensor framework
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*
*
*
*
* @author Lawrie Griffiths
*
*/
public class HiTechnicGyro extends AnalogSensor implements SensorConstants {
private static final float TO_SI=-1;
private float zero = 600f;
/**
* Supports the SampleProvider interface.
* The sensor measures the angular velocity in degrees per second.
* A positive rate indicates a counterclockwise rotation. A negative rate indicates a clockwise rotation.
*
* @param port the Analog port
*/
public HiTechnicGyro(AnalogPort port) {
super(port);
init();
}
/**
* Supports the SampleProvider interface.
* The sensor measures the angular velocity in degrees per second.
* A positive rate indicates a counterclockwise rotation. A negative rate indicates a clockwise rotation.
*
* @param port the Sensor port
*/
public HiTechnicGyro(Port port) {
super(port);
init();
}
protected void init() {
setModes(new SensorMode[]{ new RateMode() });
port.setTypeAndMode(TYPE_CUSTOM, MODE_RAW);
}
/**
* HiTechnic Gyro sensor, Rate mode
* The Rate mode measures the angular speed of the sensor over three axes
*
*
* Size and content of the sample
* The sample contains one element giving the angular speed (in degrees/second) of the sensor over its vertical axis (Z-axis).
*
*
* */
public SensorMode getRateMode() {
return getMode(0);
}
private class RateMode implements SensorMode {
@Override
public int sampleSize() {
return 1;
}
@Override
public void fetchSample(float[] sample, int offset) {
sample[offset] = ((float) NXTRawValue(port.getPin1()) - zero) * TO_SI;
}
@Override
public String getName() {
return "Rate";
}
}
}