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