1 package org
.usfirst
.frc
.team3501
.robot
.subsystems
;
3 import org
.usfirst
.frc
.team3501
.robot
.Constants
;
4 import org
.usfirst
.frc
.team3501
.robot
.Lidar
;
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
;
14 public class DriveTrain
extends Subsystem
{
15 // Drivetrain related objects
16 private Encoder leftEncoder
, rightEncoder
;
17 public static Lidar leftLidar
;
18 public static Lidar rightLidar
;
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
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
;
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
;
36 public AnalogInput channel
;
39 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
40 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
41 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
42 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
44 leftLidar
= new Lidar(I2C
.Port
.kOnboard
);
45 rightLidar
= new Lidar(I2C
.Port
.kOnboard
); // TODO: find port for second
47 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
48 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, EncodingType
.k4X
);
49 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
50 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, EncodingType
.k4X
);
51 leftEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
52 rightEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
53 leftEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
54 rightEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
59 protected void initDefaultCommand() {
62 public void resetEncoders() {
67 public double getLeftLidarDistance() {
68 return leftLidar
.pidGet();
71 public double getRightLidarDistance() {
72 return rightLidar
.pidGet();
75 public double getRightSpeed() {
76 return rightEncoder
.getRate(); // in inches per second
79 public double getLeftSpeed() {
80 return leftEncoder
.getRate(); // in inches per second
83 public double getSpeed() {
84 return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
87 public double getRightDistance() {
88 return rightEncoder
.getDistance(); // in inches
91 public double getLeftDistance() {
92 return leftEncoder
.getDistance(); // in inches
95 public double getDistance() {
96 return (getRightDistance() + getLeftDistance()) / 2.0; // in inches
100 setMotorSpeeds(0, 0);
103 public void setMotorSpeeds(double leftSpeed
, double rightSpeed
) {
104 // speed passed to right motor is negative because right motor rotates in
105 // opposite direction
106 this.frontLeft
.set(leftSpeed
);
107 this.frontRight
.set(-rightSpeed
);
108 this.rearLeft
.set(leftSpeed
);
109 this.rearRight
.set(-rightSpeed
);