编码器#

初始化编码器类#

使用以下构造函数创建编码器传感器:

编码器(端口)

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

范围

描述

端口

编码器传感器所连接的三线端口“对”,可以是大脑上的端口,也可以是三线扩展器上的端口。2
注意:**编码器传感器使用两个相邻的三线端口。这些端口对分别是 a/b、c/d、e/f 和 g/h。输入端口对作为参数时,请使用端口对的首字母,例如:“brain.three_wire_port.a”指的是 a/b 对。

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

# Create the Brain.
brain = Brain()
# Construct an Encoder Sensor "encoder" with the
# Encoder class.
encoder = Encoder(brain.three_wire_port.a)

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

类方法#

reset_position()#

reset_position() 方法将编码器的位置重置为 0。

**返回:**无。

set_position()#

set_position(value, units) 方法设置编码器的位置。

参数

描述

价值

要设置的位置值。

单位

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

**返回:**无。

# Set the value of Encoder's position to 180 degrees.
encoder.set_position(180)

position()#

position(units) 方法返回编码器的当前位置。

参数

描述

单位

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

**返回:**编码器当前位置的指定单位值。

# Get the current Encoder position.
value = encoder.position()

velocity()#

velocity(units) 方法返回编码器的当前速度。

参数

描述

单位

**可选。**有效的 VelocityUnits 类型。默认值为 RPM

返回: 以指定单位表示的编码器当前速度值。

# Get the current Encoder velocity in rpm.
value = encoder.velocity()