| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
|---|
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.
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.
- 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 (方向) – 给定正占空比值时马达应转动的方向。
示例
控制 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()