惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
月光博客
月光博客
有赞技术团队
有赞技术团队
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
WordPress大学
WordPress大学
Jina AI
Jina AI
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
B
Blog RSS Feed
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
云风的 BLOG
云风的 BLOG
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
I
InfoQ
Vercel News
Vercel News
C
Check Point Blog
美团技术团队
V
V2EX
量子位
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
G
Google Developers Blog
博客园_首页
J
Java Code Geeks
Recent Announcements
Recent Announcements
人人都是产品经理
人人都是产品经理
H
Help Net Security
博客园 - Franky
The GitHub Blog
The GitHub Blog
V
Visual Studio Blog
T
Tailwind CSS Blog
IT之家
IT之家
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
L
LangChain Blog
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟 从Python转向Pharo https://blog.just4fun.site/post/iot/iot-open-source-projects/ Python异步编程笔记 https://blog.just4fun.site/post/iot/iot-open-source-hardware-community/ 万物积木化开发者社区 CodeLab ❤️ Blender Scratch3技术分析之云变量 API(第7篇) [译]对管道(Pipes)的偏爱 [译]提出正确的问题比得到正确答案更重要 蓝牙设备与Scratch3.0 创建你的第一个Scratch3.0 Extension Scratch3技术分析之项目内部数据(第6篇) Scratch3技术分析之社区 API(第5篇) Scratch3技术分析之User API(第4篇) Scratch3技术分析之项目主页API(第3篇) Scratch3技术分析之静态资源API(第2篇) Scratch3.0、micro:bit与Windows7 https://blog.just4fun.site/post/iot/zerynth-vs-micropython/ 核聚变、方所与半宅空间 可视化编程为何是个糟糕的主意 codelab.club周末聚会 关于codelab.club '下一件大事'是一个房间 Hungry Robot - Eat everything 编程作为一种思考方式 今日简史 史蒂夫·乔布斯传 罗素自选文集 https://blog.just4fun.site/post/edx/tianjin-scratch-ai/ https://blog.just4fun.site/post/edx/richie-cms-openedx/ 徒步武功山 WebUSB与micro:bit 积木化编程与3D场景 夜宿武功山顶 scratch3-adapter接入优必选Alpha系列机器人 https://blog.just4fun.site/post/edx/video-migration-note/ scratch3-adapter重构笔记 https://blog.just4fun.site/post/edx/edx-community-members/ 两种硬件编程风格的比较 使用micro:bit自制PPT翻页笔 柏拉图对话集 scratch3.0 + micro:bit 七月电影放映计划 非营利组织的管理 Screenly--用树莓派让任何屏幕变为可编程的数字标牌 以最佳实践开始你的Django项目 micro:bit与事件驱动 为Scratch3.0设计的插件系统(上篇) OCR应用一例 近两年读过的一些好书 blockly开发之使用python驱动浏览器中的turtle(2) 牛顿新传 文学理论入门 逻辑的引擎 人生的意义 blockly开发之生成并运行js代码(1) blockly开发之hello world(0) micro:bit使用笔记 神器之Termux https://blog.just4fun.site/post/iot/micropython-notes/ Cozmo what is this Scratch的前世今生 下段旅程 我行在远方 爆裂 途中杂记 https://blog.just4fun.site/post/edx/open-edx-startup/ cozmo系列之入门 - 有性格且可编程的机器人 PaperWeekly开发笔记 创业二三事
CoCube library for Snap!
种瓜 · 2024-07-03 · via 夜行人

Reminder: here is the first paper of CoCube

The previous article mentioned:

Dr. Liang created a desktop-level robot named “CoCube” (Collaborative-Cube). CoCube uses MicroBlocks as its programming environment. CoCube has many similar features to toio, but also some capabilities that toio does not have, such as extensible hardware interfaces and a closer integration with MicroBlocks.

Recently, I make a CoCube library for Snap!

Motivation

CoCube can already be programmed in MicroBlocks (thanks to Dr. Liang for the excellent work!). There are two motivations for creating the CoCube library for Snap!:

  • Use the libraries of Snap! for CoCube
  • Making advanced libraries in a host computer with stronger computing power
    • toio also does it this way
    • As Dr. Liang said, “I did the same thing in the ROS system before: the host computer undertakes most of the computing. And send the final angular velocity or linear velocity instructions to the robot”

Idea

The key in making great and growable systems is much more to design how its modules communicate rather than what their internal properties and behaviors should be. – Alan Kay

Because Snap! and MicroBlocks are both products of the personal computing spirit, both possess exceptional dynamism, I am confident that the goal(programming MicroBlocks devices in Snap!) can be achieved through message passing. In fact, MicroBlocks IDE collaborates with VM through message passing mechanism.

I like to use message passing to design the interoperability mechanism between systems, as this can minimize the coupling between systems.

There is already a library in Snap! for directly passing messages between Snap! and MicroBlocks

This library provides the basic functionality for message passing. I intend to use this library to create the CoCube library, wanting to achieve:

  1. Users can get the status of MicroBlocks devices(CoCube) in Snap! (by reporter blocks)
  2. Users can send commands (by command blocks) to MicroBlocks devices(CoCube) in Snap!
  3. Users only need to program in Snap! without switching back and forth between Snap! and MicroBlocks.
  4. As good extensibility as possible

Regarding point 1: get the status of another system. There are usually two ways to do it:

  • push style: A system continuously broadcasts messages carrying status data to another system
  • pull style: Query the status, usually using request-response style communication.
    • RPC/HTTP is this style
    • Dynatalk is this style

