旋转传感器#
介绍#
旋转传感器用于测量轴的旋转。它可以用于机器人上任何有轴旋转的地方,即使该轴没有直接连接到电机。
电机内置编码器,可以跟踪自身的旋转。旋转传感器的工作原理类似于外部编码器,可以跟踪独立轴的旋转。
旋转传感器可以返回轴的角度、位置和速度。角度是指轴的绝对位置,范围从 0 度到 359.99 度。当项目开始或机器人断电时,角度不会重置。位置传感器跟踪轴在项目进行期间正向或反向旋转的角度或圈数,并且可以根据需要进行重置。
旋转传感器兼容 1/8 英寸和 1/4 英寸标准 VEX 轴。

This page uses rotation_sensor as the example Rotation Sensor name. Replace it with your own configured name as needed.
以下是可用方法列表:
set_position— 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.reset_position— Resets the Rotation Sensor’s current position to 0.set_reversed— Sets whether the Rotation Sensor’s output is reversed.changed— Registers a function to run whenever the Rotation Sensor’s value changes.
构造函数 — 手动初始化旋转传感器。
Rotation— Creates a Rotation Sensor.
设置位置#
set_position sets the Rotation Sensor’s current position to a value.
位置是指轴在项目进行过程中旋转的距离。设置位置会在不移动轴的情况下改变当前位置值。
Usage:
rotation_sensor.set_position(position, units)
参数 |
描述 |
|---|---|
|
要分配给旋转传感器的位置值。该值可以是整数或小数。 |
|
Optional. The position unit: |
# Set the Rotation Sensor to 2.5 Turns
rotation_sensor.set_position(2.5, TURNS)
角度#
angle returns the shaft’s current angle as a float.
角度值表示轴在一个旋转周期内的绝对位置,范围从 0 到 359.99 度。项目开始或机器人断电时,该值不会重置。
Usage:
rotation_sensor.angle(units)
参数 |
描述 |
|---|---|
|
Optional. The angle unit: |
位置#
position returns how far the shaft has rotated.
与角度不同,位置可以超过 359.99 度,也可以低于 0 度。位置参数跟踪轴在项目进行过程中正向或反向旋转的距离,并可根据需要进行重置。
Usage:
rotation_sensor.position(units)
参数 |
描述 |
|---|---|
|
Optional. The position unit: |
速度#
velocity returns how fast the shaft is rotating.
Usage:
rotation_sensor.velocity(units)
参数 |
描述 |
|---|---|
|
The velocity unit: |
重置位置#
reset_position resets the Rotation Sensor’s current position to 0.
Usage:
rotation_sensor.reset_position()
参数 |
描述 |
|---|---|
此方法没有参数。 |
设置反转#
set_reversed sets whether the Rotation Sensor’s output is reversed.
When reversed, positive values become negative and negative values become positive. This method works the same as setting the reverse parameter to True when using the Rotation constructor.
Usage:
rotation_sensor.set_reversed(value)
参数 |
描述 |
|---|---|
|
Whether to reverse the Rotation Sensor output: |
已更改#
changed registers a function to be called whenever the Rotation Sensor’s value changes.
Usage:
rotation_sensor.changed(callback, arg)
参数 |
描述 |
|---|---|
|
先前定义的 函数,当旋转传感器的值发生变化时运行。 |
|
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的函数。 |
def my_function():
brain.screen.print("Rotated!")
# Call my_function whenever rotation_sensor's value changes
rotation_sensor.changed(my_function)
构造器#
Constructors are used to manually create Rotation objects, such as when configuring a Rotation Sensor outside of VEXcode.
Rotation#
Rotation creates a Rotation Sensor object.
Usage:
Rotation(smartport)
范围 |
描述 |
|---|---|
|
The Smart Port that the Rotation Sensor is connected to, written as |
|
Optional. Whether to reverse the Rotation Sensor output: |
# Create an inverted Rotation Sensor in Port 10
rotation_sensor = Rotation(Ports.PORT10, True)