Skip to content

Commit 1297f1d

Browse files
committed
Add VisionSubsystem
1 parent 83ef208 commit 1297f1d

3 files changed

Lines changed: 120 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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Copyright 2026 Prospect Robotics SWENext Club
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package com.team2813.lib2813.vision;
17+
;
18+
import edu.wpi.first.math.geometry.Pose2d;
19+
import edu.wpi.first.math.geometry.Rotation3d;
20+
import edu.wpi.first.wpilibj.Timer;
21+
import edu.wpi.first.wpilibj2.command.SubsystemBase;
22+
import org.photonvision.EstimatedRobotPose;
23+
24+
/**
25+
* Defines a vision subsystem.
26+
*
27+
* @param <C> the type for the camera
28+
* @since 2.1.0
29+
*/
30+
public abstract class VisionSubsystem<C extends Camera> extends SubsystemBase {
31+
protected final MultiPhotonPoseEstimator<C> poseEstimator;
32+
33+
/**
34+
* Constructor.
35+
*
36+
* @param poseEstimator The pose estimator to use.
37+
*/
38+
protected VisionSubsystem(MultiPhotonPoseEstimator<C> poseEstimator) {
39+
this.poseEstimator = poseEstimator;
40+
}
41+
42+
@Override
43+
public void periodic() {
44+
if (poseEstimator.poseStrategyRequiresHeadingData()) {
45+
poseEstimator.addHeadingData(Timer.getTimestamp(), getRotation3d());
46+
}
47+
poseEstimator.processAllUnreadResults(this::handleEstimatedPose, this::handleRejectedPose);
48+
poseEstimator.publishCameraPosesRelativeTo(getDrivetrainPose());
49+
}
50+
51+
/**
52+
* Gets the current orientation of the robot as a {@link Rotation3d} from the Pigeon 2 quaternion
53+
* values.
54+
*
55+
* @return The robot orientation.
56+
*/
57+
protected abstract Rotation3d getRotation3d();
58+
59+
/**
60+
* Gets the current pose of the robot as a {@link Pose2d} from the drivetrain.
61+
*
62+
* @return The robot position.
63+
*/
64+
protected abstract Pose2d getDrivetrainPose();
65+
66+
/**
67+
* Called when new estimated robot positions are available.
68+
*
69+
* @param estimatedPose The estimated robot position.
70+
* @param camera The camera.
71+
*/
72+
protected abstract void handleEstimatedPose(EstimatedRobotPose estimatedPose, C camera);
73+
74+
/**
75+
* Called when invalid robot positions were computed by the estimator.
76+
*
77+
* @param rejectedPose The rejected estimated robot position.
78+
*/
79+
protected void handleRejectedPose(EstimatedRobotPose rejectedPose) {}
80+
}
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)