Cozmo人工智慧機器人SDK使用筆記(5)-時序部分async_sync
Cozmo首先尋找一個立方體。 找到立方體後,立方體的燈以迴圈方式綠色閃爍,然後等待輕敲立方體。
此時,程式分別為同步和非同步兩種型別,注意區分。
1. 同步
立方體閃爍同步示例
import asyncio
import sys
import cozmo
class BlinkyCube(cozmo.objects.LightCube):
'''Subclass LightCube and add a light-chaser effect.'''
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self._chaser = None
def 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] * 4
cols[i] = cozmo.lights.green_light
self.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 = BlinkyCube
def cozmo_program(robot: cozmo.robot.Robot):
cube = None
look_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 :-(")
return
finally:
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 sys
import cozmo
class BlinkyCube(cozmo.objects.LightCube):
'''Subclass LightCube and add a light-chaser effect.'''
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self._chaser = None
def 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] * 4
cols[i] = cozmo.lights.green_light
self.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 = BlinkyCube
async 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 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
'''
cube = None
look_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 :-(")
return
finally:
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使用筆記(9)-判斷部分if_this_then_that人工智慧機器人筆記
- Cozmo人工智慧機器人SDK使用筆記(1)-基礎部分basics人工智慧機器人筆記
- Cozmo人工智慧機器人SDK使用筆記(2)-顯示部分face人工智慧機器人筆記
- Cozmo人工智慧機器人SDK使用筆記(3)-視覺部分vision人工智慧機器人筆記視覺
- Cozmo人工智慧機器人SDK使用筆記(8)-應用部分apps人工智慧機器人筆記APP
- Cozmo人工智慧機器人SDK使用筆記(6)-並行部分Parallel_Action人工智慧機器人筆記並行Parallel
- Cozmo人工智慧機器人SDK使用筆記(4)-任務部分cubes_and_objects人工智慧機器人筆記Object
- Cozmo人工智慧機器人SDK使用筆記(7)-補充說明人工智慧機器人筆記
- Vector人工智慧機器人SDK使用筆記人工智慧機器人筆記
- Anki Cozmo(Vector)人工智慧機器人玩具部分文件人工智慧機器人
- Cozmo人工智慧機器人SDK使用筆記(X)-總結- |人工智慧基礎(中小學版)實踐平臺|人工智慧機器人筆記
- Cozmo機器人體驗:好迷你的人工智慧玩具機器人人工智慧
- ROS2GO之手機連線Cozmo人工智慧機器人玩具ROSGo人工智慧機器人
- Cozmo機器人脫離智慧手機使用的不完全攻略機器人
- Cozmo機器人使用中文Scratch3程式設計案例(codelab)機器人程式設計
- ROS2GO+Cozmo=口袋機器人之人工智慧模擬和實驗平臺ROSGo機器人人工智慧
- ABB機器人套介面通訊 機器人部分機器人
- 關於聊天機器人的閱讀筆記機器人筆記
- Cozmo人工智慧機器人玩具/教具完整版中文說明書和介紹(附應用下載連結)人工智慧機器人
- 一個使用Python的人工智慧聊天機器人框架Python人工智慧機器人框架
- Vector人工智慧情感機器人SDK釋出和說明(ROS2GO 2.0 + Vector 2.0)人工智慧機器人ROSGo
- 江湖微信公眾號機器人飛單機器人娛樂系統部分原始碼機器人原始碼
- 使用人工智慧機器人提高農業效率 | 資料標註人工智慧機器人
- 人工智慧NLP專案_QA機器人(7)人工智慧機器人
- 用AIML開發人工智慧聊天機器人AI人工智慧機器人
- Git筆記-部分命令Git筆記
- 人類輸了:人工智慧以及機器人可能會統治人類人工智慧機器人
- V-rep學習筆記:機器人路徑規劃1筆記機器人
- 讀寫給大家的AI極簡史筆記05機器人AI筆記機器人
- 人工智慧機器人搶人飯碗?蓋茨有話說人工智慧機器人
- 人工智慧機器人搶小編工作,還讓不讓人活?人工智慧機器人
- 多級時序系統:機器週期,時鐘週期
- PC個人微信機器人sdk介面api之微信多開原理機器人API
- 人工智慧與智慧系統2-> 機器人學2 | 時間與運動人工智慧機器人
- 諦聽機器人:深度參與人工智慧革命機器人人工智慧
- Windows sdk程式設計筆記Windows程式設計筆記
- 機器人搭建記錄 HoshinoBot機器人
- Code Complete部分筆記筆記