伺服电机#

介绍#

The servo class is used to control and configure a V5 3-Wire Servo Motor. The V5 3-Wire Servo Motor provides position-based control within a fixed range of motion.

VEX 三线伺服电机

类构造函数#

servo( triport::port& port );

类析构函数#

Destroys the servo object and releases associated resources.

~servo();

参数#

范围

类型

描述

port

triport::port&

The 3-Wire Port that the Servo Motor is connected to, written as Brain.ThreeWirePort.X or ExpanderName.X, where X is the port letter (for example, Brain.ThreeWirePort.A or Expander1.A).

笔记#

  • A Brain or 3-Wire Expander must be created first before they can be used to create an object with the servo class constructor.

例子#

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

成员功能#

The servo class includes the following member functions:

  • setPosition — Moves the servo motor to a specified position.

Before calling any servo member functions, a servo instance must be created, as shown below:

/* 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

The position unit: percent / pct — percent

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);