manipulator pivot on controller pid

This commit is contained in:
Tylr-J42 2025-02-11 09:15:19 -05:00
parent ed1ffe7044
commit 619b3f4b7f
3 changed files with 32 additions and 44 deletions

View File

@ -49,9 +49,6 @@ public class ManipulatorPivotConstants {
/**The forward rotation limit of the arm */ /**The forward rotation limit of the arm */
public static final double kRotationLimit = Units.degreesToRadians(175.0); public static final double kRotationLimit = Units.degreesToRadians(175.0);
public static final double kMagnetOffset = 0.0;
public static final double kAbsoluteSensorDiscontinuityPoint = 0.0;
public static final double kSysIDRampRate = 1; public static final double kSysIDRampRate = 1;
public static final double kSysIDStepVolts = 7; public static final double kSysIDStepVolts = 7;
public static final double kSysIDTimeout = 10; public static final double kSysIDTimeout = 10;
@ -73,15 +70,11 @@ public class ManipulatorPivotConstants {
public static final SparkMaxConfig motorConfig = new SparkMaxConfig(); public static final SparkMaxConfig motorConfig = new SparkMaxConfig();
static { static {
canCoderConfig.MagnetSensor.SensorDirection = SensorDirectionValue.CounterClockwise_Positive;
canCoderConfig.MagnetSensor.MagnetOffset = kMagnetOffset;
// TODO Need to do more reading on this setting, and how to properly offset the Arm so that horizontal is 0
//canCoderConfig.MagnetSensor.AbsoluteSensorDiscontinuityPoint = 0.5;
motorConfig motorConfig
.smartCurrentLimit(kMotorAmpsMax) .smartCurrentLimit(kMotorAmpsMax)
.idleMode(kIdleMode); .idleMode(kIdleMode);
motorConfig.encoder.positionConversionFactor(kPivotConversion); motorConfig.absoluteEncoder.positionConversionFactor(kPivotConversion);
motorConfig.closedLoop motorConfig.closedLoop
.p(kPositionalP) .p(kPositionalP)
.i(kPositionalI) .i(kPositionalI)

View File

@ -1,17 +1,16 @@
package frc.robot.subsystems; package frc.robot.subsystems;
import java.util.function.DoubleSupplier;
import com.ctre.phoenix6.hardware.CANcoder;
import com.revrobotics.spark.SparkMax; import com.revrobotics.spark.SparkMax;
import com.revrobotics.spark.ClosedLoopSlot;
import com.revrobotics.spark.SparkAbsoluteEncoder;
import com.revrobotics.spark.SparkBase;
import com.revrobotics.spark.SparkBase.PersistMode; import com.revrobotics.spark.SparkBase.PersistMode;
import com.revrobotics.spark.SparkBase.ResetMode; import com.revrobotics.spark.SparkBase.ResetMode;
import com.revrobotics.spark.SparkClosedLoopController;
import com.revrobotics.spark.SparkLowLevel.MotorType; import com.revrobotics.spark.SparkLowLevel.MotorType;
import edu.wpi.first.math.MathUtil; import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.controller.ArmFeedforward; import edu.wpi.first.math.controller.ArmFeedforward;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase; import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.constants.ManipulatorPivotConstants; import frc.robot.constants.ManipulatorPivotConstants;
@ -19,13 +18,11 @@ import frc.robot.constants.ManipulatorPivotConstants;
public class ManipulatorPivot extends SubsystemBase { public class ManipulatorPivot extends SubsystemBase {
protected SparkMax armMotor; protected SparkMax armMotor;
private CANcoder canCoder;
private PIDController positionController;
private PIDController velocityController;
private ArmFeedforward feedForward; private ArmFeedforward feedForward;
private SparkClosedLoopController pivotClosedLoopController;
private SparkAbsoluteEncoder absoluteEncoder;
public ManipulatorPivot() { public ManipulatorPivot() {
armMotor = new SparkMax( armMotor = new SparkMax(
ManipulatorPivotConstants.kArmMotorID, ManipulatorPivotConstants.kArmMotorID,
@ -38,18 +35,8 @@ public class ManipulatorPivot extends SubsystemBase {
PersistMode.kPersistParameters PersistMode.kPersistParameters
); );
positionController = new PIDController( pivotClosedLoopController = armMotor.getClosedLoopController();
ManipulatorPivotConstants.kPositionalP, absoluteEncoder = armMotor.getAbsoluteEncoder();
ManipulatorPivotConstants.kPositionalI,
ManipulatorPivotConstants.kPositionalD
);
// TODO: Generate constants for continuous input range based on CANcoder configuration?
positionController.enableContinuousInput(Units.degreesToRadians(-180), Units.degreesToRadians(179));
positionController.setTolerance(ManipulatorPivotConstants.kPositionalTolerance);
canCoder = new CANcoder(ManipulatorPivotConstants.kCANcoderID);
canCoder.getConfigurator().apply(ManipulatorPivotConstants.canCoderConfig);
feedForward = new ArmFeedforward( feedForward = new ArmFeedforward(
ManipulatorPivotConstants.kFeedForwardS, ManipulatorPivotConstants.kFeedForwardS,
@ -85,6 +72,7 @@ public class ManipulatorPivot extends SubsystemBase {
* @param speed The velocity at which the arm rotates * @param speed The velocity at which the arm rotates
* @return Sets motor voltage to achieve the target velocity * @return Sets motor voltage to achieve the target velocity
*/ */
/*
public Command runAssistedPivot(DoubleSupplier speed) { public Command runAssistedPivot(DoubleSupplier speed) {
double clampedSpeed = MathUtil.clamp( double clampedSpeed = MathUtil.clamp(
speed.getAsDouble(), speed.getAsDouble(),
@ -106,7 +94,7 @@ public class ManipulatorPivot extends SubsystemBase {
armMotor.setVoltage(voltsOut); armMotor.setVoltage(voltsOut);
}); });
} }
*/
/** /**
* Moves the arm to a target destination (setpoint) * Moves the arm to a target destination (setpoint)
* *
@ -122,16 +110,11 @@ public class ManipulatorPivot extends SubsystemBase {
); );
return run(() -> { return run(() -> {
double voltsOut = positionController.calculate( pivotClosedLoopController.setReference(clampedSetpoint,
getEncoderPosition(), SparkBase.ControlType.kMAXMotionPositionControl,
clampedSetpoint ClosedLoopSlot.kSlot0,
) + feedForward.calculate( feedForward.calculate(absoluteEncoder.getPosition() + ManipulatorPivotConstants.kFFGravityOffset, absoluteEncoder.getVelocity()));
getEncoderPosition(), });
getEncoderVelocity()
);
armMotor.setVoltage(voltsOut);
}).until(positionController::atSetpoint);
} }
/** /**
@ -140,15 +123,14 @@ public class ManipulatorPivot extends SubsystemBase {
* @return CANCoder's position in radians * @return CANCoder's position in radians
*/ */
public double getEncoderPosition() { public double getEncoderPosition() {
return Units.rotationsToRadians(canCoder.getAbsolutePosition().getValueAsDouble()); return absoluteEncoder.getPosition();
} }
/** /**
* Returns the CANCoder's velocity in radians per second * Returns the CANCoder's velocity in radians per second
* *
* @return CANCoder's velocity in radians per second * @return CANCoder's velocity in radians per second
*/ */
public double getEncoderVelocity() { public double getEncoderVelocity() {
return Units.rotationsToRadians(canCoder.getVelocity().getValueAsDouble()); return absoluteEncoder.getVelocity();
} }
} }

View File

@ -9,11 +9,14 @@ public class Vision{
private DoubleSubscriber blackRobotRelativePose; private DoubleSubscriber blackRobotRelativePose;
private DoubleSubscriber blackClosestTag; private DoubleSubscriber blackClosestTag;
private BooleanSubscriber blackTagDetected;
private DoubleSubscriber blackFramerate; private DoubleSubscriber blackFramerate;
private DoubleSubscriber orangeRobotRelativePose; private DoubleSubscriber orangeRobotRelativePose;
private DoubleSubscriber orangeClosestTag; private DoubleSubscriber orangeClosestTag;
private BooleanSubscriber orangeTagDetected;
private DoubleSubscriber orangeFramerate; private DoubleSubscriber orangeFramerate;
public Vision(){ public Vision(){
@ -24,11 +27,13 @@ public class Vision{
blackRobotRelativePose = blackVisionTable.getDoubleTopic("blackRelativePose").subscribe(0.0); blackRobotRelativePose = blackVisionTable.getDoubleTopic("blackRelativePose").subscribe(0.0);
blackClosestTag = blackVisionTable.getDoubleTopic("blackClosestTag").subscribe(0.0); blackClosestTag = blackVisionTable.getDoubleTopic("blackClosestTag").subscribe(0.0);
blackTagDetected = blackVisionTable.getBooleanTopic("blackTagDetected").subscribe(false);
blackFramerate = blackVisionTable.getDoubleTopic("blackFPS").subscribe(0.0); blackFramerate = blackVisionTable.getDoubleTopic("blackFPS").subscribe(0.0);
orangeRobotRelativePose = orangeVisionTable.getDoubleTopic("orangeRelativePose").subscribe(0.0); orangeRobotRelativePose = orangeVisionTable.getDoubleTopic("orangeRelativePose").subscribe(0.0);
orangeClosestTag = orangeVisionTable.getDoubleTopic("orangeClosestTag").subscribe(0.0); orangeClosestTag = orangeVisionTable.getDoubleTopic("orangeClosestTag").subscribe(0.0);
orangeTagDetected = orangeVisionTable.getBooleanTopic("orangeTagDetected").subscribe(false);
orangeFramerate = orangeVisionTable.getDoubleTopic("orangeFPS").subscribe(0.0); orangeFramerate = orangeVisionTable.getDoubleTopic("orangeFPS").subscribe(0.0);
} }
@ -45,6 +50,10 @@ public class Vision{
return blackRobotRelativePose.getLastChange(); return blackRobotRelativePose.getLastChange();
} }
public boolean getBlackTagDetected(){
return blackTagDetected.get();
}
public double getBlackFPS(){ public double getBlackFPS(){
return blackFramerate.get(); return blackFramerate.get();
} }
@ -61,6 +70,10 @@ public class Vision{
return orangeRobotRelativePose.getLastChange(); return orangeRobotRelativePose.getLastChange();
} }
public boolean getOrangeTagDetected(){
return orangeTagDetected.get();
}
public double getOrangeFPS(){ public double getOrangeFPS(){
return orangeFramerate.get(); return orangeFramerate.get();
} }