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

举报
zhangrelay 发表于 2021/07/15 03:42:26 2021/07/15
【摘要】 先简单总结一下使用笔记1-3: 基础部分介绍了一些常用功能,比如运动控制、LED显示和扬声器交互等人机接口显示部分--输出,cozmo面部显示屏输出一些基本信息人机接口视觉部分--输入,cozmo摄像头获取环境信息并处理等 接着,就自然过渡到第四部分----立方体和物体任务部分,共有13个项目专题,非常有趣。 Cozmo环顾四周,找寻充电底座图标,然后行驶到...

先简单总结一下使用笔记1-3:

  1. 基础部分介绍了一些常用功能,比如运动控制、LED显示和扬声器交互等
  2. 人机接口显示部分--输出,cozmo面部显示屏输出一些基本信息
  3. 人机接口视觉部分--输入,cozmo摄像头获取环境信息并处理等

接着,就自然过渡到第四部分----立方体和物体任务部分,共有13个项目专题,非常有趣。

Cozmo环顾四周,找寻充电底座图标,然后行驶到附近,任务完成。


1. go to pose 

给定Cozmo目标位置和角度,然后行驶到对应位置和角度。

先设定目标,然后如果将relative_to_robot设置为true,就是相对量,当年机器人为原点,给定目标相对原点的距离和角度。

核心代码如下:


  
  1. def cozmo_program(robot: cozmo.robot.Robot):
  2. robot.go_to_pose(Pose(100, 100, 0, angle_z=degrees(45)), relative_to_robot=True).wait_for_completed()

2. create wall

虚拟墙,并绕过行驶到目标。

核心代码:


  
  1. import cozmo
  2. from cozmo.util import degrees, Pose
  3. def cozmo_program(robot: cozmo.robot.Robot):
  4. fixed_object = robot.world.create_custom_fixed_object(Pose(100, 0, 0, angle_z=degrees(0)),
  5. 10, 100, 100, relative_to_robot=True)
  6. if fixed_object:
  7. print("fixed_object created successfully")
  8. robot.go_to_pose(Pose(200, 0, 0, angle_z=degrees(0)), relative_to_robot=True).wait_for_completed()
  9. cozmo.run_program(cozmo_program, use_3d_viewer=True)

3. go to object 

Cozmo找到一个立方体(1-3皆可以),然后行驶到对应立方体附近。


  
  1. def go_to_object_test(robot: cozmo.robot.Robot):
  2. '''The core of the go to object test program'''
  3. # Move lift down and tilt the head up
  4. robot.move_lift(-3)
  5. robot.set_head_angle(degrees(0)).wait_for_completed()
  6. # look around and try to find a cube
  7. look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
  8. cube = None
  9. try:
  10. cube = robot.world.wait_for_observed_light_cube(timeout=30)
  11. print("Found cube: %s" % cube)
  12. except asyncio.TimeoutError:
  13. print("Didn't find a cube")
  14. finally:
  15. # whether we find it or not, we want to stop the behavior
  16. look_around.stop()
  17. if cube:
  18. # Drive to 70mm away from the cube (much closer and Cozmo
  19. # will likely hit the cube) and then stop.
  20. action = robot.go_to_object(cube, distance_mm(70.0))
  21. action.wait_for_completed()
  22. print("Completed action: result = %s" % action)
  23. print("Done.")

4. stack or roll

让Cozmo依据找到的立方体的数量执行不同的任务。

超时时间默认为10s,环顾四周,找寻立方体,根据数量进行操作:

0---恩,它很生气

1---翻滚立方体

2---堆叠立方体


  
  1. import cozmo
  2. def cozmo_program(robot: cozmo.robot.Robot):
  3. lookaround = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
  4. cubes = robot.world.wait_until_observe_num_objects(num=2, object_type=cozmo.objects.LightCube, timeout=10)
  5. print("Found %s cubes" % len(cubes))
  6. lookaround.stop()
  7. if len(cubes) == 0:
  8. robot.play_anim_trigger(cozmo.anim.Triggers.MajorFail).wait_for_completed()
  9. elif len(cubes) == 1:
  10. robot.run_timed_behavior(cozmo.behavior.BehaviorTypes.RollBlock, active_time=60)
  11. else:
  12. robot.run_timed_behavior(cozmo.behavior.BehaviorTypes.StackBlocks, active_time=60)
  13. cozmo.run_program(cozmo_program)

5. cube stack

