1844浏览
查看: 1844|回复: 1

[项目] 行空板——麦昆Plus

[复制链接]
本帖最后由 云天 于 2022-9-17 22:26 编辑

行空板——麦昆Plus图1


【项目背景】
使用行空板替换micro:bit来控制麦昆。能控制麦昆Plus后,就可以用行空板完成更多的任务,如AI麦昆等。
【制作库文件】
制作库文件dfrobot_motor.py,并将此库文件放入行空板与程序在同一目录。
  1. import time
  2. from pinpong.board import gboard,I2C,Pin
  3. import math
  4. class MOTOR():
  5.     M1                             = 0
  6.     M2                             = 1
  7.     ALL                            = 2
  8.     CW                             = 1
  9.     CCW                            = 2
  10.     RED = 1
  11.     GREEN = 2
  12.     YELLOW = 3
  13.     BLUE = 4
  14.     PINK = 5
  15.     CYAN = 6
  16.     WHITH = 7
  17.     PUT = 8
  18.     LEFT = 1
  19.     RIGHT = 2
  20.     RGBALL = 3
  21.     def __init__(self, board = None, i2c_addr = 0x10, bus_num=0):
  22.         if isinstance(board, int):
  23.             i2c_addr = board
  24.             board = gboard
  25.         elif board is None:
  26.             board = gboard
  27.         self.i2c_addr = i2c_addr
  28.         self._i2c = I2C(bus_num)
  29.     def motor_init(self):
  30.         buf=[0x00, 0, 0, 0, 0]
  31.         self.i2cWriteBuf(0x10, buf)
  32.         
  33.     def motor_run(self, index, direction, speed):
  34.         Speed = abs(speed)
  35.         if Speed > 255:
  36.             Speed = 255
  37.         
  38.         
  39.         if index > 3 or index < 0:
  40.             return
  41.         if index == self.M2:
  42.             
  43.             buf = [0x00, direction, Speed]
  44.             self.i2cWriteBuf(0x10, buf)
  45.         elif index == self.M1:
  46.             buf = [0x02,direction, Speed]
  47.             self.i2cWriteBuf(0x10, buf)
  48.         elif index == self.ALL:
  49.             buf = [0x00, direction, Speed,direction, Speed]
  50.             self.i2cWriteBuf(0x10, buf)
  51.     def motor_stop(self, index):
  52.         self.motor_run(index, 0, 0)
  53.    
  54.     def read_to_addr(self, lens):
  55.         return self._i2c.readfrom(self.i2c_addr, lens)
  56.    
  57.     def i2cWriteBuf(self, addr, buf):
  58.         self._i2c.writeto(addr, buf)
  59.     def setRGB(self,Dir,color):
  60.         if Dir==1:
  61.             buf=[0,0]
  62.             buf[0]=0x0B
  63.             buf[1]=color
  64.             self.i2cWriteBuf(0x10, buf)
  65.         elif Dir==2:
  66.             buf=[0,0]
  67.             buf[0]=0x0C
  68.             buf[1]=color
  69.             self.i2cWriteBuf(0x10, buf)
  70.         elif Dir==3:
  71.             buf=[0,0,0]
  72.             buf[0]=0x0B
  73.             buf[1]=color
  74.             buf[2]=color
  75.             self.i2cWriteBuf(0x10, buf)   
  76.     def constrain(self,amt,low,high):
  77.             return low if (amt)<(low) else (high if (amt)>(high) else (amt))
  78.     def servoRun(self,index,angle):
  79.         buf=[0,0]
  80.         buf[1] = self.constrain(angle, 40, 120)
  81.         if index == 1:
  82.             buf[0] = 0x14;
  83.         elif index == 2:
  84.             buf[0] = 0x15;
  85.         elif index == 3:
  86.             buf[0] = 0x16;
  87.         elif index == 4:
  88.             buf[0] = 0x14;
  89.             self.i2cWriteBuf(0x10, buf)
  90.             buf[0] = 0x15;
  91.             self.i2cWriteBuf(0x10, buf)
  92.             buf[0] = 0x16;
  93.         else:
  94.            return
  95.    
  96.         self.i2cWriteBuf(0x10, buf)
复制代码
【点灯程序】
行空板——麦昆Plus图2

  1. # -*- coding: utf-8 -*-
  2. import time
  3. from pinpong.board import Board
  4. from dfrobot_motor import MOTOR
  5. Board("microbit").begin()
  6. M = MOTOR()
  7. while True:
  8.    
  9.     M.setRGB(M.LEFT,M.RED)
  10.     time.sleep(2)
  11.     M.setRGB(M.RIGHT,M.RED)
  12.     time.sleep(2)
  13.     M.setRGB(M.LEFT,M.BLUE)
  14.     time.sleep(2)
  15.     M.setRGB(M.RIGHT,M.BLUE)
  16.     time.sleep(2)
复制代码


【控制舵机】


舵机接在麦昆Plus的S1舵机接口上,因我的舵机运动范围在40到120之间,所以在库中进行了限制:self.constrain(angle, 40, 120)
行空板——麦昆Plus图3

  1. # -*- coding: utf-8 -*-
  2. import time
  3. from pinpong.board import Board
  4. from dfrobot_motor import MOTOR
  5. Board("microbit").begin()
  6. M = MOTOR()
  7. while True:
  8.   
  9.     M.servoRun(1,40)
  10.     time.sleep(2)
  11.     M.servoRun(1,120)
  12.     time.sleep(2)
复制代码


【控制电机】

  1. # -*- coding: utf-8 -*-
  2. import time
  3. from pinpong.board import Board
  4. from dfrobot_motor import MOTOR
  5. Board("microbit").begin()
  6. M = MOTOR()
  7. while True:
  8.    
  9.     M.motor_run(M.LEFT,M.CW,200)
  10.     time.sleep(2)
  11.     M.motor_run(M.RIGHT,M.CW,200)
  12.     time.sleep(2)
  13.     M.motor_stop(M.ALL)
  14.     time.sleep(2)
  15.     M.motor_run(M.ALL,M.CCW,200)
  16.     time.sleep(2)
  17.     M.motor_stop(M.ALL)
  18.     time.sleep(2)
复制代码

【micro:bit掌控I/O扩展板】
行空板——麦昆Plus图7
利用micro:bit掌控I/O扩展板制作小车,也可以使用上面的库。
行空板——麦昆Plus图4行空板——麦昆Plus图5行空板——麦昆Plus图6


【micro:bit 电机驱动扩展板】
micro:bit 电机驱动扩展板,因为使用了PCA9685来驱动电机,是一款用于产生16路PWM信号的控制芯片, 采用I2C总线与主控芯片进行通信。所以这个扩展板如果要接行空板控制电机,需使用Pinpong库中的microbit_motor.py库。
行空板——麦昆Plus图8

rzegkly  版主

发表于 2022-9-18 07:12:34

漂亮
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail