# Turrets

{% hint style="info" %}
Prerequisites

* [Motors and Encoders](../please-visit-https-ftc-tech-toolbox.vercel.app-for-the-new-tech-toolbox/motors-and-encoders)
* [IMU](../sensors/imu)
* [Computer Vision](../computer-vision) or [Localization](../this-domain-is-now-depreciated-and-is-no-longer-updated-1)
{% endhint %}

In recent times, the integration of turrets in robot designs has become increasingly prevalent among the world's top-performing robots. A turret is a mechanism that enables independent rotation of one or more modules from the main body of the robot. This feature is valued for the enhanced mobility it offers. By incorporating a turret, teams can avoid the need to turn the entire robot for scoring purposes, resulting in a substantial elevation of their scoring capabilities.

{% embed url="https://www.google.com/url?cad=rja&cd=&esrc=s&q=&rct=j&sa=t&source=web&uact=8&url=https://www.youtube.com/watch?v=GXJ5LX4zIpI&usg=AOvVaw1IFsb1UiLclV4XTYhhzH06&ved=2ahUKEwjkiJiVmL7_AhVpmYQIHZSLBgkQwqsBegQIChAF" %}
Gluten Free's Turret Robot
{% endembed %}

Nevertheless, constructing and programming a turret can pose certain challenges due to the limited availability of documentation on the subject. In this section, we will explore different approaches for programming a turret.

Our discussion will assume that the turret is operated by a single motor linked to an encoder, but the techniques can be adapted to suit alternative control methods as well.

## Automatic Aiming

Typically, the objective is to have the turret mechanism automatically track and aim at a specific target. Manual control of a turret can be more time-consuming compared to manually turning the entire robot. To achieve automatic aiming, two approaches are commonly employed, both relying on the connection of an encoder to the motor responsible for rotating the turret:

