Add lidar to drivetrain
[3501/stronghold-2016] / src / org / usfirst / frc / team3501 / robot / subsystems / DriveTrain.java
index 54b0ce01131b2dd8a376e98447b30703c5bc7317..6936abf72335a05142e400b45f673cf63342fcb1 100644 (file)
@@ -1,10 +1,18 @@
 package org.usfirst.frc.team3501.robot.subsystems;
 
 import org.usfirst.frc.team3501.robot.Constants;
+import org.usfirst.frc.team3501.robot.Lidar;
+
 import edu.wpi.first.wpilibj.CANTalon;
+import edu.wpi.first.wpilibj.CounterBase.EncodingType;
+import edu.wpi.first.wpilibj.Encoder;
+import edu.wpi.first.wpilibj.I2C;
 import edu.wpi.first.wpilibj.command.Subsystem;
 
 public class DriveTrain extends Subsystem {
+  // Drivetrain related objects
+  private Encoder leftEncoder, rightEncoder;
+  public static Lidar lidar;
   private CANTalon frontLeft, frontRight, rearLeft, rearRight;
 
   public DriveTrain() {
@@ -12,10 +20,63 @@ public class DriveTrain extends Subsystem {
     frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
     rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
     rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
+
+    lidar = new Lidar(I2C.Port.kOnboard);
+    leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
+        Constants.DriveTrain.ENCODER_LEFT_B, false, EncodingType.k4X);
+    rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
+        Constants.DriveTrain.ENCODER_RIGHT_B, false, EncodingType.k4X);
+    leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE);
+    rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE);
   }
 
   @Override
   protected void initDefaultCommand() {
   }
 
+  public void resetEncoders() {
+    leftEncoder.reset();
+    rightEncoder.reset();
+  }
+
+  public double getLidarDistance() {
+    return lidar.pidGet();
+  }
+
+  public double getRightSpeed() {
+    return rightEncoder.getRate(); // in inches per second
+  }
+
+  public double getLeftSpeed() {
+    return leftEncoder.getRate(); // in inches per second
+  }
+
+  public double getSpeed() {
+    return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
+  }
+
+  public double getRightDistance() {
+    return rightEncoder.getDistance(); // in inches
+  }
+
+  public double getLeftDistance() {
+    return leftEncoder.getDistance(); // in inches
+  }
+
+  public double getDistance() {
+    return (getRightDistance() + getLeftDistance()) / 2.0; // in inches
+  }
+
+  public void stop() {
+    setMotorSpeeds(0, 0);
+  }
+
+  public void setMotorSpeeds(double leftSpeed, double rightSpeed) {
+    // speed passed to right motor is negative because right motor rotates in
+    // opposite direction
+    this.frontLeft.set(leftSpeed);
+    this.frontRight.set(-rightSpeed);
+    this.rearLeft.set(leftSpeed);
+    this.rearRight.set(-rightSpeed);
+  }
 }