Files
PhotonVision/docs/source/docs/examples/aimingatatarget.md
2026-01-12 14:00:11 -08:00

2.9 KiB

Aiming at a Target

The following example is from the PhotonLib example repository (Java).

Knowledge and Equipment Needed

  • A Robot
  • A camera mounted rigidly to the robot's frame, centered and pointed forward.
  • A coprocessor running PhotonVision with an AprilTag or ArUco 2D Pipeline.
  • A printout of AprilTag 7, mounted on a rigid and flat surface.

Code

Now that you have properly set up your vision system and have tuned a pipeline, you can now aim your robot at an AprilTag using the data from PhotonVision. The yaw of the target is the critical piece of data that will be needed first.

Yaw is reported to the roboRIO over Network Tables. PhotonLib, our vendor dependency, is the easiest way to access this data. The documentation for the Network Tables API can be found {ref}here <docs/additional-resources/nt-api:Getting Target Information> and the documentation for PhotonLib {ref}here <docs/programming/photonlib/adding-vendordep:What is PhotonLib?>.

In this example, while the operator holds a button down, the robot will turn towards the AprilTag using the P term of a PID loop. To learn more about how PID loops work, how WPILib implements them, and more, visit Advanced Controls (PID) and PID Control in WPILib.

.. tab-set::
    :sync-group: code

    .. tab-item:: Java
       :sync: java

       .. rli:: https://raw.githubusercontent.com/PhotonVision/photonvision/abe95dfaa055bbe3609f72cfcaaba0f96ee7978c/photonlib-java-examples/aimattarget/src/main/java/frc/robot/Robot.java
         :language: java
         :lines: 77-117
         :linenos:
         :lineno-start: 77

    .. tab-item:: C++ (Header)
       :sync: c++

       .. rli:: https://raw.githubusercontent.com/PhotonVision/photonvision/abe95dfaa055bbe3609f72cfcaaba0f96ee7978c/photonlib-cpp-examples/aimattarget/src/main/include/Robot.h
         :language: c++
         :lines: 25-60
         :linenos:
         :lineno-start: 25

    .. tab-item:: C++ (Source)
       :sync: c++

       .. rli:: https://raw.githubusercontent.com/PhotonVision/photonvision/abe95dfaa055bbe3609f72cfcaaba0f96ee7978c/photonlib-cpp-examples/aimattarget/src/main/cpp/Robot.cpp
         :language: c++
         :lines: 56-96
         :linenos:
         :lineno-start: 56

    .. tab-item:: Python
       :sync: python

       .. rli:: https://raw.githubusercontent.com/PhotonVision/photonvision/abe95dfaa055bbe3609f72cfcaaba0f96ee7978c/photonlib-python-examples/aimattarget/robot.py
         :language: python
         :lines: 46-70
         :linenos:
         :lineno-start: 46