| 1 | package org.usfirst.frc.team3501.robot.subsystems; |
| 2 | |
| 3 | import org.usfirst.frc.team3501.robot.Constants; |
| 4 | |
| 5 | import com.ctre.CANTalon; |
| 6 | |
| 7 | import edu.wpi.first.wpilibj.Encoder; |
| 8 | import edu.wpi.first.wpilibj.RobotDrive; |
| 9 | import edu.wpi.first.wpilibj.command.Subsystem; |
| 10 | |
| 11 | public class DriveTrain extends Subsystem { |
| 12 | private static DriveTrain driveTrain; |
| 13 | private final CANTalon frontLeft, frontRight, rearLeft, rearRight; |
| 14 | private final RobotDrive robotDrive; |
| 15 | private final Encoder leftEncoder, rightEncoder; |
| 16 | |
| 17 | private DriveTrain() { |
| 18 | // MOTOR CONTROLLERS |
| 19 | frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT); |
| 20 | frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT); |
| 21 | rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT); |
| 22 | rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT); |
| 23 | |
| 24 | // ENCODERS |
| 25 | leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A, |
| 26 | Constants.DriveTrain.ENCODER_LEFT_B); |
| 27 | rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A, |
| 28 | Constants.DriveTrain.ENCODER_RIGHT_B); |
| 29 | |
| 30 | leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); |
| 31 | rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); |
| 32 | |
| 33 | // ROBOT DRIVE |
| 34 | robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight); |
| 35 | } |
| 36 | |
| 37 | public static DriveTrain getDriveTrain() { |
| 38 | if (driveTrain == null) { |
| 39 | driveTrain = new DriveTrain(); |
| 40 | } |
| 41 | return driveTrain; |
| 42 | } |
| 43 | |
| 44 | // DRIVE METHODS |
| 45 | public void setMotorValues(final double left, final double right) { |
| 46 | robotDrive.tankDrive(left, right); |
| 47 | } |
| 48 | |
| 49 | public void joystickDrive(final double left, final double right) { |
| 50 | robotDrive.tankDrive(left, right); |
| 51 | } |
| 52 | |
| 53 | public void stop() { |
| 54 | setMotorValues(0, 0); |
| 55 | } |
| 56 | |
| 57 | public double getFrontLeftMotorVal() { |
| 58 | return frontLeft.get(); |
| 59 | } |
| 60 | |
| 61 | public double getFrontRightMotorVal() { |
| 62 | return frontRight.get(); |
| 63 | } |
| 64 | |
| 65 | public double getRearLeftMotorVal() { |
| 66 | return frontLeft.get(); |
| 67 | } |
| 68 | |
| 69 | public double getRearRightMotorVal() { |
| 70 | return frontLeft.get(); |
| 71 | } |
| 72 | |
| 73 | public CANTalon getFrontLeft() { |
| 74 | return frontLeft; |
| 75 | } |
| 76 | |
| 77 | public CANTalon getFrontRight() { |
| 78 | return frontRight; |
| 79 | } |
| 80 | |
| 81 | public CANTalon getRearLeft() { |
| 82 | return rearLeft; |
| 83 | } |
| 84 | |
| 85 | public CANTalon getRearRight() { |
| 86 | return rearRight; |
| 87 | } |
| 88 | |
| 89 | // ENCODER METHODS |
| 90 | |
| 91 | public double getLeftEncoder() { |
| 92 | return leftEncoder.getDistance(); |
| 93 | } |
| 94 | |
| 95 | public double getRightEncoder() { |
| 96 | return rightEncoder.getDistance(); |
| 97 | } |
| 98 | |
| 99 | public double getAvgEncoderDistance() { |
| 100 | return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2; |
| 101 | } |
| 102 | |
| 103 | public void resetEncoders() { |
| 104 | leftEncoder.reset(); |
| 105 | rightEncoder.reset(); |
| 106 | } |
| 107 | |
| 108 | public double getLeftSpeed() { |
| 109 | return leftEncoder.getRate(); |
| 110 | } |
| 111 | |
| 112 | public double getRightSpeed() { |
| 113 | return rightEncoder.getRate(); |
| 114 | } |
| 115 | |
| 116 | public double getSpeed() { |
| 117 | return (getLeftSpeed() + getRightSpeed()) / 2.0; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | protected void initDefaultCommand() { |
| 122 | } |
| 123 | |
| 124 | } |