-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5cf80ef
commit ab3861f
Showing
5 changed files
with
140 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/team4099/robot2023/config/constants/FieldConstants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
package com.team4099.robot2023.config.constants | ||
|
||
import org.team4099.lib.apriltag.AprilTag | ||
import org.team4099.lib.geometry.Pose3d | ||
import org.team4099.lib.units.base.feet | ||
import org.team4099.lib.units.base.inMeters | ||
|
||
object FieldConstants { | ||
val fieldLength = 54.feet | ||
val fieldWidth = 26.feet | ||
|
||
val aprilTags: List<AprilTag> = listOf() | ||
val homeAprilTags: List<AprilTag> = listOf() | ||
|
||
val wpilibAprilTags = | ||
if (Constants.Universal.REAL_FIELD) aprilTags.map { it.apriltagWpilib } | ||
else homeAprilTags.map { it.apriltagWpilib } | ||
|
||
val wpilibFieldLayout = | ||
edu.wpi.first.apriltag.AprilTagFieldLayout( | ||
wpilibAprilTags, fieldLength.inMeters, fieldWidth.inMeters | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 52 additions & 1 deletion
53
src/main/kotlin/com/team4099/robot2023/subsystems/vision/camera/CameraIOPhotonvision.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,55 @@ | ||
package com.team4099.robot2023.subsystems.vision.camera | ||
|
||
class CameraIOPhotonvision { | ||
import com.team4099.robot2023.config.constants.FieldConstants | ||
import org.photonvision.EstimatedRobotPose | ||
import org.photonvision.PhotonCamera | ||
import org.photonvision.PhotonPoseEstimator | ||
import org.photonvision.PhotonPoseEstimator.PoseStrategy | ||
import org.team4099.lib.around | ||
import org.team4099.lib.geometry.Pose3d | ||
import org.team4099.lib.geometry.Pose3dWPILIB | ||
import org.team4099.lib.geometry.Transform3dWPILIB | ||
import org.team4099.lib.units.base.Time | ||
import org.team4099.lib.units.base.inSeconds | ||
import org.team4099.lib.units.base.seconds | ||
import org.team4099.lib.units.micro | ||
import org.team4099.lib.units.milli | ||
import java.util.* | ||
|
||
|
||
class CameraIOPhotonvision(private val identifier: String): CameraIO { | ||
|
||
private val photonEstimator: PhotonPoseEstimator | ||
private val camera: PhotonCamera | ||
private var lastEstTimestamp: Time = 0.0.seconds | ||
|
||
init { | ||
camera = PhotonCamera(identifier) | ||
photonEstimator = PhotonPoseEstimator( | ||
FieldConstants.wpilibFieldLayout, PoseStrategy.MULTI_TAG_PNP_ON_COPROCESSOR, camera, Transform3dWPILIB() | ||
) | ||
photonEstimator.setMultiTagFallbackStrategy(PoseStrategy.LOWEST_AMBIGUITY) | ||
|
||
} | ||
|
||
override fun updateInputs(inputs: CameraIO.CameraInputs) { | ||
val visionEst: Optional<EstimatedRobotPose>? = photonEstimator.update() | ||
val poseEst = visionEst?.get()?.let { Pose3d(it.estimatedPose) } | ||
val latestTimestamp = visionEst?.get()?.timestampSeconds?.seconds | ||
if (latestTimestamp != null) { | ||
if ((latestTimestamp - lastEstTimestamp).absoluteValue > 10.micro.seconds) { | ||
inputs.fps = 1/(latestTimestamp - lastEstTimestamp).inSeconds | ||
lastEstTimestamp = latestTimestamp | ||
} | ||
} | ||
|
||
if (visionEst != null) { | ||
inputs.usedTargets = visionEst.get().targetsUsed.map { it.fiducialId } | ||
} | ||
|
||
inputs.timestamp = lastEstTimestamp | ||
if (poseEst != null) { | ||
inputs.frame = poseEst | ||
} | ||
} | ||
} |