| 1 | package org.usfirst.frc.team3501.robot.subsystems; |
| 2 | |
| 3 | import org.usfirst.frc.team3501.robot.Constants; |
| 4 | |
| 5 | import edu.wpi.first.wpilibj.CANTalon; |
| 6 | import edu.wpi.first.wpilibj.CounterBase.EncodingType; |
| 7 | import edu.wpi.first.wpilibj.Encoder; |
| 8 | import edu.wpi.first.wpilibj.command.Subsystem; |
| 9 | |
| 10 | public class DriveTrain extends Subsystem { |
| 11 | // Drivetrain related objects |
| 12 | private Encoder leftEncoder, rightEncoder; |
| 13 | private CANTalon frontLeft, frontRight, rearLeft, rearRight; |
| 14 | |
| 15 | public DriveTrain() { |
| 16 | frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT); |
| 17 | frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT); |
| 18 | rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT); |
| 19 | rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT); |
| 20 | |
| 21 | leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A, |
| 22 | Constants.DriveTrain.ENCODER_LEFT_B, false, EncodingType.k4X); |
| 23 | rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A, |
| 24 | Constants.DriveTrain.ENCODER_RIGHT_B, false, EncodingType.k4X); |
| 25 | leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); |
| 26 | rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); |
| 27 | } |
| 28 | |
| 29 | @Override |
| 30 | protected void initDefaultCommand() { |
| 31 | } |
| 32 | |
| 33 | public void resetEncoders() { |
| 34 | leftEncoder.reset(); |
| 35 | rightEncoder.reset(); |
| 36 | } |
| 37 | |
| 38 | public double getRightSpeed() { |
| 39 | return rightEncoder.getRate(); // in inches per second |
| 40 | } |
| 41 | |
| 42 | public double getLeftSpeed() { |
| 43 | return leftEncoder.getRate(); // in inches per second |
| 44 | } |
| 45 | |
| 46 | public double getSpeed() { |
| 47 | return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second |
| 48 | } |
| 49 | |
| 50 | public double getRightDistance() { |
| 51 | return rightEncoder.getDistance(); // in inches |
| 52 | } |
| 53 | |
| 54 | public double getLeftDistance() { |
| 55 | return leftEncoder.getDistance(); // in inches |
| 56 | } |
| 57 | |
| 58 | public double getDistance() { |
| 59 | return (getRightDistance() + getLeftDistance()) / 2.0; // in inches |
| 60 | } |
| 61 | |
| 62 | public void stop() { |
| 63 | setMotorSpeeds(0, 0); |
| 64 | } |
| 65 | |
| 66 | public void setMotorSpeeds(double leftSpeed, double rightSpeed) { |
| 67 | // speed passed to right motor is negative because right motor rotates in |
| 68 | // opposite direction |
| 69 | this.frontLeft.set(leftSpeed); |
| 70 | this.frontRight.set(-rightSpeed); |
| 71 | this.rearLeft.set(leftSpeed); |
| 72 | this.rearRight.set(-rightSpeed); |
| 73 | } |
| 74 | } |