编码器#
初始化编码器类#
使用以下构造函数创建编码器传感器:
Encoder(port)
此构造函数使用一个参数:
范围 |
描述 |
---|---|
|
The 3-Wire Port “pair” that the Encoder Sensor is connected to, whether it’s a port on the Brain, or a 3-Wire Expander. |
必须先创建 Brain 或 3-Wire Expander,然后才能使用编码器类构造函数创建对象。
# Create the Brain.
brain = Brain()
# Construct an Encoder Sensor "encoder" with the
# Encoder class.
encoder = Encoder(brain.three_wire_port.a)
This encoder
object will be used in all subsequent examples throughout this API documentation when referring to Encoder class methods.
类方法#
reset_position()#
The reset_position()
method resets the position of the Encoder to 0.
**返回:**无。
set_position()#
The set_position(value, units)
method sets the position of the Encoder.
参数 |
描述 |
---|---|
价值 |
要设置的位置值。 |
单位 |
Optional. A valid RotationUnits type. The default is |
**返回:**无。
# Set the value of Encoder's position to 180 degrees.
encoder.set_position(180)
position()#
The position(units)
method returns the current position of the Encoder.
参数 |
描述 |
---|---|
单位 |
Optional. A valid RotationUnits type. The default is |
**返回:**编码器当前位置的指定单位值。
# Get the current Encoder position.
value = encoder.position()
velocity()#
The velocity(units)
method returns the current velocity of the Encoder.
参数 |
描述 |
---|---|
单位 |
Optional. A valid VelocityUnits type. The default is |
返回: 以指定单位表示的编码器当前速度值。
# Get the current Encoder velocity in rpm.
value = encoder.velocity()