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

100 lines
3.1 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 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;
SwerveDrive m_drive;
2022-03-05 11:29:40 -07:00
VisionOdometry m_visionOdometry;
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;
2022-03-05 11:29:40 -07:00
public AimToCenter(Turret turret, SwerveDrive drive, VisionOdometry visionOdometry) {
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;
m_drive = drive;
2022-03-05 11:29:40 -07:00
m_visionOdometry = visionOdometry;
addRequirements(m_turret, m_drive, 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() {
2022-03-18 23:56:01 -06:00
x = m_drive.getOdometry().getX();
y = m_drive.getOdometry().getY();
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() {
2022-03-19 11:25:29 -06:00
m_targetAngle = (aaravAngleToCenter(x, y, m_drive.getRegGyro().getDegrees())) % 360;
2022-03-18 23:56:01 -06:00
System.out.println("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) {
2022-03-19 11:25:29 -06:00
double yes = 360 - gyro;
double exp = Math.toDegrees(Math.atan(y/x)) - yes;
2022-03-18 23:54:16 -06:00
if (x > 0) { return exp; }
if (x < 0) { return (180 + exp); }
2022-03-19 11:25:29 -06:00
if (x == 0 && y > 0) { return (90 - yes); }
if (x == 0 && y < 0) { return (-90 - yes); }
2022-03-18 23:54:16 -06:00
System.out.println("Invalid case.");
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;
}
}