【智控万物】二哈大麦
【项目意图】
在Mind+python模式下,利用Tkinter库,物联网库siot,实现在电脑显示,二哈识别到的物体的位置。
【硬件】
1、麦昆Plus
2、二哈
二哈识图(HuskyLens)是一款简单易用的人工智能视觉传感器,内置6种功能:人脸识别、物体追踪、物体识别、巡线追踪、颜色识别、标签识别。仅需一个按键即可完成AI训练,摆脱繁琐的训练和复杂的视觉算法,让你更加专注于项目的构思和实现。
3、WIFI IoT模块
WIFI IoT Module是一款支持多编程平台,多物联网平台的WIFI模块。它支持micro:bit、Arduino主控,MakeCode、Mind+、ArduinoIDE多种编程环境;支持EasyIoT、IFTTT、Thingspeak等多种流行的物联网平台。WIFI IoT Module采用易用的Gravity接口,I2C和URAT两种通讯协议,UART模式兼容OBLOQ物联网模块的功能和用法,I2C模式兼容更多主控板,并可以最大限度避免传感器之间的冲突。不论是用于课堂IoT教学,还是家电的物联网改装,选用WIFI IoT Module都是非常好的方案。【Microbit硬件程序】
上传到麦昆Plus上Micro:bit程序(Arduino C)
/*!
* MindPlus
* microbit
*
*/
#include <Microbit_Obloq.h>
#include <Microbit_Matrix.h>
#include <DFRobot_HuskyLens.h>
#include <DFRobot_MaqueenPlus.h>
// 动态变量
String mind_s_MingLing, mind_s_WeiZhiXinXi;
volatile float mind_n_CheSu, mind_n_ShuangShan;
// 函数声明
void obloqMqttEventT0(String& obloq_message);
void obloqMqttEventT1(String& obloq_message);
// 静态常量
const String topics = {"5n92uqBMg","dH6_dBZ7g","QMM4DKG7R","",""};
const uint8_t bbcBitmap[] = {
{B01010,B10101,B10001,B01010,B00100}
};
const MsgHandleCb msgHandles = {obloqMqttEventT0,obloqMqttEventT1,NULL,NULL,NULL};
// 创建对象
DFRobot_HuskyLens huskylens;
Microbit_Obloq olq;
DFRobot_MaqueenPlus maqueenPlus;
// 主程序开始
void setup() {
olq.registerMqttHandle(msgHandles);
huskylens.beginI2CUntilSuccess();
huskylens.writeAlgorithm(ALGORITHM_OBJECT_TRACKING);
olq.startConnect(0, 1, "sxs", "smj080823", "RNZYsQIWg", "gHWLswIWgz", topics, "iot.dfrobot.com.cn", 1883);
MMatrix.show(bbcBitmap);
maqueenPlus.writePid(maqueenPlus.ON);
maqueenPlus.servoRun(maqueenPlus.S3, 110);
mind_n_CheSu = 30;
mind_n_ShuangShan = 0;
Serial.begin(9600);
}
void loop() {
if ((mind_n_ShuangShan==1)) {
maqueenPlus.setRGB(maqueenPlus.ALL, maqueenPlus.RED);
delay(500);
maqueenPlus.setRGB(maqueenPlus.ALL, maqueenPlus.PUT);
delay(500);
}
else {
huskylens.request();
if (huskylens.isLearned(1)) {
if (huskylens.isAppear(1,HUSKYLENSResultBlock)) {
mind_s_MingLing = "";
mind_s_WeiZhiXinXi = (String((String(huskylens.readBlockParameter(1).xCenter))) + String(":"));
mind_s_WeiZhiXinXi = (String(mind_s_WeiZhiXinXi) + String((String((String(huskylens.readBlockParameter(1).yCenter))) + String(":"))));
mind_s_WeiZhiXinXi = (String(mind_s_WeiZhiXinXi) + String((String((String(huskylens.readBlockParameter(1).width))) + String(":"))));
mind_s_WeiZhiXinXi = (String(mind_s_WeiZhiXinXi) + String((String(huskylens.readBlockParameter(1).height))));
olq.publish(olq.topic_2, mind_s_WeiZhiXinXi);
Serial.println(mind_s_WeiZhiXinXi);
delay(500);
}
}
}
}
// 事件回调函数
void obloqMqttEventT0(String& obloq_message) {
mind_s_MingLing = obloq_message;
Serial.println(mind_s_MingLing);
if ((mind_s_MingLing==String("A"))) {
maqueenPlus.motorRun(maqueenPlus.ALL, maqueenPlus.CW, mind_n_CheSu);
maqueenPlus.setRGB(maqueenPlus.ALL, maqueenPlus.PUT);
}
if ((mind_s_MingLing==String("B"))) {
maqueenPlus.motorRun(maqueenPlus.ALL, maqueenPlus.CCW, mind_n_CheSu);
maqueenPlus.setRGB(maqueenPlus.ALL, maqueenPlus.RED);
}
if ((mind_s_MingLing==String("C"))) {
maqueenPlus.motorStop(maqueenPlus.LEFT);
maqueenPlus.motorRun(maqueenPlus.RIGHT, maqueenPlus.CW, mind_n_CheSu);
maqueenPlus.setRGB(maqueenPlus.LEFT, maqueenPlus.RED);
maqueenPlus.setRGB(maqueenPlus.RIGHT, maqueenPlus.PUT);
}
if ((mind_s_MingLing==String("D"))) {
maqueenPlus.motorStop(maqueenPlus.RIGHT);
maqueenPlus.motorRun(maqueenPlus.LEFT, maqueenPlus.CW, mind_n_CheSu);
maqueenPlus.setRGB(maqueenPlus.RIGHT, maqueenPlus.RED);
maqueenPlus.setRGB(maqueenPlus.LEFT, maqueenPlus.PUT);
}
if ((mind_s_MingLing==String("E"))) {
maqueenPlus.motorStop(maqueenPlus.ALL);
maqueenPlus.setRGB(maqueenPlus.ALL, maqueenPlus.PUT);
}
if ((mind_s_MingLing==String("F"))) {
maqueenPlus.servoRun(maqueenPlus.S3, 50);
}
if ((mind_s_MingLing==String("G"))) {
maqueenPlus.servoRun(maqueenPlus.S3, 100);
}
if ((mind_s_MingLing==String("H"))) {
mind_n_ShuangShan = 1;
}
if ((mind_s_MingLing==String("I"))) {
mind_n_ShuangShan = 0;
}
}
void obloqMqttEventT1(String& obloq_message) {
mind_n_CheSu = (String(obloq_message).toInt());
}
mind+上传模式下图形代码
被遮挡部分
【电脑Mind+python代码】
#-*- coding: UTF-8 -*-
# MindPlus
# Python
import siot
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk#图像控件
x0=0
y0=0
x2=0
y2=0
#连接物联网
siot.init(client_id="sxs12345678",server="iot.dfrobot.com.cn",port=1883,user="RNZYsQIWg",password="gHWLswIWgz")
siot.connect()
# 事件回调函数
def on_message_callback(client, userdata, msg):
global x0,y0,x2,y2
infor=str(msg.payload.decode("utf-8"))
list_me=infor.split(":")
x0=int(list_me)-int(list_me)/2
y0=int(list_me)-int(list_me)/2
x1=int(list_me)+int(list_me)/2
y1=int(list_me)-int(list_me)/2
x2=int(list_me)+int(list_me)/2
y2=int(list_me)+int(list_me)/2
x3=int(list_me)-int(list_me)/2
y3=int(list_me)+int(list_me)/2
print(x0)
print(y0)
siot.getsubscribe(topic="QMM4DKG7R")
siot.set_callback(on_message_callback)
siot.loop()
#定义函数,向物联网发送相应控制指令
def forward():#前进
siot.publish(topic="5n92uqBMg", data="A")
def back():#后退
siot.publish(topic="5n92uqBMg", data="B")
def left():#左转
siot.publish(topic="5n92uqBMg", data="C")
def right():#右转
siot.publish(topic="5n92uqBMg", data="D")
def stop():#停止
siot.publish(topic="5n92uqBMg", data="E")
def open_s():#张开
siot.publish(topic="5n92uqBMg", data="F")
def stop_s():#闭合
siot.publish(topic="5n92uqBMg", data="G")
def light_open():#双闪开
siot.publish(topic="5n92uqBMg", data="H")
def light_close():#双闪关
siot.publish(topic="5n92uqBMg", data="I")
#生成窗体,布置相应文本框和按钮
top = tk.Tk()
top.title('控制窗口')
top.geometry('900x600')
image_width = 900
image_height = 600
canvas = Canvas(top,bg = 'white',width = image_width,height = image_height )#绘制画布
canvas.pack()
canvas2 = Canvas(top,bg = 'white',width = 320,height = 240 )#绘制画布
canvas.pack()
canvas2.place(x = 290,y = 100)
img = Image.open('bg.jpg')
bg = ImageTk.PhotoImage(img)
bgid = canvas.create_image(0, 0, image=bg, anchor='nw')
canvas.place(x = 0,y = 0)
#产生标签文本背景透明效果
txtid=canvas.create_text(350,20, fill = 'red',font=("黑体", 35),anchor="nw")
canvas.insert(txtid,1,"二哈大麦")
#Label(top,text = '物联网大麦!',font = ("黑体",14),width =15,height = 1).place(x =400,y = 20,anchor = 'nw')
E1 = Entry(top, bd =5)
E1.pack(side = RIGHT)
E1.place(x=100,y=100)
wx=20
hy=350
def setV():#设置车速
siot.publish(topic="dH6_dBZ7g", data=str(E1.get()))
#生成按钮,并指定相应功能
left = tk.Button(top,text='左转',height=2,width=15,command=left)
left.place(x=wx,y=hy+100)
forward = tk.Button(top,text='前进',height=2,width=15,command=forward)
forward.place(x=wx+150,y=hy)
right = tk.Button(top,text='右转',height=2,width=15,command=right)
right.place(x=wx+300,y=hy+100)
back = tk.Button(top,text='后退',height=2,width=15,command=back)
back.place(x=wx+150,y=hy+200)
stop = tk.Button(top,text='停止',height=2,width=15,command=stop)
stop.place(x=wx+150,y=hy+100)
wx=450
hy=350
open_s = tk.Button(top,text='张开',height=2,width=15,command=open_s)
open_s.place(x=wx+150,y=hy)
stop_s = tk.Button(top,text='闭合',height=2,width=15,command=stop_s)
stop_s.place(x=wx+150,y=hy+200)
light_open = tk.Button(top,text='闪开',height=2,width=15,command=light_open)
light_open.place(x=wx,y=hy+100)
light_close = tk.Button(top,text='闪关',height=2,width=15,command=light_close)
light_close.place(x=wx+300,y=hy+100)
setV = tk.Button(top,text='设速',height=2,width=15,command=setV)
setV.place(x=wx+150,y=hy+100)
while True:
canvas2.delete("all")
canvas2.create_rectangle(x0,y0,x2,y2,fill='blue')
top.update()
top.after(100)
top.mainloop()
【演示视频】
https://v.youku.com/v_show/id_XNTE4OTg2OTQ3Mg==.htmlhttps://v.youku.com/v_show/id_XNTE4OTg2ODE4OA==.html
页:
[1]