/examples/tutorials/04_cubes_and_objects/08_drive_to_charger_test.py

https://github.com/anki/cozmo-python-sdk · Python · 84 lines · 55 code · 2 blank · 27 comment · 1 complexity · 6a69e1b6d1f1b1d6717d2f68a01b9b38 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 drive towards his charger.
  16. The script shows an example of accessing the charger object from
  17. Cozmo's world, and driving towards it.
  18. '''
  19. import asyncio
  20. import time
  21. import cozmo
  22. from cozmo.util import degrees, distance_mm, speed_mmps
  23. def drive_to_charger(robot):
  24. '''The core of the drive_to_charger program'''
  25. # If the robot was on the charger, drive them forward and clear of the charger
  26. if robot.is_on_charger:
  27. # drive off the charger
  28. robot.drive_off_charger_contacts().wait_for_completed()
  29. robot.drive_straight(distance_mm(100), speed_mmps(50)).wait_for_completed()
  30. # Start moving the lift down
  31. robot.move_lift(-3)
  32. # turn around to look at the charger
  33. robot.turn_in_place(degrees(180)).wait_for_completed()
  34. # Tilt the head to be level
  35. robot.set_head_angle(degrees(0)).wait_for_completed()
  36. # wait half a second to ensure Cozmo has seen the charger
  37. time.sleep(0.5)
  38. # drive backwards away from the charger
  39. robot.drive_straight(distance_mm(-60), speed_mmps(50)).wait_for_completed()
  40. # try to find the charger
  41. charger = None
  42. # see if Cozmo already knows where the charger is
  43. if robot.world.charger:
  44. if robot.world.charger.pose.is_comparable(robot.pose):
  45. print("Cozmo already knows where the charger is!")
  46. charger = robot.world.charger
  47. else:
  48. # Cozmo knows about the charger, but the pose is not based on the
  49. # same origin as the robot (e.g. the robot was moved since seeing
  50. # the charger) so try to look for the charger first
  51. pass
  52. if not charger:
  53. # Tell Cozmo to look around for the charger
  54. look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
  55. try:
  56. charger = robot.world.wait_for_observed_charger(timeout=30)
  57. print("Found charger: %s" % charger)
  58. except asyncio.TimeoutError:
  59. print("Didn't see the charger")
  60. finally:
  61. # whether we find it or not, we want to stop the behavior
  62. look_around.stop()
  63. if charger:
  64. # Attempt to drive near to the charger, and then stop.
  65. action = robot.go_to_object(charger, distance_mm(65.0))
  66. action.wait_for_completed()
  67. print("Completed action: result = %s" % action)
  68. print("Done.")
  69. cozmo.robot.Robot.drive_off_charger_on_connect = False # Cozmo can stay on charger for now
  70. cozmo.run_program(drive_to_charger, use_viewer=True, force_viewer_on_top=True)