旋转#
初始化旋转类#
使用以下构造函数创建旋转传感器:
旋转(左转,右转)
此构造函数使用一个参数:
范围 |
描述 |
---|---|
|
旋转传感器连接到的有效 智能端口。 |
|
**可选。**设置为 |
# Construct a Rotation Sensor "rotation" with the
# Rotation class.
rotation = Rotation(Ports.PORT1)
当引用 Rotation 类方法时,此“rotation”对象将在整个 API 文档的所有后续示例中使用。
类方法#
set_reversed()#
set_reversed(value)
方法将旋转方向设置为反转。
这是一种非等待方法,允许下一个方法无延迟运行。
参数 |
描述 |
---|---|
价值 |
设置为“True”则反转电机方向。设置为“False”则保持正常方向。 |
**返回:**无。
# Set the direction to be reversed.
rotation.set_reversed(True)
angle()#
angle(units)
方法返回旋转传感器测量的角度。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**旋转传感器以指定单位测量的角度。
# Print the current angle of the Rotation Sensor to the
# Brain's screen.
brain.screen.print(rotation.angle())
重置位置()#
reset_position()
方法将旋转传感器的位置重置为 0。
**返回:**无。
set_position()#
set_position(value, units)
方法设置旋转传感器的位置。position()
方法返回的位置将设置为该值。
参数 |
描述 |
---|---|
价值 |
要设置的位置值。 |
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**无。
position()#
position(units)
方法返回旋转传感器的当前位置。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**旋转传感器的当前位置(以指定的单位表示)。
velocity()#
velocity(units)
方法返回旋转传感器的速度。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 VelocityUnits 类型。默认值为 |
**返回:**以指定单位表示的旋转传感器的速度。
changed()#
changed(callback, arg)
方法注册一个回调函数,用于当旋转传感器测量的值发生变化时调用。
参数 |
描述 |
---|---|
打回来 |
当旋转传感器测量的值发生变化时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**无
# Define a function rotation_changed().
def rotation_changed():
# The Brain will print that the rotation value changed
# on the Brain's screen.
brain.screen.print("rotation value changed")
# Run rotation_changed when the value of the
# Rotation Sensor changes.
rotation.changed(rotation_changed)
installed()#
installed()
方法检查旋转传感器是否已连接。
返回: 如果安装了旋转传感器,则返回 True
。如果没有安装,则返回 False
。
timestamp()#
timestamp()
方法返回从旋转传感器接收到的最后一个状态包的时间戳。
**返回:**最后接收的状态包的时间戳(以毫秒为单位)。