本帖最后由 云天 于 2022-5-22 14:01 编辑

【项目特色】行空板直接控制micro:bit电机扩展板中的电机接口。
【行空板】
行空板是一款专为Python学习和使用设计的新一代国产开源硬件,采用单板计算机架构,集成LCD彩屏、WiFi蓝牙、多种常用传感器和丰富的拓展接口。同时,其自带Linux操作系统和Python环境,还预装了常用的Python库,让广大师生只需两步就能进行Python教学。

金手指:引脚编号兼容micro:bit, 19路独立I/O(支持1路I2C、1路UART、2路SPI、6路12位ADC、5路12位PWM)
【micro:bit 电机驱动扩展板】
micro:bit电机驱动扩展采用IIC外扩驱动芯片的方式控制电机和舵机,仅占用IIC两个管脚。

【电机驱动库】
在行空板中加载“microbit_motor.py”(在Pinpong库所在目录下能找到)。

【麦克纳姆行空车】






【麦克纳姆轮】
麦克纳姆轮与普通轮子的区别在于麦克纳姆轮旋转时,由于存在斜向的从动轮,会同时产生一个斜向的力,当我们控制轮子旋转的速度与方向时,将斜向的力增强或抵消,从而实现小车的全向移动。可以完成横移、斜方向移动等普通小车无法完成的高难度动作,轮子的转动方向与小车的运动方向关系如下图:

【2路18650电池座】
该款2路18650电池座使用简单,可安装两节18650电池,具有micro和type-c两个充电输入口,输出口有USB接口输出和端口输出两种方式,支持5V/2A和3.3V/1A输出,其中5V输出端口*5,3.3V端口输出*5,USB输出*1(5V)。模块具有过充和过放保护,能够保护18650电池,有效增加18650电池的使用寿命。 电池座上的拨码开关分为HOLD档和NOMAL档,当使用NOMAL档时,输出电流过低会自动关机,当使用HOLD档不会自动关机,会持续输出直到达到过放保护时才会关机。 其结合18650电池方便用户对微型设备的供电,可以用于户外设备供电和作为智能小车的移动电源使用。 
【程序代码】
-
-
- # -*- coding: utf-8 -*-
- import time
- import cv2
- import numpy as np
- from pinpong.board import Board
- from microbit_motor import Microbit_Motor #导入Microbit_Motor库
- import siot
- from pinpong.extension.unihiker import *
-
- from unihiker import GUI
- u_gui=GUI()
- xianshi=u_gui.draw_text(text="开始",x=50,y=100,font_size=50, color="#0000FF")
- # 事件回调函数
- def on_message_callback(client, userdata, msg):
-
- if (msg.payload.decode("utf-8") == 'G'):
- forward(200)
- xianshi.config(text="前进")
- if (msg.payload.decode("utf-8")== 'S'):
- stop()
- xianshi.config(text="停止")
-
- if (msg.payload.decode("utf-8") == 'B'):
- back(200)
- xianshi.config(text="后退")
-
- if (msg.payload.decode("utf-8") == 'L'):
- left(200)
- xianshi.config(text="向左")
-
- if (msg.payload.decode("utf-8") == 'R'):
- right(200)
- xianshi.config(text="向右")
-
-
- siot.init(client_id="siot_192",server="192.168.31.25",port=1883,user="siot",password="dfrobot")
- siot.connect()
- siot.loop()
- siot.set_callback(on_message_callback)
- siot.getsubscribe(topic="car/control")
-
- Board("microbit").begin()
- motorbit = Microbit_Motor()
- def forward(speed):
- #前进
- #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
- if speed>255:
- speed=255
- motorbit.motor_run(motorbit.M1, motorbit.CW, speed)
- motorbit.motor_run(motorbit.M2, motorbit.CW, speed)
- motorbit.motor_run(motorbit.M3, motorbit.CCW, speed)
- motorbit.motor_run(motorbit.M4, motorbit.CCW, speed)
- def back(speed):
- #后退
- #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
- if speed>255:
- speed=255
- motorbit.motor_run(motorbit.M1, motorbit.CCW, speed)
- motorbit.motor_run(motorbit.M2, motorbit.CCW, speed)
- motorbit.motor_run(motorbit.M3, motorbit.CW, speed)
- motorbit.motor_run(motorbit.M4, motorbit.CW, speed)
- def left_turn(speed):
- #向左转
- #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
- if speed>255:
- speed=255
- motorbit.motor_run(motorbit.M1, motorbit.CW, speed)
- motorbit.motor_run(motorbit.M2, motorbit.CW, speed)
- motorbit.motor_run(motorbit.M3, motorbit.CW, speed)
- motorbit.motor_run(motorbit.M4, motorbit.CW, speed)
- def right_turn(speed):
- #向右转
- #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
- if speed>255:
- speed=255
- motorbit.motor_run(motorbit.M1, motorbit.CCW, speed)
- motorbit.motor_run(motorbit.M2, motorbit.CCW, speed)
- motorbit.motor_run(motorbit.M3, motorbit.CCW, speed)
- motorbit.motor_run(motorbit.M4, motorbit.CCW, speed)
- def left(speed):
- #向左
- #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
- if speed>255:
- speed=255
- motorbit.motor_run(motorbit.M1, motorbit.CW, speed)
- motorbit.motor_run(motorbit.M2, motorbit.CCW, speed)
- motorbit.motor_run(motorbit.M3, motorbit.CW, speed)
- motorbit.motor_run(motorbit.M4, motorbit.CCW, speed)
- def right(speed):
- #向右
- #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
- if speed>255:
- speed=255
- motorbit.motor_run(motorbit.M1, motorbit.CCW, speed)
- motorbit.motor_run(motorbit.M2, motorbit.CW, speed)
- motorbit.motor_run(motorbit.M3, motorbit.CCW, speed)
- motorbit.motor_run(motorbit.M4, motorbit.CW, speed)
- def stop():
- motorbit.motor_stop(motorbit.M1)
- motorbit.motor_stop(motorbit.M2)
- motorbit.motor_stop(motorbit.M3)
- motorbit.motor_stop(motorbit.M4)
-
- def trace_fun():
-
- while True:
- pass
-
-
-
- if __name__ == '__main__':
- trace_fun()
复制代码
【Appinventor2】



【演示视频】
|