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; |
8e4c083c | 10 | import edu.wpi.first.wpilibj.I2C.Port; |
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 { | |
e12d6901 CZ |
15 | public static double driveP = 0.008, driveI = 0.001, driveD = -0.002; |
16 | public static double defaultGyroP = 0.009, defaultGyroI = 0.00000, | |
17 | defaultGyroD = -0.000; | |
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; | |
29 | ||
e12d6901 CZ |
30 | private BNO055 imu; |
31 | ||
cc42bd52 ME |
32 | private DriveTrain() { |
33 | // MOTOR CONTROLLERS | |
34 | frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT); | |
35 | frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT); | |
36 | rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT); | |
37 | rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT); | |
38 | ||
39 | // ENCODERS | |
40 | leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A, | |
3e2738c4 | 41 | Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X); |
cc42bd52 | 42 | rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A, |
3e2738c4 | 43 | Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X); |
cc42bd52 | 44 | |
b634ebbc CZ |
45 | leftEncoder.setDistancePerPulse(INCHES_PER_PULSE); |
46 | rightEncoder.setDistancePerPulse(INCHES_PER_PULSE); | |
cc42bd52 ME |
47 | |
48 | // ROBOT DRIVE | |
49 | robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight); | |
8e4c083c CZ |
50 | |
51 | this.imu = BNO055.getInstance(BNO055.opmode_t.OPERATION_MODE_IMUPLUS, | |
52 | BNO055.vector_type_t.VECTOR_EULER, Port.kOnboard, (byte) 0x28); | |
53 | gyroZero = imu.getHeading(); | |
cc42bd52 ME |
54 | } |
55 | ||
56 | public static DriveTrain getDriveTrain() { | |
57 | if (driveTrain == null) { | |
58 | driveTrain = new DriveTrain(); | |
59 | } | |
60 | return driveTrain; | |
61 | } | |
62 | ||
63 | // DRIVE METHODS | |
64 | public void setMotorValues(final double left, final double right) { | |
65 | frontLeft.set(left); | |
66 | rearLeft.set(left); | |
67 | ||
b081e34b ME |
68 | frontRight.set(-right); |
69 | rearRight.set(-right); | |
cc42bd52 ME |
70 | } |
71 | ||
72 | public void joystickDrive(final double thrust, final double twist) { | |
b081e34b | 73 | robotDrive.arcadeDrive(thrust, twist, true); |
cc42bd52 ME |
74 | } |
75 | ||
76 | public void stop() { | |
77 | setMotorValues(0, 0); | |
78 | } | |
79 | ||
80 | public double getFrontLeftMotorVal() { | |
81 | return frontLeft.get(); | |
82 | } | |
83 | ||
84 | public double getFrontRightMotorVal() { | |
85 | return frontRight.get(); | |
86 | } | |
87 | ||
88 | public double getRearLeftMotorVal() { | |
89 | return frontLeft.get(); | |
90 | } | |
91 | ||
92 | public double getRearRightMotorVal() { | |
93 | return frontLeft.get(); | |
94 | } | |
95 | ||
cc42bd52 ME |
96 | // ENCODER METHODS |
97 | ||
3a5d9ac7 | 98 | public double getLeftEncoderDistance() { |
cc42bd52 ME |
99 | return leftEncoder.getDistance(); |
100 | } | |
101 | ||
3a5d9ac7 | 102 | public double getRightEncoderDistance() { |
cc42bd52 ME |
103 | return rightEncoder.getDistance(); |
104 | } | |
105 | ||
8e4c083c CZ |
106 | public void printEncoderOutput() { |
107 | System.out.println("left: " + getLeftEncoderDistance()); | |
108 | System.out.println("right: " + getRightEncoderDistance()); | |
109 | } | |
110 | ||
cc42bd52 ME |
111 | public double getAvgEncoderDistance() { |
112 | return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2; | |
113 | } | |
114 | ||
115 | public void resetEncoders() { | |
116 | leftEncoder.reset(); | |
117 | rightEncoder.reset(); | |
118 | } | |
119 | ||
120 | public double getLeftSpeed() { | |
121 | return leftEncoder.getRate(); | |
122 | } | |
123 | ||
124 | public double getRightSpeed() { | |
125 | return rightEncoder.getRate(); | |
126 | } | |
127 | ||
128 | public double getSpeed() { | |
129 | return (getLeftSpeed() + getRightSpeed()) / 2.0; | |
130 | } | |
131 | ||
e12d6901 CZ |
132 | // ------Gyro------// |
133 | public double getAngle() { | |
134 | if (!this.imu.isInitialized()) | |
135 | return -1; | |
136 | return this.imu.getHeading() - this.gyroZero; | |
137 | } | |
138 | ||
139 | public void resetGyro() { | |
140 | this.gyroZero = this.getAngle(); | |
141 | ||
142 | } | |
143 | ||
144 | public double getZeroAngle() { | |
145 | return this.gyroZero; | |
146 | } | |
147 | ||
cc42bd52 ME |
148 | @Override |
149 | protected void initDefaultCommand() { | |
150 | setDefaultCommand(new JoystickDrive()); | |
151 | } | |
38a404b3 KZ |
152 | |
153 | } |