2022-02-25 20:18:21 -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.
|
|
|
|
|
|
2022-03-14 20:10:12 -06:00
|
|
|
package frc4388.robot.commands.ShooterCommands;
|
2022-02-25 20:18:21 -07:00
|
|
|
|
2022-03-06 18:02:04 -07:00
|
|
|
import edu.wpi.first.math.geometry.Pose2d;
|
|
|
|
|
import edu.wpi.first.math.geometry.Translation2d;
|
2022-03-06 00:05:54 -07:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2022-02-25 20:18:21 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.CommandBase;
|
2022-03-05 11:07:54 -07:00
|
|
|
import frc4388.robot.Constants.ShooterConstants;
|
2022-03-06 18:02:04 -07:00
|
|
|
import frc4388.robot.Constants.SwerveDriveConstants;
|
2022-02-25 20:18:21 -07:00
|
|
|
import frc4388.robot.subsystems.BoomBoom;
|
|
|
|
|
import frc4388.robot.subsystems.Hood;
|
|
|
|
|
import frc4388.robot.subsystems.SwerveDrive;
|
2022-02-26 15:00:09 -07:00
|
|
|
import frc4388.robot.subsystems.Turret;
|
2022-03-05 11:07:54 -07:00
|
|
|
import frc4388.utility.DummySensor;
|
|
|
|
|
import frc4388.utility.Gains;
|
2022-02-25 20:18:21 -07:00
|
|
|
|
|
|
|
|
public class Shoot extends CommandBase {
|
|
|
|
|
|
2022-02-26 15:00:09 -07:00
|
|
|
// subsystems
|
2022-03-18 20:02:23 -06:00
|
|
|
private SwerveDrive m_swerve;
|
|
|
|
|
private BoomBoom m_boomBoom;
|
|
|
|
|
private Turret m_turret;
|
|
|
|
|
private Hood m_hood;
|
2022-02-25 20:18:21 -07:00
|
|
|
|
2022-02-26 15:00:09 -07:00
|
|
|
// given
|
2022-03-18 20:02:23 -06:00
|
|
|
private double m_gyroAngle;
|
|
|
|
|
private double m_odoX;
|
|
|
|
|
private double m_odoY;
|
|
|
|
|
private double m_distance;
|
2022-02-26 15:00:09 -07:00
|
|
|
|
|
|
|
|
// targets
|
2022-03-18 20:02:23 -06:00
|
|
|
private double m_targetVel;
|
|
|
|
|
private double m_targetHood;
|
|
|
|
|
private double m_targetAngle;
|
|
|
|
|
private Pose2d m_targetPoint;
|
2022-02-26 15:00:09 -07:00
|
|
|
|
|
|
|
|
// pid
|
2022-03-18 20:02:23 -06:00
|
|
|
private double error;
|
|
|
|
|
private double prevError;
|
|
|
|
|
private Gains gains = ShooterConstants.SHOOT_GAINS;
|
|
|
|
|
private double kP, kI, kD;
|
|
|
|
|
private double proportional, integral, derivative;
|
|
|
|
|
private double time;
|
|
|
|
|
private double output;
|
|
|
|
|
private double normOutput;
|
|
|
|
|
private double tolerance;
|
|
|
|
|
|
|
|
|
|
private boolean isAimedInTolerance;
|
|
|
|
|
private int inverted;
|
|
|
|
|
private double initialSwerveRotation;
|
2022-02-26 15:00:09 -07:00
|
|
|
|
2022-03-05 11:07:54 -07:00
|
|
|
// testing
|
2022-03-19 19:58:21 -06:00
|
|
|
private boolean simMode = false;
|
2022-03-18 20:02:23 -06:00
|
|
|
private DummySensor driveDummy;
|
|
|
|
|
private DummySensor turretDummy;
|
2022-02-25 20:18:21 -07:00
|
|
|
|
2022-03-05 11:07:54 -07:00
|
|
|
/**
|
|
|
|
|
* Creates a new shoot command, allowing the robot to aim and be ready to fire a ball
|
|
|
|
|
* TODO: Velocity Correction
|
|
|
|
|
* @param sDrive Drive Train
|
|
|
|
|
* @param sShooter Shooter Drum
|
|
|
|
|
* @param sTurret Shooter Turret
|
|
|
|
|
* @param sHood Shooter Hood
|
|
|
|
|
*/
|
2022-02-26 15:00:09 -07:00
|
|
|
public Shoot(SwerveDrive sDrive, BoomBoom sShooter, Turret sTurret, Hood sHood) {
|
2022-02-25 20:18:21 -07:00
|
|
|
// Use addRequirements() here to declare subsystem dependencies.
|
|
|
|
|
m_swerve = sDrive;
|
|
|
|
|
m_boomBoom = sShooter;
|
2022-02-26 15:00:09 -07:00
|
|
|
m_turret = sTurret;
|
2022-02-25 20:18:21 -07:00
|
|
|
m_hood = sHood;
|
|
|
|
|
|
2022-02-26 15:00:09 -07:00
|
|
|
addRequirements(m_swerve, m_boomBoom, m_turret, m_hood);
|
|
|
|
|
|
2022-03-06 18:02:04 -07:00
|
|
|
kP = gains.kP;
|
|
|
|
|
kI = gains.kI;
|
|
|
|
|
kD = gains.kD;
|
2022-02-26 15:00:09 -07:00
|
|
|
|
2022-03-01 17:18:25 -07:00
|
|
|
proportional = 0;
|
2022-02-26 15:00:09 -07:00
|
|
|
integral = 0;
|
|
|
|
|
derivative = 0;
|
|
|
|
|
time = 0.02;
|
2022-03-05 11:07:54 -07:00
|
|
|
|
2022-03-06 00:05:54 -07:00
|
|
|
tolerance = 5.0;
|
|
|
|
|
isAimedInTolerance = false;
|
|
|
|
|
|
2022-03-06 18:02:04 -07:00
|
|
|
if (simMode) {
|
|
|
|
|
driveDummy = new DummySensor(180);
|
|
|
|
|
turretDummy = new DummySensor(180);
|
2022-03-06 13:02:17 -07:00
|
|
|
|
2022-03-06 18:02:04 -07:00
|
|
|
DummySensor.resetAll();
|
|
|
|
|
}
|
2022-02-26 15:00:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates error for custom PID.
|
|
|
|
|
*/
|
|
|
|
|
public void updateError() {
|
2022-03-16 15:35:38 -06:00
|
|
|
m_targetPoint = SwerveDriveConstants.HUB_POSE;
|
2022-03-19 19:58:21 -06:00
|
|
|
// m_targetAngle = AimToCenter.angleToCenter(m_odoX, m_odoY, driveDummy.get());
|
|
|
|
|
m_targetAngle = AimToCenter.aaravAngleToCenter(m_odoX, m_odoY, m_swerve.getRegGyro().getDegrees());
|
|
|
|
|
// error = (m_targetAngle - turretDummy.get() + 360) % 360;
|
|
|
|
|
error = (m_targetAngle - m_turret.getBoomBoomAngleDegrees() + 360) % 360;
|
2022-03-06 00:05:54 -07:00
|
|
|
isAimedInTolerance = (Math.abs(error) <= tolerance);
|
2022-03-06 18:02:04 -07:00
|
|
|
|
|
|
|
|
if (simMode) {
|
|
|
|
|
SmartDashboard.putBoolean("isAimed?", isAimedInTolerance);
|
|
|
|
|
System.out.println("Target Angle: " + m_targetAngle);
|
|
|
|
|
System.out.println("Error: " + error);
|
|
|
|
|
}
|
2022-02-25 20:18:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called when the command is initially scheduled.
|
|
|
|
|
@Override
|
|
|
|
|
public void initialize() {
|
2022-03-19 19:58:21 -06:00
|
|
|
|
|
|
|
|
m_turret.gotoMidpoint();
|
|
|
|
|
|
|
|
|
|
m_odoX = 0;//-m_swerve.getOdometry().getY();
|
|
|
|
|
m_odoY = -8;//-m_swerve.getOdometry().getX();
|
2022-02-26 15:00:09 -07:00
|
|
|
|
2022-03-01 17:18:25 -07:00
|
|
|
m_gyroAngle = m_swerve.getRegGyro().getDegrees();
|
2022-03-18 20:02:23 -06:00
|
|
|
initialSwerveRotation = m_gyroAngle;
|
2022-03-01 17:18:25 -07:00
|
|
|
|
2022-02-26 15:00:09 -07:00
|
|
|
// get targets (shooter tables)
|
2022-02-25 20:18:21 -07:00
|
|
|
m_targetVel = m_boomBoom.getVelocity(m_distance);
|
|
|
|
|
m_targetHood = m_boomBoom.getHood(m_distance);
|
2022-03-05 11:07:54 -07:00
|
|
|
|
2022-03-19 19:58:21 -06:00
|
|
|
m_targetAngle = AimToCenter.aaravAngleToCenter(m_odoX, m_odoY, m_swerve.getRegGyro().getDegrees());
|
|
|
|
|
// m_targetAngle = ((Math.atan2(m_odoY, m_odoX) * (180./Math.PI) - m_gyroAngle) + 180. + 360.) % 360.;
|
2022-02-26 15:00:09 -07:00
|
|
|
|
2022-03-01 18:56:07 -07:00
|
|
|
// deadzone processing
|
2022-03-06 15:05:08 -07:00
|
|
|
if (AimToCenter.isDeadzone(m_targetAngle)) {}
|
2022-03-05 11:07:54 -07:00
|
|
|
|
2022-02-26 15:00:09 -07:00
|
|
|
// initial error
|
|
|
|
|
updateError();
|
2022-03-19 19:58:21 -06:00
|
|
|
// System.out.println("Error: " + error);
|
2022-02-26 15:00:09 -07:00
|
|
|
prevError = error;
|
2022-02-25 20:18:21 -07:00
|
|
|
}
|
2022-02-26 15:00:09 -07:00
|
|
|
/**
|
|
|
|
|
* Run custom PID.
|
|
|
|
|
*/
|
|
|
|
|
public void runPID() {
|
2022-03-06 13:02:17 -07:00
|
|
|
if (error > 180){
|
|
|
|
|
error = 360 - error;
|
|
|
|
|
inverted = -1;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
inverted = 1;
|
|
|
|
|
}
|
2022-02-26 15:00:09 -07:00
|
|
|
prevError = error;
|
|
|
|
|
updateError();
|
2022-03-01 17:18:25 -07:00
|
|
|
|
|
|
|
|
proportional = error;
|
2022-02-26 15:00:09 -07:00
|
|
|
integral = integral + error * time;
|
|
|
|
|
derivative = (error - prevError) / time;
|
2022-03-01 17:18:25 -07:00
|
|
|
output = kP * proportional + kI * integral + kD * derivative;
|
2022-03-06 13:02:17 -07:00
|
|
|
normOutput = output/360 * inverted;
|
2022-02-26 15:00:09 -07:00
|
|
|
}
|
2022-03-06 18:02:04 -07:00
|
|
|
|
2022-02-25 20:18:21 -07:00
|
|
|
// Called every time the scheduler runs while the command is scheduled.
|
|
|
|
|
@Override
|
|
|
|
|
public void execute() {
|
2022-03-19 19:58:21 -06:00
|
|
|
|
|
|
|
|
|
2022-03-06 18:02:04 -07:00
|
|
|
if (simMode) {
|
|
|
|
|
System.out.println("Normalized Output: " + normOutput);
|
|
|
|
|
}
|
2022-03-19 19:58:21 -06:00
|
|
|
// custom pid
|
|
|
|
|
|
2022-03-06 18:02:04 -07:00
|
|
|
if (simMode) {
|
|
|
|
|
driveDummy.apply(normOutput);
|
|
|
|
|
System.out.println("Drive Dummy: " + driveDummy.get());
|
|
|
|
|
}
|
2022-03-19 19:58:21 -06:00
|
|
|
|
|
|
|
|
runPID();
|
|
|
|
|
SmartDashboard.putNumber("Error", this.error);
|
|
|
|
|
SmartDashboard.putNumber("Shoot.java TargetAngle", this.m_targetAngle);
|
|
|
|
|
SmartDashboard.putNumber("Normalized Output", normOutput);
|
2022-03-06 13:02:17 -07:00
|
|
|
m_swerve.driveWithInput(0, 0, normOutput, true); // i have no idea if this is how you rotate the
|
2022-03-05 11:07:54 -07:00
|
|
|
// entire swerve drive or its the line below
|
2022-03-06 13:02:17 -07:00
|
|
|
// m_swerve.driveWithInput(0, 0, Math.cos(output), Math.sin(output), true);
|
2022-02-26 15:00:09 -07:00
|
|
|
|
2022-03-19 19:58:21 -06:00
|
|
|
// m_hood.runAngleAdjustPID(m_targetHood);
|
|
|
|
|
// m_boomBoom.runDrumShooterVelocityPID(m_targetVel);
|
2022-03-06 18:02:04 -07:00
|
|
|
|
|
|
|
|
if (simMode) {
|
|
|
|
|
turretDummy.apply(normOutput);
|
|
|
|
|
System.out.println("Turret Dummy: " + turretDummy.get());
|
|
|
|
|
}
|
2022-03-06 13:02:17 -07:00
|
|
|
m_turret.m_boomBoomRotateMotor.set(normOutput);
|
2022-02-25 20:18:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called once the command ends or is interrupted.
|
|
|
|
|
@Override
|
2022-03-18 20:02:23 -06:00
|
|
|
public void end(boolean interrupted) {
|
|
|
|
|
// return to initial swerve rotation
|
|
|
|
|
m_swerve.driveWithInput(0, 0, initialSwerveRotation, true);
|
|
|
|
|
}
|
2022-02-25 20:18:21 -07:00
|
|
|
|
|
|
|
|
// Returns true when the command should end.
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isFinished() {
|
2022-03-17 19:41:20 -06:00
|
|
|
// if (simMode) {
|
|
|
|
|
return isAimedInTolerance;
|
|
|
|
|
// }
|
|
|
|
|
// return false;
|
2022-02-25 20:18:21 -07:00
|
|
|
}
|
|
|
|
|
}
|