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;
|
2022-03-20 19:49:28 -06:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.stream.Collector;
|
|
|
|
|
import java.util.stream.Collectors;
|
2022-03-05 15:22:14 -07:00
|
|
|
|
|
|
|
|
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-20 20:36:23 -06:00
|
|
|
import frc4388.robot.RobotContainer;
|
2022-03-20 16:25:27 -06:00
|
|
|
import frc4388.robot.Constants.ShooterConstants;
|
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. */
|
2022-03-20 16:25:27 -06:00
|
|
|
SwerveDrive m_swerve;
|
2022-03-05 15:22:14 -07:00
|
|
|
Turret m_turret;
|
|
|
|
|
VisionOdometry m_visionOdometry;
|
|
|
|
|
BoomBoom m_boomBoom;
|
|
|
|
|
Hood m_hood;
|
|
|
|
|
|
2022-03-22 00:18:37 -06:00
|
|
|
boolean isAuto;
|
|
|
|
|
|
2022-03-20 20:36:23 -06:00
|
|
|
static double velocity;
|
|
|
|
|
static double hoodPosition;
|
|
|
|
|
|
2022-03-05 15:22:14 -07:00
|
|
|
ArrayList<Point> points = new ArrayList<>();
|
|
|
|
|
|
2022-03-17 19:41:20 -06:00
|
|
|
private boolean targetLocked = false;
|
2022-03-24 08:49:37 -06:00
|
|
|
private double velocityTolerance = 300.0;
|
2022-03-17 19:41:20 -06:00
|
|
|
private double hoodTolerance = 5.0;
|
|
|
|
|
|
2022-03-13 19:59:55 -06:00
|
|
|
boolean isExecuted = false;
|
|
|
|
|
|
2022-03-22 00:18:37 -06:00
|
|
|
// timing
|
|
|
|
|
boolean isAimed;
|
|
|
|
|
|
|
|
|
|
boolean timerStarted;
|
|
|
|
|
long startTime;
|
|
|
|
|
private double timeTolerance;
|
|
|
|
|
|
|
|
|
|
public TrackTarget (Turret turret, BoomBoom boomBoom, Hood hood, VisionOdometry visionOdometry, boolean isAuto) {
|
2022-03-05 15:22:14 -07:00
|
|
|
m_turret = turret;
|
|
|
|
|
m_boomBoom = boomBoom;
|
|
|
|
|
m_hood = hood;
|
|
|
|
|
m_visionOdometry = visionOdometry;
|
2022-03-10 17:46:51 -07:00
|
|
|
|
2022-03-22 00:18:37 -06:00
|
|
|
this.isAuto = isAuto;
|
|
|
|
|
this.timeTolerance = 1000;
|
|
|
|
|
|
2022-03-14 12:16:32 -06:00
|
|
|
addRequirements(m_turret, m_boomBoom, m_hood, m_visionOdometry);
|
2022-03-05 15:22:14 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-22 00:18:37 -06:00
|
|
|
public TrackTarget(Turret turret, BoomBoom boomBoom, Hood hood, VisionOdometry visionOdometry) {
|
|
|
|
|
this(turret, boomBoom, hood, visionOdometry, false);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 15:22:14 -07:00
|
|
|
// Called when the command is initially scheduled.
|
|
|
|
|
@Override
|
|
|
|
|
public void initialize() {
|
2022-03-22 00:18:37 -06:00
|
|
|
timerStarted = false;
|
|
|
|
|
startTime = 0;
|
|
|
|
|
|
2022-03-20 20:36:23 -06:00
|
|
|
velocity = 0;
|
|
|
|
|
hoodPosition = 0;
|
2022-03-22 19:48:40 -06:00
|
|
|
m_visionOdometry.setDriverMode(false);
|
|
|
|
|
m_visionOdometry.setLEDs(true);
|
2022-03-05 15:22:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called every time the scheduler runs while the command is scheduled.
|
|
|
|
|
@Override
|
2022-03-20 20:36:23 -06:00
|
|
|
public void execute() {
|
2022-03-12 12:02:38 -07:00
|
|
|
try {
|
2022-03-22 17:23:11 -06:00
|
|
|
points = m_visionOdometry.getTargetPoints();
|
|
|
|
|
// points = getFakePoints();
|
2022-03-22 17:07:59 -06:00
|
|
|
//// points = filterPoints(points);
|
2022-03-20 19:49:28 -06:00
|
|
|
Point average = VisionOdometry.averagePoint(points);
|
2022-03-17 19:41:20 -06:00
|
|
|
|
2022-03-20 20:36:23 -06:00
|
|
|
double output = (average.x - VisionConstants.LIME_HIXELS/2.d) / VisionConstants.LIME_HIXELS;
|
2022-03-22 17:07:06 -06:00
|
|
|
output *= 2.0;
|
2022-03-20 20:36:23 -06:00
|
|
|
|
2022-03-13 21:51:55 -06:00
|
|
|
m_turret.runTurretWithInput(output);
|
2022-03-22 17:36:29 -06:00
|
|
|
// double position = m_turret.m_boomBoomRotateEncoder.getPosition();
|
2022-03-20 16:25:27 -06:00
|
|
|
|
2022-03-22 17:36:29 -06:00
|
|
|
// if(Math.abs(position - ShooterConstants.TURRET_FORWARD_SOFT_LIMIT) < 5 ||
|
|
|
|
|
// Math.abs(position - ShooterConstants.TURRET_REVERSE_SOFT_LIMIT) < 5)
|
|
|
|
|
// m_swerve.driveWithInput(RobotContainer.getDriverController().getLeftX(), RobotContainer.getDriverController().getLeftY(), output, true);
|
|
|
|
|
// else
|
|
|
|
|
// m_swerve.driveWithInput(RobotContainer.getDriverController().getLeftX(), RobotContainer.getDriverController().getLeftY(),
|
|
|
|
|
// RobotContainer.getDriverController().getRightX(), RobotContainer.getDriverController().getRightY(),
|
|
|
|
|
// true);
|
2022-03-20 16:25:27 -06:00
|
|
|
|
2022-03-17 19:41:20 -06:00
|
|
|
|
2022-03-20 20:36:23 -06:00
|
|
|
double regressedDistance = getDistance(average.y);
|
2022-03-17 19:41:20 -06:00
|
|
|
|
2022-03-21 23:13:12 -06:00
|
|
|
// ! no longer a +30 lol -aarav
|
2022-03-24 08:49:37 -06:00
|
|
|
velocity = m_boomBoom.getVelocity(regressedDistance + 20);
|
|
|
|
|
hoodPosition = m_boomBoom.getHood(regressedDistance + 20);
|
2022-03-17 19:41:20 -06:00
|
|
|
|
2022-03-24 08:49:37 -06:00
|
|
|
m_boomBoom.runDrumShooterVelocityPID(velocity);
|
|
|
|
|
m_hood.runAngleAdjustPID(hoodPosition);
|
2022-03-17 19:41:20 -06:00
|
|
|
|
|
|
|
|
double currentDrumVel = this.m_boomBoom.m_shooterFalconLeft.getSelectedSensorVelocity();
|
|
|
|
|
double currentHood = this.m_hood.getEncoderPosition();
|
|
|
|
|
|
2022-03-24 08:49:37 -06:00
|
|
|
targetLocked = (Math.abs(currentDrumVel - velocity) < velocityTolerance) && (Math.abs(currentHood - hoodPosition) < hoodTolerance) && (output < 0.2);
|
2022-03-15 14:23:37 -06:00
|
|
|
|
2022-03-24 08:49:37 -06:00
|
|
|
SmartDashboard.putNumber("Distance to Target", regressedDistance);
|
2022-03-20 20:36:23 -06:00
|
|
|
// SmartDashboard.putNumber("Distance", distance);
|
2022-03-24 08:49:37 -06:00
|
|
|
// SmartDashboard.putNumber("Hood Target Angle Track", hoodPosition);
|
|
|
|
|
// SmartDashboard.putNumber("Vel Target Track", velocity);
|
2022-03-20 20:36:23 -06:00
|
|
|
SmartDashboard.putBoolean("Target Locked", targetLocked);
|
|
|
|
|
} catch (Exception e){
|
2022-03-13 21:51:55 -06:00
|
|
|
e.printStackTrace();
|
2022-03-05 15:22:14 -07:00
|
|
|
}
|
2022-03-22 00:18:37 -06:00
|
|
|
|
2022-03-22 00:28:17 -06:00
|
|
|
// run storage
|
|
|
|
|
|
2022-03-05 15:22:14 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-22 17:07:06 -06:00
|
|
|
public ArrayList<Point> filterPoints(ArrayList<Point> points) {
|
|
|
|
|
Point average = VisionOdometry.averagePoint(points);
|
2022-03-20 19:49:28 -06:00
|
|
|
|
2022-03-22 17:07:06 -06:00
|
|
|
HashMap<Point, Double> pointDistances = new HashMap<>();
|
|
|
|
|
double distanceSum = 0;
|
2022-03-20 19:49:28 -06:00
|
|
|
|
2022-03-22 17:07:06 -06:00
|
|
|
for(Point point : points) {
|
|
|
|
|
Vector2D difference = new Vector2D(point);
|
|
|
|
|
difference.subtract(new Vector2D(average));
|
2022-03-20 19:49:28 -06:00
|
|
|
|
2022-03-22 17:07:06 -06:00
|
|
|
double mag = difference.magnitude();
|
|
|
|
|
distanceSum += mag;
|
2022-03-20 19:49:28 -06:00
|
|
|
|
2022-03-22 17:07:06 -06:00
|
|
|
pointDistances.put(point, mag);
|
|
|
|
|
}
|
2022-03-20 19:49:28 -06:00
|
|
|
|
2022-03-22 17:07:06 -06:00
|
|
|
final double averageDist = distanceSum / points.size();
|
|
|
|
|
return (ArrayList<Point>) pointDistances.keySet().stream().filter(p -> pointDistances.get(p) < 1.3 * averageDist).collect(Collectors.toList());
|
|
|
|
|
}
|
2022-03-20 19:49:28 -06:00
|
|
|
|
2022-03-22 17:07:59 -06:00
|
|
|
public final ArrayList<Point> getFakePoints() {
|
|
|
|
|
ArrayList<Point> fakePoints = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < 10; i++) {
|
|
|
|
|
Point p = new Point((Math.random() * 20) - 10 + (VisionConstants.LIME_HIXELS/2), (Math.random() * 20) - 10 + (VisionConstants.LIME_VIXELS/2));
|
|
|
|
|
fakePoints.add(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fakePoints;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 20:36:23 -06:00
|
|
|
public final double getDistance(double averageY) {
|
|
|
|
|
double y_rot = averageY / 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);
|
|
|
|
|
|
|
|
|
|
double distance = (VisionConstants.TARGET_HEIGHT - VisionConstants.LIME_HEIGHT) / Math.tan(y_rot);
|
|
|
|
|
|
|
|
|
|
double regressedDistance = distanceRegression(distance);
|
|
|
|
|
regressedDistance += VisionConstants.EDGE_TO_CENTER + VisionConstants.LIMELIGHT_RADIUS;
|
2022-03-21 20:00:34 -06:00
|
|
|
SmartDashboard.putNumber("Distance from Lime 123", distance);
|
|
|
|
|
SmartDashboard.putNumber("Regressed Distance from Lime 123", regressedDistance);
|
2022-03-20 20:36:23 -06:00
|
|
|
return regressedDistance;
|
|
|
|
|
}
|
2022-03-14 16:56:32 -06:00
|
|
|
|
2022-03-20 20:36:23 -06:00
|
|
|
public final double distanceRegression(double distance) {
|
|
|
|
|
return (79.6078 * Math.pow(1.01343, distance) - 56.6671);
|
2022-03-13 21:43:14 -06:00
|
|
|
}
|
|
|
|
|
|
2022-03-05 15:22:14 -07:00
|
|
|
// Called once the command ends or is interrupted.
|
|
|
|
|
@Override
|
|
|
|
|
public void end(boolean interrupted) {
|
2022-03-21 19:30:51 -06:00
|
|
|
m_visionOdometry.setLEDs(false);
|
|
|
|
|
m_visionOdometry.setDriverMode(true);
|
2022-03-05 15:22:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true when the command should end.
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isFinished() {
|
2022-03-22 22:10:46 -06:00
|
|
|
if (this.isAuto) {
|
2022-03-24 08:49:37 -06:00
|
|
|
if (targetLocked && !timerStarted) {
|
2022-03-22 22:10:46 -06:00
|
|
|
timerStarted = true;
|
|
|
|
|
startTime = System.currentTimeMillis();
|
|
|
|
|
}
|
|
|
|
|
return (targetLocked && timerStarted && ((System.currentTimeMillis() - startTime) > timeTolerance));
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-03-05 15:22:14 -07:00
|
|
|
}
|
|
|
|
|
}
|