Make convert method to get Z axis in Degrees per second regarding short MAX and MIN...
[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 leftLidar;
19 public static Lidar rightLidar;
20 private CANTalon frontLeft, frontRight, rearLeft, rearRight;
21 private PIDController frontLeftC, frontRightC, rearLeftC, rearRightC;
22 // Drivetrain specific constants that relate to the inches per pulse value for
23 // the encoders
24 private final static double WHEEL_DIAMETER = 6.0; // in inches
25 private final static double PULSES_PER_ROTATION = 256; // in pulses
26 private final static double OUTPUT_SPROCKET_DIAMETER = 2.0; // in inches
27 private final static double WHEEL_SPROCKET_DIAMETER = 3.5; // in inches
28 public final static double INCHES_PER_PULSE = (((Math.PI)
29 * OUTPUT_SPROCKET_DIAMETER / PULSES_PER_ROTATION)
30 / WHEEL_SPROCKET_DIAMETER) * WHEEL_DIAMETER;
31
32 // Drivetrain specific constants that relate to the PID controllers
33 private final static double Kp = 1.0, Ki = 0.0,
34 Kd = 0.0 * (OUTPUT_SPROCKET_DIAMETER / PULSES_PER_ROTATION)
35 / (WHEEL_SPROCKET_DIAMETER) * WHEEL_DIAMETER;
36
37 public AnalogInput channel;
38
39 // Gyro stuff
40 short rawValue;
41 public FirebotGyro gyro;
42
43 public DriveTrain() {
44 frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT);
45 frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
46 rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
47 rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
48
49 leftLidar = new Lidar(I2C.Port.kOnboard);
50 rightLidar = new Lidar(I2C.Port.kOnboard); // TODO: find port for second
51 // lidar
52 leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
53 Constants.DriveTrain.ENCODER_LEFT_B, false, EncodingType.k4X);
54 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
55 Constants.DriveTrain.ENCODER_RIGHT_B, false, EncodingType.k4X);
56 leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE);
57 rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE);
58 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
59 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
60
61 gyro = new FirebotGyro(I2C.Port.kOnboard, (byte) 0x68);
62 gyro.initialize();
63 }
64
65 @Override
66 protected void initDefaultCommand() {
67 }
68
69 public void resetEncoders() {
70 leftEncoder.reset();
71 rightEncoder.reset();
72 }
73
74 public double getLeftLidarDistance() {
75 return leftLidar.pidGet();
76 }
77
78 public double getRightLidarDistance() {
79 return rightLidar.pidGet();
80 }
81
82 public double getRightSpeed() {
83 return rightEncoder.getRate(); // in inches per second
84 }
85
86 public double getLeftSpeed() {
87 return leftEncoder.getRate(); // in inches per second
88 }
89
90 public double getSpeed() {
91 return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
92 }
93
94 public double getRightDistance() {
95 return rightEncoder.getDistance(); // in inches
96 }
97
98 public double getLeftDistance() {
99 return leftEncoder.getDistance(); // in inches
100 }
101
102 public double getDistance() {
103 return (getRightDistance() + getLeftDistance()) / 2.0; // in inches
104 }
105
106 public void stop() {
107 setMotorSpeeds(0, 0);
108 }
109
110 public void setMotorSpeeds(double leftSpeed, double rightSpeed) {
111 // speed passed to right motor is negative because right motor rotates in
112 // opposite direction
113 this.frontLeft.set(leftSpeed);
114 this.frontRight.set(-rightSpeed);
115 this.rearLeft.set(leftSpeed);
116 this.rearRight.set(-rightSpeed);
117 }
118 }