Skip to content

Commit

Permalink
Changed WsSparkMax to WsSpark and added support for SparkFlex
Browse files Browse the repository at this point in the history
  • Loading branch information
fruzyna committed Jan 5, 2024
1 parent 3ff4661 commit b491b59
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.wildstang.hardware.roborio.outputs.WsRelay;
import org.wildstang.hardware.roborio.outputs.WsServo;
import org.wildstang.hardware.roborio.outputs.WsSolenoid;
import org.wildstang.hardware.roborio.outputs.WsSparkMax;
import org.wildstang.hardware.roborio.outputs.WsSpark;
import org.wildstang.hardware.roborio.outputs.WsRemoteAnalogOutput;
import org.wildstang.hardware.roborio.outputs.WsRemoteDigitalOutput;
import org.wildstang.hardware.roborio.outputs.config.WsDigitalOutputConfig;
Expand All @@ -23,8 +23,8 @@
import org.wildstang.hardware.roborio.outputs.config.WsRelayConfig;
import org.wildstang.hardware.roborio.outputs.config.WsServoConfig;
import org.wildstang.hardware.roborio.outputs.config.WsSolenoidConfig;
import org.wildstang.hardware.roborio.outputs.config.WsSparkMaxConfig;
import org.wildstang.hardware.roborio.outputs.config.WsSparkMaxFollowerConfig;
import org.wildstang.hardware.roborio.outputs.config.WsSparkConfig;
import org.wildstang.hardware.roborio.outputs.config.WsSparkFollowerConfig;
import org.wildstang.hardware.roborio.outputs.config.WsRemoteAnalogOutputConfig;
import org.wildstang.hardware.roborio.outputs.config.WsRemoteDigitalOutputConfig;

