Files
PhotonVision/photonlib-python-examples/poseest/robot.py
PJ Reiniger 7b30c9306e [python] Fixup python examples / build (#2509)
## Description

This fixes up and renables the python examples builds.

Main fixes
- Test shell script just straight up didn't do what it was intended to
do
- Replace analog imus (and half refactored sims) with OnboardIMU
- Fixes swerve velocities function signature
- "regenerates" the default robotpy test

## Meta

Merge checklist:
- [x] Pull Request title is [short, imperative
summary](https://cbea.ms/git-commit/) of proposed changes
- [x] The description documents the _what_ and _why_, including events
that led to this PR
- [ ] If this PR changes behavior or adds a feature, user documentation
is updated
- [ ] If this PR touches photon-serde, all messages have been
regenerated and hashes have not changed unexpectedly
- [ ] If this PR touches configuration, this is backwards compatible
with all settings going back to the previous seasons's last release
(seasons end after champs ends)
- [ ] If this PR touches pipeline settings or anything related to data
exchange, the frontend typing is updated
- [ ] If this PR addresses a bug, a regression test for it is added
- [ ] If this PR adds a dependency, the license has been checked for
compatibility and steps taken to follow it

---------

Co-authored-by: Matt Morley <matthew.morley.ca@gmail.com>
Co-authored-by: David Vo <auscompgeek@users.noreply.github.com>
2026-05-27 14:29:26 -07:00

76 lines
3.0 KiB
Python

#!/usr/bin/env python3
###################################################################################
# MIT License
#
# Copyright (c) PhotonVision
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
###################################################################################
import drivetrain
import wpilib
import wpimath
from photonlibpy import PhotonCamera, PhotonPoseEstimator
from robotpy_apriltag import AprilTagField, AprilTagFieldLayout
kRobotToCam = wpimath.Transform3d(
wpimath.Translation3d(0.5, 0.0, 0.5),
wpimath.Rotation3d.fromDegrees(0.0, -30.0, 0.0),
)
class MyRobot(wpilib.TimedRobot):
def __init__(self) -> None:
"""Robot initialization function"""
super().__init__()
self.controller = wpilib.NiDsXboxController(0)
self.swerve = drivetrain.Drivetrain()
self.cam = PhotonCamera("YOUR CAMERA NAME")
self.camPoseEst = PhotonPoseEstimator(
AprilTagFieldLayout.loadField(AprilTagField.kDefaultField),
kRobotToCam,
)
def robotPeriodic(self) -> None:
for result in self.cam.getAllUnreadResults():
camEstPose = self.camPoseEst.estimateCoprocMultiTagPose(result)
if camEstPose is None:
camEstPose = self.camPoseEst.estimateLowestAmbiguityPose(result)
if camEstPose:
self.swerve.addVisionPoseEstimate(
camEstPose.estimatedPose, camEstPose.timestampSeconds
)
self.swerve.updateOdometry()
self.swerve.log()
def teleopPeriodic(self) -> None:
xSpeed = -1.0 * self.controller.getLeftY() * drivetrain.kMaxSpeed
ySpeed = -1.0 * self.controller.getLeftX() * drivetrain.kMaxSpeed
rot = -1.0 * self.controller.getRightX() * drivetrain.kMaxAngularSpeed
self.swerve.drive(xSpeed, ySpeed, rot, True, self.getPeriod())
def simulationPeriodic(self) -> None:
self.swerve.simulationPeriodic()
return super().simulationPeriodic()