Files

136 lines
5.1 KiB
Java
Raw Permalink Normal View History

2020-02-13 16:42:54 -07:00
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
2020-03-02 21:45:38 -07:00
package frc4388.robot.commands.shooter;
2020-02-13 16:42:54 -07:00
2020-03-02 21:45:38 -07:00
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.CommandBase;
2020-02-13 16:42:54 -07:00
import frc4388.robot.Constants.VisionConstants;
2020-03-07 10:09:44 -07:00
import frc4388.robot.subsystems.LimeLight;
2020-02-15 10:25:26 -08:00
import frc4388.robot.subsystems.Shooter;
2020-02-21 21:58:56 -07:00
import frc4388.robot.subsystems.ShooterAim;
2020-03-02 23:56:05 -07:00
import frc4388.robot.subsystems.ShooterHood;
2020-02-13 16:42:54 -07:00
import frc4388.utility.controller.IHandController;
public class TrackTarget extends CommandBase {
2020-02-25 20:01:24 -07:00
// Setup
2020-02-21 21:58:56 -07:00
ShooterAim m_shooterAim;
2020-02-15 10:25:26 -08:00
Shooter m_shooter;
2020-03-03 00:12:38 -07:00
ShooterHood m_shooterHood;
NetworkTableEntry xEntry;
2020-02-13 16:42:54 -07:00
IHandController m_driverController;
2020-02-25 20:01:24 -07:00
// Aiming
2020-02-13 16:42:54 -07:00
double turnAmount = 0;
double xAngle = 0;
double yAngle = 0;
double target = 0;
2020-02-15 10:25:26 -08:00
public double distance;
2020-03-07 11:22:32 -07:00
public double realDistance;
2020-02-19 20:48:34 -07:00
public static double fireVel;
public static double fireAngle;
2020-02-13 16:42:54 -07:00
public double m_hoodTrim;
public double m_turretTrim;
2020-02-13 16:42:54 -07:00
/**
* Uses the Limelight to track the target
2020-03-01 00:41:23 -07:00
* @param shooterSubsystem The Shooter subsystem
* @param aimSubsystem The ShooterAim subsystem
2020-02-13 16:42:54 -07:00
*/
2020-03-03 00:12:38 -07:00
public TrackTarget(ShooterAim aimSubsystem) {
2020-02-21 21:58:56 -07:00
m_shooterAim = aimSubsystem;
2020-03-03 00:12:38 -07:00
m_shooter = m_shooterAim.m_shooterSubsystem;
2020-03-02 23:56:05 -07:00
m_shooterHood = m_shooter.m_shooterHoodSubsystem;
2020-02-22 15:03:10 -07:00
addRequirements(m_shooterAim);
2020-02-13 16:42:54 -07:00
NetworkTableInstance.getDefault().getTable("limelight").getEntry("camMode").setNumber(1);
2020-03-07 10:09:44 -07:00
NetworkTableInstance.getDefault().getTable("limelight").getEntry("ledMode").setNumber(0);
2020-02-13 16:42:54 -07:00
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
2020-02-25 20:01:24 -07:00
// Vision Processing Mode
2020-02-28 20:05:56 -07:00
NetworkTableInstance.getDefault().getTable("limelight").getEntry("camMode").setNumber(0);
NetworkTableInstance.getDefault().getTable("limelight").getEntry("ledMode").setNumber(3);
2020-02-13 16:42:54 -07:00
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
target = NetworkTableInstance.getDefault().getTable("limelight").getEntry("tv").getDouble(0);
xAngle = NetworkTableInstance.getDefault().getTable("limelight").getEntry("tx").getDouble(0);
yAngle = NetworkTableInstance.getDefault().getTable("limelight").getEntry("ty").getDouble(0);
2020-03-07 17:38:14 -07:00
// Finding Distance
distance = VisionConstants.TARGET_HEIGHT / Math.tan((VisionConstants.LIME_ANGLE + yAngle) * (Math.PI / 180));
realDistance = (1.09 * distance) - 12.8;
2020-02-25 20:01:24 -07:00
if (target == 1.0) { // If target in view
// Aiming Left/Right
2020-03-07 17:38:14 -07:00
xAngle = xAngle + m_shooter.m_shooterTable.getCenterDisplacement(realDistance);
2020-02-25 20:01:24 -07:00
turnAmount = (xAngle / VisionConstants.FOV) * VisionConstants.TURN_P_VALUE;
if (Math.abs(xAngle) < VisionConstants.X_ANGLE_ERROR) {
turnAmount = 0;
} // Angle Error Zone
// Deadzones
else if (turnAmount > 0 && turnAmount < VisionConstants.MOTOR_DEAD_ZONE) {
turnAmount = VisionConstants.MOTOR_DEAD_ZONE;
} else if (turnAmount < 0 && turnAmount > -VisionConstants.MOTOR_DEAD_ZONE) {
turnAmount = -VisionConstants.MOTOR_DEAD_ZONE;
}
m_shooterAim.runShooterWithInput(-turnAmount);// - m_shooter.shooterTrims.m_turretTrim);
2020-02-25 20:01:24 -07:00
2020-03-07 11:22:32 -07:00
SmartDashboard.putNumber("Distance to Target", realDistance);
2020-03-07 17:38:14 -07:00
SmartDashboard.putNumber("Center Displacement", m_shooter.m_shooterTable.getCenterDisplacement(realDistance));
2020-03-01 00:51:13 -07:00
//START Equation Code
2020-02-25 20:01:24 -07:00
double yVel = Math.sqrt(2 * VisionConstants.GRAV * VisionConstants.TARGET_HEIGHT);
double xVel = (distance * VisionConstants.GRAV) / (yVel);
2020-02-19 20:48:34 -07:00
//fireVel = Math.sqrt((Math.pow(xVel, 2))+(Math.pow(yVel,2)));
2020-03-03 20:59:00 -07:00
//fireAngle = Math.atan(yVel/xVel) * (180/Math.PI);
2020-03-01 00:51:13 -07:00
//END Equation Code
2020-03-02 21:03:39 -07:00
//START CSV Code
2020-03-07 11:22:32 -07:00
fireVel = m_shooter.m_shooterTable.getVelocity(realDistance);
fireAngle = m_shooter.m_shooterTable.getHood(realDistance); //Note: Ensure to follow because units are different
2020-03-02 21:03:39 -07:00
//fireAngle = 33;
//END CSV Code
2020-03-03 20:59:00 -07:00
//fireVel = SmartDashboard.getNumber("Velocity Target", 0);
//fireAngle = SmartDashboard.getNumber("Angle Target", 3);
2020-02-20 20:21:28 -07:00
m_shooter.m_fireVel = fireVel;
2020-03-02 23:56:05 -07:00
m_shooterHood.m_fireAngle = fireAngle;// + m_shooter.shooterTrims.m_hoodTrim;
2020-03-10 18:41:56 -06:00
m_shooterAim.m_targetDistance = distance;
2020-02-13 16:42:54 -07:00
}
}
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
2020-02-13 16:42:54 -07:00
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
2020-03-07 10:09:44 -07:00
if (xAngle < 0.5 && xAngle > -0.5 && target == 1)
2020-02-22 13:01:50 -07:00
{
2020-03-03 01:12:07 -07:00
m_shooterAim.m_isAimReady = true;
} else {
m_shooterAim.m_isAimReady = false;
2020-02-22 13:01:50 -07:00
}
2020-03-03 01:12:07 -07:00
2020-02-13 16:42:54 -07:00
return false;
}
2020-02-15 15:42:04 -07:00
}