旋转传感器#

介绍#

rotation 类用于控制和访问来自 V5 旋转传感器的数据。

旋转传感器用于测量轴的旋转。它可以用于机器人上任何有轴旋转的地方,即使该轴没有直接连接到电机。

电机内置编码器,可以跟踪自身的旋转。旋转传感器的工作原理类似于外部编码器,可以跟踪独立轴的旋转。

旋转传感器可以返回轴的角度、位置和速度。角度是指轴的绝对位置,范围从 0 度到 359.99 度。当项目开始或机器人断电时,角度不会重置。位置传感器跟踪轴在项目进行期间正向或反向旋转的角度或圈数,并且可以根据需要进行重置。

旋转传感器兼容 1/8 英寸和 1/4 英寸标准 VEX 轴。

类构造函数#

rotation(
    int32_t index,
    bool    reverse = false );

类析构函数#

销毁旋转角度为 rotation 对象并释放相关资源。

virtual ~rotation();

参数#

范围

类型

描述

index

int32_t

旋转传感器连接的智能端口,记为 PORTx,其中 x 是端口号(例如, PORT1)。

reverse

bool

Optional. Whether to reverse the Rotation Sensor output:

  • true — Reverses the direction of the reported angle and position values.
  • false (default) — Reports angle and position values in the standard direction.

示例#

// Create a Rotation Sensor instance in Port 1
rotation Rotation1 = rotation(PORT1);

成员功能#

rotation 类包含以下成员函数:

  • setPosition — 将旋转传感器的当前位置设置为特定值。

  • angle — 返回轴的当前角度,范围从 0 到 359.99 度。

  • position — 返回轴旋转的角度或圈数。

  • velocity — 返回轴的旋转速度。

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

// Create a Rotation Sensor instance in Port 1
rotation Rotation1 = rotation(PORT1);

setPosition#

将旋转传感器的当前位置设置为特定值。

位置是指轴在项目进行过程中旋转的距离。设置位置会在不移动轴的情况下改变当前位置值。

Available Functions
void setPosition(
    double        value,
    rotationUnits units );

Parameters

范围

类型

描述

value

double

要分配给旋转传感器的位置值。该值可以是整数或小数。

units

rotationUnits

The position unit:

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

Return Values

此函数不返回值。

angle#

返回轴的当前角度。

角度值表示轴在一个旋转周期内的绝对位置,范围从 0 到 359.99 度。项目开始或机器人断电时,该值不会重置。

Available Functions
double angle( rotationUnits units = degrees );

Parameters

范围

类型

描述

units

rotationUnits

可选。角度单位:

  • deg / degrees (默认)— 度
  • turns / rev — 圈

Return Values

返回一个 double ,表示轴的当前角度(以指定的单位表示)。

Examples
// Get the current angle of the shaft
  double angle = Rotation1.angle(degrees);

  // Print the current angle of the shaft to the
  // Brain's screen.
  Brain.Screen.print(angle);

position#

返回轴旋转的距离。

与角度不同,位置可以超过 359.99 度,也可以低于 0 度。位置参数跟踪轴在项目进行过程中正向或反向旋转的距离,并可根据需要进行重置。

Available Functions
double position( rotationUnits units );

Parameters

范围

类型

描述

units

rotationUnits

The position unit:

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

Return Values

返回一个 double 表示轴以指定单位旋转了多少度。

velocity#

返回轴的旋转速度。

Available Functions
double velocity( velocityUnits units );

Parameters

范围

类型

描述

units

velocityUnits

速度单位:

  • rpm — 每分钟转数
  • dps — 每秒度数

Return Values

返回一个 double 表示轴以指定单位旋转的速度。