-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More Motor Commands #203
base: main
Are you sure you want to change the base?
More Motor Commands #203
Changes from 10 commits
2d76d0f
af4dec6
6fffef2
5c65e79
737367f
da41f56
bbc3223
951518c
36491c3
6039c0a
8454860
d70a352
f739822
b023f4e
b414db2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,30 +14,60 @@ | |
*/ | ||
public class MotorControl extends Command { | ||
protected final Motor motor; | ||
protected final Controller controller; | ||
protected final int axis; | ||
protected final Supplier<Double> speedSupplier; | ||
protected final double scale; | ||
protected final double offset; | ||
|
||
/** | ||
* This Command directly controls a Motor based on 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 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<Double> speedSupplier, double scale, double offset) { | ||
super(name); | ||
this.motor = motor; | ||
this.controller = controller; | ||
this.axis = axis; | ||
this.speedSupplier = speedSupplier; | ||
this.scale = scale; | ||
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. | ||
* | ||
* @param name | ||
* @param motor | ||
* @param controller | ||
* @param axis | ||
*/ | ||
public MotorControl(String name, Motor motor, Supplier<Double> speedSupplier) { | ||
this(name, motor, speedSupplier, 1.0, 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 controller | ||
* @param axis | ||
* @param scale | ||
*/ | ||
public MotorControl(Motor motor, Supplier<Double> speedSupplier, double scale) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the point of this? Where does the scale get factored in/used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah -- it looks like scale is just stored on the instance, and then never used. IMO scale is irrelevant when using speedSupplier; it was used when reading directly from the joystick, but with DoubleSupplier the user could just add the scale factor in the supplier itself. Nice catch. |
||
this("MotorControl", motor, speedSupplier, 1.0, 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. | ||
|
@@ -47,8 +77,8 @@ public MotorControl(Motor motor, Controller controller, int axis, double scale) | |
* @param axis | ||
* @param scale | ||
*/ | ||
public MotorControl(Motor motor, Controller controller, int axis) { | ||
this(motor, controller, axis, 1.0); | ||
public MotorControl(Motor motor, Supplier<Double> speedSupplier) { | ||
this(motor, speedSupplier, 1.0); | ||
} | ||
|
||
@Override | ||
|
@@ -61,8 +91,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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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<Double> speedSupply; | ||
protected final double scale; | ||
protected final double offset; | ||
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 | ||
* @param scale | ||
* @param offset | ||
*/ | ||
public MotorSuppliedConstant(String name, Motor motor, Supplier<Double> speedSupply, double scale, double offset) { | ||
super(name); | ||
this.motor = motor; | ||
requires(motor); | ||
this.speedSupply = speedSupply; | ||
this.scale = scale; | ||
this.offset = offset; | ||
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 | ||
* @param scale | ||
*/ | ||
public MotorSuppliedConstant(String name, Motor motor, Supplier<Double> speedSupply, double scale) { | ||
this(name, motor, speedSupply, scale, 0.0); | ||
} | ||
|
||
/** | ||
* 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<Double> speedSupply) { | ||
this("MotorSuppliedConstant", motor, speedSupply, 1.0); | ||
} | ||
|
||
@Override | ||
protected void initialize() { | ||
speed = speedSupply.get() * scale + offset; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As @ajnadel mentioned, the scale is unnecessary if we can just adjust this in the supplier, no? |
||
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"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.usfirst.frc4904.standard.custom.controllers; | ||
|
||
|
||
import java.util.function.Supplier; | ||
|
||
public class ControllerDoubleSupplier implements Supplier<Double> { | ||
protected final Controller controller; | ||
protected final int axis; | ||
|
||
/** | ||
* Wrapper for a controller that allows it to act as a double supplier | ||
* for a specific axis. | ||
* | ||
* @param controller | ||
* @param axis | ||
*/ | ||
public ControllerDoubleSupplier(Controller controller, int axis) { | ||
this.controller = controller; | ||
this.axis = axis; | ||
} | ||
|
||
/** | ||
* Returns a double for the value of the axis. | ||
*/ | ||
@Override | ||
public Double get() { | ||
return controller.getAxis(axis); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty cool, however, we may run into compatibility problems with older versions. 10/10 would recommend for 2018-code, and maybe for 2017 if we take the time to do some refactoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compatibility is something we rarely consider as we are not supporting a huge number of people on historical versions. If older software relies on something, a branch can be created to support it, or the older software can adapt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok my B.