Make methods to convert DegreesPerSecond to Degrees
[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.FirebotGyro;
5 import org.usfirst.frc.team3501.robot.Lidar;
6
7 import edu.wpi.first.wpilibj.AnalogInput;
8 import edu.wpi.first.wpilibj.CANTalon;
9 import edu.wpi.first.wpilibj.CounterBase.EncodingType;
10 import edu.wpi.first.wpilibj.Encoder;
11 import edu.wpi.first.wpilibj.I2C;
12 import edu.wpi.first.wpilibj.PIDController;
13 import edu.wpi.first.wpilibj.command.Subsystem;
14
15 public class DriveTrain extends Subsystem {
16 // Drivetrain related objects
17 private Encoder leftEncoder, rightEncoder;
18 public static Lidar lidar;
19 private CANTalon frontLeft, frontRight, rearLeft, rearRight;
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;
30
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;
35
36 public AnalogInput channel;
37
38 // Gyro stuff
39 private final static double NANOSECONDS_PER_SECOND = 1000000000;
40 short rawValue;
41 public FirebotGyro gyro;
42
43 double initialSpeedNanoseconds;
44 double finalSpeedNanoseconds;
45 double initialSpeedSeconds;
46 double finalSpeedSeconds;
47 double deltaSpeed;
48 double degrees;
49
50 public DriveTrain() {
51 frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT);
52 frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
53 rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
54 rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
55
56 lidar = new Lidar(I2C.Port.kOnboard);
57 leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
58 Constants.DriveTrain.ENCODER_LEFT_B, false, EncodingType.k4X);
59 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
60 Constants.DriveTrain.ENCODER_RIGHT_B, false, EncodingType.k4X);
61 leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE);
62 rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE);
63 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
64 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
65
66 gyro = new FirebotGyro(I2C.Port.kOnboard, (byte) 0x68);
67 gyro.initialize();
68 }
69
70 @Override
71 protected void initDefaultCommand() {
72 }
73
74 public void resetEncoders() {
75 leftEncoder.reset();
76 rightEncoder.reset();
77 }
78
79 public double getLidarDistance() {
80 return lidar.pidGet();
81 }
82
83 public double getRightSpeed() {
84 return rightEncoder.getRate(); // in inches per second
85 }
86
87 public double getLeftSpeed() {
88 return leftEncoder.getRate(); // in inches per second
89 }
90
91 public double getSpeed() {
92 return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
93 }
94
95 public double getRightDistance() {
96 return rightEncoder.getDistance(); // in inches
97 }
98
99 public double getLeftDistance() {
100 return leftEncoder.getDistance(); // in inches
101 }
102
103 public double getDistance() {
104 return (getRightDistance() + getLeftDistance()) / 2.0; // in inches
105 }
106
107 public void stop() {
108 setMotorSpeeds(0, 0);
109 }
110
111 public void setMotorSpeeds(double leftSpeed, double rightSpeed) {
112 // speed passed to right motor is negative because right motor rotates in
113 // opposite direction
114 this.frontLeft.set(leftSpeed);
115 this.frontRight.set(-rightSpeed);
116 this.rearLeft.set(leftSpeed);
117 this.rearRight.set(-rightSpeed);
118 }
119 }