X-Git-Url: http://challenge-bot.com/repos/?a=blobdiff_plain;f=src%2Forg%2Fusfirst%2Ffrc%2Fteam3501%2Frobot%2Fsubsystems%2FDriveTrain.java;h=e19b46f02643a6c24af4d7d261ae9f13fb6c74a8;hb=ca325a5866b4f601b171b02ac767393bd996d44a;hp=81f80b92ffa3d7ccd670757b3444273ca8438580;hpb=33141cddfe931d276b0068687c9bac25f29f8ba5;p=3501%2Fstronghold-2016 diff --git a/src/org/usfirst/frc/team3501/robot/subsystems/DriveTrain.java b/src/org/usfirst/frc/team3501/robot/subsystems/DriveTrain.java index 81f80b92..e19b46f0 100644 --- a/src/org/usfirst/frc/team3501/robot/subsystems/DriveTrain.java +++ b/src/org/usfirst/frc/team3501/robot/subsystems/DriveTrain.java @@ -1,32 +1,41 @@ package org.usfirst.frc.team3501.robot.subsystems; import org.usfirst.frc.team3501.robot.Constants; -import org.usfirst.frc.team3501.robot.GyroLib; 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 { - private static double pidOutput = 0; - private static double encoderTolerance = 8.0, gyroTolerance = 5.0; + // Current Drive Mode Default Drive Mode is Manual private int DRIVE_MODE = 1; - - private static final int MANUAL_MODE = 1, ENCODER_MODE = 2, GYRO_MODE = 3; + 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 + // the encoders public DriveTrain() { - super(kp, ki, kd); + super(Constants.DriveTrain.kp, Constants.DriveTrain.ki, + Constants.DriveTrain.kd); frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT); frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT); @@ -34,6 +43,9 @@ public class DriveTrain extends PIDSubsystem { rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT); robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight); + + 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, @@ -51,6 +63,11 @@ public class DriveTrain extends PIDSubsystem { this.disable(); gyro.start(); + leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.LEFT_FORWARD, + Constants.DriveTrain.LEFT_REVERSE); + rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.RIGHT_FORWARD, + Constants.DriveTrain.RIGHT_REVERSE); + Constants.DriveTrain.inverted = false; } @Override @@ -58,6 +75,7 @@ public class DriveTrain extends PIDSubsystem { setDefaultCommand(new JoystickDrive()); } + // Print tne PID Output public void printOutput() { System.out.println("PIDOutput: " + pidOutput); } @@ -66,6 +84,8 @@ public class DriveTrain extends PIDSubsystem { return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2; } + // Whether or not the PID Controller thinks we have reached the target + // setpoint public boolean reachedTarget() { if (this.onTarget()) { this.disable(); @@ -84,6 +104,10 @@ public class DriveTrain extends PIDSubsystem { rightEncoder.reset(); } + public double getLidarDistance() { + return lidar.pidGet(); + } + public double getRightSpeed() { return rightEncoder.getRate(); // in inches per second } @@ -104,6 +128,8 @@ public class DriveTrain extends PIDSubsystem { return leftEncoder.getDistance(); // in inches } + // 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()); @@ -131,15 +157,21 @@ public class DriveTrain extends PIDSubsystem { System.out.println("Gyro Angle" + -this.getGyroAngle()); } + /* + * returns the PID output that is returned by the PID Controller + */ public double getOutput() { return pidOutput; } + // 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(kp, ki, kd); + this.getPIDController().setPID(Constants.DriveTrain.kp, + Constants.DriveTrain.ki, Constants.DriveTrain.kd); else - this.getPIDController().setPID(gp, gd, gi); + this.getPIDController().setPID(Constants.DriveTrain.gp, + Constants.DriveTrain.gd, Constants.DriveTrain.gi); } public CANTalon getFrontLeft() { @@ -162,6 +194,15 @@ public class DriveTrain extends PIDSubsystem { return DRIVE_MODE; } + /* + * Method is a required method that the PID Subsystem uses to return the + * calculated PID value to the driver + * + * @param Gives the user the output from the PID algorithm that is calculated + * internally + * + * Body: Uses the output, does some filtering and drives the robot + */ @Override protected void usePIDOutput(double output) { double left = 0; @@ -171,9 +212,8 @@ public class DriveTrain extends PIDSubsystem { if (Math.abs(output) > 0 && Math.abs(output) < 0.3) output = Math.signum(output) * 0.3; left = output; - right = output + drift * kp / 10; - } - else if (DRIVE_MODE == Constants.DriveTrain.GYRO_MODE) { + right = output + drift * Constants.DriveTrain.kp / 10; + } else if (DRIVE_MODE == Constants.DriveTrain.GYRO_MODE) { left = output; right = -output; } @@ -186,6 +226,18 @@ public class DriveTrain extends PIDSubsystem { return sensorFeedback(); } + /* + * Checks the drive mode <<<<<<< 9728080f491e9fb09795494349dba1297f447c0f + * + * @return the current state of the robot in each state Average distance from + * both sides of tank drive for Encoder Mode Angle from the gyro in GYRO_MODE + * ======= + * + * @return the current state of the robot in each state Average distance from + * both sides of tank drive for Encoder Mode Angle from the gyro in GYRO_MODE + * >>>>>>> Move all constants in DeadReckoning to Auton class because it makes + * more sense + */ private double sensorFeedback() { if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE) return getAvgEncoderDistance(); @@ -196,37 +248,72 @@ public class DriveTrain extends PIDSubsystem { return 0; } + /* + * @param left and right setpoints to set to the left and right side of tank + * inverted is for Logan, wants the robot to invert all controls left = right + * and right = left negative input is required for the regular rotation + * because RobotDrive tankdrive method drives inverted + */ 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(encoderTolerance); + 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(gp, gi, gd); + this.getPIDController().setPID(Constants.DriveTrain.gp, + Constants.DriveTrain.gi, Constants.DriveTrain.gd); - this.setAbsoluteTolerance(gyroTolerance); + 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(); @@ -242,6 +329,45 @@ public class DriveTrain extends PIDSubsystem { rearRight.set(right); } - private static double kp = 0.013, ki = 0.000015, kd = -0.002; - private static double gp = 0.018, gi = 0.000015, gd = 0; + /* + * @return a value that is the current setpoint for the piston kReverse or + * kForward + */ + public Value getLeftGearPistonValue() { + return leftGearPiston.get(); + } + + /* + * @return a value that is the current setpoint for the piston kReverse or + * kForward + */ + public Value getRightGearPistonValue() { + return rightGearPiston.get(); + } + + /* + * Changes the ball shift gear assembly to high + */ + public void setHighGear() { + changeGear(Constants.DriveTrain.HIGH_GEAR); + } + + /* + * Changes the ball shift gear assembly to low + */ + public void setLowGear() { + changeGear(Constants.DriveTrain.LOW_GEAR); + } + + /* + * changes the gear to a DoubleSolenoid.Value + */ + public void changeGear(DoubleSolenoid.Value gear) { + leftGearPiston.set(gear); + rightGearPiston.set(gear); + } + + public void toggleTimeDeadReckoning() { + Constants.Auton.isUsingTime = !Constants.Auton.isUsingTime; + } }