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

举报
zhangrelay 发表于 2021/07/15 04:28:20 2021/07/15
【摘要】 Cozmo并行动作示例。 此示例演示如何并行(而不是按顺序)执行动作。 import sysimport time try: from PIL import Imageexcept ImportError: sys.exit("Cannot import from PIL: Do `pip3 install --user Pillow` to install") imp...

Cozmo并行动作示例。

此示例演示如何并行(而不是按顺序)执行动作。



  
  1. import sys
  2. import time
  3. try:
  4. from PIL import Image
  5. except ImportError:
  6. sys.exit("Cannot import from PIL: Do `pip3 install --user Pillow` to install")
  7. import cozmo
  8. from cozmo.util import degrees, distance_mm, speed_mmps
  9. # load an image to display on Cozmo's face
  10. face_image = Image.open("../../face_images/cozmosdk.png")
  11. # resize to fit on Cozmo's face screen
  12. face_image = face_image.resize(cozmo.oled_face.dimensions(), Image.BICUBIC)
  13. # convert the image to the format used by the oled screen
  14. face_image = cozmo.oled_face.convert_image_to_screen_data(face_image,
  15. invert_image=True)
  16. def example1_lift_head(robot: cozmo.robot.Robot):
  17. cozmo.logger.info("----------------------------------------")
  18. cozmo.logger.info("Example 1: Move Lift and Head in Parallel")
  19. cozmo.logger.info("First, move the lift and head down, in sequence.")
  20. robot.set_head_angle(cozmo.robot.MIN_HEAD_ANGLE).wait_for_completed()
  21. robot.set_lift_height(0.0).wait_for_completed()
  22. cozmo.logger.info("Now, move the lift and head back up, in parallel "
  23. "- we no longer have to wait for the 1st action to complete "
  24. "before starting the 2nd one because the 2nd action is in_parallel")
  25. action1 = robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE)
  26. action2 = robot.set_lift_height(1.0, in_parallel=True)
  27. action1.wait_for_completed()
  28. action2.wait_for_completed()
  29. cozmo.logger.info("action1 = %s", action1)
  30. cozmo.logger.info("action2 = %s", action2)
  31. def example2_conflicting_actions(robot: cozmo.robot.Robot):
  32. cozmo.logger.info("----------------------------------------")
  33. cozmo.logger.info("Example 2: Conflicting actions.")
  34. cozmo.logger.info("Try to drive straight and turn in parallel. This is not "
  35. "allowed, as both actions require use of the wheels, so the 2nd action "
  36. "will report failure due to tracks_locked.")
  37. action1 = robot.drive_straight(distance_mm(50), speed_mmps(25), should_play_anim=False, in_parallel=True)
  38. action2 = robot.turn_in_place(degrees(90), in_parallel=True)
  39. action2.wait_for_completed()
  40. cozmo.logger.info("action2 = %s", action2)
  41. action1.wait_for_completed()
  42. cozmo.logger.info("action1 = %s", action1)
  43. def example3_abort_one_action(robot: cozmo.robot.Robot):
  44. cozmo.logger.info("----------------------------------------")
  45. cozmo.logger.info("Example 3: Abort some parallel actions.")
  46. cozmo.logger.info("Start multiple parallel actions:")
  47. action1 = robot.set_lift_height(0.0, in_parallel=True)
  48. action2 = robot.set_head_angle(cozmo.robot.MIN_HEAD_ANGLE, duration=6.0, in_parallel=True)
  49. action3 = robot.drive_straight(distance_mm(75), speed_mmps(25), should_play_anim=False, in_parallel=True)
  50. action4 = robot.display_oled_face_image(face_image, 30000.0) # Note: face image is in_parallel by default
  51. # Lift-lowering is aborted immediately
  52. action1.abort(log_abort_messages=True)
  53. # Head-lowering is aborted shortly afterwards
  54. time.sleep(0.1)
  55. action2.abort(log_abort_messages=True)
  56. # Image on Cozmo's face is aborted another 2 seconds later
  57. time.sleep(2)
  58. action4.abort(log_abort_messages=True)
  59. # We wait for the one remaining action to complete
  60. action3.wait_for_completed()
  61. # Only action3 should succeed (as long as Cozmo had enough space to drive)
  62. cozmo.logger.info("action1 = %s", action1)
  63. cozmo.logger.info("action2 = %s", action2)
  64. cozmo.logger.info("action3 = %s", action3)
  65. cozmo.logger.info("action4 = %s", action4)
  66. def example4_abort_all_actions(robot: cozmo.robot.Robot):
  67. cozmo.logger.info("----------------------------------------")
  68. cozmo.logger.info("Example 4: Abort all parallel actions.")
  69. cozmo.logger.info("Start multiple parallel actions:")
  70. action1 = robot.set_lift_height(0.0, in_parallel=True, duration=6.0)
  71. action2 = robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE, duration=6.0, in_parallel=True)
  72. action3 = robot.drive_straight(distance_mm(75), speed_mmps(25), should_play_anim=False, in_parallel=True)
  73. action4 = robot.display_oled_face_image(face_image, 30000.0) # Note: face image is in_parallel by default
  74. # wait two seconds and abort everything
  75. time.sleep(2)
  76. robot.abort_all_actions(log_abort_messages=True)
  77. # wait for results to come back for all actions
  78. robot.wait_for_all_actions_completed()
  79. # All actions should have aborted
  80. cozmo.logger.info("action1 res = %s", action1)
  81. cozmo.logger.info("action2 res = %s", action2)
  82. cozmo.logger.info("action3 res = %s", action3)
  83. cozmo.logger.info("action4 res = %s", action4)
  84. def cozmo_program(robot: cozmo.robot.Robot):
  85. example1_lift_head(robot)
  86. example2_conflicting_actions(robot)
  87. example3_abort_one_action(robot)
  88. example4_abort_all_actions(robot)
  89. cozmo.logger.info("----------------------------------------")
  90. cozmo.run_program(cozmo_program)

Fin


 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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