Skip to content

Commit

Permalink
Merge pull request #186 from thedropbears/vision-cleanup
Browse files Browse the repository at this point in the history
Small vision code cleanups/performance improvements
  • Loading branch information
auscompgeek authored May 27, 2024
2 parents 54d1843 + b0f102a commit 52170a1
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions components/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from magicbot import tunable
from photonlibpy.photonCamera import PhotonCamera
from photonlibpy.photonTrackedTarget import PhotonTrackedTarget
from wpimath import objectToRobotPose
from wpimath.geometry import Pose2d, Rotation3d, Transform3d, Translation3d, Pose3d

from components.chassis import ChassisComponent
Expand Down Expand Up @@ -43,7 +44,8 @@ def __init__(
chassis: ChassisComponent,
) -> None:
self.camera = PhotonCamera(name)
self.camera_to_robot = Transform3d(pos, rot).inverse()
self.robot_to_camera = Transform3d(pos, rot)
self.camera_to_robot = self.robot_to_camera.inverse()
self.last_timestamp = -1
self.last_recieved_timestep = -1.0

Expand Down Expand Up @@ -92,26 +94,18 @@ def execute(self) -> None:

if self.should_log:
self.multi_best_log.setPose(
Pose2d(
p.best.x,
p.best.y,
p.best.rotation().toRotation2d().radians(),
)
Pose2d(p.best.x, p.best.y, p.best.rotation().toRotation2d())
)
self.multi_alt_log.setPose(
Pose2d(
p.alt.x,
p.alt.y,
p.alt.rotation().toRotation2d().radians(),
)
Pose2d(p.alt.x, p.alt.y, p.alt.rotation().toRotation2d())
)
else:
for target in results.getTargets():
# filter out likely bad targets
if target.getPoseAmbiguity() > 0.25:
continue

poses = estimate_poses_from_apriltag(self.camera_to_robot, target)
poses = estimate_poses_from_apriltag(self.robot_to_camera, target)
if poses is None:
# tag doesn't exist
continue
Expand All @@ -131,18 +125,14 @@ def execute(self) -> None:
Pose2d(
target.bestCameraToTarget.x,
target.bestCameraToTarget.y,
target.bestCameraToTarget.rotation()
.toRotation2d()
.radians(),
target.bestCameraToTarget.rotation().toRotation2d(),
)
)
self.single_alt_log.setPose(
Pose2d(
target.altCameraToTarget.x,
target.altCameraToTarget.y,
target.altCameraToTarget.rotation()
.toRotation2d()
.radians(),
target.altCameraToTarget.rotation().toRotation2d(),
)
)

Expand All @@ -151,22 +141,20 @@ def sees_target(self):


def estimate_poses_from_apriltag(
cam_to_robot: Transform3d, target: PhotonTrackedTarget
robot_to_camera: Transform3d, target: PhotonTrackedTarget
) -> Optional[tuple[Pose2d, Pose2d, float]]:
tag_id = target.getFiducialId()
tag_pose = apriltag_layout.getTagPose(tag_id)
if tag_pose is None:
return None

best_pose = tag_pose.transformBy(
target.getBestCameraToTarget().inverse()
).transformBy(cam_to_robot)
alternate_pose = (
tag_pose.transformBy(target.getAlternateCameraToTarget().inverse())
.transformBy(cam_to_robot)
.toPose2d()
best_pose = objectToRobotPose(
tag_pose, target.getBestCameraToTarget(), robot_to_camera
)
alternate_pose = objectToRobotPose(
tag_pose, target.getAlternateCameraToTarget(), robot_to_camera
)
return best_pose.toPose2d(), alternate_pose, best_pose.z
return best_pose.toPose2d(), alternate_pose.toPose2d(), best_pose.z


def get_target_skew(target: PhotonTrackedTarget) -> float:
Expand Down

0 comments on commit 52170a1

Please sign in to comment.