-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcamera.py
More file actions
43 lines (38 loc) · 1.54 KB
/
camera.py
File metadata and controls
43 lines (38 loc) · 1.54 KB
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
import photonlibpy
import wpimath.units
import constants
from typing import Optional, Tuple
from utils import Utils
from photonlibpy.targeting.photonTrackedTarget import PhotonTrackedTarget
class AprilTagCamera:
def __init__(self, camera: str) -> None:
self.camera = photonlibpy.PhotonCamera(camera)
def getBestTarget(self) -> Optional[PhotonTrackedTarget]:
result = self.camera.getLatestResult()
if result.hasTargets():
target = result.getBestTarget()
return target
return None
def getYaw(self, tag: int) -> float:
results = self.camera.getAllUnreadResults()
if len(results) > 0:
result = results[-1]
for target in result.getTargets():
if target.getFiducialId() == tag:
return target.getYaw()
return -1
def getYawWithRange(self, tag: int) -> Tuple[float, float]:
results = self.camera.getAllUnreadResults()
target_range = 0
if len(results) > 0:
result = results[-1]
for target in result.getTargets():
if target.getFiducialId() == tag:
target_range = Utils.calculateDistanceToTargetMeters(
constants.CAMERA_HEIGHT_METERS,
constants.TARGET_HEIGHT_METERS,
constants.CAMERA_PITCH_RADIANS,
wpimath.units.degreesToRadians(target.getPitch())
)
return target.getYaw(), target_range
return -1, -1