lejos.hardware.sensor.EV3GyroSensor Maven / Gradle / Ivy
Show all versions of lejos-ev3-api Show documentation
package lejos.hardware.sensor;
import lejos.hardware.port.Port;
import lejos.hardware.port.UARTPort;
import lejos.robotics.SampleProvider;
/**
* EV3 Gyro sensor
* The digital EV3 Gyro Sensor measures the sensors rotational motion and changes in its orientation.
*
*
*
*
*
* Supported modes
*
*
* Mode name
* Description
* unit(s)
* Getter
*
*
* Angle
* Measures the orientation of the sensor
* Degrees
* {@link #getAngleMode() }
*
*
* Rate
* Measures the angular velocity of the sensor
* Degrees / second
* {@link #getRateMode() }
*
*
* Rate and Angle
* Measures both angle and angular velocity
* Degrees, Degrees / second
* {@link #getAngleAndRateMode() }
*
*
*
*
*
* Sensor configuration
* Use {@link #reset()} to recalibrate the sensor and to reset accumulated angle to zero. Keep the sensor motionless during a reset.
* The sensor shuld also be motionless during initialization.
*
*
*
* See Sensor Product page
* See The
* leJOS sensor framework
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*
*
*
*
* @author Andy, Aswin Bouwmeester
*
*/
public class EV3GyroSensor extends UARTSensor {
private static final long SWITCHDELAY = 200;
private short[] raw=new short[2];
public EV3GyroSensor(Port port) {
super(port, 3);
setModes(new SensorMode[] { new RateMode(), new AngleMode(), new RateAndAngleMode() });
}
public EV3GyroSensor(UARTPort port) {
super(port, 3);
setModes(new SensorMode[] { new RateMode(), new AngleMode(), new RateAndAngleMode() });
}
/**
* EV3 Gyro sensor, Angle mode
* Measures the orientation of the sensor in respect to its start orientation.
*
*
* Size and content of the sample
* The sample contains one elements representing the orientation (in Degrees) of the sensor in respect to its start position.
*
*
* Configuration
* The start position can be set to the current position using the reset method of the sensor.
*
* @return A sampleProvider
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*/
public SampleProvider getAngleMode() {
return getMode(1);
}
/**
* EV3 Gyro sensor, Rate mode
* Measures angular velocity of the sensor.
*
*
* Size and content of the sample
* The sample contains one elements representing the angular velocity (in Degrees / second) of the sensor.
*
*
* Configuration
* The sensor can be recalibrated using the reset method of the sensor.
*
* @return A sampleProvider
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*/
public SampleProvider getRateMode() {
return getMode(0);
}
/**
* EV3 Gyro sensor, Rate mode
* Measures both angle and angular velocity of the sensor.
*
*
* Size and content of the sample
* The sample contains two elements. The first element contains angular velocity (in degrees / second). The second element contain angle (in degrees).
*
*
* Configuration
* The sensor can be recalibrated using the reset method of the sensor.
*
* @return A sampleProvider
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*/
public SampleProvider getAngleAndRateMode() {
return getMode(2);
}
/**
* Hardware calibration of the Gyro sensor and reset off accumulated angle to zero.
* The sensor should be motionless during calibration.
*/
public void reset() {
// Reset mode (4) is not used here as it behaves eratically. Instead the reset is done implicitly by going to mode 1.
switchMode(1, SWITCHDELAY);
// And back to 3 to prevent another reset when fetching the next sample
switchMode(3, SWITCHDELAY);
}
private class AngleMode implements SampleProvider, SensorMode {
private static final int MODE = 3;
private static final float toSI = -1;
@Override
public int sampleSize() {
return 1;
}
@Override
public void fetchSample(float[] sample, int offset) {
switchMode(MODE, SWITCHDELAY);
port.getShorts(raw, 0, raw.length);
sample[offset] = raw[0] * toSI;
}
@Override
public String getName() {
return "Angle";
}
}
private class RateMode implements SampleProvider, SensorMode {
private static final int MODE = 3;
private static final float toSI = -1;
@Override
public int sampleSize() {
return 1;
}
@Override
public void fetchSample(float[] sample, int offset) {
switchMode(MODE, SWITCHDELAY);
port.getShorts(raw, 0, raw.length);
sample[offset] = raw[1] * toSI;
}
@Override
public String getName() {
return "Rate";
}
}
private class RateAndAngleMode implements SampleProvider, SensorMode {
private static final int MODE = 3;
private static final float toSI = -1;
@Override
public int sampleSize() {
return 2;
}
@Override
public void fetchSample(float[] sample, int offset) {
switchMode(MODE, SWITCHDELAY);
port.getShorts(raw, 0, raw.length);
for (int i=0;i