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; | |
b54ad73b | 15 | public static Lidar lidar; |
d7bf2340 | 16 | private CANTalon frontLeft, frontRight, rearLeft, rearRight; |
d7bf2340 KZ |
17 | |
18 | public DriveTrain() { | |
19 | frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT); | |
20 | frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT); | |
21 | rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT); | |
22 | rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT); | |
1884c3cf | 23 | |
b54ad73b | 24 | lidar = new Lidar(I2C.Port.kOnboard); |
d7bf2340 KZ |
25 | leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A, |
26 | Constants.DriveTrain.ENCODER_LEFT_B, false, EncodingType.k4X); | |
27 | rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A, | |
28 | Constants.DriveTrain.ENCODER_RIGHT_B, false, EncodingType.k4X); | |
45bdf5b9 KZ |
29 | leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); |
30 | rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); | |
d7bf2340 KZ |
31 | } |
32 | ||
33 | @Override | |
34 | protected void initDefaultCommand() { | |
35 | } | |
36 | ||
37 | public void resetEncoders() { | |
38 | leftEncoder.reset(); | |
39 | rightEncoder.reset(); | |
40 | } | |
41 | ||
b54ad73b KZ |
42 | public double getLidarDistance() { |
43 | return lidar.pidGet(); | |
44 | } | |
45 | ||
d7bf2340 | 46 | public double getRightSpeed() { |
6833a887 | 47 | return rightEncoder.getRate(); // in inches per second |
d7bf2340 KZ |
48 | } |
49 | ||
50 | public double getLeftSpeed() { | |
6833a887 | 51 | return leftEncoder.getRate(); // in inches per second |
d7bf2340 KZ |
52 | } |
53 | ||
54 | public double getSpeed() { | |
6833a887 | 55 | return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second |
d7bf2340 KZ |
56 | } |
57 | ||
d7bf2340 | 58 | public double getRightDistance() { |
6833a887 | 59 | return rightEncoder.getDistance(); // in inches |
d7bf2340 KZ |
60 | } |
61 | ||
d7bf2340 | 62 | public double getLeftDistance() { |
6833a887 | 63 | return leftEncoder.getDistance(); // in inches |
d7bf2340 KZ |
64 | } |
65 | ||
66 | public double getDistance() { | |
6833a887 | 67 | return (getRightDistance() + getLeftDistance()) / 2.0; // in inches |
d7bf2340 KZ |
68 | } |
69 | ||
70 | public void stop() { | |
71 | setMotorSpeeds(0, 0); | |
72 | } | |
73 | ||
74 | public void setMotorSpeeds(double leftSpeed, double rightSpeed) { | |
1884c3cf KZ |
75 | // speed passed to right motor is negative because right motor rotates in |
76 | // opposite direction | |
d7bf2340 KZ |
77 | this.frontLeft.set(leftSpeed); |
78 | this.frontRight.set(-rightSpeed); | |
79 | this.rearLeft.set(leftSpeed); | |
80 | this.rearRight.set(-rightSpeed); | |
d7bf2340 | 81 | } |
38a404b3 | 82 | } |