From 7ba6bc91b8cf7205e05cdb974dc80fdd668ebe59 Mon Sep 17 00:00:00 2001 From: Cindy Zhang Date: Tue, 21 Feb 2017 10:18:47 -0800 Subject: [PATCH] code review changes --- .../shooter/DecreaseShootingSpeed.java | 2 +- .../shooter/IncreaseShootingSpeed.java | 2 +- .../commands/shooter/ResetShootingSpeed.java | 2 +- .../robot/commands/shooter/RunFlyWheel.java | 48 +++++----------- .../shooter/RunFlyWheelContinuous.java | 21 +------ .../robot/commands/shooter/RunIndexWheel.java | 4 +- .../shooter/RunIndexWheelContinuous.java | 27 +++++---- .../team3501/robot/subsystems/Shooter.java | 56 ++++++++++++++----- 8 files changed, 81 insertions(+), 81 deletions(-) diff --git a/src/org/usfirst/frc/team3501/robot/commands/shooter/DecreaseShootingSpeed.java b/src/org/usfirst/frc/team3501/robot/commands/shooter/DecreaseShootingSpeed.java index d105b60..7fa6cd0 100644 --- a/src/org/usfirst/frc/team3501/robot/commands/shooter/DecreaseShootingSpeed.java +++ b/src/org/usfirst/frc/team3501/robot/commands/shooter/DecreaseShootingSpeed.java @@ -21,7 +21,7 @@ public class DecreaseShootingSpeed extends Command { @Override protected void initialize() { - shooter.decrementCurrentShootingSpeed(); + shooter.decrementTargetShootingSpeed(); } @Override diff --git a/src/org/usfirst/frc/team3501/robot/commands/shooter/IncreaseShootingSpeed.java b/src/org/usfirst/frc/team3501/robot/commands/shooter/IncreaseShootingSpeed.java index 1588482..8a2fbd9 100644 --- a/src/org/usfirst/frc/team3501/robot/commands/shooter/IncreaseShootingSpeed.java +++ b/src/org/usfirst/frc/team3501/robot/commands/shooter/IncreaseShootingSpeed.java @@ -21,7 +21,7 @@ public class IncreaseShootingSpeed extends Command { @Override protected void initialize() { - shooter.incrementCurrentShootingSpeed(); + shooter.incrementTargetShootingSpeed(); } @Override diff --git a/src/org/usfirst/frc/team3501/robot/commands/shooter/ResetShootingSpeed.java b/src/org/usfirst/frc/team3501/robot/commands/shooter/ResetShootingSpeed.java index 1bcf897..8eb98bb 100644 --- a/src/org/usfirst/frc/team3501/robot/commands/shooter/ResetShootingSpeed.java +++ b/src/org/usfirst/frc/team3501/robot/commands/shooter/ResetShootingSpeed.java @@ -20,7 +20,7 @@ public class ResetShootingSpeed extends Command { @Override protected void initialize() { - shooter.resetCurrentShootingSpeed(); + shooter.resetTargetShootingSpeed(); } @Override diff --git a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheel.java b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheel.java index c5101fc..5abd73a 100644 --- a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheel.java +++ b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheel.java @@ -1,4 +1,3 @@ - package org.usfirst.frc.team3501.robot.commands.shooter; import org.usfirst.frc.team3501.robot.Robot; @@ -8,66 +7,49 @@ import org.usfirst.frc.team3501.robot.utils.PIDController; import edu.wpi.first.wpilibj.command.Command; /** - * This command runs the fly wheel at a specific speed using a PID Controller - * for accuracy for a given time. The fly wheel is intended to shoot balls fed - * by the intake wheel. + * This command runs the fly wheel continuously at a set speed using a PID + * Controller when OI button managing fly wheel is pressed. The command will run + * the fly wheel motor until the button triggering it is released. + * + * Should only be run from the operator interface. + * + * pre-condition: This command must be run by a button in OI, with + * button.whileHeld(...). * * @author Shaina & Chris */ public class RunFlyWheel extends Command { private Shooter shooter = Robot.getShooter(); - private double maxTimeOut; + double time; private PIDController wheelController; - private double wheelP; - private double wheelI; - private double wheelD; - private double target; - private double shooterSpeed = 0; - - public RunFlyWheel(double maxTimeOut) { - this.wheelP = this.shooter.wheelP; - this.wheelI = this.shooter.wheelI; - this.wheelD = this.shooter.wheelD; - this.wheelController = new PIDController(this.wheelP, this.wheelI, - this.wheelD); - this.wheelController.setDoneRange(0.5); - this.wheelController.setMaxOutput(1.0); - this.wheelController.setMinDoneCycles(3); - this.target = this.shooter.getCurrentShootingSpeed(); + public RunFlyWheel(double time) { + this.time = time; } - // Called just before this Command runs the first time @Override protected void initialize() { - this.wheelController.setSetPoint(this.target); + shooter.initializePIDController(); } - // Called repeatedly when this Command is scheduled to run @Override protected void execute() { - double calculatedShooterIncrement = this.wheelController - .calcPID(this.shooter.getShooterRPM()); - shooterSpeed += calculatedShooterIncrement; - this.shooter.setFlyWheelMotorVal(shooterSpeed); + shooter.setFlyWheelMotorVal(shooter.calculateShooterSpeed()); } - // Make this return true when this Command no longer needs to run execute() @Override protected boolean isFinished() { - return timeSinceInitialized() >= maxTimeOut; + return timeSinceInitialized() >= time; } - // Called once after isFinished returns true @Override protected void end() { this.shooter.stopFlyWheel(); } - // Called when another command which requires one or more of the same - // subsystems is scheduled to run @Override protected void interrupted() { + end(); } } diff --git a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheelContinuous.java b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheelContinuous.java index fa5426e..86c31f7 100644 --- a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheelContinuous.java +++ b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheelContinuous.java @@ -22,35 +22,18 @@ public class RunFlyWheelContinuous extends Command { private Shooter shooter = Robot.getShooter(); private PIDController wheelController; - private double wheelP; - private double wheelI; - private double wheelD; - private double target; - private double shooterSpeed = 0; public RunFlyWheelContinuous() { - this.wheelP = this.shooter.wheelP; - this.wheelI = this.shooter.wheelI; - this.wheelD = this.shooter.wheelD; - this.wheelController = new PIDController(this.wheelP, this.wheelI, - this.wheelD); - this.wheelController.setDoneRange(10); - this.wheelController.setMaxOutput(1.0); - this.wheelController.setMinDoneCycles(3); - this.target = shooter.getCurrentShootingSpeed(); } @Override protected void initialize() { - this.wheelController.setSetPoint(this.target); + shooter.initializePIDController(); } @Override protected void execute() { - double calculatedShooterIncrement = this.wheelController - .calcPID(this.shooter.getShooterRPM()); - shooterSpeed += calculatedShooterIncrement; - this.shooter.setFlyWheelMotorVal(shooterSpeed); + shooter.setFlyWheelMotorVal(shooter.calculateShooterSpeed()); } @Override diff --git a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunIndexWheel.java b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunIndexWheel.java index d9966f1..6abb095 100644 --- a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunIndexWheel.java +++ b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunIndexWheel.java @@ -39,7 +39,9 @@ public class RunIndexWheel extends Command { @Override protected void execute() { double shooterSpeed = shooter.getShooterRPM(); - if (shooterSpeed > 0) + double targetShooterSpeed = shooter.getTargetShootingSpeed(); + double threshold = shooter.getRPMThreshold(); + if (Math.abs(shooterSpeed - targetShooterSpeed) <= threshold) shooter.runIndexWheel(); } diff --git a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunIndexWheelContinuous.java b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunIndexWheelContinuous.java index da3fee1..08ead3a 100644 --- a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunIndexWheelContinuous.java +++ b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunIndexWheelContinuous.java @@ -1,5 +1,6 @@ package org.usfirst.frc.team3501.robot.commands.shooter; +import org.usfirst.frc.team3501.robot.Constants; import org.usfirst.frc.team3501.robot.Robot; import org.usfirst.frc.team3501.robot.subsystems.Shooter; @@ -22,35 +23,41 @@ public class RunIndexWheelContinuous extends Command { /** * See JavaDoc comment in class for details - * - * @param motorVal - * value range from -1 to 1 */ public RunIndexWheelContinuous() { requires(shooter); } - // Called just before this Command runs the first time @Override protected void initialize() { } - // Called repeatedly when this Command is scheduled to run @Override protected void execute() { double shooterSpeed = shooter.getShooterRPM(); - if (shooterSpeed > 0) - shooter.runIndexWheel(); + double targetShooterSpeed = shooter.getTargetShootingSpeed(); + double threshold = shooter.getRPMThreshold(); + // if (Math.abs(shooterSpeed - targetShooterSpeed) <= threshold) + + if (timeSinceInitialized() % 0.5 <= 0.02) { + + if (Robot.getDriveTrain() + .getLeftGearPistonValue() == Constants.DriveTrain.LOW_GEAR) { + System.out.println("shifting to low gear " + timeSinceInitialized()); + Robot.getDriveTrain().setHighGear(); + } else { + System.out.println("shifting to high gear " + timeSinceInitialized()); + Robot.getDriveTrain().setLowGear(); + } + } + shooter.runIndexWheel(); } - // Called once after isFinished returns true @Override protected void end() { shooter.stopIndexWheel(); } - // Called when another command which requires one or more of the same - // subsystems is scheduled to run @Override protected void interrupted() { end(); diff --git a/src/org/usfirst/frc/team3501/robot/subsystems/Shooter.java b/src/org/usfirst/frc/team3501/robot/subsystems/Shooter.java index 91da098..6397292 100644 --- a/src/org/usfirst/frc/team3501/robot/subsystems/Shooter.java +++ b/src/org/usfirst/frc/team3501/robot/subsystems/Shooter.java @@ -3,22 +3,27 @@ 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.utils.HallEffectSensor; +import org.usfirst.frc.team3501.robot.utils.PIDController; import com.ctre.CANTalon; import edu.wpi.first.wpilibj.command.Subsystem; public class Shooter extends Subsystem { - public double wheelP = 0.0001, wheelI = 0, wheelD = 0.0004; + public double wheelP = 0.00007, wheelI = 0, wheelD = 0.0008; private static Shooter shooter; private static HallEffectSensor hallEffect; private final CANTalon flyWheel1, flyWheel2, indexWheel; - private static final double DEFAULT_INDEXING_SPEED = -0.75; + private PIDController wheelController; + + private static final double RPM_THRESHOLD = 10; + private static final double DEFAULT_INDEXING_MOTOR_VALUE = -0.75; private static final double DEFAULT_SHOOTING_SPEED = 2700; // rpm private static final double SHOOTING_SPEED_INCREMENT = 25; - private double currentShootingSpeed = DEFAULT_SHOOTING_SPEED; + private double targetShootingSpeed = DEFAULT_SHOOTING_SPEED; + private double currentShooterMotorValue = 0; private Shooter() { flyWheel1 = new CANTalon(Constants.Shooter.FLY_WHEEL1); @@ -83,35 +88,56 @@ public class Shooter extends Subsystem { } + public double getRPMThreshold() { + return RPM_THRESHOLD; + } + public double getShooterRPM() { return hallEffect.getRPM(); } - public void setCurrentShootingSpeed(double Value) { - currentShootingSpeed = Value; + public void setTargetShootingSpeed(double Value) { + targetShootingSpeed = Value; } - public void decrementCurrentShootingSpeed() { - this.currentShootingSpeed -= this.SHOOTING_SPEED_INCREMENT; + public void decrementTargetShootingSpeed() { + this.targetShootingSpeed -= this.SHOOTING_SPEED_INCREMENT; } - public void incrementCurrentShootingSpeed() { - this.currentShootingSpeed += this.SHOOTING_SPEED_INCREMENT; + public void incrementTargetShootingSpeed() { + this.targetShootingSpeed += this.SHOOTING_SPEED_INCREMENT; } - public void resetCurrentShootingSpeed() { - this.currentShootingSpeed = this.DEFAULT_SHOOTING_SPEED; + public void resetTargetShootingSpeed() { + this.targetShootingSpeed = this.DEFAULT_SHOOTING_SPEED; } - public double getCurrentShootingSpeed() { - return currentShootingSpeed; + public double getTargetShootingSpeed() { + return targetShootingSpeed; } public void reverseIndexWheel() { - this.setIndexWheelMotorVal(-DEFAULT_INDEXING_SPEED); + this.setIndexWheelMotorVal(-DEFAULT_INDEXING_MOTOR_VALUE); } public void runIndexWheel() { - this.setIndexWheelMotorVal(DEFAULT_INDEXING_SPEED); + this.setIndexWheelMotorVal(DEFAULT_INDEXING_MOTOR_VALUE); + } + + public double calculateShooterSpeed() { + this.wheelController.setSetPoint(targetShootingSpeed); + double calculatedShooterIncrement = this.wheelController + .calcPID(this.getShooterRPM()); + currentShooterMotorValue += calculatedShooterIncrement; + return currentShooterMotorValue; + } + + public void initializePIDController() { + this.wheelController = new PIDController(wheelP, wheelI, wheelD); + this.wheelController.setDoneRange(10); + this.wheelController.setMaxOutput(1.0); + this.wheelController.setMinDoneCycles(3); + this.wheelController.setSetPoint(this.targetShootingSpeed); + this.currentShooterMotorValue = 0; } } -- 2.30.2