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
.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
;
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
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
;
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
;
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
);
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
);
53 protected void initDefaultCommand() {
56 public void resetEncoders() {
61 public double getLidarDistance() {
62 return lidar
.pidGet();
65 public double getRightSpeed() {
66 return rightEncoder
.getRate(); // in inches per second
69 public double getLeftSpeed() {
70 return leftEncoder
.getRate(); // in inches per second
73 public double getSpeed() {
74 return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
77 public double getRightDistance() {
78 return rightEncoder
.getDistance(); // in inches
81 public double getLeftDistance() {
82 return leftEncoder
.getDistance(); // in inches
85 public double getDistance() {
86 return (getRightDistance() + getLeftDistance()) / 2.0; // in inches
93 public void setMotorSpeeds(double leftSpeed
, double rightSpeed
) {
94 // speed passed to right motor is negative because right motor rotates in
96 this.frontLeft
.set(leftSpeed
);
97 this.frontRight
.set(-rightSpeed
);
98 this.rearLeft
.set(leftSpeed
);
99 this.rearRight
.set(-rightSpeed
);