/examples/tutorials/04_cubes_and_objects/03_go_to_object_test.py

https://github.com/anki/cozmo-python-sdk · Python · 60 lines · 21 code · 13 blank · 26 comment · 4 complexity · 31dc129cfa1fdcd6e361d46cf7510981 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. '''Tell Cozmo to find a cube, and then drive up to it
  16. This is a test / example usage of the robot.go_to_object call which creates a
  17. GoToObject action, that can be used to drive within a given distance of an
  18. object (e.g. a LightCube).
  19. '''
  20. import asyncio
  21. import cozmo
  22. from cozmo.util import degrees, distance_mm
  23. def go_to_object_test(robot: cozmo.robot.Robot):
  24. '''The core of the go to object test program'''
  25. # Move lift down and tilt the head up
  26. robot.move_lift(-3)
  27. robot.set_head_angle(degrees(0)).wait_for_completed()
  28. # look around and try to find a cube
  29. look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
  30. cube = None
  31. try:
  32. cube = robot.world.wait_for_observed_light_cube(timeout=30)
  33. print("Found cube: %s" % cube)
  34. except asyncio.TimeoutError:
  35. print("Didn't find a cube")
  36. finally:
  37. # whether we find it or not, we want to stop the behavior
  38. look_around.stop()
  39. if cube:
  40. # Drive to 70mm away from the cube (much closer and Cozmo
  41. # will likely hit the cube) and then stop.
  42. action = robot.go_to_object(cube, distance_mm(70.0))
  43. action.wait_for_completed()
  44. print("Completed action: result = %s" % action)
  45. print("Done.")
  46. cozmo.run_program(go_to_object_test)