旋转传感器#
介绍#
The rotation class is used to control and access data from an EXP Rotation Sensor.
旋转传感器用于测量轴的旋转。它可以用于机器人上任何有轴旋转的地方,即使该轴没有直接连接到电机。
电机内置编码器,可以跟踪自身的旋转。旋转传感器的工作原理类似于外部编码器,可以跟踪独立轴的旋转。
旋转传感器可以返回轴的角度、位置和速度。角度是指轴的绝对位置,范围从 0 度到 359.99 度。当项目开始或机器人断电时,角度不会重置。位置传感器跟踪轴在项目进行期间正向或反向旋转的角度或圈数,并且可以根据需要进行重置。
旋转传感器兼容 1/8 英寸和 1/4 英寸标准 VEX 轴。
类构造函数#
rotation(
int32_t index,
bool reverse = false );
类析构函数#
Destroys the rotation object and releases associated resources.
virtual ~rotation();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
The Smart Port that the Rotation Sensor is connected to, written as |
|
|
Optional. Whether to reverse the Rotation Sensor output:
|
示例#
// Create a Rotation Sensor instance in Port 1
rotation Rotation1 = rotation(PORT1);
成员功能#
The rotation class includes the following member functions:
setPosition— Sets the Rotation Sensor’s current position to a specific value.angle— Returns the shaft’s current angle from 0 to 359.99 degrees.position— Returns how far the shaft has rotated in degrees or turns.velocity— Returns how fast the shaft is rotating.
Before calling any rotation member functions, a rotation instance must be created, as shown below:
/* This constructor is required when using VS Code.
Rotation Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create a Rotation Sensor instance in Port 1
rotation Rotation1 = rotation(PORT1);
setPosition#
将旋转传感器的当前位置设置为特定值。
位置是指轴在项目进行过程中旋转的距离。设置位置会在不移动轴的情况下改变当前位置值。
Available Functionsvoid setPosition(
double value,
rotationUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要分配给旋转传感器的位置值。该值可以是整数或小数。 |
|
|
The position unit:
|
此函数不返回值。
angle#
返回轴的当前角度。
角度值表示轴在一个旋转周期内的绝对位置,范围从 0 到 359.99 度。项目开始或机器人断电时,该值不会重置。
Available Functionsdouble angle( rotationUnits units = degrees );
范围 |
类型 |
描述 |
|---|---|---|
|
|
Optional. The angle unit:
|
Returns a double representing the shaft’s current angle in the specified units.
// 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 Functionsdouble position( rotationUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The position unit:
|
Returns a double representing how far the shaft has rotated in the specified units.
velocity#
返回轴的旋转速度。
Available Functionsdouble velocity( velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The velocity unit:
|
Returns a double representing how fast the shaft is rotating in the specified units.