/examples/multi_robot/multi_robot_independent.py

https://github.com/anki/cozmo-python-sdk · Python · 56 lines · 23 code · 12 blank · 21 comment · 3 complexity · 2ebf4392c2aef3425c3a2e698ac6b05f 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. '''An example of running independent concurrent routines on multiple Cozmos.
  16. Each robot requires its own device to control it.
  17. '''
  18. import asyncio
  19. import sys
  20. import cozmo
  21. from cozmo.util import degrees
  22. async def turn_left(sdk_conn):
  23. robot = await sdk_conn.wait_for_robot()
  24. cozmo.logger.info("Turning robot 1")
  25. await robot.turn_in_place(degrees(90)).wait_for_completed()
  26. async def turn_right(sdk_conn):
  27. robot = await sdk_conn.wait_for_robot()
  28. cozmo.logger.info("Turning robot 2")
  29. await robot.turn_in_place(degrees(-90)).wait_for_completed()
  30. if __name__ == '__main__':
  31. cozmo.setup_basic_logging()
  32. loop = asyncio.get_event_loop()
  33. # Connect to both robots
  34. try:
  35. conn1 = cozmo.connect_on_loop(loop)
  36. conn2 = cozmo.connect_on_loop(loop)
  37. except cozmo.ConnectionError as e:
  38. sys.exit("A connection error occurred: %s" % e)
  39. # Run two independent coroutines concurrently, one on each connection
  40. task1 = asyncio.ensure_future(turn_left(conn1), loop=loop)
  41. task2 = asyncio.ensure_future(turn_right(conn2), loop=loop)
  42. # wait for both coroutines to complete before exiting the program
  43. loop.run_until_complete(asyncio.gather(task1, task2))