通过蓝牙低功耗控制 Sphero Mini 机器人球,可滚动、变色、读取传感器、绘图并与猫玩耍。使用 bleak 实现跨平台 BLE 支持(macOS/Windows/Linux)。
Sphero迷你控制器是一项面向实际任务的技能,主要用于通过蓝牙低能通过 Python 和 暗色控制您的 Sphero迷你机器人球;Features QQ LED控制器 - 改变主LED颜色和后LED强度 X运动 - 滚 i。
从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;
若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
通过 Bluetooth Low Energy(蓝牙低功耗)使用 Python 和 bleak 控制你的 Sphero Mini 机器人球。
所有平台:
pip3 install bleak
macOS / Windows:
使用附带的扫描脚本:
python3 scripts/scan_sphero.py
在结果中查找名称类似 "SM-XXXX" 的设备(即 Sphero Mini)。
编辑各脚本文件,将其中的 SPHERO_MAC 替换为你设备的实际地址。
python3 scripts/scan_sphero.py
import asyncio
from sphero_mini_bleak import SpheroMini
async def change_color():
sphero = SpheroMini("YOUR-MAC-ADDRESS")
await sphero.connect()
await sphero.wake()
# 设置为红色
await sphero.setLEDColor(255, 0, 0)
await asyncio.sleep(2)
await sphero.disconnect()
asyncio.run(change_color())
import asyncio
from sphero_mini_bleak import SpheroMini
async def roll_forward():
sphero = SpheroMini("YOUR-MAC-ADDRESS")
await sphero.connect()
await sphero.wake()
# 以速度 100 向前滚动
await sphero.roll(100, 0)
await asyncio.sleep(3)
# 停止
await sphero.roll(0, 0)
await sphero.disconnect()
asyncio.run(roll_forward())
python3 scripts/cat_play.py
使 Sphero Mini 在 1 分钟内随机运动并变换颜色——非常适合逗猫!
# 绘制正方形 python3 scripts/draw_square.py # 绘制星形 python3 scripts/draw_star.py
# 设置指定颜色 python3 scripts/set_color.py red python3 scripts/set_color.py 255 0 128 # 自定义 RGB 值
# 滚动(速度:0–255;朝向:0–359 度) await sphero.roll(speed=100, heading=0) # 向前 await sphero.roll(100, 90) # 向右 await sphero.roll(100, 180) # 向后 await sphero.roll(100, 270) # 向左 await sphero.roll(0, 0) # 停止
# 主 LED 颜色(RGB 值范围:0–255) await sphero.setLEDColor(red=255, green=0, blue=0) # 红色 await sphero.setLEDColor(0, 255, 0) # 绿色 await sphero.setLEDColor(0, 0, 255) # 蓝色 await sphero.setLEDColor(128, 0, 128) # 紫色 # 背光 LED 亮度(0–255) await sphero.setBackLED(255) # 最大亮度 await sphero.setBackLED(0) # 关闭
# 从休眠中唤醒
await sphero.wake()
# 进入休眠(低功耗,BLE 仍保持开启)
await sphero.sleep()
# 查询电池电压
voltage = await sphero.getBatteryVoltage()
print(f"Battery: {voltage}V")
猫玩模式脚本会使 Sphero Mini 执行以下行为:
专为逗猫设计!🐱
sphero_mini_bleak.py 中增大连接超时时间await sphero.wake()await asyncio.sleep(0.5)await sphero.wake()本项目使用以下开源库:
注意:本库仅适用于 Sphero Mini。如需控制其他型号(如 BB8、SPRK+、Bolt),请改用 pysphero。
创建你自己的运动路径:
async def figure_eight():
# 绘制数字“8”轨迹
for i in range(2): # 循环两次
for heading in range(0, 360, 10):
await sphero.roll(80, heading)
await asyncio.sleep(0.1)
async def rainbow():
colors = [
(255, 0, 0), (255, 127, 0), (255, 255, 0),
(0, 255, 0), (0, 0, 255), (75, 0, 130), (148, 0, 211)
]
for r, g, b in colors:
await sphero.setLEDColor(r, g, b)
await asyncio.sleep(1)
MIT
相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 133.9万人学习
共49课时 | 82.5万人学习