parrot --- 掌控拓展板驱动

../../_images/extboard_back.png

掌控拓展板parrot是mPython掌控板衍生的一款体积小巧、易于携带。支持电机驱动、语音播放、语音合成等功能的IO引脚扩展板,可扩展12路IO接口和2路I2C接口。 该库为掌控拓展板提供电机驱动,LED驱动等功能。

电机控制I2C通讯协议数据格式:

Type Command motor_no speed(int)
控制电机 0x01 0x01/0x02 -100~100

当 'speed' 为负值,反转;当为正值,正转。


parrot.MOTOR_1

M1电机编号,0x01

parrot.MOTOR_2

M2电机编号,0x02

parrot.set_speed(motor_no, speed)[源代码]

设置电机速度

参数:
  • motor_no (int) -- 控制电机编号,可以使用 MOTOR_1, MOTOR_2 ,或者直接写入电机编号。
  • speed (int) -- 电机速度,范围-100~100,负值代表反转。
parrot.get_speed(motor_no)[源代码]

返回电机速度

参数:motor_no (int) -- 控制电机编号,可以使用 MOTOR_1, MOTOR_2,或者直接写入电机编号。
返回类型:int
返回:返回该电机速度
parrot.led_on(no, brightness=50)[源代码]

打开灯。电机接口,除了可以驱动电机,还能做灯的控制。

参数:
  • no (int) -- 控制电机编号,可以使用 MOTOR_1, MOTOR_2,或者直接写入电机编号。
  • brightness (int) -- 设置亮度,范围0~100
parrot.led_off(no)[源代码]

关闭灯。

参数:no (int) -- 控制电机编号,可以使用 MOTOR_1, MOTOR_2,或者直接写入电机编号。
电机驱动示例
 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)