✅ ✅ ✅ ✅ ✅ ✅ ❌

Power Functions(动力功能)

The ColorDistanceSensor(颜色距离传感器) can send infrared signals to control Power Functions infrared receivers. You can use this technique to control medium, large, extra large, and train motors. The infrared range is limited to about 30 cm, depending on the angle and ambient conditions.

../_images/pupdevice-pfmotor.png

Figure 3 Powered Up ColorDistanceSensor(颜色距离传感器) (left), Power Functions infrared receiver (middle), and a Power Functions motor (right). Here, the receiver uses channel 1 with a motor on the red port.

sensorpf motoronblue 1with forward
class PFMotor(sensor, channel, color, positive_direction=Direction.CLOCKWISE)

Control Power Functions motors with the infrared functionality of the ColorDistanceSensor(颜色距离传感器).

参数:
  • sensor (ColorDistanceSensor(颜色距离传感器)) – Sensor object.

  • channel(int)– 接收器的通道号:1、2、3 或 4。
  • color(Color)– 接收器上的颜色标记:Color.BLUE 或 Color.RED
  • positive_direction (方向) – 给定正占空比值时马达应转动的方向。

pf motor50%power atduty cycle
await dc(duty)

以给定的占空比(也称为“功率”)旋转马达。

参数:

duty (Number, %) – 占空比(-100.0 到 100)。

pf motorcoaststop
await stop()

停止马达并让其自由旋转。

马达会因摩擦而逐渐停止。

pf motorbrakestop
await brake()

被动制动马达。

马达会因摩擦以及马达仍在转动时产生的电压而停止。

示例

控制 Power Functions 马达

from pybricks.parameters import Color, Port
from pybricks.pupdevices import ColorDistanceSensor, PFMotor
from pybricks.tools import wait

# 初始化传感器。
sensor = ColorDistanceSensor(Port.B)

# 在红色输出口的通道 1 上初始化一个马达。
motor = PFMotor(sensor, 1, Color.RED)

# 旋转然后停止。
motor.dc(100)
wait(1000)
motor.stop()
wait(1000)

# 以一半速度向另一方向旋转,然后停止。
motor.dc(-50)
wait(1000)
motor.stop()

控制多个 Power Functions 马达

from pybricks.parameters import Color, Direction, Port
from pybricks.pupdevices import ColorDistanceSensor, PFMotor
from pybricks.tools import wait

# 初始化传感器。
sensor = ColorDistanceSensor(Port.B)

# 你可以在不同通道上使用多个马达。
arm = PFMotor(sensor, 1, Color.BLUE)
wheel = PFMotor(sensor, 4, Color.RED, Direction.COUNTERCLOCKWISE)

# 让两个马达加速。只有这些值可用。
# 其他值将向下舍入到最接近的匹配值。
for duty in [15, 30, 45, 60, 75, 90, 100]:
    arm.dc(duty)
    wheel.dc(duty)
    wait(1000)

# 为了使信号更可靠,命令之间
# 有短暂的停顿。因此,它们改变速度和
# 停止的时间会略有不同。

# 制动两个马达。
arm.brake()
wheel.brake()