Expand Down Expand Up @@ -79,18 +79,17 @@ else if (config instanceof WsPhoenixFollowerConfig) {
out = Core.getOutputManager().getOutput(c.getFollowing());
((WsPhoenix) out).addFollower(c.getChannel(), c.getType(), c.isOpposing());
}
else if (config instanceof WsSparkMaxConfig) {
WsSparkMaxConfig c = (WsSparkMaxConfig) config;
out = new WsSparkMax(p_output.getName(), c.getChannel(), c.isBrushless(),
c.getDefault(), c.isInverted());
else if (config instanceof WsSparkConfig) {
WsSparkConfig c = (WsSparkConfig) config;
out = new WsSpark(p_output.getName(), c.getChannel(), c.getType(), c.getDefault(), c.isInverted());
}
// Note a WsSparkMaxFollower must be defined after its corresponding WsSparkMax
else if (config instanceof WsSparkMaxFollowerConfig) {
WsSparkMaxFollowerConfig c = (WsSparkMaxFollowerConfig) config;
else if (config instanceof WsSparkFollowerConfig) {
WsSparkFollowerConfig c = (WsSparkFollowerConfig) config;
// Returns the follwed WsSparkMax because a return is required
// and duplicate outputs are thrown out when encountered.
out = Core.getOutputManager().getOutput(c.getFollowing());
((WsSparkMax) out).addFollower(c.getChannel(), c.isBrushless(), c.isOpposing());
((WsSpark) out).addFollower(c.getChannel(), c.getType(), c.isOpposing());
}
else if (config instanceof WsSolenoidConfig) {
WsSolenoidConfig c = (WsSolenoidConfig) config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public WsPhoenix(String name, int channel, double p_default, WsMotorControllers
motor = new TalonFX(channel);
break;
default:
Log.error("Invalid motor control for WsPhoenix!");
Log.error("Invalid motor controller for WsPhoenix!");
return;
}
motor.setInverted(invert);
Expand All @@ -73,7 +73,7 @@ public void addFollower(int canConstant, WsMotorControllers controller, boolean
follower = new TalonFX(canConstant);
break;
default:
Log.error("Invalid follower motor control for WsPhoenix!");
Log.error("Invalid follower motor controller for WsPhoenix!");
return;
}
follower.follow(motor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.revrobotics.SparkPIDController;
import com.revrobotics.AbsoluteEncoder;
import com.revrobotics.CANSparkBase;
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkFlex;
import com.revrobotics.CANSparkBase.IdleMode;
import com.revrobotics.CANSparkLowLevel.MotorType;
import com.revrobotics.CANSparkBase.ControlType;
Expand All @@ -11,13 +13,13 @@
import org.wildstang.hardware.roborio.outputs.config.WsMotorControllers;

/**
* Controls a Spark Max motor controller.
* Controls a Spark Max/Flex motor controller.
* @author Liam
*/
public class WsSparkMax extends WsMotorController {
public class WsSpark extends WsMotorController {

CANSparkMax motor;
CANSparkMax follower;
CANSparkBase motor;
CANSparkBase follower;
SparkPIDController controller;
boolean isUsingController;
boolean isChanged;
Expand All @@ -28,77 +30,81 @@ public class WsSparkMax extends WsMotorController {
* Constructs the motor controller from config.
* @param name Descriptive name of the controller.
* @param channel Motor controller CAN constant.
* @param brushless True if the motor is brushless, false if brushed.
* @param controller Enumeration representing type of controller.
* @param p_default Default output value.
*/
public WsSparkMax(String name, int channel, boolean brushless, double p_default) {
this(name, channel, brushless, p_default, false);
public WsSpark(String name, int channel, WsMotorControllers controller, double p_default) {
this(name, channel, controller, p_default, false);
}

/**
* Constructs the motor controller from config.
* @param name Descriptive name of the controller.
* @param channel Motor controller CAN constant.
* @param brushless True if the motor is brushless, false if brushed.
* @param controller Enumeration representing type of controller.
* @param p_default Default output value.
* @param invert Invert the motor's direction.
*/
public WsSparkMax(String name, int channel, boolean brushless, double p_default, boolean invert) {
public WsSpark(String name, int channel, WsMotorControllers controller, double p_default, boolean invert) {
super(name, p_default);

motor = new CANSparkMax(channel, brushless ? MotorType.kBrushless : MotorType.kBrushed);
boolean brushless = controller == WsMotorControllers.SPARK_MAX_BRUSHLESS || controller == WsMotorControllers.SPARK_FLEX_BRUSHLESS;
switch (controller) {
case SPARK_MAX_BRUSHED:
case SPARK_MAX_BRUSHLESS:
motor = new CANSparkMax(channel, brushless ? MotorType.kBrushless : MotorType.kBrushed);
break;
case SPARK_FLEX_BRUSHED:
case SPARK_FLEX_BRUSHLESS:
motor = new CANSparkFlex(channel, brushless ? MotorType.kBrushless : MotorType.kBrushed);
break;
default:
Log.error("Invalid motor controller for WsSpark!");
return;
}
motor.setInverted(invert);
isUsingController = false;
isChanged = true;
controlType = ControlType.kDutyCycle;
}

/**
* Add a follower motor to the current motor.
* @param canConstant CAN constant of the new follower motor.
* @param brushless True if the motor is brushless, false if brushed.
* @param oppose True if the follow should oppose the direction of this motor.
*/
public void addFollower(int canConstant, boolean brushless, boolean oppose) {
addFollower(canConstant, brushless ? WsMotorControllers.SPARK_MAX_BRUSHLESS : WsMotorControllers.SPARK_MAX_BRUSHED, oppose);
}

/**
* Add a follower motor to the current motor.
* @param canConstant CAN constant of the new follower motor.
* @param controller Enumeration representing type of controller.
* @param oppose True if the follow should oppose the direction of this motor.
*/
public void addFollower(int canConstant, WsMotorControllers controller, boolean oppose) {
boolean brushless;
boolean brushless = controller == WsMotorControllers.SPARK_MAX_BRUSHLESS || controller == WsMotorControllers.SPARK_FLEX_BRUSHLESS;
switch (controller) {
case SPARK_MAX_BRUSHED:
brushless = false;
break;
case SPARK_MAX_BRUSHLESS:
brushless = true;
motor = new CANSparkMax(canConstant, brushless ? MotorType.kBrushless : MotorType.kBrushed);
break;
case SPARK_FLEX_BRUSHED:
case SPARK_FLEX_BRUSHLESS:
motor = new CANSparkFlex(canConstant, brushless ? MotorType.kBrushless : MotorType.kBrushed);
break;
default:
Log.error("Invalid follower motor control for WsSparkMax!");
Log.error("Invalid follower motor controller for WsSpark!");
return;
}
follower = new CANSparkMax(canConstant, brushless ? MotorType.kBrushless : MotorType.kBrushed);
follower.follow(motor, oppose);
}

/**
* Returns the raw motor controller Object.
* @return CANSparkMax Object.
* @return CANSparkBase Object.
*/
public CANSparkMax getController() {
public CANSparkBase getController() {
return motor;
}

/**
* Returns the raw follower motor controller Object.
* @return Follower motor controller object, null if no follower.
*/
public CANSparkMax getFollower() {
public CANSparkBase getFollower() {
return follower;
}

Expand Down Expand Up @@ -346,7 +352,7 @@ public void setVelocity(double target){
public void notifyConfigChange() { }

/**
* Does nothing, SparkMax has no current limit disable function.
* Does nothing, Spark has no current limit disable function.
*/
@Override
public void disableCurrentLimit() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public enum WsMotorControllers {
TALON_FX,
SPARK_MAX_BRUSHLESS,
SPARK_MAX_BRUSHED,
SPARK_FLEX_BRUSHLESS,
SPARK_FLEX_BRUSHED,
UNKNOWN
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@
import org.wildstang.framework.hardware.OutputConfig;

/**
* Contains configurations for Spark Max motor controllers.
* Contains configurations for Spark Max/Flex motor controllers.
*/
public class WsSparkMaxConfig implements OutputConfig {
public class WsSparkConfig implements OutputConfig {

private int m_channel = 0;
private double m_default;
private boolean brushless;
private WsMotorControllers controller;
private boolean invert;

/**
* Construct the Phoenix config.
* @param channel Controller CAN constant.
* @param brushless True if the motor is brushless, false if brushed.
* @param controller Enumeration representing type of controller.
*/
public WsSparkMaxConfig(int channel, boolean brushless) {
this(channel, brushless, false, 0);
public WsSparkConfig(int channel, WsMotorControllers controller) {
this(channel, controller, false, 0);
}

/**
* Construct the Phoenix config.
* @param channel Controller CAN constant.
* @param brushless True if the motor is brushless, false if brushed.
* @param controller Enumeration representing type of controller.
* @param invert True if motor output should be inverted.
*/
public WsSparkMaxConfig(int channel, boolean brushless, boolean invert) {
this(channel, brushless, invert, 0);
public WsSparkConfig(int channel, WsMotorControllers controller, boolean invert) {
this(channel, controller, invert, 0);
}

/**
* Construct the Phoenix config.
* @param channel Controller CAN constant.
* @param brushless True if the motor is brushless, false if brushed.
* @param controller Enumeration representing type of controller.
* @param invert True if motor output should be inverted.
* @param p_default Default output value.
*/
public WsSparkMaxConfig(int channel, boolean brushless, boolean invert, double p_default) {
public WsSparkConfig(int channel, WsMotorControllers controller, boolean invert, double p_default) {
m_channel = channel;
this.brushless = brushless;
this.controller = controller;
this.invert = invert;
m_default = p_default;
}
Expand All @@ -54,19 +54,19 @@ public int getChannel() {
}

/**
* Returns the default output value.
* @return The default value.
* Returns the motor controller type.
* @return The hardware motor controller type.
*/
public double getDefault() {
return m_default;
public WsMotorControllers getType() {
return controller;
}

/**
* Returns true if the motor is brushless.
* @return True if the motor is brushless, false if brushed.
* Returns the default output value.
* @return The default value.
*/
public boolean isBrushless() {
return brushless;
public double getDefault() {
return m_default;
}

/**
Expand All @@ -86,8 +86,8 @@ public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("{\"channel\": ");
buf.append(m_channel);
buf.append(", \"brushless\": ");
buf.append(brushless);
buf.append(", \"type\": ");
buf.append(controller.name());
buf.append("}");
return buf.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,37 @@
import org.wildstang.framework.hardware.OutputConfig;

/**
* Contains configurations for Spark Max motor controller followers.
* Contains configurations for Spark Max/Flex motor controller followers.
*/
public class WsSparkMaxFollowerConfig implements OutputConfig {
public class WsSparkFollowerConfig implements OutputConfig {

private String following;
private int m_channel = 0;
private boolean brushless;
private boolean oppose;
private WsMotorControllers controller;

/**
* Construct the Phoenix config.
* @param following Name of motor controller being followed.
* @param channel Hardware port number.
* @param brushless True if the motor is brushless, false if brushed.
* @param controller Enumeration representing type of controller.
*/
public WsSparkMaxFollowerConfig(String following, int channel, boolean brushless) {
this(following, channel, brushless, false);
public WsSparkFollowerConfig(String following, int channel, WsMotorControllers controller) {
this(following, channel, controller, false);
}

/**
* Construct the Phoenix config.
* @param following Name of motor controller being followed.
* @param channel Hardware port number.
* @param brushless True if the motor is brushless, false if brushed.
* @param controller Enumeration representing type of controller.
* @param oppose True if the follow should oppose the direction of this motor.
*/
public WsSparkMaxFollowerConfig(String following, int channel, boolean brushless, boolean oppose) {
public WsSparkFollowerConfig(String following, int channel, WsMotorControllers controller, boolean oppose) {
this.following = following;
m_channel = channel;
this.brushless = brushless;
this.controller = controller;
this.oppose = oppose;
this.controller = brushless ? WsMotorControllers.SPARK_MAX_BRUSHLESS : WsMotorControllers.SPARK_MAX_BRUSHED;
}

/**
Expand All @@ -54,14 +52,6 @@ public int getChannel() {
return m_channel;
}

/**
* Returns true if the motor is brushless.
* @return True if the motor is brushless, false if brushed.
*/
public boolean isBrushless() {
return brushless;
}

/**
* Returns true if the motor controller is a Talon.
* @return True if Talon, false if Victor.
Expand All @@ -87,8 +77,8 @@ public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("{\"channel\": ");
buf.append(m_channel);
buf.append(", \"brushless\": ");
buf.append(brushless);
buf.append(", \"type\": ");
buf.append(controller.name());
buf.append(", \"oppose\": ");
buf.append(oppose);
buf.append("}");
Expand Down
Loading

0 comments on commit b491b59

Please sign in to comment.