# Programming - Controlling the Arm and Wrist

## Setting up the Arm and Wrist Motors:

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FvD0cY4CKqkug1zoCqdof%2Fimage.png?alt=media&#x26;token=9a9a344b-aaa8-41fe-9545-9df387659467" alt=""><figcaption><p>Setting the mode and target position for the motors</p></figcaption></figure>

As part of our main loop, our arm and wrist motors are set to RUN\_TO\_POSITION mode with their TargetPosition set to the appropriate variable. Additionally, our arm and wrist motors are set to full power whenever they are moving.&#x20;

Because our targetArm and targetWrist values change throughout our code, we include this as part of our loop rather than initialization. &#x20;

## Preset Movements

The GAMEPAD\_INPUT\_STATE function contains all our code for controlling the arm and wrist. Let's break it down by what each button does in our If/Else statement!

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FtPLlDJ8CNTuyFOSO6hlK%2Fimage.png?alt=media&#x26;token=a4e5c391-b8df-40ec-bc12-bea146c9df87" alt=""><figcaption></figcaption></figure>

### Pressing A/Cross

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2Fsl3GEZw5bclQkvdtnmYN%2Fimage.png?alt=media&#x26;token=0553912d-4267-4e77-ad6c-8b9031a3dfca" alt=""><figcaption></figcaption></figure>

When the A/Cross button on the gamepad is pressed our currentState switches to INTAKE meaning our robot's arm will move down and wrist will unfold to be ready to pick up samples.&#x20;

### Pressing B/Circle

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FqMU4xgQQX61miXPvu3MA%2Fimage.png?alt=media&#x26;token=05b47e15-b531-4e18-a364-88aab9e2eed3" alt=""><figcaption></figcaption></figure>

This section of code allows for a togglable state between two positions of our arm/wrist when B/Circle are pressed. Because we want this to be togglable and not require the button to be held, we want to have our robot check the state of the B/Circle button each loop.&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FAOerdz2nCsmnlMBAEej0%2Fimage.png?alt=media&#x26;token=56b360f7-4478-4c86-9dc9-f1e78ef81561" alt=""><figcaption></figcaption></figure>

The lastGrab variable will change between true or false based on the state of the button. If the driver holds down the B/Circle button its state will not update again until it is first released.&#x20;

If B/Circle is pressed AND is not currently held then the robot will run a second if/then statement to determine which of the Wall positions the arm/wrist should move to.

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F583lEp8uFuWoj1S9568s%2Fimage.png?alt=media&#x26;token=ef2c0274-0708-4606-961f-2f6fb4f190c0" alt=""><figcaption></figcaption></figure>

With this statement the robot knows that if the arm is already at our WALL\_GRAB preset it should move to unhook. The opposite is also true where if the arm/wrist is in the WALL\_UNHOOK preset it will go back to grab.&#x20;

### Pressing Y/Triangle

This section functions similarly to B/Circle providing a togglable control for clipping a specimen!

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FTKEoh7El6MuoYv2cLpSg%2Fimage.png?alt=media&#x26;token=f990a7ca-6462-4541-8746-ea42d7ad8213" alt=""><figcaption></figcaption></figure>

In this case lastHook allows the robot to determine if the Y/Triangle button is being held before the robot moves between the two positions.&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F2Gng3GZopjn6KlZ8oBji%2Fimage.png?alt=media&#x26;token=97f75db8-4a7b-43c8-805a-2cf98f380206" alt=""><figcaption></figcaption></figure>

### Pressing X/Square

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FOhh7FIqAGOFEBjDqOKzM%2Fimage.png?alt=media&#x26;token=2321b734-db06-47ab-9d56-26095f8c4939" alt=""><figcaption></figcaption></figure>

When the X/Square button on the gamepad is pressed our currentState switches to LOW\_BASKET meaning our robot's arm will move up and wrist will unfold to be ready to deposit samples in the low basket.

### Pressing Left Bumper

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F25EsRLmIw5BRdTD5HH0S%2Fimage.png?alt=media&#x26;token=efee8462-e573-4af0-8340-5938071a2d78" alt=""><figcaption></figcaption></figure>

It's always good to have a way to reset our robot if needed! By pressing left bumper, currentState will be set to INIT moving our robot back to its initilization configuration, like what might be used at the start of a match.&#x20;

## Manual Control

Beyond our preset movements, we want our robot to have refined control for the arm and wrist as we navigate the field.

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F4qwmiJJgX71rzLxVUMSr%2Fimage.png?alt=media&#x26;token=b0b6af08-a323-4665-b4dc-9a72c653584c" alt=""><figcaption></figcaption></figure>

Whenever we press a button on the d-pad our currentState will switch to MANUAL allowing the arm or wrist to move in increments until the button is released.&#x20;

Because we have our motors set to "RUN\_TO\_POSITION" mode we can't just turn the power on for manual control. Instead we have the robot changing the position in the appropriate direction in chunks. Depending our your driver's preference, you may choose to adjust these values for quicker or more refined control!&#x20;

{% hint style="info" %}
By default, these position values are different in the OnBot Java and Blocks version of the provided code. This is due to a difference in how quickly OnBot Java loops through a program compared to Blocks.
{% endhint %}