Cozmo堆叠立方体。Cozmo将等到它看到两个立方体,然后拿起一个并将其放在另一个立方体上。它会拿起看到的第一个,然后把它放在第二个上。


  
  1. def cozmo_program(robot: cozmo.robot.Robot):
  2. # Attempt to stack 2 cubes
  3. # Lookaround until Cozmo knows where at least 2 cubes are:
  4. lookaround = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
  5. cubes = robot.world.wait_until_observe_num_objects(num=2, object_type=cozmo.objects.LightCube, timeout=60)
  6. lookaround.stop()
  7. if len(cubes) < 2:
  8. print("Error: need 2 Cubes but only found", len(cubes), "Cube(s)")
  9. else:
  10. # Try and pickup the 1st cube
  11. current_action = robot.pickup_object(cubes[0], num_retries=3)
  12. current_action.wait_for_completed()
  13. if current_action.has_failed:
  14. code, reason = current_action.failure_reason
  15. result = current_action.result
  16. print("Pickup Cube failed: code=%s reason='%s' result=%s" % (code, reason, result))
  17. return
  18. # Now try to place that cube on the 2nd one
  19. current_action = robot.place_on_object(cubes[1], num_retries=3)
  20. current_action.wait_for_completed()
  21. if current_action.has_failed:
  22. code, reason = current_action.failure_reason
  23. result = current_action.result
  24. print("Place On Cube failed: code=%s reason='%s' result=%s" % (code, reason, result))
  25. return
  26. print("Cozmo successfully stacked 2 blocks!")

6. pickup furthest

让Cozmo拿起最远的立方体。此实例演示对象位置姿态的简单数学运算。

(dst = translation.position.x ** 2 + translation.position.y ** 2)

机器人试图在视觉中找到三个立方体,可以在脚本打开的摄像头窗口中看到运行过程。每个立方体将显示轮廓和立方体编号。机器人等待直到找到3个立方体,然后尝试拿起最远的一个。


  
  1. import cozmo
  2. def cozmo_program(robot: cozmo.robot.Robot):
  3. lookaround = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
  4. cubes = robot.world.wait_until_observe_num_objects(num=3, object_type=cozmo.objects.LightCube, timeout=60)
  5. lookaround.stop()
  6. max_dst, targ = 0, None
  7. for cube in cubes:
  8. translation = robot.pose - cube.pose
  9. dst = translation.position.x ** 2 + translation.position.y ** 2
  10. if dst > max_dst:
  11. max_dst, targ = dst, cube
  12. if len(cubes) < 3:
  13. print("Error: need 3 Cubes but only found", len(cubes), "Cube(s)")
  14. else:
  15. robot.pickup_object(targ, num_retries=3).wait_for_completed()
  16. cozmo.run_program(cozmo_program, use_viewer=True)

7. look around

Cozmo环顾四周,做出反应,然后拿起并放下一个立方体。


  
  1. import asyncio
  2. import cozmo
  3. from cozmo.util import degrees
  4. def cozmo_program(robot: cozmo.robot.Robot):
  5. look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
  6. # try to find a block
  7. cube = None
  8. try:
  9. cube = robot.world.wait_for_observed_light_cube(timeout=30)
  10. print("Found cube", cube)
  11. except asyncio.TimeoutError:
  12. print("Didn't find a cube :-(")
  13. finally:
  14. # whether we find it or not, we want to stop the behavior
  15. look_around.stop()
  16. if cube is None:
  17. robot.play_anim_trigger(cozmo.anim.Triggers.MajorFail)
  18. return
  19. print("Yay, found cube")
  20. cube.set_lights(cozmo.lights.green_light.flash())
  21. anim = robot.play_anim_trigger(cozmo.anim.Triggers.BlockReact)
  22. anim.wait_for_completed()
  23. action = robot.pickup_object(cube)
  24. print("got action", action)
  25. result = action.wait_for_completed(timeout=30)
  26. print("got action result", result)
  27. robot.turn_in_place(degrees(90)).wait_for_completed()
  28. action = robot.place_object_on_ground_here(cube)
  29. print("got action", action)
  30. result = action.wait_for_completed(timeout=30)
  31. print("got action result", result)
  32. anim = robot.play_anim_trigger(cozmo.anim.Triggers.MajorWin)
  33. cube.set_light_corners(None, None, None, None)
  34. anim.wait_for_completed()
  35. cozmo.run_program(cozmo_program)

8. drive to charger 

