change getCurrentSetPoint in changeSpeed method to getSpeed
[3501/stronghold-2016] / src / org / usfirst / frc / team3501 / robot / subsystems / DriveTrain.java
CommitLineData
38a404b3
KZ
1package org.usfirst.frc.team3501.robot.subsystems;
2
3import org.usfirst.frc.team3501.robot.Constants;
111dc444 4
38a404b3 5import edu.wpi.first.wpilibj.CANTalon;
c96e6803 6import edu.wpi.first.wpilibj.CANTalon.TalonControlMode;
111dc444
YN
7import edu.wpi.first.wpilibj.CounterBase.EncodingType;
8import edu.wpi.first.wpilibj.Encoder;
c96e6803 9import edu.wpi.first.wpilibj.PIDController;
38a404b3
KZ
10import edu.wpi.first.wpilibj.command.Subsystem;
11
12public class DriveTrain extends Subsystem {
1884c3cf
KZ
13 // Drivetrain related objects
14 private Encoder leftEncoder, rightEncoder;
d7bf2340 15 private CANTalon frontLeft, frontRight, rearLeft, rearRight;
c96e6803 16 private PIDController frontLeftC, frontRightC, rearLeftC, rearRightC;
1884c3cf
KZ
17 // Drivetrain specific constants that relate to the inches per pulse value for
18 // the encoders
d7bf2340 19 private final static double WHEEL_DIAMETER = 6.0; // in inches
6833a887
YN
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
d7bf2340
KZ
23 public final static double INCHES_PER_PULSE = (((Math.PI)
24 * OUTPUT_SPROCKET_DIAMETER / PULSES_PER_ROTATION) / WHEEL_SPROCKET_DIAMETER)
25 * WHEEL_DIAMETER;
c96e6803
YN
26 // Drivetrain specific constants that relate to the PID controllers
27 private final static double Kp = 1.0, Ki = 0.0, Kd = 0.0;
d7bf2340
KZ
28
29 public DriveTrain() {
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);
c96e6803
YN
34 this.configureTalons();
35
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();
1884c3cf 41
d7bf2340
KZ
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);
48 }
49
50 @Override
51 protected void initDefaultCommand() {
52 }
53
c96e6803
YN
54 public void configureTalons() {
55 frontLeft.changeControlMode(TalonControlMode.Speed);
56 frontRight.changeControlMode(TalonControlMode.Speed);
57 rearLeft.changeControlMode(TalonControlMode.Speed);
58 rearRight.changeControlMode(TalonControlMode.Speed);
59
60 frontLeft.configEncoderCodesPerRev(256);
61 frontRight.configEncoderCodesPerRev(256);
62 rearLeft.configEncoderCodesPerRev(256);
63 rearRight.configEncoderCodesPerRev(256);
64
65 frontLeft.enableControl();
66 frontRight.enableControl();
67 rearLeft.enableControl();
68 rearRight.enableControl();
69 }
70
71 public void enablePIDControllers() {
72 frontLeftC.enable();
73 frontRightC.enable();
74 rearLeftC.enable();
75 rearRightC.enable();
76 }
77
78 public void drive(double left, double right) {
79 frontLeftC.setSetpoint(left);
80 rearLeftC.setSetpoint(left);
81 frontRightC.setSetpoint(right);
82 rearRightC.setSetpoint(right);
83 }
84
d7bf2340
KZ
85 public void resetEncoders() {
86 leftEncoder.reset();
87 rightEncoder.reset();
88 }
89
d7bf2340 90 public double getRightSpeed() {
6833a887 91 return rightEncoder.getRate(); // in inches per second
d7bf2340
KZ
92 }
93
94 public double getLeftSpeed() {
6833a887 95 return leftEncoder.getRate(); // in inches per second
d7bf2340
KZ
96 }
97
98 public double getSpeed() {
6833a887 99 return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
d7bf2340
KZ
100 }
101
d7bf2340 102 public double getRightDistance() {
6833a887 103 return rightEncoder.getDistance(); // in inches
d7bf2340
KZ
104 }
105
d7bf2340 106 public double getLeftDistance() {
6833a887 107 return leftEncoder.getDistance(); // in inches
d7bf2340
KZ
108 }
109
110 public double getDistance() {
6833a887 111 return (getRightDistance() + getLeftDistance()) / 2.0; // in inches
d7bf2340
KZ
112 }
113
114 public void stop() {
115 setMotorSpeeds(0, 0);
116 }
117
118 public void setMotorSpeeds(double leftSpeed, double rightSpeed) {
1884c3cf
KZ
119 // speed passed to right motor is negative because right motor rotates in
120 // opposite direction
d7bf2340
KZ
121 this.frontLeft.set(leftSpeed);
122 this.frontRight.set(-rightSpeed);
123 this.rearLeft.set(leftSpeed);
124 this.rearRight.set(-rightSpeed);
d7bf2340 125 }
38a404b3 126}