Commit | Line | Data |
---|---|---|
38a404b3 KZ |
1 | package org.usfirst.frc.team3501.robot.subsystems; |
2 | ||
3 | import org.usfirst.frc.team3501.robot.Constants; | |
81f3b7db | 4 | import org.usfirst.frc.team3501.robot.FirebotGyro; |
b54ad73b | 5 | import org.usfirst.frc.team3501.robot.Lidar; |
111dc444 | 6 | |
81f3b7db | 7 | import edu.wpi.first.wpilibj.AnalogInput; |
38a404b3 | 8 | import edu.wpi.first.wpilibj.CANTalon; |
111dc444 YN |
9 | import edu.wpi.first.wpilibj.CounterBase.EncodingType; |
10 | import edu.wpi.first.wpilibj.Encoder; | |
b54ad73b | 11 | import edu.wpi.first.wpilibj.I2C; |
d004deee | 12 | import edu.wpi.first.wpilibj.PIDController; |
38a404b3 KZ |
13 | import edu.wpi.first.wpilibj.command.Subsystem; |
14 | ||
15 | public class DriveTrain extends Subsystem { | |
1884c3cf KZ |
16 | // Drivetrain related objects |
17 | private Encoder leftEncoder, rightEncoder; | |
b54ad73b | 18 | public static Lidar lidar; |
d7bf2340 | 19 | private CANTalon frontLeft, frontRight, rearLeft, rearRight; |
d004deee E |
20 | private PIDController frontLeftC, frontRightC, rearLeftC, rearRightC; |
21 | // Drivetrain specific constants that relate to the inches per pulse value for | |
22 | // the encoders | |
23 | private final static double WHEEL_DIAMETER = 6.0; // in inches | |
24 | private final static double PULSES_PER_ROTATION = 256; // in pulses | |
25 | private final static double OUTPUT_SPROCKET_DIAMETER = 2.0; // in inches | |
26 | private final static double WHEEL_SPROCKET_DIAMETER = 3.5; // in inches | |
27 | public final static double INCHES_PER_PULSE = (((Math.PI) | |
28 | * OUTPUT_SPROCKET_DIAMETER / PULSES_PER_ROTATION) | |
29 | / WHEEL_SPROCKET_DIAMETER) * WHEEL_DIAMETER; | |
2c9f5abb | 30 | |
d004deee E |
31 | // Drivetrain specific constants that relate to the PID controllers |
32 | private final static double Kp = 1.0, Ki = 0.0, | |
33 | Kd = 0.0 * (OUTPUT_SPROCKET_DIAMETER / PULSES_PER_ROTATION) | |
34 | / (WHEEL_SPROCKET_DIAMETER) * WHEEL_DIAMETER; | |
d7bf2340 | 35 | |
81f3b7db E |
36 | public AnalogInput channel; |
37 | public FirebotGyro gyro; | |
38 | ||
d7bf2340 KZ |
39 | public DriveTrain() { |
40 | frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT); | |
41 | frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT); | |
42 | rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT); | |
43 | rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT); | |
1884c3cf | 44 | |
b54ad73b | 45 | lidar = new Lidar(I2C.Port.kOnboard); |
d7bf2340 KZ |
46 | leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A, |
47 | Constants.DriveTrain.ENCODER_LEFT_B, false, EncodingType.k4X); | |
48 | rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A, | |
49 | Constants.DriveTrain.ENCODER_RIGHT_B, false, EncodingType.k4X); | |
45bdf5b9 KZ |
50 | leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); |
51 | rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); | |
d004deee E |
52 | leftEncoder.setDistancePerPulse(INCHES_PER_PULSE); |
53 | rightEncoder.setDistancePerPulse(INCHES_PER_PULSE); | |
54 | ||
81f3b7db | 55 | gyro = new FirebotGyro(I2C.Port.kOnboard, (byte) 0x68); |
7cffac0a | 56 | gyro.initialize(); |
d7bf2340 KZ |
57 | } |
58 | ||
59 | @Override | |
60 | protected void initDefaultCommand() { | |
61 | } | |
62 | ||
63 | public void resetEncoders() { | |
64 | leftEncoder.reset(); | |
65 | rightEncoder.reset(); | |
66 | } | |
67 | ||
b54ad73b KZ |
68 | public double getLidarDistance() { |
69 | return lidar.pidGet(); | |
70 | } | |
71 | ||
d7bf2340 | 72 | public double getRightSpeed() { |
6833a887 | 73 | return rightEncoder.getRate(); // in inches per second |
d7bf2340 KZ |
74 | } |
75 | ||
76 | public double getLeftSpeed() { | |
6833a887 | 77 | return leftEncoder.getRate(); // in inches per second |
d7bf2340 KZ |
78 | } |
79 | ||
80 | public double getSpeed() { | |
6833a887 | 81 | return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second |
d7bf2340 KZ |
82 | } |
83 | ||
d7bf2340 | 84 | public double getRightDistance() { |
6833a887 | 85 | return rightEncoder.getDistance(); // in inches |
d7bf2340 KZ |
86 | } |
87 | ||
d7bf2340 | 88 | public double getLeftDistance() { |
6833a887 | 89 | return leftEncoder.getDistance(); // in inches |
d7bf2340 KZ |
90 | } |
91 | ||
92 | public double getDistance() { | |
6833a887 | 93 | return (getRightDistance() + getLeftDistance()) / 2.0; // in inches |
d7bf2340 KZ |
94 | } |
95 | ||
96 | public void stop() { | |
97 | setMotorSpeeds(0, 0); | |
98 | } | |
99 | ||
100 | public void setMotorSpeeds(double leftSpeed, double rightSpeed) { | |
1884c3cf KZ |
101 | // speed passed to right motor is negative because right motor rotates in |
102 | // opposite direction | |
d7bf2340 KZ |
103 | this.frontLeft.set(leftSpeed); |
104 | this.frontRight.set(-rightSpeed); | |
105 | this.rearLeft.set(leftSpeed); | |
106 | this.rearRight.set(-rightSpeed); | |
d7bf2340 | 107 | } |
38a404b3 | 108 | } |