package org.usfirst.frc.team3501.robot.subsystems;
import org.usfirst.frc.team3501.robot.Constants;
-import org.usfirst.frc.team3501.robot.MathLib;
import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive;
-import org.usfirst.frc.team3501.robot.sensors.GyroLib;
-import org.usfirst.frc.team3501.robot.sensors.Lidar;
import edu.wpi.first.wpilibj.CANTalon;
import edu.wpi.first.wpilibj.CounterBase.EncodingType;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.DoubleSolenoid.Value;
import edu.wpi.first.wpilibj.Encoder;
-import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.command.PIDSubsystem;
public class DriveTrain extends PIDSubsystem {
- // Current Drive Mode Default Drive Mode is Manual
- private int DRIVE_MODE = 1;
private boolean outputFlipped = false;
private static double pidOutput = 0;
private Encoder leftEncoder, rightEncoder;
- public static Lidar lidar;
-
private CANTalon frontLeft, frontRight, rearLeft, rearRight;
private RobotDrive robotDrive;
- private GyroLib gyro;
private DoubleSolenoid leftGearPiston, rightGearPiston;
// Drivetrain specific constants that relate to the inches per pulse value for
robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
- lidar = new Lidar(I2C.Port.kMXP);
leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
Constants.DriveTrain.ENCODER_LEFT_B, false, EncodingType.k4X);
rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE);
rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE);
- gyro = new GyroLib(I2C.Port.kOnboard, false);
-
- DRIVE_MODE = Constants.DriveTrain.ENCODER_MODE;
- setEncoderPID();
this.disable();
- gyro.start();
leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.LEFT_MODULE,
Constants.DriveTrain.LEFT_FORWARD, Constants.DriveTrain.LEFT_REVERSE);
rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.RIGHT_MODULE,
Constants.DriveTrain.RIGHT_FORWARD, Constants.DriveTrain.RIGHT_REVERSE);
- Constants.DriveTrain.inverted = false;
}
@Override
rightEncoder.reset();
}
- public double getLidarDistance() {
- return lidar.pidGet();
- }
-
public double getRightSpeed() {
return rightEncoder.getRate(); // in inches per second
}
// Get error between the setpoint of PID Controller and the current state of
// the robot
public double getError() {
- if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE)
- return Math.abs(this.getSetpoint() - getAvgEncoderDistance());
- else
- return Math.abs(this.getSetpoint() + getGyroAngle());
- }
-
- public double getGyroAngle() {
- return gyro.getRotationZ().getAngle();
- }
-
- public void resetGyro() {
- gyro.reset();
+ return Math.abs(this.getSetpoint() - getAvgEncoderDistance());
}
public void printEncoder(int i, int n) {
}
}
- public void printGyroOutput() {
- System.out.println("Gyro Angle" + -this.getGyroAngle());
- }
-
/*
* returns the PID output that is returned by the PID Controller
*/
// Updates the PID constants based on which control mode is being used
public void updatePID() {
- if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE)
- this.getPIDController().setPID(Constants.DriveTrain.kp,
- Constants.DriveTrain.ki, Constants.DriveTrain.kd);
- else
- this.getPIDController().setPID(Constants.DriveTrain.gp,
- Constants.DriveTrain.gd, Constants.DriveTrain.gi);
+ this.getPIDController().setPID(Constants.DriveTrain.kp,
+ Constants.DriveTrain.ki, Constants.DriveTrain.kd);
}
public CANTalon getFrontLeft() {
return rearRight;
}
- public int getMode() {
- return DRIVE_MODE;
- }
-
/*
* Method is a required method that the PID Subsystem uses to return the
* calculated PID value to the driver
protected void usePIDOutput(double output) {
double left = 0;
double right = 0;
- if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE) {
- double drift = this.getLeftDistance() - this.getRightDistance();
- if (Math.abs(output) > 0 && Math.abs(output) < 0.3)
- output = Math.signum(output) * 0.3;
- left = output;
- right = output + drift * Constants.DriveTrain.kp / 10;
- } else if (DRIVE_MODE == Constants.DriveTrain.GYRO_MODE) {
- left = output;
- right = -output;
- }
+ double drift = this.getLeftDistance() - this.getRightDistance();
+ if (Math.abs(output) > 0 && Math.abs(output) < 0.3)
+ output = Math.signum(output) * 0.3;
+ left = output;
+ right = output + drift * Constants.DriveTrain.kp / 10;
drive(left, right);
pidOutput = output;
}
* both sides of tank drive for Encoder Mode Angle from the gyro in GYRO_MODE
*/
private double sensorFeedback() {
- if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE)
- return getAvgEncoderDistance();
- else if (DRIVE_MODE == Constants.DriveTrain.GYRO_MODE)
- return -this.getGyroAngle();
- // counterclockwise is positive on joystick but we want it to be negative
- else
- return 0;
+ return getAvgEncoderDistance();
}
/*
*/
public void drive(double left, double right) {
robotDrive.tankDrive(-left, -right);
- // dunno why but inverted drive (- values is forward)
- if (!Constants.DriveTrain.inverted)
- robotDrive.tankDrive(-left, -right);
- else
- robotDrive.tankDrive(right, left);
- }
-
- /*
- * constrains the distance to within -100 and 100 since we aren't going to
- * drive more than 100 inches
- *
- * Configure Encoder PID
- *
- * Sets the setpoint to the PID subsystem
- */
- public void driveDistance(double dist, double maxTimeOut) {
- dist = MathLib.constrain(dist, -100, 100);
- setEncoderPID();
- setSetpoint(dist);
- }
-
- /*
- * Sets the encoder mode Updates the PID constants sets the tolerance and sets
- * output/input ranges Enables the PID controllers
- */
- public void setEncoderPID() {
- DRIVE_MODE = Constants.DriveTrain.ENCODER_MODE;
- this.updatePID();
- this.setAbsoluteTolerance(Constants.DriveTrain.encoderTolerance);
- this.setOutputRange(-1.0, 1.0);
- this.setInputRange(-200.0, 200.0);
- this.enable();
- }
-
- /*
- * Sets the Gyro Mode Updates the PID constants, sets the tolerance and sets
- * output/input ranges Enables the PID controllers
- */
- private void setGyroPID() {
- DRIVE_MODE = Constants.DriveTrain.GYRO_MODE;
- this.updatePID();
- this.getPIDController().setPID(Constants.DriveTrain.gp,
- Constants.DriveTrain.gi, Constants.DriveTrain.gd);
-
- this.setAbsoluteTolerance(Constants.DriveTrain.gyroTolerance);
- this.setOutputRange(-1.0, 1.0);
- this.setInputRange(-360.0, 360.0);
- this.enable();
- }
-
- /*
- * Turning method that should be used repeatedly in a command
- *
- * First constrains the angle to within -360 and 360 since that is as much as
- * we need to turn
- *
- * Configures Gyro PID and sets the setpoint as an angle
- */
- public void turnAngle(double angle) {
- angle = MathLib.constrain(angle, -360, 360);
- setGyroPID();
- setSetpoint(angle);
}
public void setMotorSpeeds(double left, double right) {