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

[项目分享] 【Mind+Python】无线控车

[复制链接]
【Mind+Python】无线控车图4

本项目使用两块micro:bit,一块用USB线与PC机相连(Mind+Python模式下用Pinpong控制),一块安装在小车上,两者通过无线功能通信。

【制作小车】
【Mind+Python】无线控车图1


【Mind+Python】无线控车图2


使用“MiniQ 桌面机器人底盘”,Microbit加扩展板,从小风扇上拆下的3.7V锂电池。制作一个小车。
【Mind+Python】无线控车图5
比较图
【特色硬件】
两个车灯,使用了“炫彩LED模块”
【Mind+Python】无线控车图3
LED拥有七色炫光,无需编程,通电即可点亮。内置控制芯片,七种颜色随机闪烁,时快时慢,绚丽缤纷,五彩斑斓。并且模块支持级联控制,只需一个接口,即可控制所有LED,是打造环境,烘托气氛的理想佳品。简单而不失浪漫,低调而不失奢华。
【小车展示】

【Mind+Python程序】
【Mind+Python】无线控车图6
为求稳定,Mind+使用版本1.7.0RC3,Pinpong库使用版本0.3.5
  1. #  -*- coding: UTF-8 -*-
  2. # MindPlus
  3. # Python
  4. import tkinter as tk
  5. from tkinter import *
  6. from PIL import Image, ImageTk#图像控件
  7. import time
  8. from pinpong.board import Board
  9. from pinpong.extension.microbit import MBWireless
  10. Board("microbit").begin()#初始化,选择板型和端口号,不输入端口号则进行自动识别
  11. micro = MBWireless()
  12. micro.set_wireless_channel(7)           #设置无线频道为7
  13. micro.open_wireless()                   #打开无线通信
  14. # 事件回调函数
  15. #定义函数,向物联网发送相应控制指令
  16. def forward():#前进
  17.       micro.send_wireless("A")         #通过无线通信发送
  18. def back():#后退
  19.       micro.send_wireless("B")         #通过无线通信发送
  20. def left():#左转
  21.       micro.send_wireless("C")         #通过无线通信发送
  22. def right():#右转
  23.       micro.send_wireless("D")         #通过无线通信发送
  24. def stop():#停止
  25.       micro.send_wireless("E")         #通过无线通信发送
  26. def open_s():#风扇开
  27.       micro.send_wireless("F")         #通过无线通信发送
  28. def stop_s():#风扇关
  29.       micro.send_wireless("G")         #通过无线通信发送
  30. def light_open():#双闪开
  31.       micro.send_wireless("H")         #通过无线通信发送
  32. def light_close():#双闪关
  33.       micro.send_wireless("I")         #通过无线通信发送
  34. #生成窗体,布置相应文本框和按钮
  35. top = tk.Tk()
  36. top.title('控制窗口')
  37. top.geometry('900x600')
  38. image_width = 900
  39. image_height = 600
  40. canvas = Canvas(top,bg = 'white',width = image_width,height = image_height )#绘制画布
  41. canvas.pack()
  42. img = Image.open('bg.jpg')
  43. bg = ImageTk.PhotoImage(img)
  44. bgid = canvas.create_image(0, 0, image=bg, anchor='nw')
  45. canvas.place(x = 0,y = 0)
  46. #产生标签文本背景透明效果
  47. txtid=canvas.create_text(350,20, fill = 'red',font=("黑体", 35),anchor="nw")
  48. canvas.insert(txtid,1,"无线控车")
  49. E1 = Entry(top, bd =5)
  50. E1.pack(side = RIGHT)
  51. E1.place(x=100,y=100)
  52. wx=20
  53. hy=350
  54. def setV():#设置车速
  55.     val=int(E1.get())
  56.     if val>=30 and val<=200:
  57.         micro.send_wireless(E1.get())         #通过无线通信发送
  58. #生成按钮,并指定相应功能
  59. left = tk.Button(top,text='左转',height=2,width=15,command=left)
  60. left.place(x=wx,y=hy+100)
  61. forward = tk.Button(top,text='前进',height=2,width=15,command=forward)
  62. forward.place(x=wx+150,y=hy)
  63. right = tk.Button(top,text='右转',height=2,width=15,command=right)
  64. right.place(x=wx+300,y=hy+100)
  65. back = tk.Button(top,text='后退',height=2,width=15,command=back)
  66. back.place(x=wx+150,y=hy+200)
  67. stop = tk.Button(top,text='停止',height=2,width=15,command=stop)
  68. stop.place(x=wx+150,y=hy+100)
  69. wx=450
  70. hy=350
  71. open_s = tk.Button(top,text='开风',height=2,width=15,command=open_s)
  72. open_s.place(x=wx+150,y=hy)
  73. stop_s = tk.Button(top,text='关风',height=2,width=15,command=stop_s)
  74. stop_s.place(x=wx+150,y=hy+200)
  75. light_open = tk.Button(top,text='闪开',height=2,width=15,command=light_open)
  76. light_open.place(x=wx,y=hy+100)
  77. light_close = tk.Button(top,text='闪关',height=2,width=15,command=light_close)
  78. light_close.place(x=wx+300,y=hy+100)
  79. setV = tk.Button(top,text='设速',height=2,width=15,command=setV)
  80. setV.place(x=wx+150,y=hy+100)
  81. while True:
  82.   top.update()
  83.   top.after(100)
  84. top.mainloop()  
