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