lejos.nxt.addon
Class GyroSensor

java.lang.Object
  extended by lejos.nxt.addon.GyroSensor
All Implemented Interfaces:
SensorConstants, Gyroscope

public class GyroSensor
extends Object
implements SensorConstants, Gyroscope

Support the HiTechnic Gyro sensor. Provides raw int (with optional offset) and float angular velocity in degrees/sec.

Notes:

Assumptions:

Author:
Lawrie Griffiths, Kirk Thompson

Field Summary
protected  ADSensorPort port
          The ADSensorPort passed in the constructor.
 
Fields inherited from interface lejos.nxt.SensorConstants
BLACK, BLANK_INDEX, BLUE, BLUE_INDEX, GREEN, GREEN_INDEX, MAX_AD_RAW, MAX_TYPE, MIN_TYPE, MODE_ANGLESTEP, MODE_BOOLEAN, MODE_CELSIUS, MODE_FARENHEIT, MODE_PCTFULLSCALE, MODE_PERIODCOUNTER, MODE_RAW, MODE_TRANSITIONCNT, RED, RED_INDEX, TYPE_ANGLE, TYPE_COLORBLUE, TYPE_COLORFULL, TYPE_COLORGREEN, TYPE_COLORNONE, TYPE_COLORRED, TYPE_CUSTOM, TYPE_HISPEED, TYPE_LIGHT_ACTIVE, TYPE_LIGHT_INACTIVE, TYPE_LOWSPEED, TYPE_LOWSPEED_9V, TYPE_NO_SENSOR, TYPE_REFLECTION, TYPE_SOUND_DB, TYPE_SOUND_DBA, TYPE_SWITCH, TYPE_TEMPERATURE, WHITE, YELLOW
 
Constructor Summary
GyroSensor(ADSensorPort port)
          Creates and initializes a new GyroSensor bound to passed ADSensorPort.
GyroSensor(ADSensorPort port, int offset)
          Creates and initializes a new GyroSensor bound to passed ADSensorPort and sets the offset to be used in readValue().
 
Method Summary
 float getAngularVelocity()
          Calculate and return the current angular velocity.
 int readValue()
          Read the gyro raw value and return with offset applied.
 void recalibrateOffset()
          Samples the stationary (make sure it is) Gyro Sensor to determine the offset.
 void recalibrateOffsetAlt()
          This function sets a suitable initial gyro offset.
 void setOffset(int offset)
          Set the offset used by readValue().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

port

protected ADSensorPort port
The ADSensorPort passed in the constructor.

Constructor Detail

GyroSensor

public GyroSensor(ADSensorPort port)
Creates and initializes a new GyroSensor bound to passed ADSensorPort.

Parameters:
port - The SensorPort the Gyro is connected to
See Also:
SensorPort

GyroSensor

public GyroSensor(ADSensorPort port,
                  int offset)
Creates and initializes a new GyroSensor bound to passed ADSensorPort and sets the offset to be used in readValue().

Parameters:
port - The SensorPort the Gyro is connected to
offset - The offset to apply to readValue()
See Also:
SensorPort
Method Detail

readValue

public int readValue()
Read the gyro raw value and return with offset applied. Set offset to zero to return raw value.

Returns:
gyro value
See Also:
setOffset(int), getAngularVelocity()

setOffset

public void setOffset(int offset)
Set the offset used by readValue(). Default at instantiation is zero.

Parameters:
offset - The int offset value
See Also:
readValue()

getAngularVelocity

public float getAngularVelocity()
Calculate and return the current angular velocity. When integrating for a heading, values less than 1.0 can be ignored to minimize perceived drift since the resolution of the Gyroscope sensor is 1 deg/sec.

Be sure to call recalibrateOffset() to establish the offset before using this method.

Specified by:
getAngularVelocity in interface Gyroscope
Returns:
The current angular velocity in degrees/sec
See Also:
recalibrateOffset()

recalibrateOffset

public void recalibrateOffset()
Samples the stationary (make sure it is) Gyro Sensor to determine the offset. Will reset the offset for ReadValue() to 0 (zero). Takes 5 seconds.

Specified by:
recalibrateOffset in interface Gyroscope
See Also:
setOffset(int)

recalibrateOffsetAlt

public void recalibrateOffsetAlt()
This function sets a suitable initial gyro offset. It takes 100 gyro samples over a time of 1/2 second and averages them to get the offset. It also check the max and min during that time and if the difference is larger than one it rejects the data and gets another set of samples.