Skip to content
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public void putShuffleboardCommands() {
new SpinFeeder(feederSubsystem));

SmartDashboard.putData(
"Spin Shooter",
"Spins Shooter",
new SpinShooter(shooterSubsystem, Constants.SHOOTER_SPEED));

SmartDashboard.putData(
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/frc/robot/subsystems/ShooterSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.revrobotics.spark.SparkBase.ResetMode;
import com.revrobotics.spark.config.SparkBaseConfig;
import com.revrobotics.spark.config.SparkMaxConfig;
import com.revrobotics.spark.config.SparkBaseConfig.IdleMode;

import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.constants.Constants;
Expand All @@ -20,6 +21,7 @@
import frc.robot.utils.logging.io.pidmotor.SparkMaxPidConfig;
import frc.robot.utils.logging.io.pidmotor.SparkMaxPidMotor;
import frc.robot.utils.logging.io.pidmotor.SparkMaxPidMotorIo;
import frc.robot.utils.motor.TunablePIDManager;
import frc.robot.utils.simulation.MotorSimulator;
import frc.robot.utils.simulation.RobotVisualizer;

Expand All @@ -30,16 +32,23 @@ public class ShooterSubsystem extends SubsystemBase {
private final SparkMaxPidMotorIo io;
private final SparkMax followerMotor;
private final SparkMaxConfig followerConfig;

private final TunablePIDManager pidManager;
public ShooterSubsystem(SparkMaxPidMotorIo io) {
this.pidManager = new TunablePIDManager(LOGGING_NAME, io, createPidConfig());
this.io = io;
io.setPid(0.0000002, 0.000015, 0.000015); // Pid needs tuning
// io.setPid(0.0000002, 0.000015, 0.000015); // Pid needs tuning
followerMotor = new SparkMax(Constants.SHOOTER_FOLLOWER_MOTOR_ID, SparkLowLevel.MotorType.kBrushless);
followerConfig = new SparkMaxConfig();
followerConfig.follow(Constants.SHOOTER_MOTOR_ID, true);
followerConfig.follow(Constants.SHOOTER_MOTOR_ID, true).idleMode(IdleMode.kCoast);
followerMotor.configure(followerConfig, ResetMode.kNoResetSafeParameters, PersistMode.kNoPersistParameters);

}
private static SparkMaxPidConfig createPidConfig() {
return new SparkMaxPidConfig(false)
.setCurrentLimit(Constants.NEO_CURRENT_LIMIT)
.setAllowedError(.1)
.setIdleMode(IdleMode.kCoast);
}

// setSpeed expects a power value from -1 to 1
public void setSpeed(double speed) {
Expand All @@ -57,6 +66,7 @@ public void setPidVelocity(double velocity) {

@Override
public void periodic() {
pidManager.periodic();
io.periodic();
}

Expand All @@ -74,7 +84,7 @@ public static SparkMaxPidMotorIo createSimIo(RobotVisualizer visualizer) {
}

public static SparkMaxPidMotor createMotor() {
return new SparkMaxPidMotor(Constants.SHOOTER_MOTOR_ID, true);
return new SparkMaxPidMotor(Constants.SHOOTER_MOTOR_ID, createPidConfig());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package frc.robot.utils.logging.io.pidmotor;


import frc.robot.utils.logging.input.MotorLoggableInputs;
import frc.robot.utils.logging.io.motor.MockSparkMaxIo;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package frc.robot.utils.logging.io.pidmotor;

import com.revrobotics.ResetMode;
import com.revrobotics.spark.SparkBase;
import com.revrobotics.spark.config.SparkBaseConfig.IdleMode;
import com.revrobotics.spark.config.SparkMaxConfig;

import frc.robot.utils.logging.input.MotorLoggableInputs;
import frc.robot.utils.logging.io.motor.RealSparkMaxIo;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package frc.robot.utils.logging.io.pidmotor;

import com.revrobotics.spark.config.SparkBaseConfig.IdleMode;

/**
* Value container helper class for configuring a PidMotor.
*/
Expand All @@ -12,13 +14,15 @@ public class SparkMaxPidConfig {
public static final double MAX_VELOCITY = 5000;
public static final double MAX_ACCELERATION = 10000;
public static final double ALLOWED_ERROR = 1.0;
public static final IdleMode DEFAULT_IDLE_MODE = IdleMode.kBrake;

private double p = DEFAULT_P;
private double i = DEFAULT_I;
private double d = DEFAULT_D;
private double iZone = DEFAULT_IZONE;
private double ff = DEFAULT_FF;
private int currentLimit = 20;
private IdleMode mode = DEFAULT_MODE;
/**
* This is the cruise velocity for the MAX_MOTION config.
*/
Expand Down Expand Up @@ -130,4 +134,14 @@ public double getAllowedError() {
public boolean getUsesMaxMotion() {
return usesMaxMotion;
}

public SparkMaxPidConfig setIdleMode(IdleMode mode){
this.mode = mode;
return this;
}

public IdleMode getIdleMode(){
return mode;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.revrobotics.spark.config.SparkBaseConfig.IdleMode;
import com.revrobotics.spark.config.SparkMaxConfig;

import frc.robot.constants.Constants;

/**
* A Wrapper utility to encapsulate the NEO motor with PID capability. This is simply a wrapper with
* some convenient defaults and initializations that make programming the PID of the NEO easier.
Expand Down Expand Up @@ -51,7 +53,7 @@ public SparkMaxPidMotor(int id, SparkMaxPidConfig pidConfig) {
config
.smartCurrentLimit(pidConfig.getCurrentLimit())
.closedLoopRampRate(RAMP_RATE)
.idleMode(IdleMode.kBrake);
.idleMode(pidConfig.getIdleMode());
config
.closedLoop
.feedbackSensor(FeedbackSensor.kPrimaryEncoder)
Expand All @@ -78,7 +80,6 @@ public SparkMaxPidMotor(int id, SparkMaxPidConfig pidConfig) {

neoMotor.configure(config, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);
}

/**
* Reconfigure the PID fully using some of the values from motor params.
* This method uses the PID, iZone, FF, maxVelocity, maxAcceleration and allowedError to reconfigure
Expand All @@ -92,7 +93,7 @@ public void configurePID(SparkMaxPidConfig params) {
.closedLoop
.pid(params.getP(), params.getI(), params.getD())
.iZone(params.getIZone())
.feedForward.kV(pidConfig.getFF());
.feedForward.kV(params.getFF());
if (params.getUsesMaxMotion()) {
config
.closedLoop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package frc.robot.utils.logging.io.pidmotor;

import com.revrobotics.spark.SparkBase;

import frc.robot.utils.logging.io.motor.SparkMaxIo;

/**
Expand Down Expand Up @@ -56,4 +57,5 @@ public interface SparkMaxPidMotorIo extends SparkMaxIo {
* @param pidFF the FF value to set
*/
void setPid(double pidP, double pidI, double pidD, double iZone, double pidFF);

}