# Programming - Flywheel Control

All the flywheel controls are contained to the function setFlywheelVelocity.

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F2MhsWLvDZZqYWwXOGFEj%2Fimage.png?alt=media&#x26;token=9a0fc0a0-b56d-4261-a840-22af5a2e5b6a" alt=""><figcaption><p>Flywheel if/else statement for auto and manual controls</p></figcaption></figure>

The flywheel has two main forms of operation:

* **Manual Control-** This will spin up ONLY the flywheel to the target velocity. The driver can then manually spin the Core Hex to feed balls.
* **Auto Control**- This will spin up the flywheel and activate the agigtator servo. Once the flywheel is in a specified range of the target velocity, the Core Hex will being to feed balls automatically. This is intended to be for a fully loaded robot to be able to make multiple shots in quick succession

Let's take a look at our entire if/else statement before exploring our Auto Control options.&#x20;

### Flywheel Control

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F3tTN5Ca5kT0GTIWPlUbw%2Fimage.png?alt=media&#x26;token=ca7744a4-671c-43b8-b64c-7f0e84d743ac" alt=""><figcaption><p>Breakdown of flywheel buttons</p></figcaption></figure>

When holding the **"option" button** on the gamepad, the flywheel will spin in reverse at half power. This is intended to help with freeing stuck balls if needed.&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2Fal5IWqyvJXyRFFdyOKZW%2Fimage.png?alt=media&#x26;token=19059e2a-038c-405a-9849-61f6c9275902" alt=""><figcaption><p>Reversing the flywheel</p></figcaption></figure>

The **left bumper** or **right bumper** on the gamepad will request their specified function to run. We'll discuss these more below!

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FvFW7OewqzFYa7BVEvgx1%2Fimage.png?alt=media&#x26;token=52cec192-5950-44b7-ae37-cdc7b0f5c8c3" alt=""><figcaption><p>Activating the flywheel launcher's "auto" modes</p></figcaption></figure>

When pressing the **"circle" button**, the flywheel will spin up to the set velocity for a "bank shot". This is the velocity for launching into the goal while against the goal or a couple inches back.&#x20;

When pressing the **"square" button**, the flywheel will spin up to the set "max" velocity. This may be used for launching balls at the goal with adjustments to the deflector, however is intended mostly for teams wanting to explore working with the flywheel and differences in velocity.&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FTOrt66psKlVj4vS72QUv%2Fimage.png?alt=media&#x26;token=32b44b43-140a-4032-8346-2582a04e2a71" alt=""><figcaption><p>Manual control for the flywheel</p></figcaption></figure>

{% hint style="info" %}
Be aware that launching balls at higher velocity can launch them to ceiling height. We do not recommend using this velocity option in rooms with low ceilings or hanging lights.&#x20;
{% endhint %}

### Turning off Actuators

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FJpdPVUXlyn7BlEg3VAzU%2Fimage.png?alt=media&#x26;token=d4cb5a62-6544-4338-b160-8d3cce0a5207" alt=""><figcaption><p>Setting all actuators to 0 power or velocity</p></figcaption></figure>

If no button is actively pressed on the gamepad, the flywheel, Core Hex feeder, and servo are told to set power or velocity to 0.&#x20;

The servo has an additional check to allow manual control on the Dpad to override this function and prevent stuttering in the movements.

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FUBH7XagOa4iTLv01pHo9%2Fimage.png?alt=media&#x26;token=570f28f1-50f4-4a0b-bb7b-2925515cf507" alt=""><figcaption><p>Check to prevent the servo from stuttering</p></figcaption></figure>

## Auto Launching with the Flywheel

While a bumper is pressed on the gamepad, the servo, Core Hex feeder, and flywheel will all activate with the intent to launch multiple balls in succession.&#x20;

### Bank "Near" Shot Auto

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FHW7u7NFNyWrX7ceZRvAW%2Fimage.png?alt=media&#x26;token=99a1dd3c-ef93-455a-bb9e-a09d91625565" alt=""><figcaption><p>Code for the bank or "near" shot launching</p></figcaption></figure>

When **right bumper** is held, the above sequence will run until release. This is intended for launching balls into the goal from against the goal or a couple inches back.

The flywheel is set to spin to the preset "bankVelocity" continously and the agigtator servo will activate.&#x20;

Lastly, the robot will check first if the velocity of the flywheel is within 50 ticks below the "bankVelocity" before it will allow balls to fire. This value can be adjusted to be a bigger or tighter window if you notice the Core Hex is not feeding as expected.&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2Fup7MdJZsP3eGYVYhVt5Y%2Fimage.png?alt=media&#x26;token=8c2a0588-6764-43fd-a71b-4ff57d086041" alt=""><figcaption><p>Check to allow the flywheel to reach velocity between launches</p></figcaption></figure>

### "Far" Power Auto

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FV32Cu3H9DNFrOTQ6SbKp%2Fimage.png?alt=media&#x26;token=3ad6f2bd-53dd-414f-9289-b3ceb457e9a2" alt=""><figcaption><p>Far power autonomous launching code</p></figcaption></figure>

When the left bumper is held, the above sequence will run until release. This is intended for launching balls into the goal from a few feet back from the goal. This may require adjustments to the deflector if teams prefer this approach.&#x20;

The flywheel is set to spin to the preset "farVelocity" continously and the agigtator servo will activate.

Similar to the bank auto, the robot will run a check to see if the flywheel velocity is currently within a specified window of the target "farVelocity" before it begins feeding. The default range is higher due to the increased velocity, but may be adjusted for further refinement.

{% hint style="info" %}
In the default code example, there is not a check for if the flywheel's velocity is above the "bankVelocity" or "farVelocity"  targets. Teams may consider adding this for additional refinement. Keep an eye out on our upgrades page for more information!
{% endhint %}
