> For the complete documentation index, see [llms.txt](https://acezxn.gitbook.io/vex-ji-qi-ren-cheng-shi-jiao-xue/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://acezxn.gitbook.io/vex-ji-qi-ren-cheng-shi-jiao-xue/ji-chu-jiao-xue/ma-da-kong-zhi.md).

# 馬達控制

## 加入馬達

### PROS

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

```cpp
pros::Motor motor(MOTOR_PORT);
```

範例：

```cpp
pros::Motor motor1(1); // 數字代表port號碼
pros::Motor motor2(-2); // 負號表示反轉
```

{% endtab %}

{% tab title="Okapilib" %}

```cpp
okapi::Motor motor(MOTOR_PORT);
```

範例：

```cpp
okapi::Motor motor1(1); // 數字代表port號碼
okapi::Motor motor2(-2); // 負號表示反轉
```

{% endtab %}
{% endtabs %}

在介面中選擇並設定馬達，之後在程式中直接呼叫馬達名就可以了。

## 控制馬達轉動

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

```cpp
// move with specific voltage
motor.move_voltage(std::int32_t voltage);

// move with specific velocity
motor.move_velocity(std::int32_t velocity);

// move to a relative position
motor.move_relative(double position, std::int32_t velocity);

// move to an absolute position
motor.move_absolute(double position, std::int32_t velocity);

// 更多指令：https://pros.cs.purdue.edu/v5/api/cpp/motors.html#move-velocity
```

{% endtab %}

{% tab title="Okapilib" %}

```cpp
// move with specific voltage
motor.moveVoltage(std::int32_t voltage);

// move with specific velocity
motor.moveVelocity(std::int32_t velocity);

// move to a relative position
motor.moveRelative(double position, std::int32_t velocity);

// move to an absolute position
motor.moveAbsolute(double position, std::int32_t velocity);

// 更多指令：https://okapilib.github.io/OkapiLib/classokapi_1_1AbstractMotor.html#ae811cd825099f2defadeb1b7f7e7764c
```

{% endtab %}
{% endtabs %}

## 設定馬達煞車模式

總共有三種brake mode

* coast: 不出力，讓機器滑行（預設模式）
* brake: 煞車使馬達減速
* hold: 出力把馬達鎖在一個角度

在控制底盤移動時，會有機器需要停下來的情況。使用brake煞車模式可以讓機器很快的從運動中停下來。另外也有些情況需要馬達出力把部分元件固定在某的位置，這時也可以用hold煞車模式。

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

<pre class="language-cpp"><code class="lang-cpp"><strong>motor.set_brake_mode(pros::motor_brake_mode_e_t mode)
</strong></code></pre>

{% endtab %}

{% tab title="Okapilib" %}

<pre class="language-cpp"><code class="lang-cpp"><strong>motor.setBrakeMode(brakeMode mode)
</strong></code></pre>

{% endtab %}
{% endtabs %}

## 遙控底盤

要遙控底盤，需要一個無限迴圈，每隔固定的時間就執行讓底盤馬達以一個速度轉動的程式。而馬達轉動的速度就取決於搖桿的數值。詳情請見：

{% content-ref url="/pages/mxYhNVsEaBXjGzgq8HBy" %}
[基礎底盤控制](/vex-ji-qi-ren-cheng-shi-jiao-xue/fan-li-cheng-shi/ji-chu-di-pan-kong-zhi.md)
{% endcontent-ref %}
