/examples/multi_robot/multi_robot_unified.py

https://github.com/anki/cozmo-python-sdk · Python · 62 lines · 25 code · 12 blank · 25 comment · 3 complexity · c414ea62c9a491b424d1082a257dd771 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 controlling two robots from within the same routine.
  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 run(sdk_conn1, sdk_conn2):
  23. robot1 = await sdk_conn1.wait_for_robot()
  24. robot2 = await sdk_conn2.wait_for_robot()
  25. # First have one turn left and one turn right, one after the other
  26. cozmo.logger.info("Turning robot 1")
  27. await robot1.turn_in_place(degrees(90)).wait_for_completed()
  28. cozmo.logger.info("Turning robot 2")
  29. await robot2.turn_in_place(degrees(-90)).wait_for_completed()
  30. # Then have them both turn back to the original position at the same time
  31. cozmo.logger.info("Turning both robots")
  32. turn1 = robot1.turn_in_place(degrees(-90))
  33. turn2 = robot2.turn_in_place(degrees(90))
  34. await turn1.wait_for_completed()
  35. await turn2.wait_for_completed()
  36. if __name__ == '__main__':
  37. cozmo.setup_basic_logging()
  38. loop = asyncio.get_event_loop()
  39. # Connect to both robots
  40. # NOTE: to connect to a specific device with a specific serial number,
  41. # create a connector (eg. `cozmo.IOSConnector(serial='abc')) and pass it
  42. # explicitly to `connect` or `connect_on_loop`
  43. try:
  44. conn1 = cozmo.connect_on_loop(loop)
  45. conn2 = cozmo.connect_on_loop(loop)
  46. except cozmo.ConnectionError as e:
  47. sys.exit("A connection error occurred: %s" % e)
  48. # Run a coroutine controlling both connections
  49. loop.run_until_complete(run(conn1, conn2))