Commit | Line | Data |
---|---|---|
38a404b3 KZ |
1 | package org.usfirst.frc.team3501.robot.subsystems; |
2 | ||
3 | import org.usfirst.frc.team3501.robot.Constants; | |
b54ad73b | 4 | import org.usfirst.frc.team3501.robot.Lidar; |
111dc444 | 5 | |
38a404b3 | 6 | import edu.wpi.first.wpilibj.CANTalon; |
111dc444 YN |
7 | import edu.wpi.first.wpilibj.CounterBase.EncodingType; |
8 | import edu.wpi.first.wpilibj.Encoder; | |
b54ad73b | 9 | import edu.wpi.first.wpilibj.I2C; |
38a404b3 KZ |
10 | import edu.wpi.first.wpilibj.command.Subsystem; |
11 | ||
12 | public class DriveTrain extends Subsystem { | |
1884c3cf KZ |
13 | // Drivetrain related objects |
14 | private Encoder leftEncoder, rightEncoder; | |
58291b3d ME |
15 | public static Lidar leftLidar; |
16 | public static Lidar rightLidar; | |
d7bf2340 | 17 | private CANTalon frontLeft, frontRight, rearLeft, rearRight; |
d7bf2340 KZ |
18 | |
19 | public DriveTrain() { | |
20 | frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT); | |
21 | frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT); | |
22 | rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT); | |
23 | rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT); | |
1884c3cf | 24 | |
58291b3d ME |
25 | leftLidar = new Lidar(I2C.Port.kOnboard); |
26 | rightLidar = new Lidar(I2C.Port.kOnboard); // TODO: find port for second | |
27 | // lidar | |
d7bf2340 KZ |
28 | leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A, |
29 | Constants.DriveTrain.ENCODER_LEFT_B, false, EncodingType.k4X); | |
30 | rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A, | |
31 | Constants.DriveTrain.ENCODER_RIGHT_B, false, EncodingType.k4X); | |
45bdf5b9 KZ |
32 | leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); |
33 | rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); | |
d7bf2340 KZ |
34 | } |
35 | ||
36 | @Override | |
37 | protected void initDefaultCommand() { | |
38 | } | |
39 | ||
40 | public void resetEncoders() { | |
41 | leftEncoder.reset(); | |
42 | rightEncoder.reset(); | |
43 | } | |
44 | ||
58291b3d ME |
45 | public double getLeftLidarDistance() { |
46 | return leftLidar.pidGet(); | |
47 | } | |
48 | ||
577926bb | 49 | public double getRightLidarDistance() { |
58291b3d | 50 | return rightLidar.pidGet(); |
b54ad73b KZ |
51 | } |
52 | ||
d7bf2340 | 53 | public double getRightSpeed() { |
6833a887 | 54 | return rightEncoder.getRate(); // in inches per second |
d7bf2340 KZ |
55 | } |
56 | ||
57 | public double getLeftSpeed() { | |
6833a887 | 58 | return leftEncoder.getRate(); // in inches per second |
d7bf2340 KZ |
59 | } |
60 | ||
61 | public double getSpeed() { | |
6833a887 | 62 | return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second |
d7bf2340 KZ |
63 | } |
64 | ||
d7bf2340 | 65 | public double getRightDistance() { |
6833a887 | 66 | return rightEncoder.getDistance(); // in inches |
d7bf2340 KZ |
67 | } |
68 | ||
d7bf2340 | 69 | public double getLeftDistance() { |
6833a887 | 70 | return leftEncoder.getDistance(); // in inches |
d7bf2340 KZ |
71 | } |
72 | ||
73 | public double getDistance() { | |
6833a887 | 74 | return (getRightDistance() + getLeftDistance()) / 2.0; // in inches |
d7bf2340 KZ |
75 | } |
76 | ||
77 | public void stop() { | |
78 | setMotorSpeeds(0, 0); | |
79 | } | |
80 | ||
81 | public void setMotorSpeeds(double leftSpeed, double rightSpeed) { | |
1884c3cf KZ |
82 | // speed passed to right motor is negative because right motor rotates in |
83 | // opposite direction | |
d7bf2340 KZ |
84 | this.frontLeft.set(leftSpeed); |
85 | this.frontRight.set(-rightSpeed); | |
86 | this.rearLeft.set(leftSpeed); | |
87 | this.rearRight.set(-rightSpeed); | |
d7bf2340 | 88 | } |
38a404b3 | 89 | } |