2024-08-18 14:05:23 -04:00
# Combining Aiming and Getting in Range
2024-06-30 16:10:12 -04:00
2024-08-18 14:05:23 -04:00
The following example is from the PhotonLib example repository ([Java ](https://github.com/PhotonVision/photonvision/tree/master/photonlib-java-examples/aimandrange )/[C++ ](https://github.com/PhotonVision/photonvision/tree/master/photonlib-cpp-examples/aimandrange )).
2024-06-30 16:10:12 -04:00
2024-08-18 14:05:23 -04:00
## Knowledge and Equipment Needed
2024-06-30 16:10:12 -04:00
2024-09-14 23:10:02 -05:00
- Everything required in {ref}`Aiming at a Target <docs/examples/aimingatatarget:Knowledge and Equipment Needed>` .
2024-06-30 16:10:12 -04:00
2024-08-18 14:05:23 -04:00
## Code
2024-06-30 16:10:12 -04:00
2024-09-14 23:10:02 -05:00
Now that you know how to aim toward the AprilTag, let's also drive the correct distance from the AprilTag.
To do this, we'll use the * pitch * of the target in the camera image and trigonometry to figure out how far away the robot is from the AprilTag. Then, like before, we'll use the P term of a PID controller to drive the robot to the correct distance.
2024-06-30 16:10:12 -04:00
2024-08-18 14:05:23 -04:00
```{eval-rst}
2024-06-30 16:10:12 -04:00
.. tab-set::
.. tab-item:: Java
2024-09-14 23:10:02 -05:00
.. rli:: https://raw.githubusercontent.com/gerth2/photonvision/adb3098fbe0cdbc1a378c6d5a41126dd1d6d6955/photonlib-java-examples/aimandrange/src/main/java/frc/robot/Robot.java
2024-06-30 16:10:12 -04:00
:language: java
2024-09-14 23:10:02 -05:00
:lines: 84-131
2024-06-30 16:10:12 -04:00
:linenos:
2024-09-14 23:10:02 -05:00
:lineno-start: 84
2024-06-30 16:10:12 -04:00
.. tab-item:: C++ (Header)
2024-09-14 23:10:02 -05:00
.. rli:: https://raw.githubusercontent.com/gerth2/photonvision/adb3098fbe0cdbc1a378c6d5a41126dd1d6d6955/photonlib-cpp-examples/aimandrange/src/main/include/Robot.h
:language: c++
:lines: 25-63
2024-06-30 16:10:12 -04:00
:linenos:
2024-09-14 23:10:02 -05:00
:lineno-start: 25
2024-06-30 16:10:12 -04:00
.. tab-item:: C++ (Source)
2024-09-14 23:10:02 -05:00
.. rli:: https://raw.githubusercontent.com/gerth2/photonvision/adb3098fbe0cdbc1a378c6d5a41126dd1d6d6955/photonlib-cpp-examples/aimandrange/src/main/cpp/Robot.cpp
:language: c++
:lines: 58-107
2024-06-30 16:10:12 -04:00
:linenos:
2024-09-14 23:10:02 -05:00
:lineno-start: 58
.. tab-item:: Python
.. rli:: https://raw.githubusercontent.com/gerth2/photonvision/adb3098fbe0cdbc1a378c6d5a41126dd1d6d6955/photonlib-python-examples/aimandrange/robot.py
:language: python
:lines: 44-98
:linenos:
:lineno-start: 44
2024-08-18 14:05:23 -04:00
```