### PhotonVision Setup and AprilTag Alignment (C++) Source: https://docs.photonvision.org/en/latest/docs/examples/aimingatatarget This C++ code snippet demonstrates the setup for PhotonVision camera and robot control, including header includes and class definitions. The source code then shows how to read AprilTag data and use the target's yaw for auto-alignment, similar to the Java example. It includes logic for processing camera results and overriding driver input for turning. ```cpp 25#pragma once 26 27#include 28 29#include 30#include 31 32#include "Constants.h" 33#include "VisionSim.h" 34#include "subsystems/SwerveDrive.h" 35 36class Robot : public frc::TimedRobot { 37 public: 38 void RobotInit() override; 39 void RobotPeriodic() override; 40 void DisabledInit() override; 41 void DisabledPeriodic() override; 42 void DisabledExit() override; 43 void AutonomousInit() override; 44 void AutonomousPeriodic() override; 45 void AutonomousExit() override; 46 void TeleopInit() override; 47 void TeleopPeriodic() override; 48 void TeleopExit() override; 49 void TestInit() override; 50 void TestPeriodic() override; 51 void TestExit() override; 52 void SimulationPeriodic() override; 53 54 private: 55 photon::PhotonCamera camera{constants::Vision::kCameraName}; 56 SwerveDrive drivetrain{}; 57 VisionSim vision{&camera}; 58 frc::XboxController controller{0}; 59 static constexpr double VISION_TURN_kP = 0.01; 60}; ``` ```cpp 56void Robot::TeleopPeriodic() { 57 // Calculate drivetrain commands from Joystick values 58 auto forward = 59 -1.0 * controller.GetLeftY() * constants::Swerve::kMaxLinearSpeed; 60 auto strafe = 61 -1.0 * controller.GetLeftX() * constants::Swerve::kMaxLinearSpeed; 62 auto turn = 63 -1.0 * controller.GetRightX() * constants::Swerve::kMaxAngularSpeed; 64 65 bool targetVisible = false; 66 double targetYaw = 0.0; 67 auto results = camera.GetAllUnreadResults(); 68 if (results.size() > 0) { 69 // Camera processed a new frame since last 70 // Get the last one in the list. 71 auto result = results[results.size() - 1]; 72 if (result.HasTargets()) { 73 // At least one AprilTag was seen by the camera 74 for (auto& target : result.GetTargets()) { 75 if (target.GetFiducialId() == 7) { 76 // Found Tag 7, record its information 77 targetYaw = target.GetYaw(); 78 targetVisible = true; 79 } 80 } 81 } 82 } 83 84 // Auto-align when requested 85 if (controller.GetAButton() && targetVisible) { 86 // Driver wants auto-alignment to tag 7 87 // And, tag 7 is in sight, so we can turn toward it. ``` -------------------------------- ### Run PhotonLib Python Example Source: https://docs.photonvision.org/en/latest/docs/contributing/building-photon Executes a Python robot project example using PhotonLib. This command should be run after building the PhotonLib Python wheel and enabling development wheels. ```bat > cd photonlib-python-examples > run.bat ``` -------------------------------- ### Install PhotonVision on Debian-Based Systems Source: https://docs.photonvision.org/en/latest/docs/advanced-installation/sw_install/other-coprocessors This script automates the installation of PhotonVision on Debian-based systems like Raspberry Pi OS Buster and Ubuntu Bionic. It requires an internet connection and uses `wget` to download the script, `chmod` to make it executable, and then executes it with `sudo`. A reboot is recommended after installation. ```bash $ wget https://git.io/JJrEP -O install.sh $ sudo chmod +x install.sh $ sudo ./install.sh $ sudo reboot now ``` -------------------------------- ### Install Python Dependencies for PhotonVision Docs Source: https://docs.photonvision.org/en/latest/docs/contributing/building-docs Installs the necessary Python dependencies required to build the PhotonVision documentation. This command should be run from the `docs` sub-folder of the cloned repository and uses pip to install packages listed in `requirements.txt`. ```bash ~/photonvision/docs$ python -m pip install -r requirements.txt ``` -------------------------------- ### Complete Example hardwareConfig.json (JSON) Source: https://docs.photonvision.org/en/latest/docs/hardware/customhardware A comprehensive example of the `hardwareConfig.json` file, demonstrating the configuration of device name, logo, support URL, LED settings, GPIO commands, restart command, and camera FOV. ```json { "deviceName" : "Blinky McBlinkface", "deviceLogoPath" : "", "supportURL" : "https://www.youtube.com/watch?v=b-CvLWbfZhU", "ledPins" : [2, 13], "ledsCanDim" : true, "ledBrightnessRange" : [ 0, 100 ], "ledPWMFrequency" : 0, "statusRGBPins" : [ ], "statusRGBActiveHigh" : false, "getGPIOCommand" : "getGPIO {p}", "setGPIOCommand" : "setGPIO {p} {s}", "setPWMCommand" : "setPWM {p} {v}", "setPWMFrequencyCommand" : "setPWMFrequency {p} {f}", "releaseGPIOCommand" : "releaseGPIO {p}", "restartHardwareCommand" : "", "vendorFOV" : 72.5 } ``` -------------------------------- ### Simulate Native Robot Project with PhotonLib Source: https://docs.photonvision.org/en/latest/docs/contributing/building-photon Runs a C++ robot project example using PhotonLib. This command should be executed after publishing PhotonLib to your local Maven repository. ```bash ~/photonvision/photonlib-cpp-examples$ ./gradlew :simulateNative ``` -------------------------------- ### Install and Manage PhotonVision systemd Service Source: https://docs.photonvision.org/en/latest/docs/advanced-installation/sw_install/advanced-cmd Commands to install, enable, and manage the PhotonVision systemd service. This includes enabling the service to start on boot, reloading systemd configurations, and setting file permissions for the service file. ```bash touch photonvision.service cp photonvision.service /etc/systemd/system/photonvision.service chmod 644 /etc/systemd/system/photonvision.service systemctl enable photonvision.service systemctl daemon-reload ``` -------------------------------- ### Install wpiformat Source: https://docs.photonvision.org/en/latest/docs/contributing/linting Installs the wpiformat linter using pipx. A specific version can be installed by appending '==' to the command. ```bash pipx install wpiformat ``` ```bash pipx install wpiformat== ``` -------------------------------- ### Simulate Java Robot Project with PhotonLib Source: https://docs.photonvision.org/en/latest/docs/contributing/building-photon Runs a Java robot project example using PhotonLib. This command should be executed after publishing PhotonLib to your local Maven repository. ```bash ~/photonvision/photonlib-java-examples$ ./gradlew :simulateJava ``` -------------------------------- ### Install Node.js Dependencies for UI Source: https://docs.photonvision.org/en/latest/docs/contributing/building-photon Installs the necessary Node.js packages for the PhotonVision UI using the pnpm package manager. This command should be run from the 'photon-client' directory. ```bash pnpm install ``` -------------------------------- ### Run PhotonVision JAR File Source: https://docs.photonvision.org/en/latest/docs/advanced-installation/sw_install/advanced-cmd Executes the PhotonVision JAR file using the Java runtime. Ensure Java is installed and environment variables are set. This command starts the PhotonVision application. ```bash java -jar /path/to/photonvision/photonvision.jar ``` -------------------------------- ### Run PhotonVision JAR file on Linux Source: https://docs.photonvision.org/en/latest/docs/advanced-installation/sw_install/linux-pc Executes the PhotonVision application using the Java runtime. Replace '/path/to/photonvision/photonvision-xxx.jar' with the actual path to your downloaded JAR file. This command assumes Java is installed and configured correctly. ```bash $ java -jar /path/to/photonvision/photonvision-xxx.jar ``` -------------------------------- ### Publish PhotonLib to Local Maven Repository Source: https://docs.photonvision.org/en/latest/docs/contributing/building-photon Publishes the PhotonLib library to your local Maven repository. This is a prerequisite for running C++/Java examples and ensures that the generated vendordep JSON file is copied to each example project. ```bash ~/photonvision$ ./gradlew publishToMavenLocal ``` -------------------------------- ### Deploy PhotonVision to Raspberry Pi (Linux/macOS) Source: https://docs.photonvision.org/en/latest/docs/contributing/building-photon Cleans the project and then builds, deploys, and starts PhotonVision on a Raspberry Pi coprocessor with a specified architecture override. This command uses Gradle and deploy-utils. ```bash ./gradlew clean ./gradlew deploy -PArchOverride=linuxarm64 ``` -------------------------------- ### Clone PhotonVision Documentation Repository using Git Source: https://docs.photonvision.org/en/latest/docs/contributing/building-docs This command clones the PhotonVision documentation repository from GitHub. It requires Git to be installed on your system. It's recommended to fork the repository first if you plan to contribute. ```bash git clone https://github.com/[your username]/photonvision ``` -------------------------------- ### Install doc8 Source: https://docs.photonvision.org/en/latest/docs/contributing/linting Installs the doc8 Python linter for documentation using pipx. To install a specific version, append '==' to the command. ```bash pipx install doc8 ``` ```bash pipx install doc8== ``` -------------------------------- ### Deploy PhotonVision to Raspberry Pi (Windows) Source: https://docs.photonvision.org/en/latest/docs/contributing/building-photon Cleans the project and then builds, deploys, and starts PhotonVision on a Raspberry Pi coprocessor with a specified architecture override. This command uses Gradle on a Windows command prompt and deploy-utils. ```batch gradlew clean gradlew deploy -PArchOverride=linuxarm64 ``` -------------------------------- ### Install PhotonVision v2023.4.2 on Romi Source: https://docs.photonvision.org/en/latest/docs/advanced-installation/sw_install/romi This snippet downloads, makes executable, and runs the PhotonVision installation script for a specific version compatible with WPILibPi 2023.2.1. It requires an internet connection and the Raspberry Pi's filesystem to be writable. ```bash wget https://git.io/JJrEP -O install.sh sudo chmod +x install.sh sudo ./install.sh -v v2023.4.2 ``` -------------------------------- ### Start PhotonVision for Remote Debugging Source: https://docs.photonvision.org/en/latest/docs/contributing/building-photon Launch PhotonVision with the necessary JVM arguments to enable remote debugging. This allows a debugger, such as the one configured in VSCode, to attach to the running process. ```bash java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5801 photonvision.jar ``` -------------------------------- ### RoboRIO PhotonVision Test Setup (Java) Source: https://docs.photonvision.org/en/latest/docs/contributing/design-descriptions/e2e-latency This Java code demonstrates how to initialize a PhotonCamera and a DigitalOutput for an LED on a RoboRIO. It includes a periodic method to test the timing accuracy between turning on an LED and the camera capturing a frame with the LED illuminated. Dependencies include the PhotonVision library and WPILib. ```java package frc.robot; import org.photonvision.PhotonCamera; import edu.wpi.first.wpilibj.DigitalOutput; import edu.wpi.first.wpilibj.TimedRobot; import edu.wpi.first.wpilibj.Timer; import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; public class Robot extends TimedRobot { PhotonCamera camera; DigitalOutput light; @Override public void robotInit() { camera = new PhotonCamera("Arducam_OV9782_USB_Camera"); light = new DigitalOutput(0); light.set(false); } @Override public void robotPeriodic() { super.robotPeriodic(); try { light.set(false); for (int i = 0; i < 50; i++) { Thread.sleep(20); camera.getAllUnreadResults(); } var t1 = Timer.getFPGATimestamp(); light.set(true); var t2 = Timer.getFPGATimestamp(); for (int i = 0; i < 100; i++) { for (var result : camera.getAllUnreadResults()) { if (result.hasTargets()) { var t3 = result.getTimestampSeconds(); var t1p5 = (t1 + t2) / 2; var error = t3-t1p5; SmartDashboard.putNumber("blink_error_ms", error * 1000); return; } } Thread.sleep(20); } } catch (InterruptedException e) { e.printStackTrace(); } } } ``` -------------------------------- ### Install Java Development Kit (JDK) 17 on Debian-based Linux Source: https://docs.photonvision.org/en/latest/docs/advanced-installation/sw_install/linux-pc Installs OpenJDK 17, the required Java Development Kit for PhotonVision. This command should be run in a terminal on a Debian-based Linux system. Ensure you have sudo privileges. ```bash $ sudo apt-get install openjdk-17-jdk ``` -------------------------------- ### Build and Serve PhotonVision Documentation Locally with Sphinx Source: https://docs.photonvision.org/en/latest/docs/contributing/building-docs Builds the PhotonVision documentation and serves it locally, automatically rebuilding on file changes. This command uses `sphinx-autobuild` to provide a live preview at `localhost:8000` by default. It should be executed from the `docs` sub-folder. ```bash ~/photonvision/docs$ sphinx-autobuild --open-browser source source/_build/html ``` -------------------------------- ### Set and Get FPS Limit - C++ Source: https://docs.photonvision.org/en/latest/docs/programming/photonlib/fps-limiter Illustrates retrieving the current FPS limit, applying a new FPS limit, and disabling the limit by setting it to -1. This functionality aids in optimizing power consumption for demanding camera setups. ```cpp int limit = camera.GetFPSLimit(); camera.SetFPSLimit(10); // This removes any previously set FPS limit. camera.SetFPSLimit(-1); ``` -------------------------------- ### Comment out camera exec and add sleep in runCamera script Source: https://docs.photonvision.org/en/latest/docs/advanced-installation/sw_install/romi This snippet demonstrates how to modify the `/home/pi/runCamera` script using nano. It comments out the existing 'exec' line by adding a '#' and inserts a 'sleep 10000' command to delay camera initialization, ensuring PhotonVision can start properly. This is done via SSH. ```bash sudo nano /home/pi/runCamera # Comment out the exec line by adding '#' at the beginning # exec ... # Add the following line above the commented exec line sleep 10000 # Save the file with Ctrl+O, Enter, and exit with Ctrl+X ``` -------------------------------- ### Update PhotonVision via SCP and SSH Source: https://docs.photonvision.org/en/latest/docs/advanced-installation/sw_install/other-coprocessors This process updates PhotonVision by transferring the new JAR file to the coprocessor using `scp`, then connecting via `ssh` to move the JAR file to the correct directory and restart the PhotonVision service using `systemctl`. Replace '[jar name]' and '[user]' with appropriate values. ```bash $ scp [jar name].jar [user]@photonvision.local:~/ $ ssh [user]@photonvision.local $ sudo mv [jar name].jar /opt/photonvision/photonvision.jar $ sudo systemctl restart photonvision.service ``` -------------------------------- ### Build and Run PhotonVision (Linux/macOS) Source: https://docs.photonvision.org/en/latest/docs/contributing/building-photon Compiles and runs the PhotonVision project using Gradle. This command is executed from the root directory of the project. ```bash ./gradlew run ``` -------------------------------- ### Install PhotonLibPy using pyproject.toml Source: https://docs.photonvision.org/en/latest/docs/programming/photonlib/adding-vendordep This snippet shows how to add the photonlibpy package to your project's pyproject.toml file for installation. It's a simple addition to the 'requires' list for pip packages. ```toml # Other pip packages to install requires = [ "photonlibpy", ] ``` -------------------------------- ### Initialize Vision System Simulation (Java, C++, Python) Source: https://docs.photonvision.org/en/latest/docs/examples/poseest Demonstrates the initialization of a `VisionSystemSim` object, which simulates the behavior of a PhotonVision coprocessor and camera. This is used for testing and development without requiring physical hardware. Code is provided in Java, C++, and Python. ```java 65 // Create the vision system simulation which handles cameras and targets on the field. 66 visionSim = new VisionSystemSim("main"); 67 // Add all the AprilTags inside the tag layout as visible targets to this simulated field. 68 visionSim.addAprilTags(kTagLayout); 69 // Create simulated camera properties. These can be set to mimic your actual camera. ``` ```cpp 49 visionSim = std::make_unique("main"); 50 51 visionSim->AddAprilTags(constants::Vision::kTagLayout); ``` ```python ``` -------------------------------- ### Build and Run PhotonVision (Windows) Source: https://docs.photonvision.org/en/latest/docs/contributing/building-photon Compiles and runs the PhotonVision project using Gradle on a Windows command prompt. This command is executed from the root directory of the project. ```batch gradlew run ``` -------------------------------- ### Publish PhotonLib to Local Maven (Windows) Source: https://docs.photonvision.org/en/latest/docs/contributing/building-photon Builds the PhotonLib and publishes it to the local Maven repository using a Windows command prompt. This allows other projects on the same machine to use this local build as a dependency. ```batch gradlew publishToMavenLocal ``` -------------------------------- ### Get Best Tracked Target Source: https://docs.photonvision.org/en/latest/docs/programming/photonlib/getting-target-data Retrieves the single best tracked target from a pipeline result. This method is available in Java and C++. Python support is listed as 'Coming Soon'. ```java // Get the current best target. PhotonTrackedTarget target = result.getBestTarget(); ``` ```cpp // Get the current best target. photonlib::PhotonTrackedTarget target = result.GetBestTarget(); ``` ```python # Coming Soon! ``` -------------------------------- ### Lint Backend with Gradle and wpiformat Source: https://docs.photonvision.org/en/latest/docs/contributing/linting Applies formatting using spotlessApply via Gradle and then runs wpiformat for backend linting. These commands are typically run from the project's root directory. ```bash ./gradlew spotlessApply wpiformat ``` -------------------------------- ### Create PhotonCamera Instance (Java, C++, Python) Source: https://docs.photonvision.org/en/latest/docs/examples/poseest Shows how to instantiate a PhotonCamera object, which is necessary for integrating PhotonVision into the robot's control loop. This allows the robot to access camera feeds and process vision data. Examples are provided for Java, C++, and Python. ```java 57 camera = new PhotonCamera(kCameraName); ``` ```cpp 145 photon::PhotonCamera camera{constants::Vision::kCameraName}; ``` ```python 44 self.swerve = drivetrain.Drivetrain() ``` -------------------------------- ### Get List of Tracked Targets Source: https://docs.photonvision.org/en/latest/docs/programming/photonlib/getting-target-data Retrieves a list of all currently tracked targets from a pipeline result. Each target object contains information such as yaw, pitch, area, and robot-relative pose. Available in Java and C++. ```java // Get a list of currently tracked targets. List targets = result.getTargets(); ``` ```cpp // Get a list of currently tracked targets. wpi::ArrayRef targets = result.GetTargets(); ``` ```python # Get a list of currently tracked targets. targets = result.getTargets() ``` -------------------------------- ### Set and Get FPS Limit - Java Source: https://docs.photonvision.org/en/latest/docs/programming/photonlib/fps-limiter Demonstrates how to get the current FPS limit, set a new FPS limit, and remove any previously set limit by passing -1. This is useful for power management with high-FPS cameras. ```java int limit = camera.getFPSLimit(); camera.setFPSLimit(10); // This removes any previously set FPS limit. camera.setFPSLimit(-1); ```