parrot
— mPython Expansion Board driver¶
PARROT, the mPython Expansion Board is derived from the mPython Board, which is compact and portable. The IO pin expansion board supporting motor drive, voice playback, voice synthesis and other functions can expand 12 IO interfaces and 2 I2C interfaces. The library provides motor drive, LED drive and other functions for controlling the expansion board.
Motor control I2C communication protocol data format:
Type | Command | motor_no | speed(int) |
Motor | 0x01 | 0x01/0x02 | -100~100 |
When ‘speed’ is negative, reverse; when positive, forward.
-
parrot.
MOTOR_1
¶ M1电机编号,0x01
-
parrot.
MOTOR_2
¶ M2电机编号,0x02
-
parrot.
get_speed
(motor_no)[source]¶ 返回电机速度
Parameters: motor_no (int) – 控制电机编号,可以使用 MOTOR_1
,MOTOR_2
,或者直接写入电机编号。Return type: int Returns: 返回该电机速度
-
class
parrot.
IR_encode
[source]¶ 红外编码类
-
class
parrot.
IR
[source]¶ 红外收发管的抽象类,用于红外发射和学习。
-
send
(buff, repeat_en=0)[source]¶ 发送红外脉冲数据buff
Parameters: - buff (bytes) – 可以是
IR_encode.encode_nec
返回的buff。或,学习后的buff。 - repeat_en (init) – 重复码。用于表示按键常按。当repeat_en为1时,会不断发送重复码,直至
stop_send
停止发送。
- buff (bytes) – 可以是
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import parrot
from mpython import sleep_ms,sleep
# 可设置速度范围为-100到100。
# 当值为正值时,电机正转;为负值时,电机反转;
while True:
# 设置正转速度100
parrot.set_speed(parrot.MOTOR_1,100)
parrot.set_speed(parrot.MOTOR_2,100)
print("current motor speend: %d,%d" %(parrot.get_speed(parrot.MOTOR_1),parrot.get_speed(parrot.MOTOR_2))) #获取电机速度
sleep(5)
# 设置反转速度100
parrot.set_speed(parrot.MOTOR_1,-100)
parrot.set_speed(parrot.MOTOR_2,-100)
print("current motor speend: %d,%d" %(parrot.get_speed(parrot.MOTOR_1),parrot.get_speed(parrot.MOTOR_2))) #获取电机速度
sleep(5)
# 停止
parrot.set_speed(parrot.MOTOR_1,0)
parrot.set_speed(parrot.MOTOR_2,0)
print("current motor speend: %d,%d" %(parrot.get_speed(parrot.MOTOR_1),parrot.get_speed(parrot.MOTOR_2))) #获取电机速度
sleep(2)
|
1 2 3 4 5 6 7 8 9 10 11 12 | import parrot
import time
ir_code = parrot.IR_encode()
ir = parrot.IR()
ir_buff = ir_code.encode_nec(1, 85)
while True:
ir.send(ir_buff, 1)
time.sleep(3)
ir.stop_send()
time.sleep(3)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | from mpython import *
import parrot
import time
def on_button_a_pressed(_):
global data
ir.learn()
time.sleep(4)
if 0 == ir.__get_learn_status():
data = ir.get_learn_data()
print(data)
else:
print('什么都没学到...')
button_a.event_pressed = on_button_a_pressed
def on_button_b_pressed(_):
global data
print(data)
ir.send(data, 0)
button_b.event_pressed = on_button_b_pressed
ir = parrot.IR()
data = None
|