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 // Drivetrain specific constants that relate to the PID controllers
29 private final static double Kp = 1.0, Ki = 0.0,
30 Kd = 0.0 * (OUTPUT_SPROCKET_DIAMETER / PULSES_PER_ROTATION)
31 / (WHEEL_SPROCKET_DIAMETER) * WHEEL_DIAMETER;
32
33 public DriveTrain() {
34 frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT);
35 frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
36 rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
37 rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
38
39 lidar = new Lidar(I2C.Port.kOnboard);
40 leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
41 Constants.DriveTrain.ENCODER_LEFT_B, false, EncodingType.k4X);
42 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
43 Constants.DriveTrain.ENCODER_RIGHT_B, false, EncodingType.k4X);
44 leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE);
45 rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE);
46 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
47 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
48
49 }
50
51 @Override
52 protected void initDefaultCommand() {
53 }
54
55 public void resetEncoders() {
56 leftEncoder.reset();
57 rightEncoder.reset();
58 }
59
60 public double getLidarDistance() {
61 return lidar.pidGet();
62 }
63
64 public double getRightSpeed() {
65 return rightEncoder.getRate(); // in inches per second
66 }
67
68 public double getLeftSpeed() {
69 return leftEncoder.getRate(); // in inches per second
70 }
71
72 public double getSpeed() {
73 return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
74 }
75
76 public double getRightDistance() {
77 return rightEncoder.getDistance(); // in inches
78 }
79
80 public double getLeftDistance() {
81 return leftEncoder.getDistance(); // in inches
82 }
83
84 public double getDistance() {
85 return (getRightDistance() + getLeftDistance()) / 2.0; // in inches
86 }
87
88 public void stop() {
89 setMotorSpeeds(0, 0);
90 }
91
92 public void setMotorSpeeds(double leftSpeed, double rightSpeed) {
93 // speed passed to right motor is negative because right motor rotates in
94 // opposite direction
95 this.frontLeft.set(leftSpeed);
96 this.frontRight.set(-rightSpeed);
97 this.rearLeft.set(leftSpeed);
98 this.rearRight.set(-rightSpeed);
99 }
100
101 }