如何用Lego Boost构建机器人
要构建机器人,请安装Boost app并按照相关教程进行操作。

Python编程要求
-
下载Dynamsoft Barcode Reader for Linux。解压缩包后,将libDynamsoftBarcodeReader.so 复制 到 / usr / lib。
-
获取Dynamsoft Barcode SDK的30天免费试用许可证。
-
获取 Dynamsoft Python条形码模块的源代码,并按照步骤进行构建和安装。
-
安装一个蓝牙后端:
pip install pygattpip install gattpip install gattlibpip install bluepy
-
安装pylgbst与Lego Boost Move Hub进行交互:
pip install https://github.com/undera/pylgbst/archive/1.0.tar.gz
-
安装OpenCV Python:
pip install opencv-python# orpip3 install opencv-python
如何控制Lego Boost机器人扫描条形码
使用OpenCV创建摄像机视图窗口:
vc = cv2.VideoCapture(0)vc.set(3, 640) #set widthvc.set(4, 480) #set height if vc.isOpened(): # try to get the first frame rval, frame = vc.read()else: return windowName = "Robot View" try: while True: rval, frame = vc.read() cv2.imshow(windowName, frame)
由于GIL(Python Global Interpreter Lock)全局锁,我们需要在另一个进程而不是线程中运行蓝牙连接代码和条形码解码算法。所有数据(包括关键事件, 络摄像头帧和条形码结果)都通过队列传输:
num = Value('i', 1)result_queue = Queue(1)key_queue = Queue(1)frame_queue = Queue(1)cond = Condition()dbr_proc = Process(target=dbr_run, args=( frame_queue, key_queue, cond, num, result_queue))dbr_proc.start()
以下是关键定义:
- a:左
- d:右
- w:上
- s:下
- q:终止app
- c:捕获图像和扫描条形码
控制乐高升力机器人的代码非常简单。小编用Gatt Backend:
conn GattConnection() try: conn.connect() hub = MoveHub(conn) print('Robot connected') speed = 0.5 if key == ord('a'): # left hub.motor_AB.angled(90, speed * -1, speed) elif key == ord('d'): # right hub.motor_AB.angled(90, speed, speed * -1) elif key == ord('w'): # up hub.motor_AB.start_speed(speed) elif key == ord('s'): # down hub.motor_AB.start_speed(speed * -1) elif key == ord('p'): hub.motor_AB.stop()finally: conn.disconnect()
阅读QR码并将结果放入队列:
conn GattConnection() dbr.initLicense('LICENSE-KEY') if key == ord('c'): inputframe = frame_queue.get() results = dbr.decodeBuffer(inputframe, 0x4000000) if (len(results) > 0): for result in results: print("Type: " + result[0]) print("Value: " + result[1] + "n") result_queue.put(Result(inputframe, results))
使用OpenCV绘制条形码位置和解码文本结果:
ret = result_queue.get_nowait()results = ret.resultsimage = ret.image thickness = 2color = (0,255,0)for result in results: print("barcode format: " + result[0]) print("barcode value: " + result[1]) x1 = result[2] y1 = result[3] x2 = result[4] y2 = result[5] x3 = result[6] y3 = result[7] x4 = result[8] y4 = result[9] cv2.line(image, (x1, y1), (x2, y2), color, thickness) cv2.line(image, (x2, y2), (x3, y3), color, thickness) cv2.line(image, (x3, y3), (x4, y4), color, thickness) cv2.line(image, (x4, y4), (x1, y1), color, thickness) cv2.putText(image, result[1], (min([x1, x2, x3, x4]), min([y1, y2, y3, y4])), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), thickness) cv2.imshow("Localization", image)
运行app
python3 app.py
点击此处下载源代码>>>>>>
购买Dynamsoft Barcode Reader正版授权的朋友可以点击”咨询在线客服“哦~~~

标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!