Cozmo人工智慧機器人SDK使用筆記(4)-任務部分cubes_and_objects
先簡單總結一下使用筆記1-3:
- 基礎部分介紹了一些常用功能,比如運動控制、LED顯示和揚聲器互動等
- 人機介面顯示部分--輸出,cozmo面部螢幕輸出一些基本資訊
- 人機介面視覺部分--輸入,cozmo攝像頭獲取環境資訊並處理等
接著,就自然過渡到第四部分----立方體和物體任務部分,共有13個專案專題,非常有趣。
Cozmo環顧四周,找尋充電底座圖示,然後行駛到附近,任務完成。
1. go to pose
給定Cozmo目標位置和角度,然後行駛到對應位置和角度。
先設定目標,然後如果將relative_to_robot設定為true,就是相對量,當年機器人為原點,給定目標相對原點的距離和角度。
核心程式碼如下:
def cozmo_program(robot: cozmo.robot.Robot):
robot.go_to_pose(Pose(100, 100, 0, angle_z=degrees(45)), relative_to_robot=True).wait_for_completed()
2. create wall
虛擬牆,並繞過行駛到目標。
核心程式碼:
import cozmo
from cozmo.util import degrees, Pose
def cozmo_program(robot: cozmo.robot.Robot):
fixed_object = robot.world.create_custom_fixed_object(Pose(100, 0, 0, angle_z=degrees(0)),
10, 100, 100, relative_to_robot=True)
if fixed_object:
print("fixed_object created successfully")
robot.go_to_pose(Pose(200, 0, 0, angle_z=degrees(0)), relative_to_robot=True).wait_for_completed()
cozmo.run_program(cozmo_program, use_3d_viewer=True)
3. go to object
Cozmo找到一個立方體(1-3皆可以),然後行駛到對應立方體附近。
def go_to_object_test(robot: cozmo.robot.Robot):
'''The core of the go to object test program'''
# Move lift down and tilt the head up
robot.move_lift(-3)
robot.set_head_angle(degrees(0)).wait_for_completed()
# look around and try to find a cube
look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
cube = None
try:
cube = robot.world.wait_for_observed_light_cube(timeout=30)
print("Found cube: %s" % cube)
except asyncio.TimeoutError:
print("Didn't find a cube")
finally:
# whether we find it or not, we want to stop the behavior
look_around.stop()
if cube:
# Drive to 70mm away from the cube (much closer and Cozmo
# will likely hit the cube) and then stop.
action = robot.go_to_object(cube, distance_mm(70.0))
action.wait_for_completed()
print("Completed action: result = %s" % action)
print("Done.")
4. stack or roll
讓Cozmo依據找到的立方體的數量執行不同的任務。
超時時間預設為10s,環顧四周,找尋立方體,根據數量進行操作:
0---恩,它很生氣
1---翻滾立方體
2---堆疊立方體
import cozmo
def cozmo_program(robot: cozmo.robot.Robot):
lookaround = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
cubes = robot.world.wait_until_observe_num_objects(num=2, object_type=cozmo.objects.LightCube, timeout=10)
print("Found %s cubes" % len(cubes))
lookaround.stop()
if len(cubes) == 0:
robot.play_anim_trigger(cozmo.anim.Triggers.MajorFail).wait_for_completed()
elif len(cubes) == 1:
robot.run_timed_behavior(cozmo.behavior.BehaviorTypes.RollBlock, active_time=60)
else:
robot.run_timed_behavior(cozmo.behavior.BehaviorTypes.StackBlocks, active_time=60)
cozmo.run_program(cozmo_program)
5. cube stack
Cozmo堆疊立方體。Cozmo將等到它看到兩個立方體,然後拿起一個並將其放在另一個立方體上。它會拿起看到的第一個,然後把它放在第二個上。
def cozmo_program(robot: cozmo.robot.Robot):
# Attempt to stack 2 cubes
# Lookaround until Cozmo knows where at least 2 cubes are:
lookaround = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
cubes = robot.world.wait_until_observe_num_objects(num=2, object_type=cozmo.objects.LightCube, timeout=60)
lookaround.stop()
if len(cubes) < 2:
print("Error: need 2 Cubes but only found", len(cubes), "Cube(s)")
else:
# Try and pickup the 1st cube
current_action = robot.pickup_object(cubes[0], num_retries=3)
current_action.wait_for_completed()
if current_action.has_failed:
code, reason = current_action.failure_reason
result = current_action.result
print("Pickup Cube failed: code=%s reason='%s' result=%s" % (code, reason, result))
return
# Now try to place that cube on the 2nd one
current_action = robot.place_on_object(cubes[1], num_retries=3)
current_action.wait_for_completed()
if current_action.has_failed:
code, reason = current_action.failure_reason
result = current_action.result
print("Place On Cube failed: code=%s reason='%s' result=%s" % (code, reason, result))
return
print("Cozmo successfully stacked 2 blocks!")
6. pickup furthest
讓Cozmo拿起最遠的立方體。此例項演示物件位置姿態的簡單數學運算。
(dst = translation.position.x ** 2 + translation.position.y ** 2)
機器人試圖在視覺中找到三個立方體,可以在指令碼開啟的攝像頭視窗中看到執行過程。每個立方體將顯示輪廓和立方體編號。機器人等待直到找到3個立方體,然後嘗試拿起最遠的一個。
import cozmo
def cozmo_program(robot: cozmo.robot.Robot):
lookaround = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
cubes = robot.world.wait_until_observe_num_objects(num=3, object_type=cozmo.objects.LightCube, timeout=60)
lookaround.stop()
max_dst, targ = 0, None
for cube in cubes:
translation = robot.pose - cube.pose
dst = translation.position.x ** 2 + translation.position.y ** 2
if dst > max_dst:
max_dst, targ = dst, cube
if len(cubes) < 3:
print("Error: need 3 Cubes but only found", len(cubes), "Cube(s)")
else:
robot.pickup_object(targ, num_retries=3).wait_for_completed()
cozmo.run_program(cozmo_program, use_viewer=True)
7. look around
Cozmo環顧四周,做出反應,然後拿起並放下一個立方體。
import asyncio
import cozmo
from cozmo.util import degrees
def cozmo_program(robot: cozmo.robot.Robot):
look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
# try to find a block
cube = None
try:
cube = robot.world.wait_for_observed_light_cube(timeout=30)
print("Found cube", cube)
except asyncio.TimeoutError:
print("Didn't find a cube :-(")
finally:
# whether we find it or not, we want to stop the behavior
look_around.stop()
if cube is None:
robot.play_anim_trigger(cozmo.anim.Triggers.MajorFail)
return
print("Yay, found cube")
cube.set_lights(cozmo.lights.green_light.flash())
anim = robot.play_anim_trigger(cozmo.anim.Triggers.BlockReact)
anim.wait_for_completed()
action = robot.pickup_object(cube)
print("got action", action)
result = action.wait_for_completed(timeout=30)
print("got action result", result)
robot.turn_in_place(degrees(90)).wait_for_completed()
action = robot.place_object_on_ground_here(cube)
print("got action", action)
result = action.wait_for_completed(timeout=30)
print("got action result", result)
anim = robot.play_anim_trigger(cozmo.anim.Triggers.MajorWin)
cube.set_light_corners(None, None, None, None)
anim.wait_for_completed()
cozmo.run_program(cozmo_program)
8. drive to charger
讓Cozmo行駛到充電底座附近。
import asyncio
import time
import cozmo
from cozmo.util import degrees, distance_mm, speed_mmps
def drive_to_charger(robot):
'''The core of the drive_to_charger program'''
# If the robot was on the charger, drive them forward and clear of the charger
if robot.is_on_charger:
# drive off the charger
robot.drive_off_charger_contacts().wait_for_completed()
robot.drive_straight(distance_mm(100), speed_mmps(50)).wait_for_completed()
# Start moving the lift down
robot.move_lift(-3)
# turn around to look at the charger
robot.turn_in_place(degrees(180)).wait_for_completed()
# Tilt the head to be level
robot.set_head_angle(degrees(0)).wait_for_completed()
# wait half a second to ensure Cozmo has seen the charger
time.sleep(0.5)
# drive backwards away from the charger
robot.drive_straight(distance_mm(-60), speed_mmps(50)).wait_for_completed()
# try to find the charger
charger = None
# see if Cozmo already knows where the charger is
if robot.world.charger:
if robot.world.charger.pose.is_comparable(robot.pose):
print("Cozmo already knows where the charger is!")
charger = robot.world.charger
else:
# Cozmo knows about the charger, but the pose is not based on the
# same origin as the robot (e.g. the robot was moved since seeing
# the charger) so try to look for the charger first
pass
if not charger:
# Tell Cozmo to look around for the charger
look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
try:
charger = robot.world.wait_for_observed_charger(timeout=30)
print("Found charger: %s" % charger)
except asyncio.TimeoutError:
print("Didn't see the charger")
finally:
# whether we find it or not, we want to stop the behavior
look_around.stop()
if charger:
# Attempt to drive near to the charger, and then stop.
action = robot.go_to_object(charger, distance_mm(65.0))
action.wait_for_completed()
print("Completed action: result = %s" % action)
print("Done.")
cozmo.robot.Robot.drive_off_charger_on_connect = False # Cozmo can stay on charger for now
cozmo.run_program(drive_to_charger, use_viewer=True, force_viewer_on_top=True)
9. custom objects
自定義物件,包括大小和形狀等。
import time
import cozmo
from cozmo.objects import CustomObject, CustomObjectMarkers, CustomObjectTypes
def handle_object_appeared(evt, **kw):
# This will be called whenever an EvtObjectAppeared is dispatched -
# whenever an Object comes into view.
if isinstance(evt.obj, CustomObject):
print("Cozmo started seeing a %s" % str(evt.obj.object_type))
def handle_object_disappeared(evt, **kw):
# This will be called whenever an EvtObjectDisappeared is dispatched -
# whenever an Object goes out of view.
if isinstance(evt.obj, CustomObject):
print("Cozmo stopped seeing a %s" % str(evt.obj.object_type))
def custom_objects(robot: cozmo.robot.Robot):
# Add event handlers for whenever Cozmo sees a new object
robot.add_event_handler(cozmo.objects.EvtObjectAppeared, handle_object_appeared)
robot.add_event_handler(cozmo.objects.EvtObjectDisappeared, handle_object_disappeared)
# define a unique cube (44mm x 44mm x 44mm) (approximately the same size as a light cube)
# with a 30mm x 30mm Diamonds2 image on every face
cube_obj = robot.world.define_custom_cube(CustomObjectTypes.CustomType00,
CustomObjectMarkers.Diamonds2,
44,
30, 30, True)
# define a unique cube (88mm x 88mm x 88mm) (approximately 2x the size of a light cube)
# with a 50mm x 50mm Diamonds3 image on every face
big_cube_obj = robot.world.define_custom_cube(CustomObjectTypes.CustomType01,
CustomObjectMarkers.Diamonds3,
88,
50, 50, True)
# define a unique wall (150mm x 120mm (x10mm thick for all walls)
# with a 50mm x 30mm Circles2 image on front and back
wall_obj = robot.world.define_custom_wall(CustomObjectTypes.CustomType02,
CustomObjectMarkers.Circles2,
150, 120,
50, 30, True)
# define a unique box (60mm deep x 140mm width x100mm tall)
# with a different 30mm x 50mm image on each of the 6 faces
box_obj = robot.world.define_custom_box(CustomObjectTypes.CustomType03,
CustomObjectMarkers.Hexagons2, # front
CustomObjectMarkers.Circles3, # back
CustomObjectMarkers.Circles4, # top
CustomObjectMarkers.Circles5, # bottom
CustomObjectMarkers.Triangles2, # left
CustomObjectMarkers.Triangles3, # right
60, 140, 100,
30, 50, True)
if ((cube_obj is not None) and (big_cube_obj is not None) and
(wall_obj is not None) and (box_obj is not None)):
print("All objects defined successfully!")
else:
print("One or more object definitions failed!")
return
print("Show the above markers to Cozmo and you will see the related objects "
"annotated in Cozmo's view window, you will also see print messages "
"everytime a custom object enters or exits Cozmo's view.")
print("Press CTRL-C to quit")
while True:
time.sleep(0.1)
cozmo.run_program(custom_objects, use_3d_viewer=True, use_viewer=True)
10. object moving
檢測移動立方體並實時跟蹤其速度等。
import time
import cozmo
def handle_object_moving_started(evt, **kw):
# This will be called whenever an EvtObjectMovingStarted event is dispatched -
# whenever we detect a cube starts moving (via an accelerometer in the cube)
print("Object %s started moving: acceleration=%s" %
(evt.obj.object_id, evt.acceleration))
def handle_object_moving(evt, **kw):
# This will be called whenever an EvtObjectMoving event is dispatched -
# whenever we detect a cube is still moving a (via an accelerometer in the cube)
print("Object %s is moving: acceleration=%s, duration=%.1f seconds" %
(evt.obj.object_id, evt.acceleration, evt.move_duration))
def handle_object_moving_stopped(evt, **kw):
# This will be called whenever an EvtObjectMovingStopped event is dispatched -
# whenever we detect a cube stopped moving (via an accelerometer in the cube)
print("Object %s stopped moving: duration=%.1f seconds" %
(evt.obj.object_id, evt.move_duration))
def cozmo_program(robot: cozmo.robot.Robot):
# Add event handlers that will be called for the corresponding event
robot.add_event_handler(cozmo.objects.EvtObjectMovingStarted, handle_object_moving_started)
robot.add_event_handler(cozmo.objects.EvtObjectMoving, handle_object_moving)
robot.add_event_handler(cozmo.objects.EvtObjectMovingStopped, handle_object_moving_stopped)
# keep the program running until user closes / quits it
print("Press CTRL-C to quit")
while True:
time.sleep(1.0)
cozmo.robot.Robot.drive_off_charger_on_connect = False # Cozmo can stay on his charger for this example
cozmo.run_program(cozmo_program)
11. dock with cube
行駛到立方體附近,並對接立方體,但是並不拿起立方體。
import cozmo
from cozmo.util import degrees
async def dock_with_cube(robot: cozmo.robot.Robot):
await robot.set_head_angle(degrees(-5.0)).wait_for_completed()
print("Cozmo is waiting until he sees a cube.")
cube = await robot.world.wait_for_observed_light_cube()
print("Cozmo found a cube, and will now attempt to dock with it:")
# Cozmo will approach the cube he has seen
# using a 180 approach angle will cause him to drive past the cube and approach from the opposite side
# num_retries allows us to specify how many times Cozmo will retry the action in the event of it failing
action = robot.dock_with_cube(cube, approach_angle=cozmo.util.degrees(180), num_retries=2)
await action.wait_for_completed()
print("result:", action.result)
cozmo.run_program(dock_with_cube)
12. roll cube
翻滾立方體。
import cozmo
from cozmo.util import degrees
async def roll_a_cube(robot: cozmo.robot.Robot):
await robot.set_head_angle(degrees(-5.0)).wait_for_completed()
print("Cozmo is waiting until he sees a cube")
cube = await robot.world.wait_for_observed_light_cube()
print("Cozmo found a cube, and will now attempt to roll with it:")
# Cozmo will approach the cube he has seen and roll it
# check_for_object_on_top=True enforces that Cozmo will not roll cubes with anything on top
action = robot.roll_cube(cube, check_for_object_on_top=True, num_retries=2)
await action.wait_for_completed()
print("result:", action.result)
cozmo.run_program(roll_a_cube)
13. pop a wheelie
藉助立方體,將Cozmo身體直立起來。
import cozmo
async def pop_a_wheelie(robot: cozmo.robot.Robot):
print("Cozmo is waiting until he sees a cube")
cube = await robot.world.wait_for_observed_light_cube()
print("Cozmo found a cube, and will now attempt to pop a wheelie on it")
action = robot.pop_a_wheelie(cube, num_retries=2)
await action.wait_for_completed()
cozmo.run_program(pop_a_wheelie)
Fin
相關文章
- Cozmo人工智慧機器人SDK使用筆記(1)-基礎部分basics人工智慧機器人筆記
- Cozmo人工智慧機器人SDK使用筆記(3)-視覺部分vision人工智慧機器人筆記視覺
- Cozmo人工智慧機器人SDK使用筆記(2)-顯示部分face人工智慧機器人筆記
- Cozmo人工智慧機器人SDK使用筆記(9)-判斷部分if_this_then_that人工智慧機器人筆記
- Cozmo人工智慧機器人SDK使用筆記(8)-應用部分apps人工智慧機器人筆記APP
- Cozmo人工智慧機器人SDK使用筆記(6)-並行部分Parallel_Action人工智慧機器人筆記並行Parallel
- Cozmo人工智慧機器人SDK使用筆記(5)-時序部分async_sync人工智慧機器人筆記
- Cozmo人工智慧機器人SDK使用筆記(7)-補充說明人工智慧機器人筆記
- Vector人工智慧機器人SDK使用筆記人工智慧機器人筆記
- Cozmo人工智慧機器人SDK使用筆記(X)-總結- |人工智慧基礎(中小學版)實踐平臺|人工智慧機器人筆記
- Anki Cozmo(Vector)人工智慧機器人玩具部分文件人工智慧機器人
- ROS2GO之手機連線Cozmo人工智慧機器人玩具ROSGo人工智慧機器人
- Cozmo機器人使用中文Scratch3程式設計案例(codelab)機器人程式設計
- Tello無人機的使用筆記之dji-sdk/Tello-Python無人機筆記Python
- Cozmo機器人脫離智慧手機使用的不完全攻略機器人
- ROS2GO+Cozmo=口袋機器人之人工智慧模擬和實驗平臺ROSGo機器人人工智慧
- 揭祕任務型對話機器人(下篇)機器人
- 揭祕任務型對話機器人(上篇)機器人
- WGCLOUD使用筆記 - 監測主機的Crontab定時任務資訊GCCloud筆記
- 【機器學習】支援向量機(個人筆記)機器學習筆記
- 機器人戰勝人類時間表:2061年機器人能完成所有人類任務!機器人
- 任務4
- [筆記] 關於任務佇列在專案中的使用筆記佇列
- 關於聊天機器人的閱讀筆記機器人筆記
- Cozmo人工智慧機器人玩具/教具完整版中文說明書和介紹(附應用下載連結)人工智慧機器人
- 讀人工智慧時代與人類未來筆記06_機器學習的力量人工智慧筆記機器學習
- tensorflow 學習筆記使用CNN做英文文字分類任務筆記CNN文字分類
- 如何選擇適合部署RPA機器人的流程和任務?機器人
- 人性化-完成需要人類感官的任務的機器人機器人
- Linux 學習筆記--任務計劃 crontabLinux筆記
- [筆記]laravel定時任務的實現筆記Laravel
- 《深入理解Java虛擬機器》個人筆記Java虛擬機筆記
- Git筆記-部分命令Git筆記
- Vector人工智慧情感機器人SDK釋出和說明(ROS2GO 2.0 + Vector 2.0)人工智慧機器人ROSGo
- 谷歌機器學習課程筆記(4)——降低損失谷歌機器學習筆記
- 猿輔導MSMARCO冠軍團隊:用MARS模型解決機器閱讀任務 | 吃瓜筆記模型筆記
- 機器學習筆記機器學習筆記
- 【機器學習】李宏毅——機器學習任務攻略機器學習