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

92 lines
2.9 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.
package frc4388.robot.commands;
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() {
x = 0;
y = 0;
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-02-28 19:47:47 -07:00
m_targetAngle = angleToCenter(x, y, m_drive.getRegGyro().getDegrees());
2022-02-19 13:32:39 -07: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){
m_visionOdometry.updateOdometryWithVision();
m_visionOdometry.setLEDs(true);
}
else{
m_visionOdometry.setLEDs(false);
}
}
2022-02-22 17:14:48 -07:00
public static double angleToCenter(double x, double y, double gyro) {
double angle = ((Math.atan2(y, x) * (180./Math.PI) - gyro) + 180. + 360.) % 360.; // Finds the angle between the gyro of the robot and the target (positive x is gyro 0)
2022-02-28 16:50:55 -07:00
return angle;
2022-02-22 17:14:48 -07:00
}
2022-02-25 20:18:21 -07:00
/**
2022-02-28 16:46:40 -07:00
* Checks if in hardware deadzone (due to mechanical limitations).
* @param angle Angle to check.
* @return True if in hardware deadzone.
2022-02-25 20:18:21 -07:00
*/
2022-02-28 16:46:40 -07:00
public static boolean isHardwareDeadzone(double angle) {
return ((ShooterConstants.HARD_DEADZONE_LEFT > angle) || (angle > ShooterConstants.HARD_DEADZONE_RIGHT));
}
/**
* Checks if in digital deadzone (due to climber).
* @param angle Angle to check.
* @return True if in digital deadzone.
*/
public static boolean isDigitalDeadzone(double angle) {
return ((ShooterConstants.DIG_DEADZONE_LEFT < angle) && (angle < ShooterConstants.DIG_DEADZONE_RIGHT));
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;
}
}