2022-03-05 14:21:48 -07:00
|
|
|
package frc4388.robot.subsystems;
|
|
|
|
|
|
|
|
|
|
import com.ctre.phoenix.motorcontrol.LimitSwitchNormal;
|
|
|
|
|
import com.revrobotics.CANSparkMax;
|
|
|
|
|
import com.revrobotics.SparkMaxLimitSwitch;
|
|
|
|
|
|
|
|
|
|
import edu.wpi.first.wpilibj.DigitalInput;
|
|
|
|
|
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
|
|
|
|
import frc4388.robot.Constants.ClawConstants;
|
|
|
|
|
|
|
|
|
|
public class Claws extends SubsystemBase {
|
2022-03-05 14:38:54 -07:00
|
|
|
public CANSparkMax m_leftClaw;
|
|
|
|
|
public CANSparkMax m_rightClaw;
|
2022-03-05 14:21:48 -07:00
|
|
|
|
|
|
|
|
private SparkMaxLimitSwitch m_leftLimitSwitchF;
|
|
|
|
|
private SparkMaxLimitSwitch m_rightLimitSwitchF;
|
|
|
|
|
private SparkMaxLimitSwitch m_leftLimitSwitchR;
|
|
|
|
|
private SparkMaxLimitSwitch m_rightLimitSwitchR;
|
|
|
|
|
|
|
|
|
|
private double m_leftOffset;
|
|
|
|
|
private double m_rightOffset;
|
|
|
|
|
|
|
|
|
|
private boolean m_open;
|
2022-03-05 14:38:54 -07:00
|
|
|
public static enum ClawType {LEFT, RIGHT}
|
2022-03-05 14:21:48 -07:00
|
|
|
|
|
|
|
|
public Claws(CANSparkMax leftClaw, CANSparkMax rightClaw) {
|
|
|
|
|
m_leftClaw = leftClaw;
|
|
|
|
|
m_rightClaw = rightClaw;
|
|
|
|
|
|
|
|
|
|
m_leftLimitSwitchF = m_leftClaw.getForwardLimitSwitch(SparkMaxLimitSwitch.Type.kNormallyOpen);
|
|
|
|
|
m_rightLimitSwitchF = m_rightClaw.getForwardLimitSwitch(SparkMaxLimitSwitch.Type.kNormallyOpen);
|
|
|
|
|
m_leftLimitSwitchR = m_leftClaw.getReverseLimitSwitch(SparkMaxLimitSwitch.Type.kNormallyClosed); //Wired wrong lol
|
|
|
|
|
m_rightLimitSwitchR = m_rightClaw.getReverseLimitSwitch(SparkMaxLimitSwitch.Type.kNormallyOpen);
|
|
|
|
|
|
|
|
|
|
m_leftLimitSwitchF.enableLimitSwitch(true);
|
|
|
|
|
m_rightLimitSwitchF.enableLimitSwitch(true);
|
|
|
|
|
m_leftLimitSwitchR.enableLimitSwitch(true);
|
|
|
|
|
m_rightLimitSwitchR.enableLimitSwitch(true);
|
|
|
|
|
leftClaw.setInverted(true);
|
|
|
|
|
rightClaw.setInverted(true);
|
|
|
|
|
|
|
|
|
|
m_open = false;
|
|
|
|
|
|
|
|
|
|
// m_leftClaw.set(ClawConstants.CALIBRATION_SPEED);
|
|
|
|
|
// m_rightClaw.set(ClawConstants.CALIBRATION_SPEED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setSpeed(double speed){
|
|
|
|
|
m_leftClaw.set(speed);
|
|
|
|
|
m_rightClaw.set(speed);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 14:38:54 -07:00
|
|
|
/**
|
|
|
|
|
* Run a specific claw to open or close.
|
|
|
|
|
* @param which Which claw to run.
|
|
|
|
|
* @param open Whether to open or close the claw.
|
|
|
|
|
*/
|
|
|
|
|
public void runClaw(ClawType which, boolean open) {
|
2022-03-05 23:07:12 -07:00
|
|
|
|
|
|
|
|
int direction = open ? 1 : -1;
|
|
|
|
|
|
2022-03-05 14:38:54 -07:00
|
|
|
if (which == Claws.ClawType.LEFT) {
|
|
|
|
|
|
|
|
|
|
// double setPos = open ? ClawsConstants.OPEN_POSITION + m_leftOffset : ClawsConstants.CLOSE_POSITION + m_leftOffset;
|
|
|
|
|
// m_leftClaw.getEncoder().setPosition(setPos);
|
2022-03-05 23:07:12 -07:00
|
|
|
m_leftClaw.set(direction * 0.1);
|
2022-03-05 14:38:54 -07:00
|
|
|
|
|
|
|
|
} else if (which == Claws.ClawType.RIGHT) {
|
|
|
|
|
|
|
|
|
|
// double setPos = open ? ClawsConstants.OPEN_POSITION + m_rightOffset : ClawsConstants.CLOSE_POSITION + m_rightOffset;
|
|
|
|
|
// m_rightClaw.getEncoder().setPosition(setPos);
|
2022-03-05 23:07:12 -07:00
|
|
|
m_rightClaw.set(direction * 0.1);
|
2022-03-05 14:38:54 -07:00
|
|
|
}
|
2022-03-05 23:07:12 -07:00
|
|
|
|
|
|
|
|
m_open = open;
|
2022-03-05 14:38:54 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-05 14:21:48 -07:00
|
|
|
public void setOpen(boolean open) {
|
|
|
|
|
if(open) {
|
2022-03-05 14:38:54 -07:00
|
|
|
// m_leftClaw.getEncoder().setPosition(ClawsConstants.OPEN_POSITION + m_leftOffset);
|
|
|
|
|
// m_rightClaw.getEncoder().setPosition(ClawsConstants.OPEN_POSITION + m_rightOffset);
|
|
|
|
|
m_leftClaw.set(0.1);
|
|
|
|
|
m_rightClaw.set(0.1);
|
2022-03-05 14:21:48 -07:00
|
|
|
} else {
|
2022-03-05 14:38:54 -07:00
|
|
|
// m_leftClaw.getEncoder().setPosition(ClawsConstants.CLOSE_POSITION + m_leftOffset);
|
|
|
|
|
// m_rightClaw.getEncoder().setPosition(ClawsConstants.CLOSE_POSITION + m_rightOffset);
|
|
|
|
|
m_leftClaw.set(-0.1);
|
|
|
|
|
m_rightClaw.set(-0.1);
|
2022-03-05 14:21:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_open = open;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 14:38:54 -07:00
|
|
|
public double[] getOffsets() {
|
|
|
|
|
return new double[] {m_leftOffset, m_rightOffset};
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 14:21:48 -07:00
|
|
|
public boolean getOpen() {
|
|
|
|
|
return m_open;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 14:38:54 -07:00
|
|
|
/**
|
|
|
|
|
* Check if a limit switch is pressed or current limit exceeded for a claw.
|
|
|
|
|
* @param which Which claw to check.
|
|
|
|
|
* @param limit The current limit.
|
|
|
|
|
* @return Whether to interrupt the RunClaw command or not.
|
|
|
|
|
*/
|
2022-03-05 23:43:29 -07:00
|
|
|
public boolean checkSwitchAndCurrent(ClawType which) {
|
2022-03-05 14:38:54 -07:00
|
|
|
if (which == ClawType.LEFT) {
|
2022-03-05 23:16:29 -07:00
|
|
|
if (m_leftLimitSwitchF.isPressed() || m_leftLimitSwitchR.isPressed() || m_leftClaw.getOutputCurrent() >= ClawConstants.CURRENT_LIMIT) {
|
2022-03-05 14:38:54 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
2022-03-05 23:43:29 -07:00
|
|
|
}
|
|
|
|
|
else if (which == ClawType.RIGHT) {
|
2022-03-05 23:16:29 -07:00
|
|
|
if (m_rightLimitSwitchF.isPressed() || m_rightLimitSwitchR.isPressed() || m_rightClaw.getOutputCurrent() >= ClawConstants.CURRENT_LIMIT) {
|
2022-03-05 14:38:54 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 14:21:48 -07:00
|
|
|
@Override
|
|
|
|
|
public void periodic() {
|
2022-03-05 14:38:54 -07:00
|
|
|
if(m_leftLimitSwitchF.isPressed() || m_leftLimitSwitchR.isPressed())
|
2022-03-05 14:21:48 -07:00
|
|
|
m_leftOffset = m_leftClaw.getEncoder().getPosition();
|
|
|
|
|
|
2022-03-05 14:38:54 -07:00
|
|
|
if(m_rightLimitSwitchF.isPressed() || m_rightLimitSwitchR.isPressed())
|
2022-03-05 14:21:48 -07:00
|
|
|
m_rightOffset = m_rightClaw.getEncoder().getPosition();
|
|
|
|
|
}
|
2022-03-05 14:38:54 -07:00
|
|
|
}
|