Gyro

class wpilib.Gyro(channel)[source]

Bases: wpilib.SensorBase

Interface to a gyro device via an AnalogInput

Use a rate gyro to return the robots heading relative to a starting position. The Gyro class tracks the robots heading based on the starting position. As the robot rotates the new heading is computed by integrating the rate of rotation returned by the sensor. When the class is instantiated, it does a short calibration routine where it samples the gyro while at rest to determine the default offset. This is subtracted from each sample to determine the heading.

Gyro constructor.

Also initializes the gyro. Calibrate the gyro by running for a number of samples and computing the center value. Then use the center value as the Accumulator center value for subsequent measurements. It’s important to make sure that the robot is not moving while the centering calculations are in progress, this is typically done when the robot is first turned on while it’s sitting at rest before the competition starts.

Parameters:channel – The analog channel index or AnalogInput object that the gyro is connected to. Gyros can only be used on on-board channels 0-1.
free()[source]

Delete (free) the accumulator and the analog components used for the gyro.

getAngle()[source]

Return the actual angle in degrees that the robot is currently facing.

The angle is based on the current accumulator value corrected by the oversampling rate, the gyro type and the A/D calibration values. The angle is continuous, that is it will continue from 360 to 361 degrees. This allows algorithms that wouldn’t want to see a discontinuity in the gyro output as it sweeps past from 360 to 0 on the second time around.

Returns:The current heading of the robot in degrees. This heading is based on integration of the returned rate from the gyro.
Return type:float
getRate()[source]

Return the rate of rotation of the gyro

The rate is based on the most recent reading of the gyro analog value

Returns:the current rate in degrees per second
Return type:float
kAverageBits = 0
kCalibrationSampleTime = 5.0
kDefaultVoltsPerDegreePerSecond = 0.007
kOversampleBits = 10
kSamplesPerSecond = 50.0
pidGet()[source]

Get the output of the gyro for use with PIDControllers

Returns:the current angle according to the gyro
Return type:float
reset()[source]

Reset the gyro. Resets the gyro to a heading of zero. This can be used if there is significant drift in the gyro and it needs to be recalibrated after it has been running.

setDeadband(volts)[source]

Set the size of the neutral zone. Any voltage from the gyro less than this amount from the center is considered stationary. Setting a deadband will decrease the amount of drift when the gyro isn’t rotating, but will make it less accurate.

Parameters:volts (float) – The size of the deadband in volts
setPIDSourceParameter(pidSource)[source]

Set which parameter of the gyro you are using as a process control variable. The Gyro class supports the rate and angle parameters.

Parameters:pidSource (PIDSource.PIDSourceParameter) – An enum to select the parameter.
setSensitivity(voltsPerDegreePerSecond)[source]

Set the gyro sensitivity. This takes the number of volts/degree/second sensitivity of the gyro and uses it in subsequent calculations to allow the code to work with multiple gyros. This value is typically found in the gyro datasheet.

Parameters:voltsPerDegreePerSecond (float) – The sensitivity in Volts/degree/second