Files
2022NoWayHome/src/main/java/frc4388/robot/commands/ShooterCommands/AimToCenter.java
T

118 lines
3.6 KiB
Java
Raw Normal View History

2022-02-17 19:54:24 -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-17 19:54:24 -07:00
import java.util.function.Supplier;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
2022-02-17 19:54:24 -07:00
import edu.wpi.first.wpilibj2.command.CommandBase;
2022-02-24 19:32:33 -07:00
import frc4388.robot.Constants.ShooterConstants;
2022-03-05 11:29:40 -07:00
import frc4388.robot.Constants.VisionConstants;
2022-02-19 11:05:44 -07:00
import frc4388.robot.subsystems.SwerveDrive;
import frc4388.robot.subsystems.Turret;
2022-03-05 11:29:40 -07:00
import frc4388.robot.subsystems.VisionOdometry;
2022-02-17 19:54:24 -07:00
2022-02-19 11:05:44 -07:00
public class AimToCenter extends CommandBase {
2022-02-17 19:54:24 -07:00
/** Creates a new AimWithOdometry. */
2022-02-19 11:05:44 -07:00
Turret m_turret;
2022-03-05 11:29:40 -07:00
VisionOdometry m_visionOdometry;
2022-02-19 11:05:44 -07:00
Supplier<Pose2d> supplier;
Pose2d odo;
2022-02-19 11:05:44 -07:00
// use odometry to find x and y later
2022-02-28 16:50:55 -07:00
double x;
double y;
2022-02-19 11:05:44 -07:00
double m_targetAngle;
2022-02-19 11:05:44 -07:00
// public static Gains m_aimGains;
public AimToCenter(Turret turret, VisionOdometry visionOdometry, Supplier<Pose2d> supplier) {
2022-02-17 19:54:24 -07:00
// Use addRequirements() here to declare subsystem dependencies.
2022-02-19 11:05:44 -07:00
m_turret = turret;
2022-03-05 11:29:40 -07:00
m_visionOdometry = visionOdometry;
this.supplier = supplier;
addRequirements(m_turret, m_visionOdometry);
2022-02-17 19:54:24 -07:00
}
// Called when the command is initially scheduled.
@Override
2022-02-19 11:05:44 -07:00
public void initialize() {
odo = this.supplier.get();
// ! Yes I realize this stupid, yes it works I promise, coordinate system is funky
x = -odo.getY();
y = -odo.getX();
SmartDashboard.putNumber("trans x", x);
SmartDashboard.putNumber("trans y", y);
2022-02-19 11:05:44 -07:00
}
2022-02-17 19:54:24 -07:00
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
odo = this.supplier.get(); // * update odometry using really cool supplier -aarav
m_targetAngle = (aaravAngleToCenter(x, y, odo.getRotation().getDegrees())) % 360;
SmartDashboard.putNumber("Target Angle", m_targetAngle);
2022-03-14 18:58:44 -06:00
m_turret.runShooterRotatePID(m_targetAngle);
2022-03-05 11:29:40 -07:00
2022-03-06 00:24:51 -07:00
// Check if limelight is within range (comment out to disable vision odo)
if (Math.abs(m_turret.getBoomBoomAngleDegrees() - m_targetAngle) < VisionConstants.RANGE){
2022-03-18 23:56:01 -06:00
// m_visionOdometry.updateOdometryWithVision();
// m_visionOdometry.setLEDs(true);
2022-03-06 00:24:51 -07:00
}
else{
2022-03-18 23:56:01 -06:00
// m_visionOdometry.setLEDs(false);
2022-03-06 00:24:51 -07:00
}
}
2022-02-22 17:14:48 -07:00
public static double angleToCenter(double x, double y, double gyro) {
2022-03-18 23:56:01 -06:00
double angle = ((Math.atan2(y, x) * (180./Math.PI) - gyro) + 180. + (360. * 4)) % 360.; // Finds the angle between the gyro of the robot and the target (positive x is gyro 0)
return (angle - 360);
2022-02-22 17:14:48 -07:00
}
2022-03-18 23:54:16 -06:00
public static double aaravAngleToCenter(double x, double y, double gyro) {
double actualGyro = -gyro + 90;
double exp = Math.toDegrees(Math.atan(y/-x)) - actualGyro;
if (-x > 0) { return (180 + exp); }
if (-x < 0) { return (360 + exp); }
2022-03-18 23:54:16 -06:00
if (x == 0 && y > 0) { return (270 - actualGyro); }
if (x == 0 && y < 0) { return (90 - actualGyro); }
2022-03-18 23:54:16 -06:00
System.err.println("Invalid case.");
2022-03-18 23:54:16 -06:00
return 0;
}
2022-02-25 20:18:21 -07:00
/**
2022-03-06 15:05:08 -07:00
* Checks if in deadzone.
2022-02-28 16:46:40 -07:00
* @param angle Angle to check.
2022-03-06 15:05:08 -07:00
* @return True if in deadzone.
2022-02-25 20:18:21 -07:00
*/
2022-03-06 15:05:08 -07:00
public static boolean isDeadzone(double angle) {
2022-03-06 15:20:20 -07:00
if (angle == Double.NaN) {
return false;
}
return !((ShooterConstants.TURRET_REVERSE_SOFT_LIMIT <= angle) && (angle <= ShooterConstants.TURRET_FORWARD_SOFT_LIMIT));
2022-02-19 11:05:44 -07:00
}
2022-02-17 19:54:24 -07:00
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
}
2022-02-17 19:54:24 -07:00
// Returns true when the command should end.
@Override
public boolean isFinished() {
return false;
}
}