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

类构造函数#
servo( triport::port& port );
类析构函数#
销毁 servo 对象并释放相关资源。
~servo();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
伺服电机连接的 3 线端口,写成 |
笔记#
必须先创建 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#
将伺服电机移动到指定位置。

1 — 使用百分比值设置伺服位置。
void setPosition( int32_t value, percentUnits units );
Parameters2 — 使用旋转单位设置伺服位置。
void setPosition( double value, rotationUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
使用百分比单位表示的伺服位置值。 |
|
|
使用旋转单元时的伺服位置值。 |
|
|
位置单位: |
|
|
The position unit:
|
此函数不返回值。
Notes使用百分比单位时,有效范围为 0 – 100%。
伺服电机在开始每个项目之前,都会自动将自身调整到 50 度中心位置,然后再进行任何运动。
使用旋转单元时,伺服电机将移动到指定的绝对位置。
// 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);