让Cozmo行驶到充电底座附近。


  
  1. import asyncio
  2. import time
  3. import cozmo
  4. from cozmo.util import degrees, distance_mm, speed_mmps
  5. def drive_to_charger(robot):
  6. '''The core of the drive_to_charger program'''
  7. # If the robot was on the charger, drive them forward and clear of the charger
  8. if robot.is_on_charger:
  9. # drive off the charger
  10. robot.drive_off_charger_contacts().wait_for_completed()
  11. robot.drive_straight(distance_mm(100), speed_mmps(50)).wait_for_completed()
  12. # Start moving the lift down
  13. robot.move_lift(-3)
  14. # turn around to look at the charger
  15. robot.turn_in_place(degrees(180)).wait_for_completed()
  16. # Tilt the head to be level
  17. robot.set_head_angle(degrees(0)).wait_for_completed()
  18. # wait half a second to ensure Cozmo has seen the charger
  19. time.sleep(0.5)
  20. # drive backwards away from the charger
  21. robot.drive_straight(distance_mm(-60), speed_mmps(50)).wait_for_completed()
  22. # try to find the charger
  23. charger = None
  24. # see if Cozmo already knows where the charger is
  25. if robot.world.charger:
  26. if robot.world.charger.pose.is_comparable(robot.pose):
  27. print("Cozmo already knows where the charger is!")
  28. charger = robot.world.charger
  29. else:
  30. # Cozmo knows about the charger, but the pose is not based on the
  31. # same origin as the robot (e.g. the robot was moved since seeing
  32. # the charger) so try to look for the charger first
  33. pass
  34. if not charger:
  35. # Tell Cozmo to look around for the charger
  36. look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
  37. try:
  38. charger = robot.world.wait_for_observed_charger(timeout=30)
  39. print("Found charger: %s" % charger)
  40. except asyncio.TimeoutError:
  41. print("Didn't see the charger")
  42. finally:
  43. # whether we find it or not, we want to stop the behavior
  44. look_around.stop()
  45. if charger:
  46. # Attempt to drive near to the charger, and then stop.
  47. action = robot.go_to_object(charger, distance_mm(65.0))
  48. action.wait_for_completed()
  49. print("Completed action: result = %s" % action)
  50. print("Done.")
  51. cozmo.robot.Robot.drive_off_charger_on_connect = False # Cozmo can stay on charger for now
  52. cozmo.run_program(drive_to_charger, use_viewer=True, force_viewer_on_top=True)

9. custom objects

自定义对象,包括大小和形状等。


  
  1. import time
  2. import cozmo
  3. from cozmo.objects import CustomObject, CustomObjectMarkers, CustomObjectTypes
  4. def handle_object_appeared(evt, **kw):
  5. # This will be called whenever an EvtObjectAppeared is dispatched -
  6. # whenever an Object comes into view.
  7. if isinstance(evt.obj, CustomObject):
  8. print("Cozmo started seeing a %s" % str(evt.obj.object_type))
  9. def handle_object_disappeared(evt, **kw):
  10. # This will be called whenever an EvtObjectDisappeared is dispatched -
  11. # whenever an Object goes out of view.
  12. if isinstance(evt.obj, CustomObject):
  13. print("Cozmo stopped seeing a %s" % str(evt.obj.object_type))
  14. def custom_objects(robot: cozmo.robot.Robot):
  15. # Add event handlers for whenever Cozmo sees a new object
  16. robot.add_event_handler(cozmo.objects.EvtObjectAppeared, handle_object_appeared)
  17. robot.add_event_handler(cozmo.objects.EvtObjectDisappeared, handle_object_disappeared)
  18. # define a unique cube (44mm x 44mm x 44mm) (approximately the same size as a light cube)
  19. # with a 30mm x 30mm Diamonds2 image on every face
  20. cube_obj = robot.world.define_custom_cube(CustomObjectTypes.CustomType00,
  21. CustomObjectMarkers.Diamonds2,
  22. 44,
  23. 30, 30, True)
  24. # define a unique cube (88mm x 88mm x 88mm) (approximately 2x the size of a light cube)
  25. # with a 50mm x 50mm Diamonds3 image on every face
  26. big_cube_obj = robot.world.define_custom_cube(CustomObjectTypes.CustomType01,
  27. CustomObjectMarkers.Diamonds3,
  28. 88,
  29. 50, 50, True)
  30. # define a unique wall (150mm x 120mm (x10mm thick for all walls)
  31. # with a 50mm x 30mm Circles2 image on front and back
  32. wall_obj = robot.world.define_custom_wall(CustomObjectTypes.CustomType02,
  33. CustomObjectMarkers.Circles2,
  34. 150, 120,
  35. 50, 30, True)
  36. # define a unique box (60mm deep x 140mm width x100mm tall)
  37. # with a different 30mm x 50mm image on each of the 6 faces
  38. box_obj = robot.world.define_custom_box(CustomObjectTypes.CustomType03,
  39. CustomObjectMarkers.Hexagons2, # front
  40. CustomObjectMarkers.Circles3, # back
  41. CustomObjectMarkers.Circles4, # top
  42. CustomObjectMarkers.Circles5, # bottom
  43. CustomObjectMarkers.Triangles2, # left
  44. CustomObjectMarkers.Triangles3, # right
  45. 60, 140, 100,
  46. 30, 50, True)
  47. if ((cube_obj is not None) and (big_cube_obj is not None) and
  48. (wall_obj is not None) and (box_obj is not None)):
  49. print("All objects defined successfully!")
  50. else:
  51. print("One or more object definitions failed!")
  52. return
  53. print("Show the above markers to Cozmo and you will see the related objects "
  54. "annotated in Cozmo's view window, you will also see print messages "
  55. "everytime a custom object enters or exits Cozmo's view.")
  56. print("Press CTRL-C to quit")
  57. while True:
  58. time.sleep(0.1)
  59. cozmo.run_program(custom_objects, use_3d_viewer=True, use_viewer=True)

