diff --git a/commands/motor/MotorControl.java b/commands/motor/MotorControl.java index 7f5c1d67..5d0458bf 100644 --- a/commands/motor/MotorControl.java +++ b/commands/motor/MotorControl.java @@ -1,8 +1,8 @@ package org.usfirst.frc4904.standard.commands.motor; +import java.util.function.Supplier; import org.usfirst.frc4904.standard.LogKitten; -import org.usfirst.frc4904.standard.custom.controllers.Controller; import org.usfirst.frc4904.standard.subsystems.motor.Motor; import org.usfirst.frc4904.standard.subsystems.motor.PositionSensorMotor; import edu.wpi.first.wpilibj.command.Command; @@ -14,41 +14,54 @@ */ public class MotorControl extends Command { protected final Motor motor; - protected final Controller controller; - protected final int axis; - protected final double scale; + protected final Supplier speedSupplier; + protected final double offset; /** - * This Command directly controls a Motor based on an axis of the Controller. - * This can allow an Operator to easily control a single Motor from an axis of the Controller. + * This Command directly controls a Motor based on a double supplier; This + * can allow an Operator to easily control a single Motor from an axis of + * the Controller. * + * @param name * @param motor - * @param controller - * @param axis - * @param scale + * @param speedSupplier + * @param offset + * A constant to add to the motor's speed Useful for using a + * controller for fine-tuning a constant speed */ - public MotorControl(Motor motor, Controller controller, int axis, double scale) { - super("MotorControl"); + public MotorControl(String name, Motor motor, Supplier speedSupplier, double offset) { + super(name); this.motor = motor; - this.controller = controller; - this.axis = axis; - this.scale = scale; + this.speedSupplier = speedSupplier; + this.offset = offset; requires(motor); setInterruptible(true); LogKitten.d("MotorControl created for " + motor.getName()); } /** - * This Command directly controls a Motor based on an axis of the Controller. - * This can allow an Operator to easily control a single Motor from an axis of the Controller. + * This Command directly controls a Motor based on an axis of the + * Controller. This can allow an Operator to easily control a single Motor + * from an axis of the Controller. * + * @param name * @param motor - * @param controller - * @param axis - * @param scale + * @param speedSupplier */ - public MotorControl(Motor motor, Controller controller, int axis) { - this(motor, controller, axis, 1.0); + public MotorControl(String name, Motor motor, Supplier speedSupplier) { + this(name, motor, speedSupplier, 0.0); + } + + /** + * This Command directly controls a Motor based on an axis of the + * Controller. This can allow an Operator to easily control a single Motor + * from an axis of the Controller. + * + * @param motor + * @param speedSupplier + */ + public MotorControl(Motor motor, Supplier speedSupplier) { + this("MotorControl", motor, speedSupplier, 0.0); } @Override @@ -61,8 +74,9 @@ protected void initialize() { @Override protected void execute() { - LogKitten.d("MotorControl executing: " + controller.getAxis(axis)); - motor.set(controller.getAxis(axis) * scale); + double speed = speedSupplier.get() + offset; + LogKitten.d("MotorControl executing: " + speed); + motor.set(speed); } @Override diff --git a/commands/motor/MotorSet.java b/commands/motor/MotorSet.java index 04f9b1b1..dab5f84f 100644 --- a/commands/motor/MotorSet.java +++ b/commands/motor/MotorSet.java @@ -8,12 +8,9 @@ /** * Sets a motor to a speed. - * The speed can change through - * use of the set command. - * This is better than setting - * the motor because it uses - * requires to avoid having - * multiple attempts to set a + * The speed can change through use of the set command. + * This is better than setting the motor because it uses + * requires to avoid having multiple attempts to set a * motor simultaneously. * */ @@ -21,8 +18,14 @@ public class MotorSet extends Command { protected final SpeedController motor; protected double speed; - public MotorSet(Motor motor) { - super("MotorSet"); + /** + * Command that wraps the set function of a motor. + * + * @param name + * @param motor + */ + public MotorSet(String name, Motor motor) { + super(name); this.motor = motor; speed = 0; LogKitten.d("MotorSet created for " + motor.getName()); @@ -30,17 +33,26 @@ public MotorSet(Motor motor) { setInterruptible(true); } + /** + * Command that wraps the set function of a motor. + * + * @param motor + */ + public MotorSet(Motor motor) { + this("MotorSet", motor); + } + @Override protected void initialize() { LogKitten.d("MotorSet initialized"); } /** - * Set the speed of the motor + * Set the speed of the motor in this class */ public void set(double speed) { this.speed = speed; - LogKitten.d("MotorSet writePipe set to " + speed); + LogKitten.d("MotorSet set to " + speed); } @Override diff --git a/commands/motor/MotorSuppliedConstant.java b/commands/motor/MotorSuppliedConstant.java new file mode 100644 index 00000000..43dce65e --- /dev/null +++ b/commands/motor/MotorSuppliedConstant.java @@ -0,0 +1,70 @@ +package org.usfirst.frc4904.standard.commands.motor; + + +import java.util.function.Supplier; +import org.usfirst.frc4904.standard.LogKitten; +import org.usfirst.frc4904.standard.subsystems.motor.Motor; +import edu.wpi.first.wpilibj.command.Command; + +/** + * Set the speed of a motor based on a speed supplied on initialize. + * This should be used when the speed might change between construction time and when the motor should start. + */ +public class MotorSuppliedConstant extends Command { + protected final Motor motor; + protected final Supplier speedSupply; + protected double speed; + + /** + * Set the speed of a motor based on a speed supplied on initialize. + * This should be used when the speed might change between construction time and when the motor should start. + * + * @param name + * @param motor + * @param speedSupply + */ + public MotorSuppliedConstant(String name, Motor motor, Supplier speedSupply) { + super(name); + this.motor = motor; + requires(motor); + this.speedSupply = speedSupply; + setInterruptible(true); + LogKitten.d("MotorSuppliedConstant created for " + motor.getName()); + } + + /** + * Set the speed of a motor based on a speed supplied on initialize. + * This should be used when the speed might change between construction time and when the motor should start. + * + * @param name + * @param motor + * @param speedSupply + */ + public MotorSuppliedConstant(Motor motor, Supplier speedSupply) { + this("MotorSuppliedConstant", motor, speedSupply); + } + + @Override + protected void initialize() { + speed = speedSupply.get(); + motor.set(speed); + } + + @Override + protected void execute() { + motor.set(speed); + } + + @Override + protected boolean isFinished() { + return false; + } + + @Override + protected void end() {} + + @Override + protected void interrupted() { + LogKitten.d("MotorSuppliedConstant interrupted"); + } +} diff --git a/custom/controllers/ControllerDoubleSupplier.java b/custom/controllers/ControllerDoubleSupplier.java new file mode 100644 index 00000000..7bdbc714 --- /dev/null +++ b/custom/controllers/ControllerDoubleSupplier.java @@ -0,0 +1,57 @@ +package org.usfirst.frc4904.standard.custom.controllers; + + +import java.util.function.Supplier; + +public class ControllerDoubleSupplier implements Supplier { + protected final Controller controller; + protected final int axis; + protected final double scale; + protected final double offset; + + /** + * Wrapper for a controller that allows it to act as a double supplier for a + * specific axis set to a certain scale. + * + * @param controller + * @param axis + * @param scale + */ + public ControllerDoubleSupplier(Controller controller, int axis, double scale, double offset) { + this.controller = controller; + this.axis = axis; + this.scale = scale; + this.offset = offset; + } + + /** + * Wrapper for a controller that allows it to act as a double supplier for a + * specific axis. Offset assumed as 0. + * + * @param controller + * @param axis + * @param scale + */ + public ControllerDoubleSupplier(Controller controller, int axis, double scale) { + this(controller, axis, scale, 0.0); + } + + /** + * Wrapper for a controller that allows it to act as a double supplier for a + * specific axis. Scale assumed as 1, offset as 0. + * + * @param controller + * @param axis + */ + public ControllerDoubleSupplier(Controller controller, int axis) { + this(controller, axis, 1.0, 0.0); + } + + /** + * Returns a double for the value of the axis. + */ + @Override + public Double get() { + return controller.getAxis(axis) * scale + offset; + } +}