The MicroBlocks messaging(BLE) library provides a PubSub style message passing mechanism. I intend to build dynatalk-over-ble based on it, and then implement a pull style MicroBlocks Client library based on dynatalk-over-ble (similar to dynatalk-over-postmessage)

In our use case, we only need to implement a unidirectional C/S architecture (Snap! as the client, MicroBlocks as the server). Therefore, the complexity of dynatalk-over-ble is only half that of regular dynatalk. The good news is that the half handled by the computationally weaker MicroBlocks is extremely simple.

Implement

On MicroBlocks side

As mentioned earlier, we want:

Users only need to program in Snap! without switching back and forth between Snap! and MicroBlocks.

Therefore, I hope the code on the MicroBlocks side is completely universal. Users only need to save this universal program to the device and then no longer need to open the MicroBlocks platform, allowing them to program the device entirely in Snap!

To achieve this, we save this universal program to the device:

This program is responsible for the following tasks:

  • Interpret the received message and “ground” the semantics of the message to the specific functions of the hardware device. The “grounding” work is implemented using the call block, which is similar to eval in other programming languages and can dynamically run custom blocks and VM primitives.
    • handle_message block is used to handle non-blocking messages
    • handle_blocking_message block is used to handle blocking messages

You can think of this program as a server running on hardware, and Snap! as its client. We used the C/S architecture.

On Snap! side

As mentioned earlier, we want:

  1. Users can get the status of MicroBlocks devices(CoCube) in Snap! (by reporter blocks)
  2. Users can send commands (by command blocks) to MicroBlocks devices(CoCube) in Snap!

We show how they are implemented separately.

For point 1, get the status of MicroBlocks devices(CoCube) in Snap! (by reporter blocks):

For point 2, send commands (by command blocks) to MicroBlocks devices(CoCube) in Snap!

The message is in pure string form, carrying the method name (custom block name or vm primitives) and parameters, separated by ,. Here is an example: call, <msg-id>, Move Forward, 50

How to use CoCube library?

To use the CoCube library, simply open the Snap!.

Load the CoCube library,

Click open MicroBlocks project block to open the MicroBlocks window. After connecting the device and uploading the program, you can disconnect the device and close the MicroBlocks window.

Then start programming in Snap!.

You can even use any vm primitive:

FAQ

How to add more CoCube library?

Download https://snap.codelab.club/libraries/CoCube2.xml. (Open in browser, save as local file)

then:

  • Renamed CoCube2.xml to CoCube3.xml
  • Edit CoCube3.xml:
    • Replace all 🚙 to another emoji icon (eg: 🚕) (VSCode has a batch replace function)
    • Replace all 🚕 CoCube2 with 🚕 CoCube3
    • Replace all _CoCube2_ with _CoCube3_
  • Drag CoCube3.xml into Snap!.

By analogy, you can add any number of CoCube library!

Usage scenarios

When there are multiple MicroBlocks devices, Snap! is perfect as a dashboard to control them all.

How to debug Snap! and MicroBlocks code simultaneously?

Yes, you can debug both software and hardware side code simultaneously! Everything is liveness!

You can connect the same device simultaneously in Snap! and MicroBlocks , then observe the flow of messages between them:

How to save and load MicroBlocks projects in the MicroBlocks window of Snap!?

Save project

Due to iframe limitations, you cannot use the Save button in the menu to save local files

Alternative solution: click Copy project url to clipboard , then paste it into the browser’s address bar, and then save it.

open project

Due to iframe limitations, you cannot use the Open button in the menu to open local files

Alternative solution: drag the local files directly into the MicroBlocks window.

How to find the vm primitives provided by MicroBlocks?

You can find these primitives in the ubl library file, in the form of . [a:b] , such as [display:mbPlot]

Peter shared some examples on Discord:

the push style version

On MicroBlocks side:

This program is responsible for the following tasks:

  1. Continuously push out the status of the device (when started block), it can be extended. The current update rate is 20/second (customizable).
  2. Interpret the received message ( handle_message block), grounding the semantics of the message to the specific functions of the hardware device. The “grounding” work is achieved using the call block, which is similar to eval in other programming languages, allowing for the dynamic execution of custom blocks and VM primitives.

Project link (Click to Run)

It is worth noting that the speed of push is much faster than pull. If necessary, they can be combined.

Is it possible to create a similar Python client library?

The design of this article can be easily migrated to Python. By using the microblocks_messaging_library and dynatalk-py libraries, it is easy to achieve the same functionality as the MicroBlocks Client library of Snap!.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# pip install -U cocube
from cocube import CoCube

client = CoCube()
found_devices = client.discover(timeout=3) # timeout=5 in windows
print("found devices:", found_devices)
client.connect("MicroBlocks QCQ", timeout=3)

client.display_character("c")

while True:
    position_x = client.position_x
    print(position_x)

Remember to first load this program into the device (same as Snap!)

For more API examples: notebooks

The source code of the CoCube library

Connect multiple devices

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from cocube import CoCube

client = CoCube()
found_devices = client.discover(timeout=5)
print("found devices:", found_devices)
# connect all discovered devices
devices = [CoCube(i) for i in found_devices]

for index, device in enumerate(devices):
    device.display_character(index)

Parallel Control

1
2
3
4
5
6
from concurrent.futures import ThreadPoolExecutor

# Launching parallel tasks: https://docs.python.org/3/library/concurrent.futures.html
with ThreadPoolExecutor() as executor:
    for client in devices:
        executor.submit(client.scroll_text, "hello")