Cozmo人工智能机器人SDK使用笔记(5)-时序部分async_sync
Cozmo首先寻找一个立方体。 找到立方体后,立方体的灯以循环方式绿色闪烁,然后等待轻敲立方体。
此时,程序分别为同步和异步两种类型,注意区分。
1. 同步
立方体闪烁同步示例
import asyncio
import sysimport cozmoclass BlinkyCube(cozmo.objects.LightCube):'''Subclass LightCube and add a light-chaser effect.'''def __init__(self, *a, **kw):super().__init__(*a, **kw)self._chaser = Nonedef start_light_chaser(self):'''Cycles the lights around the cube with 1 corner lit up green,changing to the next corner every 0.1 seconds.'''if self._chaser:raise ValueError("Light chaser already running")async def _chaser():while True:for i in range(4):cols = [cozmo.lights.off_light] * 4cols[i] = cozmo.lights.green_lightself.set_light_corners(*cols)await asyncio.sleep(0.1, loop=self._loop)self._chaser = asyncio.ensure_future(_chaser(), loop=self._loop)def stop_light_chaser(self):if self._chaser:self._chaser.cancel()self._chaser = None# Make sure World knows how to instantiate the subclass
cozmo.world.World.light_cube_factory = BlinkyCubedef cozmo_program(robot: cozmo.robot.Robot):cube = Nonelook_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)try:cube = robot.world.wait_for_observed_light_cube(timeout=60)except asyncio.TimeoutError:print("Didn't find a cube :-(")returnfinally:look_around.stop()cube.start_light_chaser()try:print("Waiting for cube to be tapped")cube.wait_for_tap(timeout=10)print("Cube tapped")except asyncio.TimeoutError:print("No-one tapped our cube :-(")finally:cube.stop_light_chaser()cube.set_lights_off()cozmo.run_program(cozmo_program)
2. 异步
立方体闪烁异步示例
注意注释(效果相似但实现过程有差异):
The async equivalent of 01_cube_blinker_sync.
The usage of ``async def`` makes the cozmo_program method a coroutine.
Within a coroutine, ``await`` can be used. With ``await``, the statement
blocks until the request being waited for has completed. Meanwhile
the event loop continues in the background.For instance, the statement
``await robot.world.wait_for_observed_light_cube(timeout=60)``
blocks until Cozmo discovers a light cube or the 60 second timeout
elapses, whichever occurs first.Likewise, the statement ``await cube.wait_for_tap(timeout=10)``
blocks until the tap event is received or the 10 second timeout occurs,
whichever occurs first.For more information, see
https://docs.python.org/3/library/asyncio-task.html
import asyncio
import sysimport cozmoclass BlinkyCube(cozmo.objects.LightCube):'''Subclass LightCube and add a light-chaser effect.'''def __init__(self, *a, **kw):super().__init__(*a, **kw)self._chaser = Nonedef start_light_chaser(self):'''Cycles the lights around the cube with 1 corner lit up green,changing to the next corner every 0.1 seconds.'''if self._chaser:raise ValueError("Light chaser already running")async def _chaser():while True:for i in range(4):cols = [cozmo.lights.off_light] * 4cols[i] = cozmo.lights.green_lightself.set_light_corners(*cols)await asyncio.sleep(0.1, loop=self._loop)self._chaser = asyncio.ensure_future(_chaser(), loop=self._loop)def stop_light_chaser(self):if self._chaser:self._chaser.cancel()self._chaser = None# Make sure World knows how to instantiate the subclass
cozmo.world.World.light_cube_factory = BlinkyCubeasync def cozmo_program(robot: cozmo.robot.Robot):'''The async equivalent of 01_cube_blinker_sync.The usage of ``async def`` makes the cozmo_program method a coroutine.Within a coroutine, ``await`` can be used. With ``await``, the statementblocks until the request being waited for has completed. Meanwhilethe event loop continues in the background.For instance, the statement``await robot.world.wait_for_observed_light_cube(timeout=60)``blocks until Cozmo discovers a light cube or the 60 second timeoutelapses, whichever occurs first.Likewise, the statement ``await cube.wait_for_tap(timeout=10)``blocks until the tap event is received or the 10 second timeout occurs,whichever occurs first.For more information, seehttps://docs.python.org/3/library/asyncio-task.html'''cube = Nonelook_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)try:cube = await robot.world.wait_for_observed_light_cube(timeout=60)except asyncio.TimeoutError:print("Didn't find a cube :-(")returnfinally:look_around.stop()cube.start_light_chaser()try:print("Waiting for cube to be tapped")await cube.wait_for_tap(timeout=10)print("Cube tapped")except asyncio.TimeoutError:print("No-one tapped our cube :-(")finally:cube.stop_light_chaser()cube.set_lights_off()cozmo.run_program(cozmo_program)
Fin
Cozmo人工智能机器人SDK使用笔记(5)-时序部分async_sync相关推荐
- Cozmo人工智能机器人SDK使用笔记(1)-基础部分basics
APP和SDK有对应关系 如(3.0.0和1.4.6)或(3.1.0和1.4.7).不严格对应,无法正常使用SDK. cozmosdk.anki.com/docs/ Cozmo SDK经常更新,以便提 ...
- Cozmo人工智能机器人SDK使用笔记(4)-任务部分cubes_and_objects
先简单总结一下使用笔记1-3: 基础部分介绍了一些常用功能,比如运动控制.LED显示和扬声器交互等 人机接口显示部分--输出,cozmo面部显示屏输出一些基本信息 人机接口视觉部分--输入,cozmo ...
- Cozmo人工智能机器人SDK使用笔记(2)-显示部分face
这篇博文针对SDK教程中的第二部分cozmo_face进行简单介绍,如下: face是cozmo显示的核心部分: 来学习一下,如何操作吧- 分为3个文件,如上图所示. 1. face image co ...
- Cozmo人工智能机器人SDK使用笔记(3)-视觉部分vision
关于机器人感知-视觉部分,有过一次公开分享,讲稿全文和视屏实录,参考如下CSDN链接: 机器人感知-视觉部分(Robotic Perception-Vision Section): https://b ...
- Cozmo人工智能机器人SDK使用笔记(6)-并行部分Parallel_Action
Cozmo并行动作示例. 此示例演示如何并行(而不是按顺序)执行动作. import sys import timetry:from PIL import Image except ImportErr ...
- Vector人工智能机器人SDK使用笔记
Cozmo是2016年推出的,2两年后的2018年Vector上市,具备语音助手和更多功能,元件数由300+升级到700+. Vector的SDK具体说明在:developer.anki.com/ve ...
- ROS2GO之手机连接Cozmo人工智能机器人玩具
ROS2GO之手机连接Cozmo人工智能机器人玩具 参考anki的开发者页面,硬件基础: 1. Cozmo机器人:: 2. 兼容Cozmo APP的手机或平板:: 3. 运行Windows.MacOS ...
- Cozmo人工智能机器人玩具/教具完整版中文说明书和介绍(附应用下载链接)
Cozmo(Anki)人工智能AI机器人Robot完整版中文说明书和介绍 (附应用下载链接)https://download.csdn.net/download/zhangrelay/10854427 ...
- 曾推出Anki Drive和Cozmo人工智能机器人的独角兽企业Anki谢幕
Anki DRIVE | 极客东东 听说 Anki 出事了,即将在未来的一周关闭,公司近200名员工将获得一周薪酬补偿.很遗憾这家小林很欣赏的初创公司,前后融资多达2亿美元,其实他们已经很有钱了,但却 ...
最新文章
- 引用(Reference)
- 愤怒的小鸟素材包_点映预售开启|愤怒的小鸟2搞笑升级,萌贱无敌!
- 程序员入职锦囊妙计 --读书笔记
- 《C++入门经典(第6版)》——1.5 问与答
- 第一次能够在电影开场前20分钟到的经历:感谢滴答清单
- 全能HOOK框架 JNI NATIVE JAVA ART DALVIK
- pythonselenium获取html标签内容_python selenium 如何获取网页页面所有可以点击的元素?...
- 第74句Lies, Damned Lies And Statistics: How Bad Statistics Are Feeding Fake News
- 如何选择毕业设计的题目?
- 《计算机组网试验-DNS域名服务协议 》杭州电子科技大学
- go 并发编程之-工作池
- ARM9开发之学习过程总结
- Apache Avro 入门
- 【循环自相关和循环谱系列6】信号的循环平稳性(循环自相关函数)基本原理及推导
- CSS3 @Media 媒体查询
- 等一个人好累,爱一个人好苦
- 翼支付成烫手山芋 被电信“倒手”后的几大猜想
- Shiro 权限绕过漏洞分析(CVE-2020-1957)
- Uboot sandbox
- 免费送书 | 《自动化测试实战宝典:Robot Framework + Python从小工到专家》