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

169 lines
5.5 KiB
Java
Raw Normal View History

2022-03-05 15:22:14 -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-03-05 15:22:14 -07:00
import java.util.ArrayList;
import org.opencv.core.Point;
import edu.wpi.first.math.geometry.Pose2d;
2022-03-17 19:41:20 -06:00
import edu.wpi.first.wpilibj.motorcontrol.MotorController;
2022-03-16 10:32:15 -06:00
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
2022-03-05 15:22:14 -07:00
import edu.wpi.first.wpilibj2.command.CommandBase;
2022-03-06 15:45:01 -07:00
2022-03-05 15:22:14 -07:00
import frc4388.robot.Constants.VisionConstants;
import frc4388.robot.subsystems.BoomBoom;
import frc4388.robot.subsystems.Hood;
import frc4388.robot.subsystems.SwerveDrive;
import frc4388.robot.subsystems.Turret;
2022-03-13 19:59:55 -06:00
import frc4388.robot.subsystems.Vision;
2022-03-05 15:22:14 -07:00
import frc4388.robot.subsystems.VisionOdometry;
2022-03-16 18:58:58 -06:00
import frc4388.utility.Vector2D;
2022-03-14 18:29:11 -06:00
import frc4388.utility.desmos.DesmosServer;
2022-03-05 15:22:14 -07:00
public class TrackTarget extends CommandBase {
/** Creates a new TrackTarget. */
Turret m_turret;
VisionOdometry m_visionOdometry;
BoomBoom m_boomBoom;
Hood m_hood;
// use odometry to find x and y later
double x;
double y;
double distance;
double vel;
double hood;
double average;
double output;
Pose2d pos = new Pose2d();
ArrayList<Point> points = new ArrayList<>();
2022-03-17 19:41:20 -06:00
private boolean targetLocked = false;
private double velocityTolerance = 100.0;
private double hoodTolerance = 5.0;
double m=0;
double b=0;
2022-03-13 19:59:55 -06:00
boolean isExecuted = false;
2022-03-05 15:22:14 -07:00
// public static Gains m_aimGains;
public TrackTarget (Turret turret, BoomBoom boomBoom, Hood hood, VisionOdometry visionOdometry) {
2022-03-05 15:22:14 -07:00
// Use addRequirements() here to declare subsystem dependencies.
m_turret = turret;
m_boomBoom = boomBoom;
m_hood = hood;
m_visionOdometry = visionOdometry;
2022-03-10 17:46:51 -07:00
addRequirements(m_turret, m_boomBoom, m_hood, m_visionOdometry);
2022-03-05 15:22:14 -07:00
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
x = 0;
y = 0;
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
//m_targetAngle = angleToCenter(x, y, m_drive.getRegGyro().getDegrees());
2022-03-17 19:41:20 -06:00
SmartDashboard.putBoolean("Target Locked", this.targetLocked);
2022-03-12 12:02:38 -07:00
try {
m_visionOdometry.setLEDs(true);
points = m_visionOdometry.getTargetPoints();
2022-03-17 19:41:20 -06:00
2022-03-13 19:59:55 -06:00
Point average = VisionOdometry.averagePoint(points);
2022-03-17 19:41:20 -06:00
2022-03-16 18:58:58 -06:00
for(Point point : points) {
Vector2D difference = new Vector2D(point);
difference.subtract(new Vector2D(average));
if(difference.magnitude() < VisionConstants.POINTS_THRESHOLD)
2022-03-17 19:41:20 -06:00
points.remove(point);
2022-03-16 18:58:58 -06:00
}
2022-03-17 19:41:20 -06:00
2022-03-16 18:58:58 -06:00
average = VisionOdometry.averagePoint(points);
2022-03-13 19:59:55 -06:00
DesmosServer.putPoint("average", average);
2022-03-17 19:41:20 -06:00
2022-03-16 18:58:58 -06:00
for(int i = 0; i < points.size(); i++) {
DesmosServer.putPoint("Point" + i, points.get(i));
}
2022-03-17 19:41:20 -06:00
2022-03-13 19:59:55 -06:00
output = (average.x - VisionConstants.LIME_HIXELS/2.d) / VisionConstants.LIME_HIXELS;
2022-03-16 10:32:15 -06:00
output *= 4;
// output *= 0.5;
2022-03-13 19:59:55 -06:00
DesmosServer.putDouble("output", output);
2022-03-13 21:51:55 -06:00
m_turret.runTurretWithInput(output);
2022-03-17 19:41:20 -06:00
2022-03-13 19:59:55 -06:00
double y_rot = average.y / VisionConstants.LIME_VIXELS;
y_rot *= Math.toRadians(VisionConstants.V_FOV);
y_rot -= Math.toRadians(VisionConstants.V_FOV) / 2;
y_rot += Math.toRadians(VisionConstants.LIME_ANGLE);
2022-03-17 19:41:20 -06:00
2022-03-13 19:59:55 -06:00
double distance = (VisionConstants.TARGET_HEIGHT - VisionConstants.LIME_HEIGHT) / Math.tan(y_rot);
DesmosServer.putDouble("distance", distance);
2022-03-17 19:41:20 -06:00
updateRegressionDesmos();
double regressedDistance = distanceRegression(distance);
2022-03-15 14:23:37 -06:00
regressedDistance += VisionConstants.EDGE_TO_CENTER + VisionConstants.LIMELIGHT_RADIUS;
DesmosServer.putDouble("distanceReg", regressedDistance);
2022-03-17 19:41:20 -06:00
//Vision odometry circle fit based pose estimate
2022-03-16 10:32:15 -06:00
// Point targetOffset = m_visionOdometry.getTargetOffset();
// DesmosServer.putPoint("targetOff", targetOffset);
2022-03-17 19:41:20 -06:00
2022-03-16 10:32:15 -06:00
vel = m_boomBoom.getVelocity(regressedDistance + 30);
hood = m_boomBoom.getHood(regressedDistance + 30);
2022-03-15 14:23:37 -06:00
// m_boomBoom.runDrumShooter(vel);
m_boomBoom.runDrumShooterVelocityPID(vel);
m_hood.runAngleAdjustPID(hood);
2022-03-17 19:41:20 -06:00
double currentDrumVel = this.m_boomBoom.m_shooterFalconLeft.getSelectedSensorVelocity();
double currentHood = this.m_hood.getEncoderPosition();
this.targetLocked = (Math.abs(currentDrumVel - vel) < velocityTolerance) && (Math.abs(currentHood - hood) < hoodTolerance);
2022-03-15 14:23:37 -06:00
2022-03-16 10:32:15 -06:00
SmartDashboard.putNumber("Regressed Distance", regressedDistance);
SmartDashboard.putNumber("Distance", distance);
SmartDashboard.putNumber("Hood Target Angle Track", hood);
SmartDashboard.putNumber("Vel Target Track", vel);
2022-03-13 19:59:55 -06:00
// isExecuted = true;
2022-03-05 15:22:14 -07:00
}
catch (Exception e){
2022-03-13 21:51:55 -06:00
e.printStackTrace();
// System.err.println("Exception: " + e.toString() + ", Line 78 at TrackTarget.java");
2022-03-05 15:22:14 -07:00
}
2022-03-13 19:59:55 -06:00
// m_turret.runshooterRotatePID(m_targetAngle);
2022-03-05 15:22:14 -07:00
}
public final double distanceRegression(double distance) {
2022-03-16 10:32:15 -06:00
return (79.6078 * Math.pow(1.01343, distance) - 56.6671);
}
public void updateRegressionDesmos() {
m = DesmosServer.readDouble("m");
b = DesmosServer.readDouble("b");
2022-03-14 16:56:32 -06:00
DesmosServer.putArray("MB", m, b);
}
2022-03-05 15:22:14 -07:00
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
return false;
2022-03-13 19:59:55 -06:00
// return isExecuted && Math.abs(output) < .1;
2022-03-05 15:22:14 -07:00
}
}