Skip to content
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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions commands/motor/MotorControl.java
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;
Expand All @@ -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);
Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok my B.

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) {
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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.
Expand All @@ -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
Expand All @@ -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
Expand Down
32 changes: 22 additions & 10 deletions commands/motor/MotorSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,51 @@

/**
* 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.
*
*/
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());
requires(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
Expand Down
89 changes: 89 additions & 0 deletions commands/motor/MotorSuppliedConstant.java
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;
Copy link
Member

Choose a reason for hiding this comment

The 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");
}
}
29 changes: 29 additions & 0 deletions custom/controllers/ControllerDoubleSupplier.java
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);
}
}