行空板——麦昆Plus
本帖最后由 云天 于 2022-9-17 22:26 编辑【项目背景】
使用行空板替换Micro:bit来控制麦昆。能控制麦昆Plus后,就可以用行空板完成更多的任务,如AI麦昆等。
【制作库文件】
制作库文件dfrobot_motor.py,并将此库文件放入行空板与程序在同一目录。
import time
from pinpong.board import gboard,I2C,Pin
import math
class MOTOR():
M1 = 0
M2 = 1
ALL = 2
CW = 1
CCW = 2
RED = 1
GREEN = 2
YELLOW = 3
BLUE = 4
PINK = 5
CYAN = 6
WHITH = 7
PUT = 8
LEFT = 1
RIGHT = 2
RGBALL = 3
def __init__(self, board = None, i2c_addr = 0x10, bus_num=0):
if isinstance(board, int):
i2c_addr = board
board = gboard
elif board is None:
board = gboard
self.i2c_addr = i2c_addr
self._i2c = I2C(bus_num)
def motor_init(self):
buf=
self.i2cWriteBuf(0x10, buf)
def motor_run(self, index, direction, speed):
Speed = abs(speed)
if Speed > 255:
Speed = 255
if index > 3 or index < 0:
return
if index == self.M2:
buf =
self.i2cWriteBuf(0x10, buf)
elif index == self.M1:
buf =
self.i2cWriteBuf(0x10, buf)
elif index == self.ALL:
buf =
self.i2cWriteBuf(0x10, buf)
def motor_stop(self, index):
self.motor_run(index, 0, 0)
def read_to_addr(self, lens):
return self._i2c.readfrom(self.i2c_addr, lens)
def i2cWriteBuf(self, addr, buf):
self._i2c.writeto(addr, buf)
def setRGB(self,Dir,color):
if Dir==1:
buf=
buf=0x0B
buf=color
self.i2cWriteBuf(0x10, buf)
elif Dir==2:
buf=
buf=0x0C
buf=color
self.i2cWriteBuf(0x10, buf)
elif Dir==3:
buf=
buf=0x0B
buf=color
buf=color
self.i2cWriteBuf(0x10, buf)
def constrain(self,amt,low,high):
return low if (amt)<(low) else (high if (amt)>(high) else (amt))
def servoRun(self,index,angle):
buf=
buf = self.constrain(angle, 40, 120)
if index == 1:
buf = 0x14;
elif index == 2:
buf = 0x15;
elif index == 3:
buf = 0x16;
elif index == 4:
buf = 0x14;
self.i2cWriteBuf(0x10, buf)
buf = 0x15;
self.i2cWriteBuf(0x10, buf)
buf = 0x16;
else:
return
self.i2cWriteBuf(0x10, buf)
【点灯程序】
# -*- coding: utf-8 -*-
import time
from pinpong.board import Board
from dfrobot_motor import MOTOR
Board("microbit").begin()
M = MOTOR()
while True:
M.setRGB(M.LEFT,M.RED)
time.sleep(2)
M.setRGB(M.RIGHT,M.RED)
time.sleep(2)
M.setRGB(M.LEFT,M.BLUE)
time.sleep(2)
M.setRGB(M.RIGHT,M.BLUE)
time.sleep(2)
https://www.bilibili.com/video/BV1EG411G7hQ?share_source=copy_web
【控制舵机】
舵机接在麦昆Plus的S1舵机接口上,因我的舵机运动范围在40到120之间,所以在库中进行了限制:self.constrain(angle, 40, 120)
# -*- coding: utf-8 -*-
import time
from pinpong.board import Board
from dfrobot_motor import MOTOR
Board("microbit").begin()
M = MOTOR()
while True:
M.servoRun(1,40)
time.sleep(2)
M.servoRun(1,120)
time.sleep(2)
https://www.bilibili.com/video/BV1o24y1d7Yz?share_source=copy_web
【控制电机】
# -*- coding: utf-8 -*-
import time
from pinpong.board import Board
from dfrobot_motor import MOTOR
Board("microbit").begin()
M = MOTOR()
while True:
M.motor_run(M.LEFT,M.CW,200)
time.sleep(2)
M.motor_run(M.RIGHT,M.CW,200)
time.sleep(2)
M.motor_stop(M.ALL)
time.sleep(2)
M.motor_run(M.ALL,M.CCW,200)
time.sleep(2)
M.motor_stop(M.ALL)
time.sleep(2)
https://www.bilibili.com/video/BV1cd4y1g7NL?share_source=copy_web【micro:bit掌控I/O扩展板】利用micro:bit掌控I/O扩展板制作小车,也可以使用上面的库。
https://www.bilibili.com/video/BV1MG4y1B7hM?share_source=copy_web
【micro:bit 电机驱动扩展板】micro:bit 电机驱动扩展板,因为使用了PCA9685来驱动电机,是一款用于产生16路PWM信号的控制芯片, 采用I2C总线与主控芯片进行通信。所以这个扩展板如果要接行空板控制电机,需使用Pinpong库中的microbit_motor.py库。可参考:https://mc.dfrobot.com.cn/thread-313275-1-1.html。 漂亮
页:
[1]