Files
PhotonVision/photonlib-python-examples/aimattarget/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

75 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
from photonlibpy import PhotonCamera
VISION_TURN_kP = 0.01
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")
def robotPeriodic(self) -> None:
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
# Get information from the camera
targetYaw = 0.0
targetVisible = False
results = self.cam.getAllUnreadResults()
if len(results) > 0:
result = results[-1] # take the most recent result the camera had
for target in result.getTargets():
if target.getFiducialId() == 7:
# Found tag 7, record its information
targetVisible = True
targetYaw = target.getYaw()
if self.controller.getAButton() and targetVisible:
# Driver wants auto-alignment to tag 7
# And, tag 7 is in sight, so we can turn toward it.
# Override the driver's turn command with an automatic one that turns toward the tag.
rot = -1.0 * targetYaw * VISION_TURN_kP * drivetrain.kMaxAngularSpeed
self.swerve.drive(xSpeed, ySpeed, rot, True, self.getPeriod())
def simulationPeriodic(self) -> None:
self.swerve.simulationPeriodic()
return super().simulationPeriodic()