/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
- #!/usr/bin/env python3
- # Copyright (c) 2016 Anki, Inc.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License in the file LICENSE.txt or at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- '''Tell Cozmo to find a cube, and then drive up to it
- This is a test / example usage of the robot.go_to_object call which creates a
- GoToObject action, that can be used to drive within a given distance of an
- object (e.g. a LightCube).
- '''
- import asyncio
- import cozmo
- from cozmo.util import degrees, distance_mm
- def go_to_object_test(robot: cozmo.robot.Robot):
- '''The core of the go to object test program'''
- # Move lift down and tilt the head up
- robot.move_lift(-3)
- robot.set_head_angle(degrees(0)).wait_for_completed()
- # look around and try to find a cube
- look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
- cube = None
- try:
- cube = robot.world.wait_for_observed_light_cube(timeout=30)
- print("Found cube: %s" % cube)
- except asyncio.TimeoutError:
- print("Didn't find a cube")
- finally:
- # whether we find it or not, we want to stop the behavior
- look_around.stop()
- if cube:
- # Drive to 70mm away from the cube (much closer and Cozmo
- # will likely hit the cube) and then stop.
- action = robot.go_to_object(cube, distance_mm(70.0))
- action.wait_for_completed()
- print("Completed action: result = %s" % action)
- print("Done.")
- cozmo.run_program(go_to_object_test)