76f4a89ef25540f0461f2c3f4818edf4c534ca7b
1 package org
.usfirst
.frc
.team3501
.robot
.subsystems
;
3 import org
.usfirst
.frc
.team3501
.robot
.Constants
;
5 import edu
.wpi
.first
.wpilibj
.CANTalon
;
6 import edu
.wpi
.first
.wpilibj
.CANTalon
.TalonControlMode
;
7 import edu
.wpi
.first
.wpilibj
.CounterBase
.EncodingType
;
8 import edu
.wpi
.first
.wpilibj
.Encoder
;
9 import edu
.wpi
.first
.wpilibj
.PIDController
;
10 import edu
.wpi
.first
.wpilibj
.command
.Subsystem
;
12 public class DriveTrain
extends Subsystem
{
13 // Drivetrain related objects
14 private Encoder leftEncoder
, rightEncoder
;
15 private CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
16 private PIDController frontLeftC
, frontRightC
, rearLeftC
, rearRightC
;
17 // Drivetrain specific constants that relate to the inches per pulse value for
19 private final static double WHEEL_DIAMETER
= 6.0; // in inches
20 private final static double PULSES_PER_ROTATION
= 256; // in pulses
21 private final static double OUTPUT_SPROCKET_DIAMETER
= 2.0; // in inches
22 private final static double WHEEL_SPROCKET_DIAMETER
= 3.5; // in inches
23 public final static double INCHES_PER_PULSE
= (((Math
.PI
)
24 * OUTPUT_SPROCKET_DIAMETER
/ PULSES_PER_ROTATION
) / WHEEL_SPROCKET_DIAMETER
)
26 // Drivetrain specific constants that relate to the PID controllers
27 private final static double Kp
= 1.0, Ki
= 0.0, Kd
= 0.0;
30 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
31 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
32 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
33 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
34 this.configureTalons();
36 frontLeftC
= new PIDController(Kp
, Ki
, Kd
, frontLeft
, frontLeft
);
37 frontRightC
= new PIDController(Kp
, Ki
, Kd
, frontRight
, frontRight
);
38 rearLeftC
= new PIDController(Kp
, Ki
, Kd
, frontLeft
, rearLeft
);
39 rearRightC
= new PIDController(Kp
, Ki
, Kd
, frontRight
, rearRight
);
40 this.enablePIDControllers();
42 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
43 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, EncodingType
.k4X
);
44 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
45 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, EncodingType
.k4X
);
46 leftEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
47 rightEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
51 protected void initDefaultCommand() {
54 public void configureTalons() {
55 frontLeft
.changeControlMode(TalonControlMode
.Speed
);
56 frontRight
.changeControlMode(TalonControlMode
.Speed
);
57 rearLeft
.changeControlMode(TalonControlMode
.Speed
);
58 rearRight
.changeControlMode(TalonControlMode
.Speed
);
60 frontLeft
.configEncoderCodesPerRev(256);
61 frontRight
.configEncoderCodesPerRev(256);
62 rearLeft
.configEncoderCodesPerRev(256);
63 rearRight
.configEncoderCodesPerRev(256);
65 frontLeft
.enableControl();
66 frontRight
.enableControl();
67 rearLeft
.enableControl();
68 rearRight
.enableControl();
71 public void enablePIDControllers() {
78 public void drive(double left
, double right
) {
79 frontLeftC
.setSetpoint(left
);
80 rearLeftC
.setSetpoint(left
);
81 frontRightC
.setSetpoint(right
);
82 rearRightC
.setSetpoint(right
);
85 public void resetEncoders() {
90 public double getRightSpeed() {
91 return rightEncoder
.getRate(); // in inches per second
94 public double getLeftSpeed() {
95 return leftEncoder
.getRate(); // in inches per second
98 public double getSpeed() {
99 return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
102 public double getRightDistance() {
103 return rightEncoder
.getDistance(); // in inches
106 public double getLeftDistance() {
107 return leftEncoder
.getDistance(); // in inches
110 public double getDistance() {
111 return (getRightDistance() + getLeftDistance()) / 2.0; // in inches
115 setMotorSpeeds(0, 0);
118 public void setMotorSpeeds(double leftSpeed
, double rightSpeed
) {
119 // speed passed to right motor is negative because right motor rotates in
120 // opposite direction
121 this.frontLeft
.set(leftSpeed
);
122 this.frontRight
.set(-rightSpeed
);
123 this.rearLeft
.set(leftSpeed
);
124 this.rearRight
.set(-rightSpeed
);