-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoteDetection.java
84 lines (70 loc) · 3.48 KB
/
NoteDetection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package frc.robot.subsystems.localization;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import java.util.List;
import java.util.function.Supplier;
import org.littletonrobotics.junction.Logger;
import org.photonvision.PhotonCamera;
import org.photonvision.targeting.PhotonTrackedTarget;
import org.photonvision.targeting.TargetCorner;
public class NoteDetection extends SubsystemBase {
private PhotonCamera colorCamera = new PhotonCamera("note-detector");
private Supplier<Pose2d> robotPose;
public NoteDetection(Supplier<Pose2d> robotPose) {
this.robotPose = robotPose;
SmartDashboard.putBoolean("running", true);
}
private Pose2d getNotePoseFromTarget(PhotonTrackedTarget target) {
double transformWidth = 1 * 78; // = distance at setpoint / width at setpoint
double noteWidth = 0.1;
// w of real note = 35 cm
// .405 * 74 * 35 = d * wd
List<TargetCorner> corners = target.getMinAreaRectCorners();
double[][] cornersCoordinates = new double[4][2];
SmartDashboard.putNumber("corners", corners.size());
SmartDashboard.putNumber("area", target.getArea());
for (int i = 0; i < corners.size(); i++) {
cornersCoordinates[i][0] = corners.get(i).x;
cornersCoordinates[i][1] = corners.get(i).y;
SmartDashboard.putNumber("coordinate" + (i) + "x", cornersCoordinates[i][0]);
SmartDashboard.putNumber("coordinate" + (i) + "y", cornersCoordinates[i][1]);
}
double width = cornersCoordinates[1][0] - cornersCoordinates[0][0];
double distanceForward = transformWidth / width;
// alternatively: instead of using magnification maybe try using just y instead?
SmartDashboard.putNumber("distanceForward", distanceForward);
// THEORETICALLY WORKING
double widthShift = 0.180 * 199 / 2;
double distanceSide =
widthShift
* noteWidth
/ ((cornersCoordinates[1][0] + cornersCoordinates[0][0]) / 2 - 160)
/ distanceForward;
SmartDashboard.putNumber("distanceSize", distanceSide);
double distance =
Math.sqrt(distanceForward * distanceForward + distanceSide * distanceSide);
double angle = Math.tanh(distanceSide / distanceForward);
Rotation2d fromForward = robotPose.get().getRotation().plus(new Rotation2d(angle));
SmartDashboard.putNumber("width", width);
SmartDashboard.putNumber(
"centerx", (cornersCoordinates[1][0] + cornersCoordinates[0][0]) / 2);
SmartDashboard.putNumber(
"centery", (cornersCoordinates[1][1] + cornersCoordinates[0][1]) / 2);
return new Pose2d(
robotPose.get().getX() + Math.cos(fromForward.getRadians()) * distance,
robotPose.get().getY() + Math.sin(fromForward.getRadians()) * distance,
fromForward);
}
@Override
public void periodic() {
SmartDashboard.putBoolean("hello", true);
var cameraResult = colorCamera.getLatestResult();
if (cameraResult.hasTargets()) {
Pose2d notePose = getNotePoseFromTarget(cameraResult.getBestTarget());
Logger.recordOutput("Note Position", notePose);
SmartDashboard.putBoolean("target found", true);
}
}
}