001/*----------------------------------------------------------------------------*/ 002/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */ 003/* Open Source Software - may be modified and shared by FRC teams. The code */ 004/* must be accompanied by the FIRST BSD license file in the root directory of */ 005/* the project. */ 006/*----------------------------------------------------------------------------*/ 007 008package edu.wpi.first.wpilibj; 009 010import edu.wpi.first.wpilibj.interfaces.Gyro; 011import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder; 012 013/** 014 * GyroBase is the common base class for Gyro implementations such as AnalogGyro. 015 */ 016public abstract class GyroBase extends SensorBase implements Gyro, PIDSource, Sendable { 017 private PIDSourceType m_pidSource = PIDSourceType.kDisplacement; 018 019 /** 020 * Set which parameter of the gyro you are using as a process control variable. The Gyro class 021 * supports the rate and displacement parameters 022 * 023 * @param pidSource An enum to select the parameter. 024 */ 025 @Override 026 public void setPIDSourceType(PIDSourceType pidSource) { 027 m_pidSource = pidSource; 028 } 029 030 @Override 031 public PIDSourceType getPIDSourceType() { 032 return m_pidSource; 033 } 034 035 /** 036 * Get the output of the gyro for use with PIDControllers. May be the angle or rate depending on 037 * the set PIDSourceType 038 * 039 * @return the output according to the gyro 040 */ 041 @Override 042 public double pidGet() { 043 switch (m_pidSource) { 044 case kRate: 045 return getRate(); 046 case kDisplacement: 047 return getAngle(); 048 default: 049 return 0.0; 050 } 051 } 052 053 @Override 054 public void initSendable(SendableBuilder builder) { 055 builder.setSmartDashboardType("Gyro"); 056 builder.addDoubleProperty("Value", this::getAngle, null); 057 } 058}