| 1 | package org.usfirst.frc.team3501.robot.subsystems; |
| 2 | |
| 3 | import org.usfirst.frc.team3501.robot.Constants; |
| 4 | import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive; |
| 5 | import org.usfirst.frc.team3501.robot.utils.PIDController; |
| 6 | |
| 7 | import com.ctre.CANTalon; |
| 8 | |
| 9 | import edu.wpi.first.wpilibj.ADXRS450_Gyro; |
| 10 | import edu.wpi.first.wpilibj.DoubleSolenoid; |
| 11 | import edu.wpi.first.wpilibj.DoubleSolenoid.Value; |
| 12 | import edu.wpi.first.wpilibj.Encoder; |
| 13 | import edu.wpi.first.wpilibj.RobotDrive; |
| 14 | import edu.wpi.first.wpilibj.command.Subsystem; |
| 15 | |
| 16 | public class DriveTrain extends Subsystem { |
| 17 | public static double driveP, driveI, driveD; |
| 18 | public static double turnP, turnI, turnD; |
| 19 | public static double driveStraightGyroP = 0.01; |
| 20 | |
| 21 | public static final String DRIVE_P_Val = "DriveP"; |
| 22 | public static final String DRIVE_I_Val = "DriveI"; |
| 23 | public static final String DRIVE_D_Val = "DriveD"; |
| 24 | public static final String DRIVE_TARGET_DIST = "SET_DIST"; |
| 25 | public static final String DRIVE_MOTOR_VAL = "SPEED"; |
| 26 | public static final String GYRO_P_Val = "GyroP"; |
| 27 | public static final String GYRO_I_Val = "GyroI"; |
| 28 | public static final String GYRO_D_Val = "GyroD"; |
| 29 | public static final String GYRO_TARGET_ANGLE = "SET_ANGLE"; |
| 30 | public static final int PID_ERROR = -1; |
| 31 | public static final int TARGET_DISTANCE_ERROR = -1; |
| 32 | |
| 33 | public static final double WHEEL_DIAMETER = 6; // inches |
| 34 | public static final int ENCODER_PULSES_PER_REVOLUTION = 256; |
| 35 | public static final double INCHES_PER_PULSE = WHEEL_DIAMETER * Math.PI |
| 36 | / ENCODER_PULSES_PER_REVOLUTION; |
| 37 | |
| 38 | public static final double MAINTAIN_CLIMBED_POSITION = 0; |
| 39 | public static final double TIME_TO_CLIMB_FOR = 0; |
| 40 | public static final double CLIMBER_SPEED = 0; |
| 41 | |
| 42 | private static DriveTrain driveTrain; |
| 43 | |
| 44 | private final CANTalon frontLeft, frontRight, rearLeft, rearRight; |
| 45 | private final RobotDrive robotDrive; |
| 46 | private final Encoder leftEncoder, rightEncoder; |
| 47 | private final DoubleSolenoid leftGearPiston, rightGearPiston; |
| 48 | |
| 49 | private ADXRS450_Gyro imu; |
| 50 | |
| 51 | public boolean shouldBeClimbing = false; |
| 52 | |
| 53 | private PIDController driveController; |
| 54 | private PIDController gyroController; |
| 55 | |
| 56 | private boolean isClimbing; |
| 57 | |
| 58 | private DriveTrain() { |
| 59 | |
| 60 | // PID TUNING |
| 61 | driveController = new PIDController(driveP, driveI, driveD); |
| 62 | gyroController = new PIDController(turnP, turnI, turnD); |
| 63 | |
| 64 | // PID TUNING |
| 65 | |
| 66 | public static final String DRIVE_P_Val = "DriveP"; |
| 67 | public static final String DRIVE_I_Val = "DriveI"; |
| 68 | public static final String DRIVE_D_Val = "DriveD"; |
| 69 | public static final String DRIVE_TARGET_DIST = "SET_DIST"; |
| 70 | public static final String DRIVE_MOTOR_VAL = "SPEED"; |
| 71 | public static final String GYRO_P_Val = "GyroP"; |
| 72 | public static final String GYRO_I_Val = "GyroI"; |
| 73 | public static final String GYRO_D_Val = "GyroD"; |
| 74 | public static final String GYRO_TARGET_ANGLE = "SET_ANGLE"; |
| 75 | public static final int PID_ERROR = -1; |
| 76 | public static final int TARGET_DISTANCE_ERROR = -1; |
| 77 | |
| 78 | // MOTOR CONTROLLERS |
| 79 | frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT); |
| 80 | frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT); |
| 81 | rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT); |
| 82 | rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT); |
| 83 | |
| 84 | // ENCODERS |
| 85 | leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A, |
| 86 | Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X); |
| 87 | rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A, |
| 88 | Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X); |
| 89 | |
| 90 | leftEncoder.setDistancePerPulse(INCHES_PER_PULSE); |
| 91 | rightEncoder.setDistancePerPulse(INCHES_PER_PULSE); |
| 92 | |
| 93 | // ROBOT DRIVE |
| 94 | robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight); |
| 95 | |
| 96 | this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT); |
| 97 | |
| 98 | // TODO: Not sure if MODULE_NUMBER should be the same for both |
| 99 | leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE, |
| 100 | Constants.DriveTrain.LEFT_GEAR_PISTON_FORWARD, |
| 101 | Constants.DriveTrain.LEFT_GEAR_PISTON_REVERSE); |
| 102 | rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE, |
| 103 | Constants.DriveTrain.RIGHT_GEAR_PISTON_FORWARD, |
| 104 | Constants.DriveTrain.RIGHT_GEAR_PISTON_REVERSE); |
| 105 | } |
| 106 | |
| 107 | public PIDController getDriveController() { |
| 108 | return this.driveController; |
| 109 | } |
| 110 | |
| 111 | public PIDController getGyroController() { |
| 112 | return this.gyroController; |
| 113 | } |
| 114 | |
| 115 | public static DriveTrain getDriveTrain() { |
| 116 | if (driveTrain == null) { |
| 117 | driveTrain = new DriveTrain(); |
| 118 | } |
| 119 | return driveTrain; |
| 120 | } |
| 121 | |
| 122 | // DRIVE METHODS |
| 123 | public void setMotorValues(final double left, final double right) { |
| 124 | frontLeft.set(left); |
| 125 | rearLeft.set(left); |
| 126 | |
| 127 | frontRight.set(-right); |
| 128 | rearRight.set(-right); |
| 129 | } |
| 130 | |
| 131 | public void joystickDrive(final double thrust, final double twist) { |
| 132 | robotDrive.arcadeDrive(thrust, twist, true); |
| 133 | } |
| 134 | |
| 135 | public void stop() { |
| 136 | setMotorValues(0, 0); |
| 137 | } |
| 138 | |
| 139 | public double getLeftMotorVal() { |
| 140 | return (frontLeft.get() + rearLeft.get()) / 2; |
| 141 | } |
| 142 | |
| 143 | public double getRightMotorVal() { |
| 144 | return (frontRight.get() + rearRight.get()) / 2; |
| 145 | } |
| 146 | |
| 147 | // ENCODER METHODS |
| 148 | |
| 149 | public double getLeftEncoderDistance() { |
| 150 | return leftEncoder.getDistance(); |
| 151 | } |
| 152 | |
| 153 | public double getRightEncoderDistance() { |
| 154 | return rightEncoder.getDistance(); |
| 155 | } |
| 156 | |
| 157 | public void printEncoderOutput() { |
| 158 | System.out.println("left: " + getLeftEncoderDistance()); |
| 159 | System.out.println("right: " + getRightEncoderDistance()); |
| 160 | // System.out.println(getAvgEncoderDistance()); |
| 161 | } |
| 162 | |
| 163 | public double getAvgEncoderDistance() { |
| 164 | return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2; |
| 165 | } |
| 166 | |
| 167 | public void resetEncoders() { |
| 168 | leftEncoder.reset(); |
| 169 | rightEncoder.reset(); |
| 170 | } |
| 171 | |
| 172 | public double getLeftSpeed() { |
| 173 | return leftEncoder.getRate(); |
| 174 | } |
| 175 | |
| 176 | public double getRightSpeed() { |
| 177 | return rightEncoder.getRate(); |
| 178 | } |
| 179 | |
| 180 | public double getSpeed() { |
| 181 | return (getLeftSpeed() + getRightSpeed()) / 2.0; |
| 182 | } |
| 183 | |
| 184 | // ------Gyro------// |
| 185 | public double getAngle() { |
| 186 | return this.imu.getAngle(); |
| 187 | } |
| 188 | |
| 189 | public void resetGyro() { |
| 190 | this.imu.reset(); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * @return a value that is the current setpoint for the piston kReverse or |
| 195 | * KForward |
| 196 | */ |
| 197 | public Value getLeftGearPistonValue() { |
| 198 | return leftGearPiston.get(); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * @return a value that is the current setpoint for the piston kReverse or |
| 203 | * KForward |
| 204 | */ |
| 205 | public Value getRightGearPistonValue() { |
| 206 | return rightGearPiston.get(); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Changes the ball shift gear assembly to high |
| 211 | */ |
| 212 | public void setHighGear() { |
| 213 | changeGear(Constants.DriveTrain.HIGH_GEAR); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Changes the ball shift gear assembly to low |
| 218 | */ |
| 219 | public void setLowGear() { |
| 220 | changeGear(Constants.DriveTrain.LOW_GEAR); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Changes the gear to a DoubleSolenoid.Value |
| 225 | */ |
| 226 | private void changeGear(DoubleSolenoid.Value gear) { |
| 227 | leftGearPiston.set(gear); |
| 228 | rightGearPiston.set(gear); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | protected void initDefaultCommand() { |
| 233 | setDefaultCommand(new JoystickDrive()); |
| 234 | } |
| 235 | } |