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; |
e12d6901 | 5 | import org.usfirst.frc.team3501.robot.utils.BNO055; |
04f2cb52 | 6 | |
a1c76caf CZ |
7 | import com.ctre.CANTalon; |
8 | ||
fa4e4a97 | 9 | import edu.wpi.first.wpilibj.Encoder; |
ccbc35ae | 10 | import edu.wpi.first.wpilibj.RobotDrive; |
38a404b3 KZ |
11 | import edu.wpi.first.wpilibj.command.Subsystem; |
12 | ||
13 | public class DriveTrain extends Subsystem { | |
e12d6901 CZ |
14 | public static double driveP = 0.008, driveI = 0.001, driveD = -0.002; |
15 | public static double defaultGyroP = 0.009, defaultGyroI = 0.00000, | |
16 | defaultGyroD = -0.000; | |
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 | ||
e12d6901 CZ |
29 | private BNO055 imu; |
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); | |
49 | } | |
50 | ||
51 | public static DriveTrain getDriveTrain() { | |
52 | if (driveTrain == null) { | |
53 | driveTrain = new DriveTrain(); | |
54 | } | |
55 | return driveTrain; | |
56 | } | |
57 | ||
58 | // DRIVE METHODS | |
59 | public void setMotorValues(final double left, final double right) { | |
60 | frontLeft.set(left); | |
61 | rearLeft.set(left); | |
62 | ||
b081e34b ME |
63 | frontRight.set(-right); |
64 | rearRight.set(-right); | |
cc42bd52 ME |
65 | } |
66 | ||
67 | public void joystickDrive(final double thrust, final double twist) { | |
b081e34b | 68 | robotDrive.arcadeDrive(thrust, twist, true); |
cc42bd52 ME |
69 | } |
70 | ||
71 | public void stop() { | |
72 | setMotorValues(0, 0); | |
73 | } | |
74 | ||
75 | public double getFrontLeftMotorVal() { | |
76 | return frontLeft.get(); | |
77 | } | |
78 | ||
79 | public double getFrontRightMotorVal() { | |
80 | return frontRight.get(); | |
81 | } | |
82 | ||
83 | public double getRearLeftMotorVal() { | |
84 | return frontLeft.get(); | |
85 | } | |
86 | ||
87 | public double getRearRightMotorVal() { | |
88 | return frontLeft.get(); | |
89 | } | |
90 | ||
cc42bd52 ME |
91 | // ENCODER METHODS |
92 | ||
3a5d9ac7 | 93 | public double getLeftEncoderDistance() { |
cc42bd52 ME |
94 | return leftEncoder.getDistance(); |
95 | } | |
96 | ||
3a5d9ac7 | 97 | public double getRightEncoderDistance() { |
cc42bd52 ME |
98 | return rightEncoder.getDistance(); |
99 | } | |
100 | ||
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() { | |
124 | if (!this.imu.isInitialized()) | |
125 | return -1; | |
126 | return this.imu.getHeading() - this.gyroZero; | |
127 | } | |
128 | ||
129 | public void resetGyro() { | |
130 | this.gyroZero = this.getAngle(); | |
131 | ||
132 | } | |
133 | ||
134 | public double getZeroAngle() { | |
135 | return this.gyroZero; | |
136 | } | |
137 | ||
cc42bd52 ME |
138 | @Override |
139 | protected void initDefaultCommand() { | |
140 | setDefaultCommand(new JoystickDrive()); | |
141 | } | |
38a404b3 KZ |
142 | |
143 | } |