> For the complete documentation index, see [llms.txt](https://marrow.skeletonarmyftc.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://marrow.skeletonarmyftc.com/automations/retrycommand.md).

# RetryCommand

package com.skeletonarmy.marrow.\<command\_library>

## Overview

`RetryCommand` is a powerful command for building adaptable and reliable command sequences. It executes a command and, if the specified condition is not met upon completion, automatically retries up to the specified amount of times.

This is especially useful for actions that may fail on the first attempt, such as vision-based alignment, object grabbing, or precise mechanism positioning.

Check out the ["Retries" page](https://marrow.skeletonarmyftc.com/concepts/retries.md) to learn how to use `RetryCommand` for autonomous behaviors.

{% hint style="info" %}
**Available in:**

🟢 [SolversLib](https://docs.seattlesolvers.com/command-base/command-system/convenience-commands#retrycommand) - included natively (Marrow not required)\
🟠 [FTCLib](https://docs.ftclib.org/ftclib/) - provided through Marrow\
🟠 [NextFTC](https://nextftc.dev/) - provided through Marrow\
🟠 [Ivy](https://pedropathing.com/docs/ivy) - provided through Marrow
{% endhint %}

***

## Usage

### Setup

To use `RetryCommand`, you need to provide the constructor with:

* **Command to run** - the initial action you want to execute.
* **(Optional) Alternative command to run on retries** - lets you customize the retry behavior per attempt (e.g., switching to a vision-assisted command if the initial attempt fails).
* **Success condition** - a boolean supplier that checks whether the action was successful. If this condition returns `false`, the command will be retried.
* **Maximum number of retries** - defines the maximum number of times the command can be retried.

```java
new RetryCommand(
    new GrabCommand(claw),   // Command to run.
    () -> claw.isGrabbed(),  // If this condition is false
    5                        // retry up to 5 times.
)
```

Or with an alternative retry command:

```java
new RetryCommand(
    new GrabCommand(claw),                  // Command to run initially.
    new DetectAndGrabCommand(claw, vision), // Command to run on each retry.
    () -> claw.isGrabbed(),                 // If this condition is false
    5                                       // retry up to 5 times.
)
```

***

## Example Usage

Here’s an example autonomous that uses `RetryCommand` together with SolversLib’s command system:

{% hint style="info" %}
This is by no means a functional autonomous program.
{% endhint %}

```java
@Autonomous
public class MyAuto extends CommandOpMode {
    private ClawSubsystem clawSubsystem;
    private VisionSubsystem visionSubsystem;

    @Override
    public void initialize() {
        clawSubsystem = new ClawSubsystem(hardwareMap);
        visionSubsystem = new VisionSubsystem(hardwareMap);

        schedule(
            new SequentialCommandGroup(
                // First, try to grab. If unsuccessful, retry grabbing up to 3 times.
                new RetryCommand(
                    new GrabCommand(clawSubsystem),
                    () -> clawSubsystem.isHoldingGameElement(),
                    3
                ),
                
                // ---- OR ----
                
                // First, try to grab. If unsuccessful, try to detect and grab using vision up to 3 times.
                new RetryCommand(
                    new GrabCommand(clawSubsystem),
                    new DetectAndGrabCommand(clawSubsystem, visionSubsystem), 
                    () -> clawSubsystem.isHoldingGameElement(),
                    3
                )
            )
        );
    }
}
```

***

## Source Code

As with all of Marrow's code, you are more than welcome to view it in our [GitHub repository](https://github.com/Skeleton-Army/Marrow/tree/main/customLibraries).

If you are using an unsupported command-based library and want to implement this command in your codebase, you are welcome do so.

If you wish to add to it Marrow, please submit a Pull Request.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://marrow.skeletonarmyftc.com/automations/retrycommand.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
