伺服电机#

介绍#

servo 类用于控制和配置 V5 三线伺服电机。V5 三线伺服电机可在固定的运动范围内提供基于位置的控制。

VEX 三线伺服电机

类构造函数#

servo( triport::port& port );

类析构函数#

销毁 servo 对象并释放相关资源。

~servo();

参数#

范围

类型

描述

port

triport::port&

伺服电机连接的 3 线端口,写成 Brain.ThreeWirePort.X 或 ExpanderName.X,其中 X 是端口字母(例如, Brain.ThreeWirePort.A 或 Expander1.A)。

笔记#

  • 必须先创建 Brain 或 3-Wire Expander,然后才能使用 servo 类构造函数创建对象。

例子#

// Create the servo instance on 3-Wire Port A
servo myServo = servo(Brain.ThreeWirePort.A);

成员功能#

servo 类包含以下成员函数:

  • setPosition — 将伺服电机移动到指定位置。

在调用任何 servo 成员函数之前,必须先创建一个 servo 实例,如下所示:

/* This constructor is required when using VS Code.
Servo configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */

// Create the servo instance on 3-Wire Port A
servo myServo = servo(Brain.ThreeWirePort.A);

setPosition#

将伺服电机移动到指定位置。

伺服电机示意图,显示其旋转角度,从左侧的 100 度到中间的 50 度,再到最右侧的 0 度。

Available Functions

1 — 使用百分比值设置伺服位置。

void setPosition(
 int32_t value,
 percentUnits units );

2 — 使用旋转单位设置伺服位置。

void setPosition(
 double value,
 rotationUnits units );

Parameters

范围

类型

描述

value

int32_t

使用百分比单位表示的伺服位置值。

value

double

使用旋转单元时的伺服位置值。

units

percentUnits

位置单位: percent / pct%

units

rotationUnits

The position unit:

  • deg / degrees — degrees
  • turns / rev — revolutions

Return Values

此函数不返回值。

Notes
  • 使用百分比单位时,有效范围为 0 – 100%。

  • 伺服电机在开始每个项目之前,都会自动将自身调整到 50 度中心位置,然后再进行任何运动。

  • 使用旋转单元时,伺服电机将移动到指定的绝对位置。

Examples
// Set the servo position to 10 percent
myServo.setPosition(10, percent);

// Set the servo position to 45.5 degrees
myServo.setPosition(45.5, degrees);

// Set the servo position to 90 degrees
myServo.setPosition(90, degrees);