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