# Commanding Servos

## Servo Control Basics

Servo motors are specialized motors that can be controlled to move to a specific angle instead of continuously rotating like a DC motor. For more general information on servos, see [this page](https://app.gitbook.com/s/H9K1InCLC1ZxIkdPJt31/motion/servos#servo-basics); for more detailed information, see <https://en.wikipedia.org/wiki/Servo_control>.

## Setting up Servo Hub

Servo control in REVLib is accessed through the ServoHub's object. This object manages the individual ServoChannel objects and monitors the overall device. The ServoChannel object controls each servo motor and monitors its operation. It also contains all the methods to configure and control your servo. It can be accessed as shown below:

{% tabs %}
{% tab title="Java" %}

```java
// Initialize the servo hub
ServoHub m_servoHub = new ServoHub(deviceID);

// Obtain a servo channel controller
ServoChannel m_channel0 = m_servoHub.getServoChannel(ChannelId.kChannelId0);
ServoChannel m_channel1 = m_servoHub.getServoChannel(ChannelId.kChannelId1);
...
ServoChannel m_channel5 = m_servoHub.getServoChannel(ChannelId.kChannelId5);
```

API Docs: [ServoHub](https://codedocs.revrobotics.com/java/com/revrobotics/servohub/servohub), [ServoChannel](https://codedocs.revrobotics.com/java/com/revrobotics/servohub/servochannel)
{% endtab %}

{% tab title="C++" %}

```cpp
using namespace rev::servohub;
using namespace rev::servohub::ServoChannel;

// Initialize the servo hub
ServoHub m_servoHub{ deviceID };

//Obtain a reference to a servo channel controller
ServoChannel& m_channel0 = m_servoHub.GetServoChannel(ChannelId.kChannelId0);
ServoChannel& m_channel1 = m_servoHub.GetServoChannel(ChannelId.kChannelId1);
...
ServoChannel& m_channel5 = m_servoHub.GetServoChannel(ChannelId.kChannelId5);
```

API Docs: [ServoHub](https://codedocs.revrobotics.com/cpp/classrev_1_1servohub_1_1_servo_hub), [ServoChannel](https://docs.revrobotics.com/revlib/servo-hub/broken-reference)
{% endtab %}
{% endtabs %}

## Setting the Servo Pulse Period

With a servo motor, the width of the pulse will determine how far the motor turns. The Pulse Period, on the other hand, will determine how often the pulse is sent to the servo. The ServoHub supports a Pulse Period of 4.5 - 20ms (specified in microseconds). The ServoHub supports a separate Pulse Period for each bank, composing servos 0-2 and 3-5, respectively. These settings can be accessed as shown below:

{% tabs %}
{% tab title="Java" %}

```java
// Set the pulse period for channels 0-2 to 5ms (5000 microseconds)
m_servoHub.setBankPulsePeriod(ServoHub.Bank.kBank0_2, 5000);

// Set the pulse period for channels 3-5 to 20ms (20000 microseconds)
m_servoHub.setBankPulsePeriod(ServoHub.Bank.kBank3_5, 20000);
```

API Docs: [setBankPulsePeriod](https://codedocs.revrobotics.com/java/com/revrobotics/servohub/servohub#setBankPulsePeriod\(com.revrobotics.servohub.ServoHub.Bank,int\))
{% endtab %}

{% tab title="C++" %}

```cpp
using namespace rev::servohub;

// Set the pulse period for channels 0-2 to 5ms (5000 microseconds)
m_servoHub.SetBankPulsePeriod(ServoHub::Bank::kBank0_2, 5000);

// Set the pulse period for channels 3-5 to 20ms (20000 microseconds)
m_servoHub.SetBankPulsePeriod(ServoHub::Bank::kBank3_5, 20000);
```

API Docs: [SetBankPulsePeriod](https://codedocs.revrobotics.com/cpp/classrev_1_1servohub_1_1_servo_hub#a41907c0a14d75b30c6819693b6953370)
{% endtab %}
{% endtabs %}

## Controlling an Individual Servo

Individual servos are controlled via the ServoChannel objects. As shown above, you obtain a reference to a ServoChannel object by calling the getServoChannel() method on the ServoHub. You may set the following on a ServoChannel:

* PulseWidth - determines the servo's position (500 - 2500 microseconds).
* Enabled - enables/disables the servo - when disabled, the servo will maintain power according to the DisableBehavior configured for the specific channel.
* Powered - turns on/off the power to the servo

{% tabs %}
{% tab title="Java" %}

<pre class="language-java"><code class="lang-java"><strong>// Power on channels 0, 1, and 4
</strong>m_channel0.setPowered(true);
m_channel1.setPowered(true);
m_channel4.setPowered(true);

// Enabled them as well
m_channel0.setEnabled(true);
m_channel1.setEnabled(true);
m_channel4.setEnabled(true)

// Set the servo on channel 0 to the center (1500 microseconds)
m_channel0.setPulseWidth(1500);

// Set the servo on channel 1 to the far left (500 microseconds)
m_channel1.setPulseWidth(500);

// Set the servo on channel 4 to the far right(2500 microseconds)
m_channel4.setPulseWidth(2500);
</code></pre>

API Docs: [setPulseWidth](https://codedocs.revrobotics.com/java/com/revrobotics/servohub/servochannel#setPulseWidth\(int\)), [setPowered](https://codedocs.revrobotics.com/java/com/revrobotics/servohub/servochannel#setPowered\(boolean\)), [setEnabled](https://codedocs.revrobotics.com/java/com/revrobotics/servohub/servochannel#setPowered\(boolean\))
{% endtab %}

{% tab title="C++" %}

```cpp
using namespace rev::servohub;

// Power on channels 0, 1, and 4
m_channel0.SetPowered(true);
m_channel1.SetPowered(true);
m_channel4.SetPowered(true);

// Enabled them as well
m_channel0.SetEnabled(true);
m_channel1.SetEnabled(true);
m_channel4.SetEnabled(true)

// Set the servo on channel 0 to the center (1500 microseconds)
m_channel0.SetPulseWidth(1500);

// Set the servo on channel 1 to the far left (500 microseconds)
m_channel1.SetPulseWidth(500);

// Set the servo on channel 4 to the far right(2500 microseconds)
m_channel4.SetPulseWidth(2500);
```

API Docs: [SetPulseWidth](https://codedocs.revrobotics.com/cpp/classrev_1_1servohub_1_1_servo_channel#aba895cccc035836f0d3249f3f77c5362), [SetEnabled](https://codedocs.revrobotics.com/cpp/classrev_1_1servohub_1_1_servo_channel#a64ed852b293594cf83564d5c81316097), [SetPowered](https://codedocs.revrobotics.com/cpp/classrev_1_1servohub_1_1_servo_channel#a0f8df65143a581f029d356e9ed8430dc)
{% endtab %}
{% endtabs %}


---

# 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/revlib/servo-hub/commanding-servos.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.
