/examples/tutorials/03_vision/01_light_when_face.py

https://github.com/anki/cozmo-python-sdk · Python · 55 lines · 20 code · 13 blank · 22 comment · 6 complexity · 854e6abcb19a6118b4e6a618b9ee3dd5 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. '''Wait for Cozmo to see a face, and then turn on his backpack light.
  16. This is a script to show off faces, and how they are easy to use.
  17. It waits for a face, and then will light up his backpack when that face is visible.
  18. '''
  19. import asyncio
  20. import time
  21. import cozmo
  22. def light_when_face(robot: cozmo.robot.Robot):
  23. '''The core of the light_when_face program'''
  24. # Move lift down and tilt the head up
  25. robot.move_lift(-3)
  26. robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE).wait_for_completed()
  27. face = None
  28. print("Press CTRL-C to quit")
  29. while True:
  30. if face and face.is_visible:
  31. robot.set_all_backpack_lights(cozmo.lights.blue_light)
  32. else:
  33. robot.set_backpack_lights_off()
  34. # Wait until we we can see another face
  35. try:
  36. face = robot.world.wait_for_observed_face(timeout=30)
  37. except asyncio.TimeoutError:
  38. print("Didn't find a face.")
  39. return
  40. time.sleep(.1)
  41. cozmo.run_program(light_when_face, use_viewer=True, force_viewer_on_top=True)