Skip to content

Commit 7cfde09

Browse files
committed
Add VisionSubsystem
1 parent 6cee637 commit 7cfde09

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ To upgrade the version of the lib2813 libraries you are using, simply update the
5656
- `com.team2813.lib2813:limelight`:
5757
- `Phoenix6.json`
5858
- `com.team2813.lib2813:vision`:
59+
- `WPILibNewCommands.json`
5960
- `photonlib.json`
6061
- `com.team2813.lib2813:testing`:
6162
- `WPILibNewCommands.json`
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.team2813.lib2813.vision;
2+
;
3+
import edu.wpi.first.math.geometry.Pose2d;
4+
import edu.wpi.first.math.geometry.Rotation3d;
5+
import edu.wpi.first.wpilibj.Timer;
6+
import edu.wpi.first.wpilibj2.command.SubsystemBase;
7+
import org.photonvision.EstimatedRobotPose;
8+
9+
/**
10+
* Defines a vision subsystem.
11+
*
12+
* @param <C> the type for the camera
13+
*/
14+
public abstract class VisionSubsystem<C extends Camera> extends SubsystemBase {
15+
protected final MultiPhotonPoseEstimator<C> poseEstimator;
16+
17+
/**
18+
* Constructor.
19+
*
20+
* @param poseEstimator The pose estimator to use.
21+
*/
22+
protected VisionSubsystem(MultiPhotonPoseEstimator<C> poseEstimator) {
23+
this.poseEstimator = poseEstimator;
24+
}
25+
26+
/**
27+
* Constructor.
28+
*
29+
* @param poseEstimator The pose estimator to use.
30+
* @param name Name of the subsystem for telemetry and logging.
31+
*/
32+
protected VisionSubsystem(MultiPhotonPoseEstimator<C> poseEstimator, String name) {
33+
super(name);
34+
this.poseEstimator = poseEstimator;
35+
}
36+
37+
@Override
38+
public void periodic() {
39+
if (poseEstimator.poseStrategyRequiresHeadingData()) {
40+
poseEstimator.addHeadingData(Timer.getTimestamp(), getRotation3d());
41+
}
42+
poseEstimator.processAllUnreadResults(this::handleEstimatedPose, this::handleRejectedPose);
43+
poseEstimator.publishCameraPosesRelativeTo(getDrivetrainPose());
44+
}
45+
46+
/**
47+
* Gets the current orientation of the robot as a {@link Rotation3d} from
48+
* the Pigeon 2 quaternion values.
49+
*
50+
* @return The robot orientation.
51+
*/
52+
protected abstract Rotation3d getRotation3d();
53+
54+
/**
55+
* Gets the current pose of the robot as a {@link Pose2d} from the drivetrain.
56+
*
57+
* @return The robot position.
58+
*/
59+
protected abstract Pose2d getDrivetrainPose();
60+
61+
/**
62+
* Called when new estimated robot positions are available.
63+
*
64+
* @param estimatedPose The estimated robot position.
65+
* @param camera The camera.
66+
*/
67+
protected abstract void handleEstimatedPose(EstimatedRobotPose estimatedPose, C camera);
68+
69+
/**
70+
* Called when invalid robot positions were computed by the estimator.
71+
*
72+
* @param rejectedPose The rejected estimated robot position.
73+
*/
74+
protected void handleRejectedPose(EstimatedRobotPose rejectedPose) {}
75+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"fileName": "WPILibNewCommands.json",
3+
"name": "WPILib-New-Commands",
4+
"version": "1.0.0",
5+
"uuid": "111e20f7-815e-48f8-9dd6-e675ce75b266",
6+
"frcYear": "2026",
7+
"mavenUrls": [],
8+
"jsonUrl": "",
9+
"javaDependencies": [
10+
{
11+
"groupId": "edu.wpi.first.wpilibNewCommands",
12+
"artifactId": "wpilibNewCommands-java",
13+
"version": "wpilib"
14+
}
15+
],
16+
"jniDependencies": [],
17+
"cppDependencies": [
18+
{
19+
"groupId": "edu.wpi.first.wpilibNewCommands",
20+
"artifactId": "wpilibNewCommands-cpp",
21+
"version": "wpilib",
22+
"libName": "wpilibNewCommands",
23+
"headerClassifier": "headers",
24+
"sourcesClassifier": "sources",
25+
"sharedLibrary": true,
26+
"skipInvalidPlatforms": true,
27+
"binaryPlatforms": [
28+
"linuxsystemcore",
29+
"linuxathena",
30+
"linuxarm32",
31+
"linuxarm64",
32+
"windowsx86-64",
33+
"windowsx86",
34+
"linuxx86-64",
35+
"osxuniversal"
36+
]
37+
}
38+
]
39+
}

0 commit comments

Comments
 (0)