/examples/tutorials/04_cubes_and_objects/07_lookaround.py

https://github.com/anki/cozmo-python-sdk · Python · 74 lines · 33 code · 21 blank · 20 comment · 4 complexity · ebd64d3f5e5caaea753517c0f5d625f5 MD5 · raw file

  1. #!/usr/bin/env python3
  2. # Copyright (c) 2016 Anki, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License in the file LICENSE.txt or at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. '''Make Cozmo look around for a cube.
  16. Cozmo looks around, reacts, and picks up and puts down a cube if found.
  17. '''
  18. import asyncio
  19. import cozmo
  20. from cozmo.util import degrees
  21. def cozmo_program(robot: cozmo.robot.Robot):
  22. look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
  23. # try to find a block
  24. cube = None
  25. try:
  26. cube = robot.world.wait_for_observed_light_cube(timeout=30)
  27. print("Found cube", cube)
  28. except asyncio.TimeoutError:
  29. print("Didn't find a cube :-(")
  30. finally:
  31. # whether we find it or not, we want to stop the behavior
  32. look_around.stop()
  33. if cube is None:
  34. robot.play_anim_trigger(cozmo.anim.Triggers.MajorFail)
  35. return
  36. print("Yay, found cube")
  37. cube.set_lights(cozmo.lights.green_light.flash())
  38. anim = robot.play_anim_trigger(cozmo.anim.Triggers.BlockReact)
  39. anim.wait_for_completed()
  40. action = robot.pickup_object(cube)
  41. print("got action", action)
  42. result = action.wait_for_completed(timeout=30)
  43. print("got action result", result)
  44. robot.turn_in_place(degrees(90)).wait_for_completed()
  45. action = robot.place_object_on_ground_here(cube)
  46. print("got action", action)
  47. result = action.wait_for_completed(timeout=30)
  48. print("got action result", result)
  49. anim = robot.play_anim_trigger(cozmo.anim.Triggers.MajorWin)
  50. cube.set_light_corners(None, None, None, None)
  51. anim.wait_for_completed()
  52. cozmo.run_program(cozmo_program)