2022-02-28 16:57:41 -07:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
package frc4388.robot.subsystems;
|
|
|
|
|
|
|
|
|
|
import com.revrobotics.CANSparkMax;
|
|
|
|
|
import com.revrobotics.RelativeEncoder;
|
|
|
|
|
import com.revrobotics.SparkMaxPIDController;
|
|
|
|
|
import com.revrobotics.SparkMaxLimitSwitch;
|
|
|
|
|
import com.revrobotics.CANSparkMax.ControlType;
|
|
|
|
|
import com.revrobotics.CANSparkMax.SoftLimitDirection;
|
2022-03-12 20:22:42 -07:00
|
|
|
import com.revrobotics.SparkMaxRelativeEncoder.Type;
|
2022-02-28 16:57:41 -07:00
|
|
|
|
2022-03-12 20:39:05 -07:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2022-02-28 16:57:41 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
2022-02-28 16:54:46 -07:00
|
|
|
|
2022-01-20 20:00:31 -07:00
|
|
|
import frc4388.robot.Constants.ShooterConstants;
|
2022-02-28 16:57:41 -07:00
|
|
|
import frc4388.utility.Gains;
|
|
|
|
|
|
|
|
|
|
public class Hood extends SubsystemBase {
|
2022-01-25 18:57:28 -07:00
|
|
|
public BoomBoom m_shooterSubsystem;
|
2022-02-28 16:57:41 -07:00
|
|
|
|
2022-03-06 00:24:47 -07:00
|
|
|
public CANSparkMax m_angleAdjusterMotor;
|
2022-03-08 16:14:34 -07:00
|
|
|
// public SparkMaxLimitSwitch m_hoodUpLimitSwitch;
|
|
|
|
|
// public SparkMaxLimitSwitch m_hoodDownLimitSwitch;
|
2022-03-06 00:27:06 -07:00
|
|
|
public static Gains m_angleAdjusterGains;
|
|
|
|
|
public RelativeEncoder m_angleEncoder;
|
2022-01-25 19:56:32 -07:00
|
|
|
|
2022-03-06 00:27:06 -07:00
|
|
|
public SparkMaxPIDController m_angleAdjusterPIDController;
|
2022-02-28 16:57:41 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean m_isHoodReady = false;
|
|
|
|
|
|
2022-03-12 20:43:36 -07:00
|
|
|
public double m_fireAngle;
|
2022-02-28 16:57:41 -07:00
|
|
|
|
2022-03-17 13:49:13 -06:00
|
|
|
public double speedLimiter;
|
2022-03-19 21:03:01 -06:00
|
|
|
public double calibrationSpeed = 1.0;
|
2022-02-28 16:57:41 -07:00
|
|
|
|
|
|
|
|
/** Creates a new Hood. */
|
2022-03-06 00:24:47 -07:00
|
|
|
public Hood(CANSparkMax angleAdjusterMotor) {
|
|
|
|
|
|
|
|
|
|
m_angleAdjusterMotor = angleAdjusterMotor;
|
2022-03-06 00:27:06 -07:00
|
|
|
m_angleEncoder = m_angleAdjusterMotor.getEncoder();
|
|
|
|
|
m_angleAdjusterPIDController = m_angleAdjusterMotor.getPIDController();
|
|
|
|
|
m_angleAdjusterGains = ShooterConstants.SHOOTER_ANGLE_GAINS;
|
2022-01-20 20:00:31 -07:00
|
|
|
|
2022-03-08 16:14:34 -07:00
|
|
|
// m_hoodUpLimitSwitch = m_angleAdjusterMotor.getForwardLimitSwitch(SparkMaxLimitSwitch.Type.kNormallyOpen);
|
|
|
|
|
// m_hoodDownLimitSwitch = m_angleAdjusterMotor.getReverseLimitSwitch(SparkMaxLimitSwitch.Type.kNormallyOpen);
|
|
|
|
|
// m_hoodUpLimitSwitch.enableLimitSwitch(true);
|
|
|
|
|
// m_hoodDownLimitSwitch.enableLimitSwitch(true);
|
2022-02-28 17:20:01 -07:00
|
|
|
|
2022-03-17 13:49:13 -06:00
|
|
|
m_angleAdjusterMotor.setSoftLimit(SoftLimitDirection.kForward, (float) ShooterConstants.HOOD_FORWARD_SOFT_LIMIT);
|
|
|
|
|
m_angleAdjusterMotor.setSoftLimit(SoftLimitDirection.kReverse, (float) ShooterConstants.HOOD_REVERSE_SOFT_LIMIT);
|
2022-03-12 22:19:42 -07:00
|
|
|
setHoodSoftLimits(true);
|
2022-03-17 13:49:13 -06:00
|
|
|
|
|
|
|
|
this.speedLimiter = 1.0;
|
2022-02-28 16:57:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void periodic() {
|
|
|
|
|
// This method will be called once per scheduler run
|
2022-03-12 22:19:42 -07:00
|
|
|
SmartDashboard.putNumber("Hood Angle", m_angleEncoder.getPosition());
|
2022-03-17 13:49:13 -06:00
|
|
|
|
|
|
|
|
// * speed limiting near soft limits. tolerance (distance when ramping starts) is 20 rotations. speed at hard limits is 0.2 (percent output).
|
|
|
|
|
|
2022-03-19 21:03:01 -06:00
|
|
|
if (areSoftLimitsEnabled()) {
|
|
|
|
|
double currentPos = this.getEncoderPosition();
|
|
|
|
|
double forwardDistance = Math.abs(currentPos - ShooterConstants.HOOD_FORWARD_SOFT_LIMIT);
|
|
|
|
|
double reverseDistance = Math.abs(currentPos - ShooterConstants.HOOD_REVERSE_SOFT_LIMIT);
|
2022-03-17 13:49:13 -06:00
|
|
|
|
2022-03-19 21:03:01 -06:00
|
|
|
if (forwardDistance < ShooterConstants.HOOD_SOFT_LIMIT_TOLERANCE) {
|
|
|
|
|
this.speedLimiter = 0.2 + (forwardDistance * (1 / ShooterConstants.HOOD_SOFT_LIMIT_TOLERANCE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reverseDistance < ShooterConstants.HOOD_SOFT_LIMIT_TOLERANCE) {
|
|
|
|
|
this.speedLimiter = 0.2 + (reverseDistance * (1 / ShooterConstants.HOOD_SOFT_LIMIT_TOLERANCE));
|
|
|
|
|
}
|
2022-03-17 13:49:13 -06:00
|
|
|
|
2022-03-19 21:03:01 -06:00
|
|
|
if ((forwardDistance > ShooterConstants.HOOD_SOFT_LIMIT_TOLERANCE) && (reverseDistance > ShooterConstants.HOOD_SOFT_LIMIT_TOLERANCE)) {
|
|
|
|
|
this.speedLimiter = 1.0;
|
|
|
|
|
}
|
2022-03-17 13:49:13 -06:00
|
|
|
}
|
2022-02-28 16:57:41 -07:00
|
|
|
}
|
2022-02-28 17:20:01 -07:00
|
|
|
|
2022-03-19 21:03:01 -06:00
|
|
|
public boolean areSoftLimitsEnabled() {
|
|
|
|
|
return this.m_angleAdjusterMotor.isSoftLimitEnabled(SoftLimitDirection.kForward) && this.m_angleAdjusterMotor.isSoftLimitEnabled(SoftLimitDirection.kReverse);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 17:20:01 -07:00
|
|
|
/**
|
|
|
|
|
* Set status of hood motor soft limits.
|
|
|
|
|
* @param set Boolean to set soft limits to.
|
2022-03-05 11:04:40 -07:00
|
|
|
*/
|
2022-02-28 17:20:01 -07:00
|
|
|
public void setHoodSoftLimits(boolean set) {
|
2022-03-05 11:07:54 -07:00
|
|
|
m_angleAdjusterMotor.enableSoftLimit(SoftLimitDirection.kForward, set);
|
|
|
|
|
m_angleAdjusterMotor.enableSoftLimit(SoftLimitDirection.kReverse, set);
|
2022-02-28 16:57:41 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-20 20:00:31 -07:00
|
|
|
public void runAngleAdjustPID(double targetAngle)
|
|
|
|
|
{
|
|
|
|
|
//Set PID Coefficients
|
2022-03-05 11:07:54 -07:00
|
|
|
m_angleAdjusterPIDController.setP(m_angleAdjusterGains.kP);
|
|
|
|
|
m_angleAdjusterPIDController.setI(m_angleAdjusterGains.kI);
|
|
|
|
|
m_angleAdjusterPIDController.setD(m_angleAdjusterGains.kD);
|
|
|
|
|
m_angleAdjusterPIDController.setIZone(m_angleAdjusterGains.kIzone);
|
|
|
|
|
m_angleAdjusterPIDController.setFF(m_angleAdjusterGains.kF);
|
|
|
|
|
m_angleAdjusterPIDController.setOutputRange(ShooterConstants.SHOOTER_TURRET_MIN, m_angleAdjusterGains.kPeakOutput);
|
|
|
|
|
|
|
|
|
|
m_angleAdjusterPIDController.setReference(targetAngle, ControlType.kPosition);
|
2022-01-20 20:00:31 -07:00
|
|
|
}
|
2022-01-25 19:56:32 -07:00
|
|
|
|
2022-03-12 14:59:21 -07:00
|
|
|
/**
|
|
|
|
|
* Runs the hood with the given input
|
|
|
|
|
* @param input value from -1.0 to 1.0, postive is upward (more horizontal shootijng angle)
|
|
|
|
|
*/
|
2022-01-20 20:00:31 -07:00
|
|
|
public void runHood(double input) {
|
2022-03-19 21:03:01 -06:00
|
|
|
m_angleAdjusterMotor.set(input * this.speedLimiter * this.calibrationSpeed);
|
2022-01-20 20:00:31 -07:00
|
|
|
}
|
2022-02-28 16:57:41 -07:00
|
|
|
|
2022-03-14 18:58:44 -06:00
|
|
|
/**
|
|
|
|
|
* Run a PID to go to the zero position.
|
|
|
|
|
*/
|
|
|
|
|
public void gotoZero() {
|
|
|
|
|
runAngleAdjustPID(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void resetGyroAngleAdj() {
|
2022-02-28 16:57:41 -07:00
|
|
|
m_angleEncoder.setPosition(0);
|
|
|
|
|
}
|
2022-01-20 20:00:31 -07:00
|
|
|
|
2022-03-17 13:49:13 -06:00
|
|
|
public double getEncoderPosition(){
|
|
|
|
|
return m_angleEncoder.getPosition();
|
2022-02-28 16:57:41 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-20 20:00:31 -07:00
|
|
|
public double getAnglePositionDegrees(){
|
2022-02-05 14:27:11 -07:00
|
|
|
return 0.0;//((m_angleEncoder.getPosition() - ShooterConstants.HOOD_MOTOR_POS_AT_ZERO_ROT) * 360/ShooterConstants.HOOD_MOTOR_ROTS_PER_ROT) - 90;
|
2022-01-20 20:00:31 -07:00
|
|
|
}
|
2022-03-12 11:36:10 -07:00
|
|
|
|
|
|
|
|
public double getCurrent(){
|
|
|
|
|
return m_angleAdjusterMotor.getOutputCurrent();
|
|
|
|
|
}
|
2022-02-28 16:57:41 -07:00
|
|
|
}
|