2023-01-20 18:06:53 -07:00
|
|
|
package frc4388.robot.subsystems;
|
|
|
|
|
|
2023-02-09 19:13:55 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.ControlMode;
|
2023-02-28 17:47:35 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.FeedbackDevice;
|
2023-03-06 19:36:15 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.LimitSwitchNormal;
|
|
|
|
|
import com.ctre.phoenix.motorcontrol.LimitSwitchSource;
|
2023-02-28 17:47:35 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.RemoteSensorSource;
|
2023-03-13 16:00:09 -06:00
|
|
|
import com.ctre.phoenix.motorcontrol.TalonFXFeedbackDevice;
|
2023-02-28 17:47:35 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.can.TalonFXConfiguration;
|
2023-01-20 18:06:53 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
|
2023-02-28 17:47:35 -07:00
|
|
|
import com.ctre.phoenix.sensors.CANCoder;
|
|
|
|
|
import com.ctre.phoenix.sensors.CANCoderConfiguration;
|
2023-02-09 19:13:55 -07:00
|
|
|
import frc4388.robot.Constants.ArmConstants;
|
2023-03-06 16:35:20 -07:00
|
|
|
import frc4388.robot.Constants.SwerveDriveConstants;
|
2023-03-14 10:35:53 -06:00
|
|
|
import frc4388.utility.DeferredBlock;
|
2023-02-09 19:13:55 -07:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2023-03-11 14:03:36 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.InstantCommand;
|
|
|
|
|
import edu.wpi.first.wpilibj2.command.RunCommand;
|
2023-01-20 18:06:53 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
|
|
|
|
|
|
|
|
|
public class Arm extends SubsystemBase {
|
2023-02-09 19:13:55 -07:00
|
|
|
private WPI_TalonFX m_tele;
|
2023-03-14 14:26:47 -06:00
|
|
|
public WPI_TalonFX m_pivot;
|
2023-02-28 17:47:35 -07:00
|
|
|
private CANCoder m_pivotEncoder;
|
|
|
|
|
private boolean m_debug;
|
2023-02-13 18:12:32 -07:00
|
|
|
|
|
|
|
|
// Moves arm to distance [dist] then returns new ang
|
2023-02-28 17:47:35 -07:00
|
|
|
public Arm(WPI_TalonFX pivot, WPI_TalonFX tele, CANCoder encoder, boolean debug) {
|
|
|
|
|
m_tele = tele;
|
|
|
|
|
m_pivot = pivot;
|
|
|
|
|
m_pivotEncoder = encoder;
|
|
|
|
|
|
2023-03-06 19:36:15 -07:00
|
|
|
m_tele.configFactoryDefault();
|
|
|
|
|
m_tele.configReverseLimitSwitchSource(LimitSwitchSource.FeedbackConnector, LimitSwitchNormal.NormallyOpen);
|
|
|
|
|
m_tele.configForwardLimitSwitchSource(LimitSwitchSource.FeedbackConnector, LimitSwitchNormal.NormallyOpen);
|
2023-03-06 16:35:20 -07:00
|
|
|
m_pivot.configFactoryDefault();
|
|
|
|
|
|
2023-03-14 10:35:53 -06:00
|
|
|
// * Example of deferred code
|
2023-03-14 15:25:53 -06:00
|
|
|
new DeferredBlock(() -> resetTeleSoftLimit());
|
2023-03-02 16:56:37 -07:00
|
|
|
|
2023-02-28 17:47:35 -07:00
|
|
|
CANCoderConfiguration config = new CANCoderConfiguration();
|
|
|
|
|
config.magnetOffsetDegrees = ArmConstants.OFFSET;
|
|
|
|
|
m_pivotEncoder.configAllSettings(config);
|
2023-02-09 19:13:55 -07:00
|
|
|
}
|
|
|
|
|
|
2023-02-28 17:47:35 -07:00
|
|
|
public Arm(WPI_TalonFX pivot, WPI_TalonFX tele, CANCoder encoder) {
|
|
|
|
|
this(pivot, tele, encoder, false);
|
2023-02-09 19:13:55 -07:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 16:45:57 -07:00
|
|
|
public void setRotVel(double vel) {
|
2023-03-14 14:26:47 -06:00
|
|
|
var degrees = Math.abs(getArmRotation()) - 135;
|
|
|
|
|
SmartDashboard.putNumber("arm degrees", degrees);
|
|
|
|
|
SmartDashboard.putNumber("arm rot vel", vel);
|
|
|
|
|
|
|
|
|
|
if ((degrees < 2 && vel < 0) || (degrees > 110 && vel > 0)) {
|
|
|
|
|
m_pivot.set(ControlMode.PercentOutput, 0);
|
|
|
|
|
} else if (degrees > 90 && vel > 0) {
|
2023-03-14 15:13:41 -06:00
|
|
|
m_pivot.set(ControlMode.PercentOutput, .15 * vel);
|
2023-03-14 14:26:47 -06:00
|
|
|
} else {
|
2023-03-14 15:13:41 -06:00
|
|
|
m_pivot.set(ControlMode.PercentOutput, .3 * vel);
|
2023-03-14 14:26:47 -06:00
|
|
|
}
|
2023-03-01 16:45:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setTeleVel(double vel) {
|
2023-03-11 14:03:36 -07:00
|
|
|
m_tele.set(ControlMode.PercentOutput, -0.5 * vel);
|
2023-03-01 16:45:57 -07:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 09:53:52 -06:00
|
|
|
public boolean isTeleIn() {
|
|
|
|
|
return m_tele.isFwdLimitSwitchClosed() == 1 ||
|
|
|
|
|
m_tele.getSelectedSensorPosition() < reverse_tele;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 19:13:55 -07:00
|
|
|
public void armSetRotation(double rot) {
|
|
|
|
|
if (rot > 1 || rot < 0) return;
|
|
|
|
|
// Move arm code
|
2023-02-13 19:16:23 -07:00
|
|
|
m_pivot.set(ControlMode.Position, rot * Math.abs(ArmConstants.PIVOT_REVERSE_SOFT_LIMIT - ArmConstants.PIVOT_FORWARD_SOFT_LIMIT) +
|
2023-02-09 19:13:55 -07:00
|
|
|
ArmConstants.PIVOT_FORWARD_SOFT_LIMIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void armSetLength(double len) {
|
|
|
|
|
if (len > 1 || len < 0) return;
|
|
|
|
|
// Move arm code
|
2023-02-13 19:16:23 -07:00
|
|
|
m_tele.set(ControlMode.Position, len * Math.abs(ArmConstants.TELE_REVERSE_SOFT_LIMIT - ArmConstants.TELE_FORWARD_SOFT_LIMIT) +
|
2023-02-09 19:13:55 -07:00
|
|
|
ArmConstants.TELE_FORWARD_SOFT_LIMIT);
|
2023-02-28 17:47:35 -07:00
|
|
|
|
|
|
|
|
if (m_tele.isRevLimitSwitchClosed() == 1) {
|
|
|
|
|
m_tele.setSelectedSensorPosition(ArmConstants.TELE_REVERSE_SOFT_LIMIT);
|
|
|
|
|
} else if (m_tele.isFwdLimitSwitchClosed() == 1) {
|
|
|
|
|
m_tele.setSelectedSensorPosition(ArmConstants.TELE_FORWARD_SOFT_LIMIT);
|
|
|
|
|
}
|
2023-02-09 19:13:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getArmLength() {
|
2023-03-14 10:54:00 -06:00
|
|
|
return m_tele.getSelectedSensorPosition();
|
2023-02-09 19:13:55 -07:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 16:45:57 -07:00
|
|
|
public double getArmRotation() {
|
2023-03-14 10:54:00 -06:00
|
|
|
return m_pivotEncoder.getAbsolutePosition();
|
2023-03-01 16:45:57 -07:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 19:13:55 -07:00
|
|
|
public void runPivotTele(double pivot, double tele) {
|
2023-03-14 10:58:27 -06:00
|
|
|
double abs_pivot = Math.toRadians(getArmRotation() - 135);
|
|
|
|
|
double abs_tele = (getArmLength() - ArmConstants.TELE_REVERSE_SOFT_LIMIT) /
|
|
|
|
|
(ArmConstants.TELE_FORWARD_SOFT_LIMIT - ArmConstants.TELE_REVERSE_SOFT_LIMIT);
|
|
|
|
|
|
|
|
|
|
if (pivot > 0 || tele < 0 || checkLimits(abs_tele, abs_pivot)) {
|
2023-03-14 10:54:00 -06:00
|
|
|
setRotVel(pivot);
|
|
|
|
|
setTeleVel(tele);
|
2023-02-09 19:13:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks that an input is within bounds
|
|
|
|
|
* @param _len length from 0 to 1
|
|
|
|
|
* @param _theta radians from the zero (straight up)
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static boolean checkLimits(double _len, double _theta) {
|
|
|
|
|
var len = _len * (ArmConstants.MAX_ARM_LEN - ArmConstants.MIN_ARM_LEN) + ArmConstants.MIN_ARM_LEN;
|
|
|
|
|
var x = len * Math.cos(_theta);
|
|
|
|
|
var y = ArmConstants.ARM_HEIGHT + len * Math.sin(_theta);
|
|
|
|
|
|
|
|
|
|
var minHeight = Math.pow(ArmConstants.CURVE_POWER, Math.abs(x));
|
2023-03-14 10:54:00 -06:00
|
|
|
return y < minHeight;
|
2023-02-09 19:13:55 -07:00
|
|
|
}
|
|
|
|
|
|
2023-03-06 16:35:20 -07:00
|
|
|
boolean tele_softLimit = false;
|
|
|
|
|
public void resetTeleSoftLimit() {
|
|
|
|
|
if (!tele_softLimit) {
|
|
|
|
|
var tele_soft = m_tele.getSelectedSensorPosition();
|
|
|
|
|
m_tele.configForwardSoftLimitThreshold(91000 - tele_soft);
|
|
|
|
|
m_tele.configReverseSoftLimitThreshold(tele_soft);
|
|
|
|
|
m_tele.configForwardSoftLimitEnable(true);
|
|
|
|
|
m_tele.configReverseSoftLimitEnable(true);
|
2023-03-15 09:53:52 -06:00
|
|
|
|
|
|
|
|
reverse_tele = tele_soft;
|
2023-03-06 16:35:20 -07:00
|
|
|
} else {
|
|
|
|
|
m_tele.configForwardSoftLimitEnable(false);
|
|
|
|
|
m_tele.configReverseSoftLimitEnable(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tele_softLimit = !tele_softLimit;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 09:53:52 -06:00
|
|
|
boolean resetable = true;
|
|
|
|
|
boolean tele_reset = true;
|
|
|
|
|
double reverse_tele = 0;
|
2023-03-06 16:35:20 -07:00
|
|
|
|
2023-02-09 19:13:55 -07:00
|
|
|
@Override
|
|
|
|
|
public void periodic() {
|
2023-03-06 16:35:20 -07:00
|
|
|
double degrees = Math.abs(m_pivotEncoder.getAbsolutePosition() - 135);
|
|
|
|
|
|
2023-03-11 14:03:36 -07:00
|
|
|
if (m_tele.isFwdLimitSwitchClosed() == 1 && tele_reset) {
|
|
|
|
|
var tele_soft = m_tele.getSelectedSensorPosition();
|
|
|
|
|
m_tele.configForwardSoftLimitThreshold(91000 - tele_soft);
|
|
|
|
|
m_tele.configReverseSoftLimitThreshold(1000 - tele_soft);
|
|
|
|
|
m_tele.configForwardSoftLimitEnable(true);
|
|
|
|
|
m_tele.configReverseSoftLimitEnable(true);
|
2023-03-15 09:53:52 -06:00
|
|
|
|
|
|
|
|
reverse_tele = 1000 - tele_soft;
|
2023-03-11 14:03:36 -07:00
|
|
|
tele_reset = false;
|
|
|
|
|
} else if (m_tele.isFwdLimitSwitchClosed() == 0) {
|
|
|
|
|
tele_reset = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-14 15:33:43 -06:00
|
|
|
// double x = Math.cos(Math.toRadians(degrees));
|
2023-02-09 19:13:55 -07:00
|
|
|
}
|
2023-03-11 14:03:36 -07:00
|
|
|
|
|
|
|
|
boolean soft_limits = true;
|
|
|
|
|
public void killSoftLimits() {
|
2023-03-14 14:26:47 -06:00
|
|
|
resetTeleSoftLimit();
|
2023-03-11 14:03:36 -07:00
|
|
|
var pivot_soft = m_pivot.getSelectedSensorPosition();
|
|
|
|
|
var tele_soft = m_tele.getSelectedSensorPosition();
|
|
|
|
|
|
|
|
|
|
m_pivot.configForwardSoftLimitEnable(!soft_limits);
|
|
|
|
|
m_pivot.configReverseSoftLimitEnable(!soft_limits);
|
|
|
|
|
|
|
|
|
|
soft_limits = !soft_limits;
|
|
|
|
|
}
|
2023-01-20 18:06:53 -07:00
|
|
|
}
|