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相关推荐

  1. 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经常更新,以便提 ...

  2. Cozmo人工智能机器人SDK使用笔记(4)-任务部分cubes_and_objects

    先简单总结一下使用笔记1-3: 基础部分介绍了一些常用功能,比如运动控制.LED显示和扬声器交互等 人机接口显示部分--输出,cozmo面部显示屏输出一些基本信息 人机接口视觉部分--输入,cozmo ...

  3. Cozmo人工智能机器人SDK使用笔记(2)-显示部分face

    这篇博文针对SDK教程中的第二部分cozmo_face进行简单介绍,如下: face是cozmo显示的核心部分: 来学习一下,如何操作吧- 分为3个文件,如上图所示. 1. face image co ...

  4. Cozmo人工智能机器人SDK使用笔记(3)-视觉部分vision

    关于机器人感知-视觉部分,有过一次公开分享,讲稿全文和视屏实录,参考如下CSDN链接: 机器人感知-视觉部分(Robotic Perception-Vision Section): https://b ...

  5. Cozmo人工智能机器人SDK使用笔记(6)-并行部分Parallel_Action

    Cozmo并行动作示例. 此示例演示如何并行(而不是按顺序)执行动作. import sys import timetry:from PIL import Image except ImportErr ...

  6. Vector人工智能机器人SDK使用笔记

    Cozmo是2016年推出的,2两年后的2018年Vector上市,具备语音助手和更多功能,元件数由300+升级到700+. Vector的SDK具体说明在:developer.anki.com/ve ...

  7. ROS2GO之手机连接Cozmo人工智能机器人玩具

    ROS2GO之手机连接Cozmo人工智能机器人玩具 参考anki的开发者页面,硬件基础: 1. Cozmo机器人:: 2. 兼容Cozmo APP的手机或平板:: 3. 运行Windows.MacOS ...

  8. Cozmo人工智能机器人玩具/教具完整版中文说明书和介绍(附应用下载链接)

    Cozmo(Anki)人工智能AI机器人Robot完整版中文说明书和介绍 (附应用下载链接)https://download.csdn.net/download/zhangrelay/10854427 ...

  9. 曾推出Anki Drive和Cozmo人工智能机器人的独角兽企业Anki谢幕

    Anki DRIVE | 极客东东 听说 Anki 出事了,即将在未来的一周关闭,公司近200名员工将获得一周薪酬补偿.很遗憾这家小林很欣赏的初创公司,前后融资多达2亿美元,其实他们已经很有钱了,但却 ...

最新文章

  1. 引用(Reference)
  2. 愤怒的小鸟素材包_点映预售开启|愤怒的小鸟2搞笑升级,萌贱无敌!
  3. 程序员入职锦囊妙计 --读书笔记
  4. 《C++入门经典(第6版)》——1.5 问与答
  5. 第一次能够在电影开场前20分钟到的经历:感谢滴答清单
  6. 全能HOOK框架 JNI NATIVE JAVA ART DALVIK
  7. pythonselenium获取html标签内容_python selenium 如何获取网页页面所有可以点击的元素?...
  8. 第74句Lies, Damned Lies And Statistics: How Bad Statistics Are Feeding Fake News
  9. 如何选择毕业设计的题目?
  10. 《计算机组网试验-DNS域名服务协议 》杭州电子科技大学
  11. go 并发编程之-工作池
  12. ARM9开发之学习过程总结
  13. Apache Avro 入门
  14. 【循环自相关和循环谱系列6】信号的循环平稳性(循环自相关函数)基本原理及推导
  15. CSS3 @Media 媒体查询
  16. 等一个人好累,爱一个人好苦
  17. 翼支付成烫手山芋 被电信“倒手”后的几大猜想
  18. Shiro 权限绕过漏洞分析(CVE-2020-1957)
  19. Uboot sandbox
  20. 免费送书 | 《自动化测试实战宝典:Robot Framework + Python从小工到专家》

热门文章

  1. 人生三量:度量,胆量,心量
  2. 巴科斯-诺尔(BNF范式)范式
  3. 使用Python制作爬虫程序总结
  4. c语言绝对值题目,初中数学绝对值的练习题(整理)
  5. 蜜罐系统 – Artillery 0.5.1 alpha发布
  6. 高精密切割加工,博捷芯划片机
  7. ios之模拟器(一)
  8. 工业互联网---人工智能技术-仿谷歌图片搜索算法SimilarImageSearch
  9. 二极管:Irush与我何干?
  10. 【字符集】emoji字符集 - 旗帜、国旗、区旗等