fix bugs for TurnForAngle and tune constants
[3501/2017steamworks] / 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;
5483fde9 4import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive;
04f2cb52 5
a1c76caf
CZ
6import com.ctre.CANTalon;
7
bf921ece 8import edu.wpi.first.wpilibj.ADXRS450_Gyro;
fa4e4a97 9import edu.wpi.first.wpilibj.Encoder;
ccbc35ae 10import edu.wpi.first.wpilibj.RobotDrive;
38a404b3
KZ
11import edu.wpi.first.wpilibj.command.Subsystem;
12
13public class DriveTrain extends Subsystem {
571fb5c9
CZ
14 public static double driveP = 0.006, driveI = 0.001, driveD = -0.002;
15 public static double defaultGyroP = 0.004, defaultGyroI = 0.0013,
16 defaultGyroD = -0.005;
e12d6901
CZ
17 private double gyroZero = 0;
18
b634ebbc
CZ
19 public static final double WHEEL_DIAMETER = 6; // inches
20 public static final int ENCODER_PULSES_PER_REVOLUTION = 256;
21 public static final double INCHES_PER_PULSE = WHEEL_DIAMETER * Math.PI
22 / ENCODER_PULSES_PER_REVOLUTION;
23
cc42bd52
ME
24 private static DriveTrain driveTrain;
25 private final CANTalon frontLeft, frontRight, rearLeft, rearRight;
26 private final RobotDrive robotDrive;
27 private final Encoder leftEncoder, rightEncoder;
28
bf921ece 29 private ADXRS450_Gyro imu;
e12d6901 30
cc42bd52
ME
31 private DriveTrain() {
32 // MOTOR CONTROLLERS
33 frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT);
34 frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
35 rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
36 rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
37
38 // ENCODERS
39 leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
3e2738c4 40 Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X);
cc42bd52 41 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
3e2738c4 42 Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X);
cc42bd52 43
b634ebbc
CZ
44 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
45 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
cc42bd52
ME
46
47 // ROBOT DRIVE
48 robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
8e4c083c 49
bf921ece 50 this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT);
cc42bd52
ME
51 }
52
53 public static DriveTrain getDriveTrain() {
54 if (driveTrain == null) {
55 driveTrain = new DriveTrain();
56 }
57 return driveTrain;
58 }
59
60 // DRIVE METHODS
61 public void setMotorValues(final double left, final double right) {
62 frontLeft.set(left);
63 rearLeft.set(left);
64
b081e34b
ME
65 frontRight.set(-right);
66 rearRight.set(-right);
cc42bd52
ME
67 }
68
69 public void joystickDrive(final double thrust, final double twist) {
b081e34b 70 robotDrive.arcadeDrive(thrust, twist, true);
cc42bd52
ME
71 }
72
73 public void stop() {
74 setMotorValues(0, 0);
75 }
76
f0a71840
CZ
77 public double getLeftMotorVal() {
78 return (frontLeft.get() + rearLeft.get()) / 2;
cc42bd52
ME
79 }
80
f0a71840
CZ
81 public double getRightMotorVal() {
82 return (frontRight.get() + rearRight.get()) / 2;
cc42bd52
ME
83 }
84
cc42bd52
ME
85 // ENCODER METHODS
86
3a5d9ac7 87 public double getLeftEncoderDistance() {
cc42bd52
ME
88 return leftEncoder.getDistance();
89 }
90
3a5d9ac7 91 public double getRightEncoderDistance() {
cc42bd52
ME
92 return rightEncoder.getDistance();
93 }
94
8e4c083c 95 public void printEncoderOutput() {
f0a71840
CZ
96 // System.out.println("left: " + getLeftEncoderDistance());
97 // System.out.println("right: " + getRightEncoderDistance());
98 System.out.println(getAvgEncoderDistance());
8e4c083c
CZ
99 }
100
cc42bd52
ME
101 public double getAvgEncoderDistance() {
102 return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2;
103 }
104
105 public void resetEncoders() {
106 leftEncoder.reset();
107 rightEncoder.reset();
108 }
109
110 public double getLeftSpeed() {
111 return leftEncoder.getRate();
112 }
113
114 public double getRightSpeed() {
115 return rightEncoder.getRate();
116 }
117
118 public double getSpeed() {
119 return (getLeftSpeed() + getRightSpeed()) / 2.0;
120 }
121
e12d6901
CZ
122 // ------Gyro------//
123 public double getAngle() {
bf921ece 124 return this.imu.getAngle() - this.gyroZero;
e12d6901
CZ
125 }
126
127 public void resetGyro() {
571fb5c9 128 this.imu.reset();
e12d6901
CZ
129 }
130
131 public double getZeroAngle() {
132 return this.gyroZero;
133 }
134
cc42bd52
ME
135 @Override
136 protected void initDefaultCommand() {
137 setDefaultCommand(new JoystickDrive());
138 }
38a404b3
KZ
139
140}