# Programming - Autonomous Code

Once our OpMode is selected, we're ready to actually hit play and let the robot run! For this example, the [TeleOp portion of the code ](https://docs.revrobotics.com/ftc-kickoff-concepts/decode-2025-26/programming-teleop)has remained the same so we won't repeat looking at it here.&#x20;

Instead let's break down our autonomous code options. For both Blue and Red Alliance versions of the code, the robot is intended to start against the goal while touching the launch line. It will automatically fire the pre-loaded balls for 10 seconds before backing up from the goal, turning, and driving straight back off the line.&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FvUkAt6F99lAnUcgOMvMR%2Fimage.png?alt=media&#x26;token=af094fa6-48a9-4cd8-9de7-5e84a92aa26a" alt=""><figcaption><p>Auto Blue Alliance</p></figcaption></figure>

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2Fw8t0NoqeBFLv4QEkeVTI%2Fimage.png?alt=media&#x26;token=5404703b-5e7d-4e85-8deb-8e64839a0f02" alt=""><figcaption><p>Auto Red Alliance</p></figcaption></figure>

The two versions of the code are nearly identical with the difference being which direction the robot needs to turn before backing up.&#x20;

<div><figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FLbGF7oDHNbKw9MtEeBEA%2Fimage.png?alt=media&#x26;token=5480d112-3b00-4a09-ac63-3c35dffdc27e" alt="" width="299"><figcaption><p>Turn Blue</p></figcaption></figure> <figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FCXi6V71wQH11hCiovbhf%2Fimage.png?alt=media&#x26;token=1e291c25-789b-4b5b-92ce-d7657922d053" alt=""><figcaption><p>Turn Red</p></figcaption></figure></div>

Let's take a closer look at how our auto code works.

## Running Auto

### Launching Balls

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FvFJmUuGh4ltFqUqZUBJT%2Fimage.png?alt=media&#x26;token=82bd10c4-90ae-4ccf-963e-8095b74a579f" alt=""><figcaption><p>Code for launching balls for 10 seconds</p></figcaption></figure>

Our entire auto is contained to an if/else statement checking if opModeIsActive is true, meaning the "play" button has been pressed on the Driver Hub. Once this occurs, it'll read out that the OpMode is running.&#x20;

To start launching balls into the target, the associated timer will first be reset. Elapsedtimers begin counting at their creation so it is important to reset them when they're actually going to be used to get a correct time.&#x20;

With the timer running, the "bankShotAuto", the same used in TeleOp, will begin running for 10 seconds. This time can be adjusted, but was set to 10 seconds in the example as a safe window for teams to begin testing with and observing how their robot acts.&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FzbJXSqrZvsYkcqL2Oy4q%2Fimage.png?alt=media&#x26;token=fb62a3a7-7e5a-4f9f-a2aa-16c28b8c45d9" alt=""><figcaption><p>10 second timer for launching in autonomous</p></figcaption></figure>

{% hint style="info" %}
When using a "whileLoop" in Blocks a call of if the "opModeIsActive" is required for the loop to properly run.
{% endhint %}

A telemetry readout to the Driver Hub will show how much time has passed according to the "autoLaunchTimer" to aid with making adjustments.

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F3TNpnN8yQFiMjXiuBOQB%2Fimage.png?alt=media&#x26;token=d76b5412-5c28-4b40-8b3e-4f4b45e5eaed" alt=""><figcaption><p>Telemetry readout for the launcher timer</p></figcaption></figure>

Once our timer is up, all our actuators will be set back to 0 power or velocity.

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FDjpf92PZsEBSG1Fx6Iy1%2Fimage.png?alt=media&#x26;token=b7447b16-e02b-475f-82f2-faddd5a6a21c" alt=""><figcaption><p>Setting actuators back to 0</p></figcaption></figure>

### Driving in Auto

There are two parts to our program for the robot driving in autonomous. In our if/else statement, we have a portion of the "autoDrive" function taking in inputs to calculate the robot's movements. Let's looking at the function itself first.

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FRRI6SRhUBecJmzuWMVPQ%2Fimage.png?alt=media&#x26;token=0f0cad49-ff39-428a-a633-62d1e6233d90" alt=""><figcaption><p>Full autoDrive code</p></figcaption></figure>