* [Through the use of odometry localization and geometry](#via-geometry)
* [Through the use of computer vision](#via-computer-vision)

### Via Geometry

Prior code needed:

* An accurate odometry localization system (x, y, angle)

In this approach, we leverage geometry alongside odometry data, which continuously provides the robot's current position. By utilizing mathematical principles, we can determine the angle at which the turret needs to rotate. It is important to note that the robot's starting point, serving as the origin, must remain constant for accurate turret aiming using geometry.

By obtaining the coordinates of the target object relative to the origin, we enable the application of mathematical formulas for precise turret tracking.

In summary, we now have three pieces of information:

* The robot's current position relative to the origin
* The object's position relative to the origin
* The robot's angle

{% hint style="warning" %}
- All units must be the same, for example, all distances between objects must be in inches. And all angle measurements must be in degrees.
- Note that your angles should be in the range of \[0, 360] rather than \[-180, 180], which is different from the angle you use for robot movement which requires your angle to be within \[-180, 180].&#x20;
{% endhint %}

<img src="https://200969929-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fe2VFH30qUL2leysQBnYJ%2Fuploads%2FvO6nwbAwV9yjZ3MkFzQh%2Ffile.excalidraw.svg?alt=media&#x26;token=734a7101-b4a9-4633-857d-c8cebb8640ee" alt="" class="gitbook-drawing">

For now, let's continue under the assumption that the robot's angle doesn't change but its (x,y) position does. $$θ$$ is the angle from the robot to the object, and thus it is also the angle the turrent must turn to. Lucky for us, there is already an established formula for finding the angle between two points as $$θ$$  is known as the bearing angle.&#x20;

We can find the bearing angle like this:

Define the _bearing angle_ $$θ$$ from a point $$A(a1,a2)$$ to a point $$B(b1,b2)$$ as the angle measured in the clockwise direction from the north line as the origin to the line segment $$AB$$

<figure><img src="https://i.stack.imgur.com/VdvaP.png" alt=""><figcaption></figcaption></figure>

Then,$$(b1,b2)=(a1+rsinθ,a2+rcosθ),$$ where $$r$$is the length of the line segment $$AB$$. It follows that $$θ$$satisfies the equation: $$tanθ=(b1−a1)/(b2−a2)$$

We can use the $$atan2$$ function to compute $$θ$$.&#x20;

Note that the equations are given in terms of Cartesian coordinates, so it is necessary to transform them to screen coordinates. The formula for $$θ$$ in terms of screen coordinates $$(a1,a2)$$ and $$(b1,b2)$$ is $$θ=atan2(b1−a1,a2−b2)$$.

Formally, it would look like this:&#x20;

$$
θ=atan2(y_2−y_1/x_2−x_1)
$$

In Java we would implement it like this:&#x20;

```java
double theta = toDegrees(Math.atan2(y2 - y1, x2 - x1)); 
```

#### Sample Calculation

Using the formula we can solve for the desired turrent angle seen in the image above:

First, let's define the coordinates:

* Origin Point:  $$O(0,0)$$&#x20;
* Robot Position:  $$A(a1,a2)=A(x_1,y_1)=A(0,0)$$
* Object Position:  $$B(b1,b2)=B(x_2,y_2)=B(2,2)$$

From here we apply the formula derived above: $$θ=atan2(y_2−y_1/x_2−x_1)$$.

Now just substitute the values:

$$θ=atan2(2/2)$$

#### Accounting for Robot Rotation

As mentioned above an additional step is needed correctly calculate the turn angle if the robot has rotated. You can see why we need an extra step through the following diagram:

<img src="https://200969929-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fe2VFH30qUL2leysQBnYJ%2Fuploads%2FTGN9QJh3zuOwxi6FXVvt%2Ffile.excalidraw.svg?alt=media&#x26;token=5493bbab-169c-4f92-b7f0-32ce38214bc3" alt="" class="gitbook-drawing">

As you can see, even though the turret motor hasn't changed the turret angle directly, the robot's change in angle has indirectly changed the turret angle.&#x20;

Therefore, when calculating how much the turret must turn to reach the correct angle you must add the robot angle, to the turret's current angle.&#x20;

By subtracting this value from the θ value calculated above, we can find the number of degrees the turrent must turn to reach the target angle.&#x20;

All in all, it would look like this in code:&#x20;

```java
double destinationAngle = Math.atan2(objectYPosition - robotYPosition, 
objectXPosition - robotXPosition); 

double toTurn = destinationAngle - (turretAngle + robotAngle);
```

#### Getting Angle Values

Now there is only one piece of the puzzle missing, finding the angle of the turrent and the angle of the robot.&#x20;

Finding the angle of the robot is trivial through the IMU (refer to the IMU section if you don't know how to do this).&#x20;

Finding the angle of the turret is also fairly simple. All you have to do is multiply the encoder tick values of the turrent in order to scale encoder ticks into degrees. Unfortunately, the only way that this scaling factor can be found is by manual tuning.&#x20;

{% hint style="info" %}
An easy way to do this is to ask your turret to turn 90 degrees. Feed different encoder tick values until you get something close to a 90-degree rotation. After that, your scaling constant should be **90/tickValue.**
{% endhint %}

All in all, your code should look something like this:&#x20;

```java
public double scalingFactor = 0.05;
 
public double encoderTicksToAngle(int ticks){
    return (ticks*scalingFactor); 
}

public int angleToEncoderTicks(double degrees){
    return (int)(degrees/scalingFactor); 
}
```

<pre class="language-java"><code class="lang-java">public double turnTurretBy(double degrees){
    double currentPosition = turretMotor.getCurrentPosition();
    double TARGET_TICK_VALUE = angleToEncoderTicks(degrees) + currentPosition;    
      
<strong>    turretMotor.setTargetPosition(TARGET_TICK_VALUE);    
</strong>    turretMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION); 
    turretMotor.setPower(1);         
}
</code></pre>

<pre class="language-java"><code class="lang-java"><strong>public void aimToObject(){
</strong><strong>    double robotYPosition = odometry.getY(), robotXPosition = odometry.getX(); 
</strong><strong>    double destinationAngle = Math.atan2(objectYPosition - robotYPosition, 
</strong><strong>    objectXPosition - robotXPosition); 
</strong><strong>    
</strong><strong>    turretAngle = encoderTicksToAngle(turretMotor.getEncoderReading());
</strong><strong>    robotAngle = getIMU(angle);  
</strong><strong>    
</strong><strong>    double toTurn = destinationAngle - (turretAngle + robotAngle);
</strong><strong>    turnTurretBy(toTurn%360); // take the mod/remainder of toTurn/360
</strong><strong>    // to keep the angle in the range of [0,360] 
</strong><strong>}
</strong></code></pre>

### Via Computer Vision

This algorithm assumes that you have a camera attached to the center of your turret mechanism such that your camera rotates with the turret.&#x20;

Prior Code Needed:

* Camera class
* An accurate TensorFlow model that detects the object you want the turrent to track.

Creating a turret alignment algorithm using computer vision is much easier to implement and understand. However, if your algorithm to detect the image target is inaccurate, then this method will fail.&#x20;

Let's start off by taking a look at the concept TensorFlow object detection file in the FTC samples:&#x20;

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

```java
double col = (recognition.getLeft() + recognition.getRight()) / 2 ;
double row = (recognition.getTop()  + recognition.getBottom()) / 2 ;
```

You will see that when your camera picks up a detection, one of the values it will return is the bounds of the bounding box which contains the object. We can calculate the center column and pow pixel of the bounding box through the code above. &#x20;

<img src="https://200969929-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fe2VFH30qUL2leysQBnYJ%2Fuploads%2FmL3wSISVKIRdwYTaj5eu%2Ffile.excalidraw.svg?alt=media&#x26;token=1f4f20c8-c4fa-474e-8a68-01fc5843d200" alt="" class="gitbook-drawing">

The objective of this algorithm is to center the camera towards either the center row or the column pixel, depending on how your turret moves. If it moves horizontally then we will be aligning with the row pixel, otherwise, we will be aligning with the column pixel.&#x20;

{% hint style="info" %}
If you are still confused about which pixel your turret should aim toward, take another look at the video above. If your turret rotates in the same fashion as the turret in the video, then your camera will be attempting to align with the center row pixel of the detected object. Otherwise, you will be aiming at the center column pixel.&#x20;
{% endhint %}

To align the camera accurately, we will be creating a PID loop, with the difference between the center pixel of the camera and the center pixel of the object's bounding box as the error input for the PID loop.&#x20;

<img src="https://200969929-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fe2VFH30qUL2leysQBnYJ%2Fuploads%2F15asWG14ebpk3Q7sI8Ll%2Ffile.excalidraw.svg?alt=media&#x26;token=bce4c99d-787a-4211-b9c3-c50bf8a64869" alt="" class="gitbook-drawing">

All in all, your aiming function would look something like this:&#x20;

{% hint style="info" %}
Note that some variable declarations have been omitted for brevity.&#x20;
{% endhint %}

```java
public void aimTurret(int pixelTarget, int cameraCenterPixel, boolean objectDetected){
  if(!objectDetected){
    // if the object is not detected then try making a full revolution in some direction. 
    //turretMotor.setPower(-0.2); 
    /*
     * If your turret motor has a limit on how much it can spin, 
     * set some kind of encoder limit on the motor such that it will
     * spin in the opposite direction when it hits the limit
     */

    if(turretMotor.getCurrentPosition()<=lowerEncoderLimit){
      turretMotor.setPower(0.2); 
    }else if(turretMotor.getCurrentPosition()>=upperEncoderLimit){
      turretMotor.setPower(-0.2); 
    }
  }else{
  // only includes proportional term for brevity, try including the other ones!
    double error = pixelTarget - cameraCenterPixel;
    double p = 0.05; 
    double output = output*p; 
    turretMotor.setPower(output); 
  }
}


```

## Case Study

Take a look at FTC team Tech Turtles and their turret which makes use of computer vision to aim at various objects:

{% embed url="https://github.com/Tech-Turtles/Power-Play/blob/dev/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/Vision/CombinedTracker.java" %}

&#x20;



