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-24 18:12:57 -07:00
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-02-26 13:48:50 -07:00
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-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 ) {
m_visionOdometry . updateOdometryWithVision ( ) ;
m_visionOdometry . setLEDs ( true ) ;
}
else {
m_visionOdometry . setLEDs ( false ) ;
}
2022-02-26 13:48:50 -07:00
}
2022-02-22 17:14:48 -07:00
2022-02-26 13:48:50 -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-24 18:12:57 -07:00
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_LIMIT < = angle ) & & ( angle < = ShooterConstants . TURRET_FORWARD_LIMIT ) ) ;
2022-02-19 11:05:44 -07:00
}
2022-02-24 18:12:57 -07:00
2022-02-17 19:54:24 -07:00
// Called once the command ends or is interrupted.
@Override
2022-02-24 18:12:57 -07:00
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 ;
}
}