Here our elapsed timer will reset to be ready for use with the robot's movements. Then the drivetrain motors are given a target position. This target position is calculated using the current position of the motor, the inputted distance in inches from the main function, and our conversion set up during initialization.&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FM48n6P5t2dsKqgjoeXAt%2Fimage.png?alt=media&#x26;token=c746fb9d-df18-488e-bb6b-0f57d3e78fe5" alt=""><figcaption><p>Calculating drivetrain movements using the motors' encoders</p></figcaption></figure>

Next, we're change our motors to "RUN\_TO\_POSITION" mode for this autonomous. The power to be provided is set by the "speed" given in the main function.&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FCrxnNggOEJC11SoNYCUh%2Fimage.png?alt=media&#x26;token=d4b906ae-e738-4632-b181-b8b28a2e2cf1" alt=""><figcaption><p>Updating the mode and power for the drivetrain motors</p></figcaption></figure>

With all that information gathered, our robot will now moved the specified direction and distance stopping when either the motors halt or the timer runs out. Using "call idle" allows our program to progress between the two functions.&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2Fxhe7HO40sWOR4axG8puS%2Fimage.png?alt=media&#x26;token=c0a318fe-791f-445c-8f88-38c9ca6483ec" alt=""><figcaption><p>Checking that the motors are active and the timer has not run out</p></figcaption></figure>

Once our movement is complete, our drivetrain motors are set to turn to 0 power and reset back to "RUN\_WITHOUT\_ENCODER" in preparation for TeleOp.

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FZrJI1H4Pc9eeTAOy3VO9%2Fimage.png?alt=media&#x26;token=f479364e-1829-40d2-902a-f9a04bfd0c9f" alt=""><figcaption><p>Updating the mode and power for the draintrain motors after running</p></figcaption></figure>

Now let's look at the remainder of our main function of "doAuto".

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2Fs1at3dxSaLfEDldZk2ka%2Fimage.png?alt=media&#x26;token=30c21a89-13a2-4179-9e9e-fc7806999ef3" alt=""><figcaption><p>Movements of the robot in autonomous</p></figcaption></figure>

For each portion of movement we have inputted:

* **speed**- The set power for the motors&#x20;
* **leftDistanceInch/rightDistanceInch**- The target distance we want our robot to move to be calculated with our equation. This will be different for each motor when the robot is turning.
* **timeout\_ms**- this is the maximum time the elapsedtimer can count up to in milliseconds.&#x20;

Let's take a look at each step of the robot's movement

#### Back Up

The robot will first back away from the goal, at half power, roughly 12 inches. If something happens where the robot cannot complete this, such as colliding with another robot, this step will timeout after 5 seconds.

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FeozK3D2eQvi3CdBuSmni%2Fimage.png?alt=media&#x26;token=a53313b2-663e-4090-9c7c-9205fc1543e6" alt=""><figcaption><p>Code for the robot backing up</p></figcaption></figure>

#### Turn

Next the robot will turn at half power about 6 inches making a 90 degree angle. If something happens where the robot cannot complete this, such as colliding with another robot, this step will timeout after 5 seconds.

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FhSrXmQRBFi0kkZQbdB9g%2Fimage.png?alt=media&#x26;token=182256e0-c67f-4c73-8f07-117671a5217f" alt=""><figcaption><p>Code for the robot turning</p></figcaption></figure>

#### Drive Off Line

Finally the robot will back up at full power 50 inches to be off the line. This is much further than needed to be off the line so we encourage teams to adjust this value to get the robot positioned where they prefer.&#x20;

If something happens where the robot cannot complete this, such as colliding with another robot, this step will timeout after 5 seconds.

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F4yB8Q22rRT8CpbRQGtmb%2Fimage.png?alt=media&#x26;token=3ae09ad0-319d-4a00-a86d-fb0a7ca0386e" alt=""><figcaption><p>Code for the robot driving off the line</p></figcaption></figure>

Once the robot has finished moving, the code will stop and return to the initialization option.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.revrobotics.com/ftc-kickoff-concepts/decode-2025-26/programming-teleop/programming-autonomous-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
