From 8124fa7a3a77f9a17e4b8c88689cf64248f2f3ba Mon Sep 17 00:00:00 2001 From: nightsedge46 Date: Sun, 2 Feb 2014 14:03:27 -0700 Subject: [PATCH 1/6] Started serious work on autonomous driving. --- org/bitbuckets/frc2014/RandomConstants.java | 2 - org/bitbuckets/frc2014/RobotMap.java | 4 +- .../frc2014/commands/DriveStraight.java | 50 +++++++++++++++++++ .../frc2014/subsystems/DriveTrain.java | 45 +++++++++++++++-- 4 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 org/bitbuckets/frc2014/commands/DriveStraight.java diff --git a/org/bitbuckets/frc2014/RandomConstants.java b/org/bitbuckets/frc2014/RandomConstants.java index b2d1fc4..85d37c3 100644 --- a/org/bitbuckets/frc2014/RandomConstants.java +++ b/org/bitbuckets/frc2014/RandomConstants.java @@ -35,8 +35,6 @@ public class RandomConstants { public static final double INCH_PID_KI = 0.3; /** The KD for the pid controller used to drive n inches. **/ public static final double INCH_PID_KD = 0.3; - /** The KF for the pid controller used to drive n inches. **/ - public static final double INCH_PID_KF = 0.3; /** The tolerence for the pid controller used to drive n inches. **/ public static final double INCH_PID_TOL = .1; diff --git a/org/bitbuckets/frc2014/RobotMap.java b/org/bitbuckets/frc2014/RobotMap.java index d39a1a0..d53847e 100644 --- a/org/bitbuckets/frc2014/RobotMap.java +++ b/org/bitbuckets/frc2014/RobotMap.java @@ -82,7 +82,7 @@ public class RobotMap { /** * The right encoder for the drivetrain. */ - public static final int R_ENCODER_B = 2; + public static final int R_ENCODER_B = 3; /** * The left encoder for the drivetrain. */ @@ -90,7 +90,7 @@ public class RobotMap { /** * The left encoder for the drivetrain. */ - public static final int L_ENCODER_B = 4; + public static final int L_ENCODER_B = 5; /** * The switch that tells the compressor the max pressure */ diff --git a/org/bitbuckets/frc2014/commands/DriveStraight.java b/org/bitbuckets/frc2014/commands/DriveStraight.java new file mode 100644 index 0000000..58ec0cc --- /dev/null +++ b/org/bitbuckets/frc2014/commands/DriveStraight.java @@ -0,0 +1,50 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.bitbuckets.frc2014.commands; + +import edu.wpi.first.wpilibj.command.PIDCommand; +import org.bitbuckets.frc2014.RandomConstants; + +/** + * + * @author Alia + */ +public class DriveStraight extends PIDCommand { + + public DriveStraight() { + super("DriveStriaght", RandomConstants.INCH_PID_KP, RandomConstants.INCH_PID_KI, RandomConstants.INCH_PID_KD); + } + + // Called just before this Command runs the first time + protected void initialize() { + } + + // Called repeatedly when this Command is scheduled to run + protected void execute() { + } + + // Make this return true when this Command no longer needs to run execute() + protected boolean isFinished() { + return false; + } + + // Called once after isFinished returns true + protected void end() { + } + + // Called when another command which requires one or more of the same + // subsystems is scheduled to run + protected void interrupted() { + } + + protected double returnPIDInput() { + return (CommandBase.driveTrain.getEncRateL() + CommandBase.driveTrain.getEncRateR())/2; + } + + protected void usePIDOutput(double output) { + CommandBase.driveTrain.autonDrive(output, output); + } +} diff --git a/org/bitbuckets/frc2014/subsystems/DriveTrain.java b/org/bitbuckets/frc2014/subsystems/DriveTrain.java index b59534c..df40aad 100644 --- a/org/bitbuckets/frc2014/subsystems/DriveTrain.java +++ b/org/bitbuckets/frc2014/subsystems/DriveTrain.java @@ -6,6 +6,7 @@ package org.bitbuckets.frc2014.subsystems; +import edu.wpi.first.wpilibj.Encoder; import edu.wpi.first.wpilibj.RobotDrive; import edu.wpi.first.wpilibj.command.Subsystem; import org.bitbuckets.frc2014.RandomConstants; @@ -18,12 +19,16 @@ * Collection of actuators and sensors that form the drive train subsystem. */ public class DriveTrain extends Subsystem { - /** Base driving system standard in wpilibj */ + /** Base driving system standard in wpilibj **/ public RobotDrive drive; - /** Forward/backward power ranging from -1 (bwd) to 1 (fwd) */ - double throttle = 1; - /** Rotational power ranging from -1 (ccw) to 1 (cw) */ - double rotation = 1; + /** Forward/backward power ranging from -1 (bwd) to 1 (fwd) **/ + private double throttle = 1; + /** Rotational power ranging from -1 (ccw) to 1 (cw) **/ + private double rotation = 1; + /** Right encoder. **/ + private Encoder encR; + /** Left encoder. **/ + private Encoder encL; /** * Drivetrain constructor, sets up basic robot drive. @@ -31,6 +36,8 @@ public class DriveTrain extends Subsystem { public DriveTrain() { super(); drive = new RobotDrive(RobotMap.R_MOTOR, RobotMap.L_MOTOR); + encR = new Encoder(RobotMap.R_ENCODER_A, RobotMap.R_ENCODER_B); + encL = new Encoder(RobotMap.L_ENCODER_A, RobotMap.L_ENCODER_B); } /** @@ -40,6 +47,24 @@ public void initDefaultCommand() { } + /** + * Gets the rate of the right encoder. + * + * @return The rate of the right encoder. + */ + public double getEncRateR(){ + return encR.getRate(); + } + + /** + * Gets the rate of the left encoder. + * + * @return The rate of the left encoder. + */ + public double getEncRateL(){ + return encL.getRate(); + } + /** * Basic driving method. Can be used in autonomous and teleop. * @@ -52,6 +77,16 @@ public void drive(double throttle, double rotation){ drive.arcadeDrive(throttle, rotation); } + /** + * Used for auton. + * + * @param left + * @param right + */ + public void autonDrive(double left, double right){ + drive.tankDrive(left, right); + } + /** * More sophicsticated driving method for teleop based on team 254's * "cheesy drive". From a511974fd06deedb21ccb9cf47cea3a016da80f5 Mon Sep 17 00:00:00 2001 From: bitbuckets4183 Date: Sun, 2 Feb 2014 17:07:27 -0700 Subject: [PATCH 2/6] Added straight driving code. --- org/bitbuckets/frc2014/RandomConstants.java | 3 +- .../frc2014/commands/DriveStraight.java | 47 +++++++++++++++++-- .../frc2014/subsystems/DriveTrain.java | 20 +++++++- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/org/bitbuckets/frc2014/RandomConstants.java b/org/bitbuckets/frc2014/RandomConstants.java index 85d37c3..79a80b3 100644 --- a/org/bitbuckets/frc2014/RandomConstants.java +++ b/org/bitbuckets/frc2014/RandomConstants.java @@ -37,7 +37,8 @@ public class RandomConstants { public static final double INCH_PID_KD = 0.3; /** The tolerence for the pid controller used to drive n inches. **/ public static final double INCH_PID_TOL = .1; - + /** The max acceleration for the auton drivestraight. **/ + public static final double INCH_MAX_ACCEL = .1; ///////////////----Catapault----/////////////// /** **/ diff --git a/org/bitbuckets/frc2014/commands/DriveStraight.java b/org/bitbuckets/frc2014/commands/DriveStraight.java index 58ec0cc..ab68080 100644 --- a/org/bitbuckets/frc2014/commands/DriveStraight.java +++ b/org/bitbuckets/frc2014/commands/DriveStraight.java @@ -13,22 +13,63 @@ * @author Alia */ public class DriveStraight extends PIDCommand { + private int state; + private double distance; + private double maxVel; + private long timeInit; + private boolean finish = false; - public DriveStraight() { + public DriveStraight(int dist, int vel) { super("DriveStriaght", RandomConstants.INCH_PID_KP, RandomConstants.INCH_PID_KI, RandomConstants.INCH_PID_KD); + distance = dist; + maxVel = vel; + setSetpoint(0); } // Called just before this Command runs the first time protected void initialize() { + timeInit = System.currentTimeMillis(); + state = 1; } // Called repeatedly when this Command is scheduled to run protected void execute() { + switch(state){ + case 1: + setSetpoint(RandomConstants.INCH_MAX_ACCEL*(System.currentTimeMillis()-timeInit)/1000); + if(getSetpoint() >= maxVel){ + state = 2; + } + if((CommandBase.driveTrain.getEncDistL() + CommandBase.driveTrain.getEncDistR()) >= distance){ + state = 3; + maxVel = getSetpoint(); + timeInit = System.currentTimeMillis(); + } + break; + case 2: + setSetpoint(maxVel); + //the distance under the last trapezoid of the curve + if(distance - (CommandBase.driveTrain.getEncDistL() + CommandBase.driveTrain.getEncDistR())/2 <= (maxVel*maxVel)/RandomConstants.INCH_MAX_ACCEL){ + state = 4; + timeInit = System.currentTimeMillis(); + } + break; + case 3: + setSetpoint(maxVel-RandomConstants.INCH_MAX_ACCEL*(System.currentTimeMillis()-timeInit)/1000); + if(getSetpoint() <= 0){ + state = 4; + } + break; + case 4: + setSetpoint(0); + finish = true; + break; + } } // Make this return true when this Command no longer needs to run execute() protected boolean isFinished() { - return false; + return finish; } // Called once after isFinished returns true @@ -45,6 +86,6 @@ protected double returnPIDInput() { } protected void usePIDOutput(double output) { - CommandBase.driveTrain.autonDrive(output, output); + CommandBase.driveTrain.tankDrive(output, output); } } diff --git a/org/bitbuckets/frc2014/subsystems/DriveTrain.java b/org/bitbuckets/frc2014/subsystems/DriveTrain.java index df40aad..a5830a8 100644 --- a/org/bitbuckets/frc2014/subsystems/DriveTrain.java +++ b/org/bitbuckets/frc2014/subsystems/DriveTrain.java @@ -47,6 +47,24 @@ public void initDefaultCommand() { } + /** + * Gets the distance of the right encoder since the last reset. + * + * @return The distance of the right encoder. + */ + public double getEncDistR(){ + return encR.getDistance(); + } + + /** + * Gets the distance of the left encoder since the last reset. + * + * @return The distance of the left encoder. + */ + public double getEncDistL(){ + return encL.getDistance(); + } + /** * Gets the rate of the right encoder. * @@ -83,7 +101,7 @@ public void drive(double throttle, double rotation){ * @param left * @param right */ - public void autonDrive(double left, double right){ + public void tankDrive(double left, double right){ drive.tankDrive(left, right); } From 4004826f8870ad7b688e97043b87feec6f97c912 Mon Sep 17 00:00:00 2001 From: bitbuckets4183 Date: Wed, 5 Feb 2014 18:39:34 -0700 Subject: [PATCH 3/6] Added DriveTurn, finished DriveStraight. --- org/bitbuckets/frc2014/RandomConstants.java | 14 ++- org/bitbuckets/frc2014/RobotMap.java | 8 ++ .../frc2014/commands/DriveStraight.java | 56 +++++++--- .../frc2014/commands/DriveTurn.java | 100 ++++++++++++++++++ .../frc2014/commands/ExampleCommand.java | 42 -------- .../frc2014/subsystems/DriveTrain.java | 21 ++++ 6 files changed, 180 insertions(+), 61 deletions(-) create mode 100644 org/bitbuckets/frc2014/commands/DriveTurn.java delete mode 100644 org/bitbuckets/frc2014/commands/ExampleCommand.java diff --git a/org/bitbuckets/frc2014/RandomConstants.java b/org/bitbuckets/frc2014/RandomConstants.java index 79a80b3..ce7316c 100644 --- a/org/bitbuckets/frc2014/RandomConstants.java +++ b/org/bitbuckets/frc2014/RandomConstants.java @@ -26,20 +26,26 @@ public class RandomConstants { /** The amount of ticks given out by the encoder per rotation. **/ public static final int TICKS_PER_ROTATION = 1000; - /** The amount of encoder ticks it takes to go one inch. Ticks per rotation divided by wheel circumference. **/ - public static final int TICKS_PER_INCH = TICKS_PER_ROTATION/1; + public static final int TICKS_PER_INCH = (int)(TICKS_PER_ROTATION/(4*Math.PI)); /** The KP for the pid controller used to drive n inches. **/ public static final double INCH_PID_KP = 0.3; /** The KI for the pid controller used to drive n inches. **/ public static final double INCH_PID_KI = 0.3; /** The KD for the pid controller used to drive n inches. **/ public static final double INCH_PID_KD = 0.3; - /** The tolerence for the pid controller used to drive n inches. **/ - public static final double INCH_PID_TOL = .1; /** The max acceleration for the auton drivestraight. **/ public static final double INCH_MAX_ACCEL = .1; + /** The KP for the pid controller used to turn n degrees. **/ + public static final double DEG_PID_KP = 0.3; + /** The KI for the pid controller used to turn n degrees.. **/ + public static final double DEG_PID_KI = 0.3; + /** The KD for the pid controller used to turn n degrees. **/ + public static final double DEG_PID_KD = 0.3; + /** The max acceleration for the auton driveturn. **/ + public static final double DEG_MAX_ACCEL = .1; + ///////////////----Catapault----/////////////// /** **/ public static final double WINCH_SPEED = 1.0; diff --git a/org/bitbuckets/frc2014/RobotMap.java b/org/bitbuckets/frc2014/RobotMap.java index d53847e..7a6028e 100644 --- a/org/bitbuckets/frc2014/RobotMap.java +++ b/org/bitbuckets/frc2014/RobotMap.java @@ -96,6 +96,14 @@ public class RobotMap { */ public static final int PRESSURE_SWITCH = 14; + //////////////////////////////////////////////////////////////////////////// + /////////////////////////////////----AI----///////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + + /** + * The gyroscope. + */ + public static final int GYRO = 1; /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////----OTHER----///////////////////////////////// diff --git a/org/bitbuckets/frc2014/commands/DriveStraight.java b/org/bitbuckets/frc2014/commands/DriveStraight.java index ab68080..e091b5b 100644 --- a/org/bitbuckets/frc2014/commands/DriveStraight.java +++ b/org/bitbuckets/frc2014/commands/DriveStraight.java @@ -1,7 +1,7 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. +/* FRC 4183 - The Bit Buckets + * Tucson, AZ + * + * FRC 2014 Codebase */ package org.bitbuckets.frc2014.commands; @@ -10,29 +10,38 @@ /** * - * @author Alia + * @author James Wyeth james.wyeth@gmail.com */ public class DriveStraight extends PIDCommand { private int state; private double distance; private double maxVel; private long timeInit; - private boolean finish = false; - public DriveStraight(int dist, int vel) { + /** + * + * + * @param dist + * @param vel + */ + public DriveStraight(int dist, double vel) { super("DriveStriaght", RandomConstants.INCH_PID_KP, RandomConstants.INCH_PID_KI, RandomConstants.INCH_PID_KD); distance = dist; maxVel = vel; setSetpoint(0); } - // Called just before this Command runs the first time + /** + * + */ protected void initialize() { timeInit = System.currentTimeMillis(); state = 1; } - // Called repeatedly when this Command is scheduled to run + /** + * + */ protected void execute() { switch(state){ case 1: @@ -62,29 +71,46 @@ protected void execute() { break; case 4: setSetpoint(0); - finish = true; + state = 5; break; } } - // Make this return true when this Command no longer needs to run execute() + /** + * + * + * @return + */ protected boolean isFinished() { - return finish; + return state == 5; } - // Called once after isFinished returns true + /** + * + */ protected void end() { } - // Called when another command which requires one or more of the same - // subsystems is scheduled to run + /** + * + */ protected void interrupted() { } + /** + * + * + * @return + */ protected double returnPIDInput() { return (CommandBase.driveTrain.getEncRateL() + CommandBase.driveTrain.getEncRateR())/2; } + /** + * + * + * @param output + */ protected void usePIDOutput(double output) { CommandBase.driveTrain.tankDrive(output, output); } diff --git a/org/bitbuckets/frc2014/commands/DriveTurn.java b/org/bitbuckets/frc2014/commands/DriveTurn.java new file mode 100644 index 0000000..faf1474 --- /dev/null +++ b/org/bitbuckets/frc2014/commands/DriveTurn.java @@ -0,0 +1,100 @@ +/* FRC 4183 - The Bit Buckets + * Tucson, AZ + * + * FRC 2014 Codebase + */ +package org.bitbuckets.frc2014.commands; + +import edu.wpi.first.wpilibj.command.PIDCommand; +import org.bitbuckets.frc2014.RandomConstants; + +/** + * + * @author James Wyeth james.wyeth@gmail.com + */ +public class DriveTurn extends PIDCommand { + private int sign; + private int state; + private double angle; + private double maxVel; + private long timeInit; + + public DriveTurn(double ang, double vel) { + super("DriveTurn", RandomConstants.DEG_PID_KP, RandomConstants.DEG_PID_KI, RandomConstants.DEG_PID_KD); + angle = ang; + maxVel = vel; + sign = ang > 0? 1 : -1; + setSetpoint(0); + } + + /** + * + */ + protected void initialize() { + timeInit = System.currentTimeMillis(); + state = 1; + } + + /** + * + */ + protected void execute() { + switch(state){ + case 1: + setSetpoint(RandomConstants.DEG_MAX_ACCEL*(System.currentTimeMillis()-timeInit)/1000); + if(getSetpoint() >= maxVel){ + state = 2; + } + if(CommandBase.driveTrain.getGyroAngle() >= angle/2){ + state = 3; + maxVel = getSetpoint(); + timeInit = System.currentTimeMillis(); + } + break; + case 2: + setSetpoint(maxVel); + //the distance under the last trapezoid of the curve + if(angle - CommandBase.driveTrain.getGyroAngle() <= (maxVel*maxVel)/RandomConstants.DEG_MAX_ACCEL){ + state = 4; + timeInit = System.currentTimeMillis(); + } + break; + case 3: + setSetpoint(maxVel-RandomConstants.DEG_MAX_ACCEL*(System.currentTimeMillis()-timeInit)/1000); + if(getSetpoint() <= 0){ + state = 4; + } + break; + case 4: + setSetpoint(0); + state = 5; + break; + } + } + + /** + * + * + * @return + */ + protected boolean isFinished() { + return state == 5; + } + + // Called once after isFinished returns true + protected void end() { + } + + // Called when another command which requires one or more of the same + // subsystems is scheduled to run + protected void interrupted() { + } + + protected double returnPIDInput() { + return (Math.abs(CommandBase.driveTrain.getEncRateL()) + Math.abs(CommandBase.driveTrain.getEncRateR()))/2; + } + + protected void usePIDOutput(double output) { + CommandBase.driveTrain.tankDrive(output*sign, -output*sign); + } +} diff --git a/org/bitbuckets/frc2014/commands/ExampleCommand.java b/org/bitbuckets/frc2014/commands/ExampleCommand.java deleted file mode 100644 index 6b93159..0000000 --- a/org/bitbuckets/frc2014/commands/ExampleCommand.java +++ /dev/null @@ -1,42 +0,0 @@ -/* FRC 4183 - The Bit Buckets - * Tucson, AZ - * - * FRC 2014 Codebase - */ - -package org.bitbuckets.frc2014.commands; - -/** - * @author - * - * - */ -public class ExampleCommand extends CommandBase { - - public ExampleCommand() { - // Use requires() here to declare subsystem dependencies - // eg. requires(chassis); - } - - // Called just before this Command runs the first time - protected void initialize() { - } - - // Called repeatedly when this Command is scheduled to run - protected void execute() { - } - - // Make this return true when this Command no longer needs to run execute() - protected boolean isFinished() { - return false; - } - - // Called once after isFinished returns true - protected void end() { - } - - // Called when another command which requires one or more of the same - // subsystems is scheduled to run - protected void interrupted() { - } -} diff --git a/org/bitbuckets/frc2014/subsystems/DriveTrain.java b/org/bitbuckets/frc2014/subsystems/DriveTrain.java index a5830a8..4b47ae7 100644 --- a/org/bitbuckets/frc2014/subsystems/DriveTrain.java +++ b/org/bitbuckets/frc2014/subsystems/DriveTrain.java @@ -7,6 +7,7 @@ package org.bitbuckets.frc2014.subsystems; import edu.wpi.first.wpilibj.Encoder; +import edu.wpi.first.wpilibj.Gyro; import edu.wpi.first.wpilibj.RobotDrive; import edu.wpi.first.wpilibj.command.Subsystem; import org.bitbuckets.frc2014.RandomConstants; @@ -29,6 +30,8 @@ public class DriveTrain extends Subsystem { private Encoder encR; /** Left encoder. **/ private Encoder encL; + /** Gyroscope. **/ + private Gyro gyro; /** * Drivetrain constructor, sets up basic robot drive. @@ -38,6 +41,8 @@ public DriveTrain() { drive = new RobotDrive(RobotMap.R_MOTOR, RobotMap.L_MOTOR); encR = new Encoder(RobotMap.R_ENCODER_A, RobotMap.R_ENCODER_B); encL = new Encoder(RobotMap.L_ENCODER_A, RobotMap.L_ENCODER_B); + gyro = new Gyro(RobotMap.GYRO); + gyro.reset(); } /** @@ -83,6 +88,22 @@ public double getEncRateL(){ return encL.getRate(); } + /** + * Gets the angle of the gyro. + * + * @return The angle of the gyro. + */ + public double getGyroAngle(){ + return gyro.getAngle(); + } + + /** + * Resets the gyro to an angle of 0. + */ + public void resetGyro(){ + gyro.reset(); + } + /** * Basic driving method. Can be used in autonomous and teleop. * From 46828b0aac32a814e035f1102751324030c8c8b9 Mon Sep 17 00:00:00 2001 From: bitbuckets4183 Date: Wed, 5 Feb 2014 19:18:39 -0700 Subject: [PATCH 4/6] Fixed some issues. --- .../frc2014/commands/DriveTurn.java | 2 +- .../frc2014/subsystems/DriveTrain.java | 28 +++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/org/bitbuckets/frc2014/commands/DriveTurn.java b/org/bitbuckets/frc2014/commands/DriveTurn.java index faf1474..bbfb698 100644 --- a/org/bitbuckets/frc2014/commands/DriveTurn.java +++ b/org/bitbuckets/frc2014/commands/DriveTurn.java @@ -91,7 +91,7 @@ protected void interrupted() { } protected double returnPIDInput() { - return (Math.abs(CommandBase.driveTrain.getEncRateL()) + Math.abs(CommandBase.driveTrain.getEncRateR()))/2; + return CommandBase.driveTrain.getGyroRate(); } protected void usePIDOutput(double output) { diff --git a/org/bitbuckets/frc2014/subsystems/DriveTrain.java b/org/bitbuckets/frc2014/subsystems/DriveTrain.java index 4b47ae7..29f2f2a 100644 --- a/org/bitbuckets/frc2014/subsystems/DriveTrain.java +++ b/org/bitbuckets/frc2014/subsystems/DriveTrain.java @@ -40,7 +40,9 @@ public DriveTrain() { super(); drive = new RobotDrive(RobotMap.R_MOTOR, RobotMap.L_MOTOR); encR = new Encoder(RobotMap.R_ENCODER_A, RobotMap.R_ENCODER_B); + encR.start(); encL = new Encoder(RobotMap.L_ENCODER_A, RobotMap.L_ENCODER_B); + encL.start(); gyro = new Gyro(RobotMap.GYRO); gyro.reset(); } @@ -88,6 +90,14 @@ public double getEncRateL(){ return encL.getRate(); } + /** + * Resets the encoders to 0. + */ + public void resetEncoders(){ + encR.reset(); + encL.reset(); + } + /** * Gets the angle of the gyro. * @@ -97,6 +107,15 @@ public double getGyroAngle(){ return gyro.getAngle(); } + /** + * Gets the rate that the gyro is turning. + * + * @return + */ + public double getGyroRate(){ + return gyro.getRate(); + } + /** * Resets the gyro to an angle of 0. */ @@ -110,9 +129,9 @@ public void resetGyro(){ * @param throttle forward/backward power from -1 (bwd) to 1 (fwd) * @param rotation rotational power from -1 (ccw) to 1 (cw) */ - public void drive(double throttle, double rotation){ - throttle = accelerationLimiter(throttle, throttle, RandomConstants.MAX_MAG_CHANGE); - rotation = accelerationLimiter(rotation, rotation, RandomConstants.MAX_CUR_CHANGE); + public void drive(double outputMagnitude, double curve){ + throttle = accelerationLimiter(throttle, outputMagnitude, RandomConstants.MAX_MAG_CHANGE); + rotation = accelerationLimiter(rotation, curve, RandomConstants.MAX_CUR_CHANGE); drive.arcadeDrive(throttle, rotation); } @@ -169,8 +188,7 @@ private double skim(double val) { * @param maxChange The maximum amount the value can change per iteration. * @return The limited value. */ - public double accelerationLimiter(double oldValue, - double requestedValue, double maxChange) { + public double accelerationLimiter(double oldValue, double requestedValue, double maxChange) { if(requestedValue - oldValue > maxChange) { return oldValue + maxChange; } else if(oldValue - requestedValue > maxChange) { From 1bc836962ca03561ccf99710e292bef6db43289a Mon Sep 17 00:00:00 2001 From: bitbuckets4183 Date: Mon, 10 Feb 2014 16:24:39 -0700 Subject: [PATCH 5/6] Added a command to drive a motor at a specific speed for a specified amount of time. --- .../frc2014/commands/MotorDriveTime.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 org/bitbuckets/frc2014/commands/MotorDriveTime.java diff --git a/org/bitbuckets/frc2014/commands/MotorDriveTime.java b/org/bitbuckets/frc2014/commands/MotorDriveTime.java new file mode 100644 index 0000000..65bdc22 --- /dev/null +++ b/org/bitbuckets/frc2014/commands/MotorDriveTime.java @@ -0,0 +1,73 @@ +/* FRC 4183 - The Bit Buckets + * Tucson, AZ + * + * FRC 2014 Codebase + */ +package org.bitbuckets.frc2014.commands; + +import edu.wpi.first.wpilibj.SpeedController; + +/** + * Drives a specified speed controller for a certain time at a specified speed. + * + * @author James Wyeth james.wyeth@gmail.com + */ +public class MotorDriveTime extends CommandBase { + /** The speed controller the motor to drive is connected to. **/ + private SpeedController motor; + /** The time when the command was started. Used to check the time the motor has driven for. **/ + private long timeInit; + /** The speed at which the motor is run. A double from 0 to 1. **/ + private double speed; + /** The amount of time the motor is run in milliseconds. **/ + private int time; + + /**. + * The constructor. Sets the fields. + * + * @param controller The speed controller the motor to drive is connected to. + * @param speedPercent The speed at which the motor is run. A double from 0 to 1. + * @param timeMillis The amount of time the motor should be run in milliseconds. + */ + public MotorDriveTime(SpeedController controller, double speedPercent, int timeMillis) { + motor = controller; + speed = speedPercent; + time = timeMillis; + } + + /** + * Initializes the command by sitting the initial time. + */ + protected void initialize() { + timeInit = System.currentTimeMillis(); + } + + /** + * Called repeatedly. Set the speed of the motor. + */ + protected void execute() { + motor.set(speed); + } + + /** + * Tells the controller whether the command is finished. + * + * @return Returns true if the command is finished. false otherwise. + */ + protected boolean isFinished() { + return System.currentTimeMillis() - timeInit >= time; + } + + /** + * The code to run after isFinished returns true. Stops the motor. + */ + protected void end() { + motor.set(0); + } + + /** + * The code to run if the command is interrupted. Calls end(). + */ + protected void interrupted() { + } +} From ae89087ee2ebc86303c9863bbe3527a4073c6472 Mon Sep 17 00:00:00 2001 From: roryee Date: Thu, 13 Feb 2014 08:34:42 -0700 Subject: [PATCH 6/6] Added a command that waits a certain time. --- .../frc2014/commands/WaitMillis.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 org/bitbuckets/frc2014/commands/WaitMillis.java diff --git a/org/bitbuckets/frc2014/commands/WaitMillis.java b/org/bitbuckets/frc2014/commands/WaitMillis.java new file mode 100644 index 0000000..b21ce8e --- /dev/null +++ b/org/bitbuckets/frc2014/commands/WaitMillis.java @@ -0,0 +1,60 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.bitbuckets.frc2014.commands; + +/** + * + * @author james + */ +public class WaitMillis extends CommandBase { + /** The time at the beginning of the command. **/ + private long timeInit; + /** The time to wait for. **/ + private int time; + + /** + * Constructor. Sets the time to wait for. + * + * @param timeMillis The time to wait. + */ + public WaitMillis(int timeMillis) { + time = timeMillis; + } + + /** + * Called at the beginning of the command. + */ + protected void initialize() { + timeInit = System.currentTimeMillis(); + } + + /** + * Called continuously while the command is running. Does nothing. + */ + protected void execute() { + } + + /** + * Tells the command whether the tame has passed yet. + * + * @return Whether the time has passed. + */ + protected boolean isFinished() { + return System.currentTimeMillis() - timeInit >= time; + } + + /** + * Called when isFinished returns false. + */ + protected void end() { + } + + /** + * Called if something else needs the required subsystem. Never called in this command. + */ + protected void interrupted() { + } +}