10. object moving

检测移动立方体并实时跟踪其速度等。


  
  1. import time
  2. import cozmo
  3. def handle_object_moving_started(evt, **kw):
  4. # This will be called whenever an EvtObjectMovingStarted event is dispatched -
  5. # whenever we detect a cube starts moving (via an accelerometer in the cube)
  6. print("Object %s started moving: acceleration=%s" %
  7. (evt.obj.object_id, evt.acceleration))
  8. def handle_object_moving(evt, **kw):
  9. # This will be called whenever an EvtObjectMoving event is dispatched -
  10. # whenever we detect a cube is still moving a (via an accelerometer in the cube)
  11. print("Object %s is moving: acceleration=%s, duration=%.1f seconds" %
  12. (evt.obj.object_id, evt.acceleration, evt.move_duration))
  13. def handle_object_moving_stopped(evt, **kw):
  14. # This will be called whenever an EvtObjectMovingStopped event is dispatched -
  15. # whenever we detect a cube stopped moving (via an accelerometer in the cube)
  16. print("Object %s stopped moving: duration=%.1f seconds" %
  17. (evt.obj.object_id, evt.move_duration))
  18. def cozmo_program(robot: cozmo.robot.Robot):
  19. # Add event handlers that will be called for the corresponding event
  20. robot.add_event_handler(cozmo.objects.EvtObjectMovingStarted, handle_object_moving_started)
  21. robot.add_event_handler(cozmo.objects.EvtObjectMoving, handle_object_moving)
  22. robot.add_event_handler(cozmo.objects.EvtObjectMovingStopped, handle_object_moving_stopped)
  23. # keep the program running until user closes / quits it
  24. print("Press CTRL-C to quit")
  25. while True:
  26. time.sleep(1.0)
  27. cozmo.robot.Robot.drive_off_charger_on_connect = False # Cozmo can stay on his charger for this example
  28. cozmo.run_program(cozmo_program)

11. dock with cube

行驶到立方体附近,并对接立方体,但是并不拿起立方体。


  
  1. import cozmo
  2. from cozmo.util import degrees
  3. async def dock_with_cube(robot: cozmo.robot.Robot):
  4. await robot.set_head_angle(degrees(-5.0)).wait_for_completed()
  5. print("Cozmo is waiting until he sees a cube.")
  6. cube = await robot.world.wait_for_observed_light_cube()
  7. print("Cozmo found a cube, and will now attempt to dock with it:")
  8. # Cozmo will approach the cube he has seen
  9. # using a 180 approach angle will cause him to drive past the cube and approach from the opposite side
  10. # num_retries allows us to specify how many times Cozmo will retry the action in the event of it failing
  11. action = robot.dock_with_cube(cube, approach_angle=cozmo.util.degrees(180), num_retries=2)
  12. await action.wait_for_completed()
  13. print("result:", action.result)
  14. cozmo.run_program(dock_with_cube)

12. roll cube

翻滚立方体。


  
  1. import cozmo
  2. from cozmo.util import degrees
  3. async def roll_a_cube(robot: cozmo.robot.Robot):
  4. await robot.set_head_angle(degrees(-5.0)).wait_for_completed()
  5. print("Cozmo is waiting until he sees a cube")
  6. cube = await robot.world.wait_for_observed_light_cube()
  7. print("Cozmo found a cube, and will now attempt to roll with it:")
  8. # Cozmo will approach the cube he has seen and roll it
  9. # check_for_object_on_top=True enforces that Cozmo will not roll cubes with anything on top
  10. action = robot.roll_cube(cube, check_for_object_on_top=True, num_retries=2)
  11. await action.wait_for_completed()
  12. print("result:", action.result)
  13. cozmo.run_program(roll_a_cube)

13. pop a wheelie

借助立方体,将Cozmo身体直立起来。


  
  1. import cozmo
  2. async def pop_a_wheelie(robot: cozmo.robot.Robot):
  3. print("Cozmo is waiting until he sees a cube")
  4. cube = await robot.world.wait_for_observed_light_cube()
  5. print("Cozmo found a cube, and will now attempt to pop a wheelie on it")
  6. action = robot.pop_a_wheelie(cube, num_retries=2)
  7. await action.wait_for_completed()
  8. cozmo.run_program(pop_a_wheelie)

Fin


 

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/86626580

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。