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 all 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
60 changes: 37 additions & 23 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,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<Double> 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<Double> speedSupplier, 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.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<Double> 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<Double> speedSupplier) {
this("MotorControl", motor, speedSupplier, 0.0);
}

@Override
Expand All @@ -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
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
70 changes: 70 additions & 0 deletions commands/motor/MotorSuppliedConstant.java
Original file line number Diff line number Diff line change
@@ -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<Double> 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<Double> 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<Double> 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");
}
}
57 changes: 57 additions & 0 deletions custom/controllers/ControllerDoubleSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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;
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;
}
}