复制代码

【小车代码】
【Mind+Python】无线控车图7

Arduino C代码(同上)
  1. /*!
  2. * MindPlus
  3. * microbit
  4. *
  5. */
  6. #include <Microbit_Motor.h>
  7. #include <Microbit_Matrix.h>
  8. #include <DFMicrobit_Radio.h>
  9. // 动态变量
  10. String         mind_s_MingLing;
  11. volatile float mind_n_CheSu;
  12. // 函数声明
  13. void onRadioReceive(String radio_message);
  14. // 静态常量
  15. const uint8_t bbcBitmap[][5] = {
  16.         {B01010,B10101,B10001,B01010,B00100}
  17. };
  18. // 创建对象
  19. Microbit_Motor motorbit;
  20. // 主程序开始
  21. void setup() {
  22.         Radio.setCallback(onRadioReceive);
  23.         Radio.setGroup(7);
  24.         Radio.turnOn();
  25.         MMatrix.show(bbcBitmap[0]);
  26.         digitalWrite(12, LOW);
  27.         digitalWrite(13, LOW);
  28.         mind_n_CheSu = 30;
  29. }
  30. void loop() {
  31. }
  32. // 事件回调函数
  33. void onRadioReceive(String radio_message) {
  34.         mind_s_MingLing = radio_message;
  35.         if ((mind_s_MingLing==String("A"))) {
  36.                 motorbit.motorRun(M1, CW, mind_n_CheSu);
  37.                 motorbit.motorRun(M2, CCW, mind_n_CheSu);
  38.                 digitalWrite(12, LOW);
  39.                 digitalWrite(13, LOW);
  40.         }
  41.         if ((mind_s_MingLing==String("B"))) {
  42.                 motorbit.motorRun(M1, CCW, mind_n_CheSu);
  43.                 motorbit.motorRun(M2, CW, mind_n_CheSu);
  44.                 digitalWrite(12, HIGH);
  45.                 digitalWrite(13, HIGH);
  46.         }
  47.         if ((mind_s_MingLing==String("C"))) {
  48.                 motorbit.motorStop(M1);
  49.                 motorbit.motorRun(M2, CCW, mind_n_CheSu);
  50.                 digitalWrite(12, HIGH);
  51.                 digitalWrite(13, LOW);
  52.         }
  53.         if ((mind_s_MingLing==String("D"))) {
  54.                 motorbit.motorStop(M2);
  55.                 motorbit.motorRun(M1, CW, mind_n_CheSu);
  56.                 digitalWrite(12, LOW);
  57.                 digitalWrite(13, HIGH);
  58.         }
  59.         if ((mind_s_MingLing==String("E"))) {
  60.                 motorbit.motorStop(ALL);
  61.                 digitalWrite(12, LOW);
  62.                 digitalWrite(13, LOW);
  63.         }
  64.         if ((mind_s_MingLing==String("F"))) {
  65.                 digitalWrite(16, HIGH);
  66.         }
  67.         if ((mind_s_MingLing==String("G"))) {
  68.                 digitalWrite(16, LOW);
  69.         }
  70.         if ((mind_s_MingLing==String("H"))) {
  71.                 digitalWrite(12, HIGH);
  72.                 digitalWrite(13, HIGH);
  73.         }
  74.         if ((mind_s_MingLing==String("I"))) {
  75.                 digitalWrite(12, LOW);
  76.                 digitalWrite(13, LOW);
  77.         }
  78.         if (((String(mind_s_MingLing).length())>1)) {
  79.                 mind_n_CheSu = (String(mind_s_MingLing).toInt());
  80.                 MMatrix.print(mind_s_MingLing);
  81.         }
  82. }
复制代码


【演示视频】

前进不走直线,问题原因:1、速度快2、两轮驱动

IMG_20210811_130205.jpg

gray6666  初级技神

发表于 2021-8-11 16:34:24

四大金刚啊
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail