电位器V2#

初始化 PotentiometerV2 类#

使用以下构造函数创建 PotentiometerV2:

电位器V2(端口)

此构造函数使用一个参数:

范围

描述

端口

电位器 V2 连接到的 3 线端口,无论它是 Brain 上的端口,还是 3 线扩展器 上的端口。

必须先创建 Brain3-Wire Expander,然后才能使用 PotentiometerV2 类构造函数创建对象。

# Create the Brain.
brain = Brain()
# Construct a PotentiometerV2 "pot2" with the PotentiometerV2 class.
pot2 = PotentiometerV2(brain.three_wire_port.a)

当引用 PotentiometerV2 类方法时,此“pot2”对象将在整个 API 文档的所有后续示例中使用。

类方法#

angle()#

angle(units) 方法返回 PotentiometerV2 测量的角度。

参数

描述

单位

**可选。**有效的 RotationUnits 类型或 PERCENT。默认值为 DEGREES

**返回:**电位器V2测量的角度。

# Print the current angle of the PotentiometerV2 to the Brain's screen.
brain.screen.print(pot2.angle())

changed()#

changed(callback, arg) 方法注册一个回调函数,用于当 PotentiometerV2 测量的值发生变化时执行。

参数

描述

打回来

当电位器测量的值发生变化时调用的回调函数。

arg

**可选。**传递给回调函数的参数元组。

**返回:**无。

# Define a function potentiometerV2_changed()
def potentiometerV2_changed():
    # The Brain will print that the potentiometer value changed
    #  on the Brain's screen.
    brain.screen.print("potentiometer value changed")

# Run PotentiometerV2_changed when the value measured by 
# the Potentiometer changes.
pot2.changed(potentiometerV2_changed)