> For the complete documentation index, see [llms.txt](https://ftc-code.gitbook.io/tech-toolbox/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ftc-code.gitbook.io/tech-toolbox/computer-vision/object-tracking-driving-to-an-object.md).

# Object Tracking / Driving to an Object

{% hint style="info" %}
Prerequisites

* [Object Distance Estimation](/tech-toolbox/computer-vision/object-distance-estimation.md)
* [Autonomous Movement](/tech-toolbox/this-domain-is-now-depreciated-and-is-no-longer-updated-please-visit-this-domain-for-the-new-tt.md)
  {% endhint %}

## Object Tracking

Our turret module provides a tutorial on object tracking as well as a codebase where object tracking is implemented **(be sure to also look at the case study section in the linked module):**&#x20;

{% embed url="<https://app.gitbook.com/o/E1bAGH51cASZHB3EWq2f/s/e2VFH30qUL2leysQBnYJ/~/changes/215/commonly-programmed-modules/turrets#via-computer-vision>" %}

## Driving to an Object

### Our Way

Implement the following algorithm:&#x20;

* Detect the object and store its bounding box (refer to the Machine Learning Toolchain module)
* Determine the distance from the object to the camera (refer to the Object Distance Estimation module). You will only need the distance on the z and x axis for this.&#x20;
* You now have the hypotenuse **(distance on the z-axis)** and one side length **(absolute value of the distance on the x-axis)** of a right triangle! Solve for the other side length through the Pythagorean theorem. This second side is your y distance!
* Add the x (horizontal) and newly calculated y distance to your robot's current position, this is the object's real-world position. **Note that you have to account for the robot's orientation!**
* Plug the position into your autonomous movement functions (refer to the autonomous movement sections)

### Alternative Methods

This video tutorial shows you how to drive to a detected object:

{% embed url="<https://youtu.be/qDoLmZyH69o?list=PLMQ6o0o7Nw_9-WBWSvgt3BeLDrjVb5hjt>" %}

The FTC samples also give an excellent tutorial on driving to a detected object:&#x20;

{% embed url="<https://github.com/FIRST-Tech-Challenge/FtcRobotController/blob/e0282fcbd3de5116132a69a1d0ed229f3ca97e0f/FtcRobotController/src/main/java/org/firstinspires/ftc/robotcontroller/external/samples/ConceptVuforiaDriveToTargetWebcam.java>" %}

## Real World Example

FTC Team Hazen Robotics has implemented code that can drive to a game element:&#x20;

{% embed url="<https://www.youtube.com/watch?v=bfZT--sZf6U>" %}

{% embed url="<https://github.com/HazenRobotics/freight-frenzy/blob/master/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/utils/TargetPositionCalculator.java>" %}

{% embed url="<https://github.com/HazenRobotics/freight-frenzy/blob/master/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/vision/HomographyTargetDistance.java>" %}
