You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

run.py 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. # Copyright (c) 2016-2017 Anki, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License in the file LICENSE.txt or at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. '''The run module contains helper classes and functions for opening a connection to the engine.
  15. To get started, the :func:`run_program` function can be used for most cases,
  16. it handles connecting to a device and then running the function you provide with
  17. the SDK-provided Robot object passed in.
  18. The :func:`connect` function can be used to open a connection
  19. and run your own code connected to a :class:`cozmo.conn.CozmoConnection`
  20. instance. It takes care of setting up an event loop, finding the Android or
  21. iOS device running the Cozmo app and making sure the connection is ok.
  22. You can also use the :func:`connect_with_tkviewer` or :func:`connect_with_3dviewer`
  23. functions which works in a similar way to :func:`connect`, but will also display
  24. either a a window on the screen showing a view from Cozmo's camera (using Tk), or
  25. a 3d viewer (with optional 2nd window showing Cozmo's camera) (using OpenGL), if
  26. supported on your system.
  27. Finally, more advanced progarms can integrate the SDK with an existing event
  28. loop by using the :func:`connect_on_loop` function.
  29. All of these functions make use of a :class:`DeviceConnector` subclass to
  30. deal with actually connecting to an Android or iOS device. There shouldn't
  31. normally be a need to modify them or write your own.
  32. '''
  33. # __all__ should order by constants, event classes, other classes, functions.
  34. __all__ = ['DeviceConnector', 'IOSConnector', 'AndroidConnector', 'TCPConnector',
  35. 'connect', 'connect_with_3dviewer', 'connect_with_tkviewer', 'connect_on_loop',
  36. 'run_program', 'setup_basic_logging']
  37. import threading
  38. import asyncio
  39. import concurrent.futures
  40. import functools
  41. import inspect
  42. import logging
  43. import os
  44. import os.path
  45. import queue
  46. import shutil
  47. import subprocess
  48. import sys
  49. import types
  50. import warnings
  51. from . import logger, logger_protocol
  52. from . import base
  53. from . import clad_protocol
  54. from . import conn
  55. from . import event
  56. from . import exceptions
  57. from . import usbmux
  58. #: The TCP port number we expect the Cozmo app to be listening on.
  59. COZMO_PORT = 5106
  60. if sys.platform in ('win32', 'cygwin'):
  61. DEFAULT_ADB_CMD = 'adb.exe'
  62. else:
  63. DEFAULT_ADB_CMD = 'adb'
  64. def _observe_connection_lost(proto, cb):
  65. meth = proto.connection_lost
  66. @functools.wraps(meth)
  67. def connection_lost(self, exc):
  68. meth(exc)
  69. cb()
  70. proto.connection_lost = types.MethodType(connection_lost, proto)
  71. class DeviceConnector:
  72. '''Base class for objects that setup the physical connection to a device.'''
  73. def __init__(self, cozmo_port=COZMO_PORT, enable_env_vars=True):
  74. self.cozmo_port = cozmo_port
  75. if enable_env_vars:
  76. self.parse_env_vars()
  77. async def connect(self, loop, protocol_factory, conn_check):
  78. '''Connect attempts to open a connection transport to the Cozmo app on a device.
  79. On opening a transport it will create a protocol from the supplied
  80. factory and connect it to the transport, returning a (transport, protocol)
  81. tuple. See :meth:`asyncio.BaseEventLoop.create_connection`
  82. '''
  83. raise NotImplementedError
  84. def parse_env_vars(self):
  85. try:
  86. self.cozmo_port = int(os.environ['COZMO_PORT'])
  87. except (KeyError, ValueError):
  88. pass
  89. class IOSConnector(DeviceConnector):
  90. '''Connects to an attached iOS device over USB.
  91. Opens a connection to the first iOS device that's found to be running
  92. the Cozmo app in SDK mode.
  93. iTunes (or another service providing usbmuxd) must be installed in order
  94. for this connector to be able to open a connection to a device.
  95. An instance of this class can be passed to the ``connect_`` prefixed
  96. functions in this module.
  97. Args:
  98. serial (string): Serial number of the device to connect to.
  99. If None, then connect to the first available iOS device running
  100. the Cozmo app in SDK mode.
  101. '''
  102. def __init__(self, serial=None, **kw):
  103. super().__init__(**kw)
  104. self.usbmux = None
  105. self._connected = set()
  106. self.serial = serial
  107. async def connect(self, loop, protocol_factory, conn_check):
  108. if not self.usbmux:
  109. self.usbmux = await usbmux.connect_to_usbmux(loop=loop)
  110. try:
  111. if self.serial is None:
  112. device_info, transport, proto = await self.usbmux.connect_to_first_device(
  113. protocol_factory, self.cozmo_port, exclude=self._connected)
  114. else:
  115. device_id = await self.usbmux.wait_for_serial(self.serial)
  116. device_info, transport, proto = await self.usbmux.connect_to_device(
  117. protocol_factory, device_id, self.cozmo_port)
  118. except asyncio.TimeoutError as exc:
  119. raise exceptions.ConnectionError("No connected iOS devices running Cozmo in SDK mode") from exc
  120. device_id = device_info.get('DeviceID')
  121. proto.device_info={
  122. 'device_type': 'ios',
  123. 'device_id': device_id,
  124. 'serial': device_info.get('SerialNumber')
  125. }
  126. if conn_check is not None:
  127. await conn_check(proto)
  128. self._connected.add(device_id)
  129. logger.info('Connected to iOS device_id=%s serial=%s', device_id,
  130. device_info.get('SerialNumber'))
  131. _observe_connection_lost(proto, functools.partial(self._disconnect, device_id))
  132. return transport, proto
  133. def _disconnect(self, device_id):
  134. logger.info('iOS device_id=%s disconnected.', device_id)
  135. self._connected.discard(device_id)
  136. class AndroidConnector(DeviceConnector):
  137. '''Connects to an attached Android device over USB.
  138. This requires the Android Studio command line tools to be installed,
  139. specifically `adb`.
  140. By default the connector will attempt to locate `adb` (or `adb.exe`
  141. on Windows) in common locations, but it may also be supplied by setting
  142. the ``ANDROID_ADB_PATH`` environment variable, or by passing it
  143. to the constructor.
  144. An instance of this class can be passed to the ``connect_`` prefixed
  145. functions in this module.
  146. Args:
  147. serial (string): Serial number of the device to connect to.
  148. If None, then connect to the first available Android device running
  149. the Cozmo app in SDK mode.
  150. '''
  151. def __init__(self, adb_cmd=None, serial=None, **kw):
  152. self._adb_cmd = None
  153. super().__init__(**kw)
  154. self.serial = serial
  155. self.portspec = 'tcp:' + str(self.cozmo_port)
  156. self._connected = set()
  157. if adb_cmd:
  158. self._adb_cmd = adb_cmd
  159. else:
  160. self._adb_cmd = shutil.which(DEFAULT_ADB_CMD)
  161. def parse_env_vars(self):
  162. super().parse_env_vars()
  163. self._adb_cmd = os.environ.get('ANDROID_ADB_PATH')
  164. @property
  165. def adb_cmd(self):
  166. if self._adb_cmd is not None:
  167. return self._adb_cmd
  168. if sys.platform != 'win32':
  169. return DEFAULT_ADB_CMD
  170. # C:\Users\IEUser\AppData\Local\Android\android-sdk
  171. # C:\Program Files (x86)\Android\android-sdk
  172. try_paths = []
  173. for path in [os.environ[key] for key in ('LOCALAPPDATA', 'ProgramFiles', 'ProgramFiles(x86)') if key in os.environ]:
  174. try_paths.append(os.path.join(path, 'Android', 'android-sdk'))
  175. for path in try_paths:
  176. adb_path = os.path.join(path, 'platform-tools', 'adb.exe')
  177. if os.path.exists(adb_path):
  178. self._adb_cmd = adb_path
  179. logger.debug('Found adb.exe at %s', adb_path)
  180. return adb_path
  181. raise ValueError('Could not find Android development tools')
  182. def _exec(self, *args):
  183. try:
  184. result = subprocess.run([self.adb_cmd] + list(args),
  185. stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
  186. except Exception as e:
  187. raise ValueError('Failed to execute adb command %s: %s' % (self.adb_cmd, e))
  188. if result.returncode != 0:
  189. raise ValueError('Failed to execute adb command %s: %s' % (result.args, result.stderr))
  190. return result.stdout.split(b'\n')
  191. def _devices(self):
  192. for line in self._exec('devices'):
  193. line = line.split()
  194. if len(line) != 2 or line[1] != b'device':
  195. continue
  196. yield line[0].decode('ascii') # device serial #
  197. def _add_forward(self, serial):
  198. self._exec('-s', serial, 'forward', self.portspec, self.portspec)
  199. def _remove_forward(self, serial):
  200. self._exec('-s', serial, 'forward', '--remove', self.portspec)
  201. async def connect(self, loop, protocol_factory, conn_check):
  202. version_mismatch = None
  203. for serial in self._devices():
  204. if serial in self._connected:
  205. continue
  206. if self.serial is not None and serial.lower() != self.serial.lower():
  207. continue
  208. logger.debug('Checking connection to Android device: %s', serial)
  209. try:
  210. self._remove_forward(serial)
  211. except:
  212. pass
  213. self._add_forward(serial)
  214. try:
  215. transport, proto = await loop.create_connection(
  216. protocol_factory, '127.0.0.1', self.cozmo_port)
  217. proto.device_info={
  218. 'device_type': 'android',
  219. 'serial': serial,
  220. }
  221. if conn_check:
  222. # Check that we have a good connection before returning
  223. try:
  224. await conn_check(proto)
  225. except Exception as e:
  226. logger.debug('Failed connection check: %s', e)
  227. raise
  228. logger.info('Connected to Android device serial=%s', serial)
  229. self._connected.add(serial)
  230. _observe_connection_lost(proto, functools.partial(self._disconnect, serial))
  231. return transport, proto
  232. except exceptions.SDKVersionMismatch as e:
  233. version_mismatch = e
  234. except:
  235. pass
  236. self._remove_forward(serial)
  237. if version_mismatch is not None:
  238. raise version_mismatch
  239. raise exceptions.ConnectionError("No connected Android devices running Cozmo in SDK mode")
  240. def _disconnect(self, serial):
  241. logger.info('Android serial=%s disconnected.', serial)
  242. self._connected.discard(serial)
  243. class TCPConnector(DeviceConnector):
  244. '''Connects to the Cozmo app directly via TCP.
  245. Generally only used for testing and debugging.
  246. Requires that a SDK_TCP_PORT environment variable be set to the port
  247. number to connect to.
  248. '''
  249. def __init__(self, tcp_port=None, ip_addr='127.0.0.1', **kw):
  250. super().__init__(**kw)
  251. self.ip_addr = ip_addr
  252. if tcp_port is not None:
  253. # override SDK_TCP_PORT environment variable
  254. self.tcp_port = tcp_port
  255. def parse_env_vars(self):
  256. super().parse_env_vars()
  257. self.tcp_port = None
  258. try:
  259. self.tcp_port = int(os.environ['SDK_TCP_PORT'])
  260. except (KeyError, ValueError):
  261. pass
  262. @property
  263. def enabled(self):
  264. return self.tcp_port is not None
  265. async def connect(self, loop, protocol_factory, conn_check):
  266. transport, proto = await loop.create_connection(protocol_factory, self.ip_addr, self.tcp_port)
  267. proto.device_info={
  268. 'device_type': 'tcp',
  269. 'host': '%s:%s' % (self.ip_addr, self.tcp_port),
  270. }
  271. if conn_check:
  272. try:
  273. await conn_check(proto)
  274. except Exception as e:
  275. logger.debug('Failed connection check: %s', e)
  276. raise
  277. logger.info("Connected to device on TCP port %d" % self.tcp_port)
  278. return transport, proto
  279. class FirstAvailableConnector(DeviceConnector):
  280. '''Connects to the first Android or iOS device running the Cozmo app in SDK mode.
  281. This class creates an :class:`AndroidConnector` or :class:`IOSConnector`
  282. instance and returns the first successful connection.
  283. This is the default connector used by ``connect_`` functions.
  284. '''
  285. def __init__(self):
  286. super().__init__(self, enable_env_vars=False)
  287. self.tcp = TCPConnector()
  288. self.ios = IOSConnector()
  289. self.android = AndroidConnector()
  290. async def _do_connect(self, connector,loop, protocol_factory, conn_check):
  291. connect = connector.connect(loop, protocol_factory, conn_check)
  292. result = await asyncio.gather(connect, loop=loop, return_exceptions=True)
  293. return result[0]
  294. async def connect(self, loop, protocol_factory, conn_check):
  295. conn_args = (loop, protocol_factory, conn_check)
  296. tcp_result = None
  297. if self.tcp.enabled:
  298. tcp_result = await self._do_connect(self.tcp, *conn_args)
  299. if not isinstance(tcp_result, BaseException):
  300. return tcp_result
  301. logger.warning('No TCP connection found running Cozmo: %s', tcp_result)
  302. android_result = await self._do_connect(self.android, *conn_args)
  303. if not isinstance(android_result, BaseException):
  304. return android_result
  305. ios_result = await self._do_connect(self.ios, *conn_args)
  306. if not isinstance(ios_result, BaseException):
  307. return ios_result
  308. logger.warning('No iOS device found running Cozmo: %s', ios_result)
  309. logger.warning('No Android device found running Cozmo: %s', android_result)
  310. if isinstance(tcp_result, exceptions.SDKVersionMismatch):
  311. raise tcp_result
  312. if isinstance(ios_result, exceptions.SDKVersionMismatch):
  313. raise ios_result
  314. if isinstance(android_result, exceptions.SDKVersionMismatch):
  315. raise android_result
  316. raise exceptions.NoDevicesFound('No devices connected running Cozmo in SDK mode')
  317. # Create an instance of a connector to use by default
  318. # The instance will maintain state about which devices are currently connected.
  319. _DEFAULT_CONNECTOR = FirstAvailableConnector()
  320. def _sync_exception_handler(abort_future, loop, context):
  321. loop.default_exception_handler(context)
  322. exception = context.get('exception')
  323. if exception is not None:
  324. abort_future.set_exception(context['exception'])
  325. else:
  326. abort_future.set_exception(RuntimeError(context['message']))
  327. class _LoopThread:
  328. '''Takes care of managing an event loop running in a dedicated thread.
  329. Args:
  330. loop (:class:`asyncio.BaseEventLoop`): The loop to run
  331. f (callable): Optional code to execute on the loop's thread
  332. conn_factory (callable): Override the factory function to generate a
  333. :class:`cozmo.conn.CozmoConnection` (or subclass) instance.
  334. connector (:class:`DeviceConnector`): Optional instance of a DeviceConnector
  335. subclass that handles opening the USB connection to a device.
  336. By default, it will connect to the first Android or iOS device that
  337. has the Cozmo app running in SDK mode.
  338. abort_future (:class:`concurrent.futures.Future): Optional future to
  339. raise an exception on in the event of an exception occurring within
  340. the thread.
  341. '''
  342. def __init__(self, loop, f=None, conn_factory=conn.CozmoConnection, connector=None, abort_future=None):
  343. self.loop = loop
  344. self.f = f
  345. if not abort_future:
  346. abort_future = concurrent.futures.Future()
  347. self.abort_future = abort_future
  348. self.conn_factory = conn_factory
  349. self.connector = connector
  350. self.thread = None
  351. self._running = False
  352. def start(self):
  353. '''Start a thread and open a connection to a device.
  354. Returns:
  355. :class:`cozmo.conn.CozmoConnection` instance
  356. '''
  357. q = queue.Queue()
  358. abort_future = concurrent.futures.Future()
  359. def run_loop():
  360. asyncio.set_event_loop(self.loop)
  361. try:
  362. coz_conn = connect_on_loop(self.loop, self.conn_factory, self.connector)
  363. q.put(coz_conn)
  364. except Exception as e:
  365. self.abort_future.set_exception(e)
  366. q.put(e)
  367. return
  368. if self.f:
  369. asyncio.ensure_future(self.f(coz_conn))
  370. self.loop.run_forever()
  371. self.thread = threading.Thread(target=run_loop)
  372. self.thread.start()
  373. coz_conn = q.get(10)
  374. if coz_conn is None:
  375. raise TimeoutError("Timed out waiting for connection to device")
  376. if isinstance(coz_conn, Exception):
  377. raise coz_conn
  378. self.coz_conn = coz_conn
  379. self._running = True
  380. return coz_conn
  381. def stop(self):
  382. '''Cleaning shutdown the running loop and thread.'''
  383. if self._running:
  384. async def _stop():
  385. await self.coz_conn.shutdown()
  386. self.loop.call_soon(lambda: self.loop.stop())
  387. asyncio.run_coroutine_threadsafe(_stop(), self.loop).result()
  388. self.thread.join()
  389. self._running = False
  390. def abort(self, exc):
  391. '''Abort the running loop and thread.'''
  392. if self._running:
  393. async def _abort(exc):
  394. self.coz_conn.abort(exc)
  395. asyncio.run_coroutine_threadsafe(_abort(exc), self.loop).result()
  396. self.stop()
  397. def _connect_async(f, conn_factory=conn.CozmoConnection, connector=None):
  398. # use the default loop, if one is available for the current thread,
  399. # if not create a new loop and make it the default.
  400. #
  401. # the expectation is that if the user wants explicit control over which
  402. # loop the code is executed on, they'll just use connect_on_loop directly.
  403. loop = None
  404. try:
  405. loop = asyncio.get_event_loop()
  406. except:
  407. pass
  408. if loop is None:
  409. loop = asyncio.new_event_loop()
  410. asyncio.set_event_loop(loop)
  411. coz_conn = connect_on_loop(loop, conn_factory, connector)
  412. try:
  413. loop.run_until_complete(f(coz_conn))
  414. except KeyboardInterrupt:
  415. logger.info('Exit requested by user')
  416. finally:
  417. loop.run_until_complete(coz_conn.shutdown())
  418. loop.stop()
  419. loop.run_forever()
  420. _sync_loop = asyncio.new_event_loop()
  421. def _connect_sync(f, conn_factory=conn.CozmoConnection, connector=None):
  422. abort_future = concurrent.futures.Future()
  423. conn_factory = functools.partial(conn_factory, _sync_abort_future=abort_future)
  424. lt = _LoopThread(_sync_loop, conn_factory=conn_factory, connector=connector, abort_future=abort_future)
  425. _sync_loop.set_exception_handler(functools.partial(_sync_exception_handler, abort_future))
  426. coz_conn = lt.start()
  427. try:
  428. f(base._SyncProxy(coz_conn))
  429. finally:
  430. lt.stop()
  431. def connect_on_loop(loop, conn_factory=conn.CozmoConnection, connector=None):
  432. '''Uses the supplied event loop to connect to a device.
  433. Will run the event loop in the current thread until the
  434. connection succeeds or fails.
  435. If you do not want/need to manage your own loop, then use the
  436. :func:`connect` function to handle setup/teardown and execute
  437. a user-supplied function.
  438. Args:
  439. loop (:class:`asyncio.BaseEventLoop`): The event loop to use to
  440. connect to Cozmo.
  441. conn_factory (callable): Override the factory function to generate a
  442. :class:`cozmo.conn.CozmoConnection` (or subclass) instance.
  443. connector (:class:`DeviceConnector`): Optional instance of a DeviceConnector
  444. subclass that handles opening the USB connection to a device.
  445. By default, it will connect to the first Android or iOS device that
  446. has the Cozmo app running in SDK mode.
  447. Returns:
  448. A :class:`cozmo.conn.CozmoConnection` instance.
  449. '''
  450. if connector is None:
  451. connector = _DEFAULT_CONNECTOR
  452. factory = functools.partial(conn_factory, loop=loop)
  453. async def conn_check(coz_conn):
  454. await coz_conn.wait_for(conn.EvtConnected, timeout=5)
  455. async def connect():
  456. return await connector.connect(loop, factory, conn_check)
  457. transport, coz_conn = loop.run_until_complete(connect())
  458. return coz_conn
  459. def connect(f, conn_factory=conn.CozmoConnection, connector=None):
  460. '''Connects to the Cozmo Engine on the mobile device and supplies the connection to a function.
  461. Accepts a function, f, that is given a :class:`cozmo.conn.CozmoConnection` object as
  462. a parameter.
  463. The supplied function may be either an asynchronous coroutine function
  464. (normally defined using ``async def``) or a regular synchronous function.
  465. If an asynchronous function is supplied it will be run on the same thread
  466. as the Cozmo event loop and must use the ``await`` keyword to yield control
  467. back to the loop.
  468. If a synchronous function is supplied then it will run on the main thread
  469. and Cozmo's event loop will run on a separate thread. Calls to
  470. asynchronous methods returned from CozmoConnection will automatically
  471. be translated to synchronous ones.
  472. The connect function will return once the supplied function has completed,
  473. as which time it will terminate the connection to the robot.
  474. Args:
  475. f (callable): The function to execute
  476. conn_factory (callable): Override the factory function to generate a
  477. :class:`cozmo.conn.CozmoConnection` (or subclass) instance.
  478. connector (:class:`DeviceConnector`): Optional instance of a DeviceConnector
  479. subclass that handles opening the USB connection to a device.
  480. By default it will connect to the first Android or iOS device that
  481. has the Cozmo app running in SDK mode.
  482. '''
  483. if asyncio.iscoroutinefunction(f):
  484. return _connect_async(f, conn_factory, connector)
  485. return _connect_sync(f, conn_factory, connector)
  486. def _connect_viewer(f, conn_factory, connector, viewer):
  487. # Run the viewer in the main thread, with the SDK running on a new background thread.
  488. loop = asyncio.new_event_loop()
  489. abort_future = concurrent.futures.Future()
  490. async def view_connector(coz_conn):
  491. try:
  492. await viewer.connect(coz_conn)
  493. if inspect.iscoroutinefunction(f):
  494. await f(coz_conn)
  495. else:
  496. await coz_conn._loop.run_in_executor(None, f, base._SyncProxy(coz_conn))
  497. finally:
  498. viewer.disconnect()
  499. try:
  500. if not inspect.iscoroutinefunction(f):
  501. conn_factory = functools.partial(conn_factory, _sync_abort_future=abort_future)
  502. lt = _LoopThread(loop, f=view_connector, conn_factory=conn_factory, connector=connector)
  503. lt.start()
  504. viewer.mainloop()
  505. except BaseException as e:
  506. abort_future.set_exception(exceptions.SDKShutdown(repr(e)))
  507. raise
  508. finally:
  509. lt.stop()
  510. def connect_with_3dviewer(f, conn_factory=conn.CozmoConnection, connector=None,
  511. enable_camera_view=False, show_viewer_controls=True):
  512. '''Setup a connection to a device and run a user function while displaying Cozmo's 3d world.
  513. This displays an OpenGL window on the screen with a 3D view of Cozmo's
  514. understanding of the world. Optionally, if `use_viewer` is True, a 2nd OpenGL
  515. window will also display showing a view of Cozmo's camera. It will return an
  516. error if the current system does not support PyOpenGL.
  517. The function may be either synchronous or asynchronous (defined
  518. used ``async def``).
  519. The function must accept a :class:`cozmo.CozmoConnection` object as
  520. its only argument.
  521. This call will block until the supplied function completes.
  522. Args:
  523. f (callable): The function to execute
  524. conn_factory (callable): Override the factory function to generate a
  525. :class:`cozmo.conn.CozmoConnection` (or subclass) instance.
  526. connector (:class:`DeviceConnector`): Optional instance of a DeviceConnector
  527. subclass that handles opening the USB connection to a device.
  528. By default it will connect to the first Android or iOS device that
  529. has the Cozmo app running in SDK mode.
  530. enable_camera_view (bool): Specifies whether to also open a 2D camera
  531. view in a second OpenGL window.
  532. show_viewer_controls (bool): Specifies whether to draw controls on the view.
  533. '''
  534. try:
  535. from . import opengl
  536. except ImportError as exc:
  537. opengl = exc
  538. if isinstance(opengl, Exception):
  539. if isinstance(opengl, exceptions.InvalidOpenGLGlutImplementation):
  540. raise NotImplementedError('GLUT (OpenGL Utility Toolkit) is not available:\n%s'
  541. % opengl)
  542. else:
  543. raise NotImplementedError('opengl is not available; '
  544. 'make sure the PyOpenGL and Pillow packages are installed:\n'
  545. 'Do `pip3 install --user cozmo[3dviewer]` to install. Error: %s' % opengl)
  546. viewer = opengl.OpenGLViewer(enable_camera_view=enable_camera_view, show_viewer_controls=show_viewer_controls)
  547. _connect_viewer(f, conn_factory, connector, viewer)
  548. def connect_with_tkviewer(f, conn_factory=conn.CozmoConnection, connector=None, force_on_top=False):
  549. '''Setup a connection to a device and run a user function while displaying Cozmo's camera.
  550. This displays a Tk window on the screen showing a view of Cozmo's camera.
  551. It will return an error if the current system does not support Tk.
  552. The function may be either synchronous or asynchronous (defined
  553. used ``async def``).
  554. The function must accept a :class:`cozmo.CozmoConnection` object as
  555. its only argument.
  556. This call will block until the supplied function completes.
  557. Args:
  558. f (callable): The function to execute
  559. conn_factory (callable): Override the factory function to generate a
  560. :class:`cozmo.conn.CozmoConnection` (or subclass) instance.
  561. connector (:class:`DeviceConnector`): Optional instance of a DeviceConnector
  562. subclass that handles opening the USB connection to a device.
  563. By default it will connect to the first Android or iOS device that
  564. has the Cozmo app running in SDK mode.
  565. force_on_top (bool): Specifies whether the window should be forced on top of all others
  566. '''
  567. try:
  568. from . import tkview
  569. except ImportError as exc:
  570. tkview = exc
  571. if isinstance(tkview, Exception):
  572. raise NotImplementedError('tkviewer not available on this platform; '
  573. 'make sure Tkinter, NumPy and Pillow packages are installed (%s)' % tkview)
  574. viewer = tkview.TkImageViewer(force_on_top=force_on_top)
  575. _connect_viewer(f, conn_factory, connector, viewer)
  576. def setup_basic_logging(general_log_level=None, protocol_log_level=None,
  577. protocol_log_messages=clad_protocol.LOG_ALL, target=sys.stderr,
  578. deprecated_filter="default"):
  579. '''Helper to perform basic setup of the Python logging machinery.
  580. The SDK defines two loggers:
  581. * :data:`logger` ("cozmo.general") - For general purpose information
  582. about events within the SDK; and
  583. * :data:`logger_protocol` ("cozmo.protocol") - For low level
  584. communication messages between the device and the SDK.
  585. Generally only :data:`logger` is interesting.
  586. Args:
  587. general_log_level (str): 'DEBUG', 'INFO', 'WARN', 'ERROR' or an equivalent
  588. constant from the :mod:`logging` module. If None then a
  589. value will be read from the COZMO_LOG_LEVEL environment variable.
  590. protocol_log_level (str): as general_log_level. If None then a
  591. value will be read from the COZMO_PROTOCOL_LOG_LEVEL environment
  592. variable.
  593. protocol_log_messages (list): The low level messages that should be
  594. logged to the protocol log. Defaults to all. Will read from
  595. the COMZO_PROTOCOL_LOG_MESSAGES if available which should be
  596. a comma separated list of message names (case sensitive).
  597. target (object): The stream to send the log data to; defaults to stderr
  598. deprecated_filter (str): The filter for any DeprecationWarning messages.
  599. This is defaulted to "default" which shows the warning once per
  600. location. You can hide all deprecated warnings by passing in "ignore",
  601. see https://docs.python.org/3/library/warnings.html#warning-filter
  602. for more information.
  603. '''
  604. if deprecated_filter is not None:
  605. warnings.filterwarnings(deprecated_filter, category=DeprecationWarning)
  606. if general_log_level is None:
  607. general_log_level = os.environ.get('COZMO_LOG_LEVEL', logging.INFO)
  608. if protocol_log_level is None:
  609. protocol_log_level = os.environ.get('COZMO_PROTOCOL_LOG_LEVEL', logging.INFO)
  610. if protocol_log_level:
  611. if 'COMZO_PROTOCOL_LOG_MESSAGES' in os.environ:
  612. lm = os.environ['COMZO_PROTOCOL_LOG_MESSAGES']
  613. if lm.lower() == 'all':
  614. clad_protocol.CLADProtocol._clad_log_which = clad_protocol.LOG_ALL
  615. else:
  616. clad_protocol.CLADProtocol._clad_log_which = set(lm.split(','))
  617. else:
  618. clad_protocol.CLADProtocol._clad_log_which = protocol_log_messages
  619. h = logging.StreamHandler(stream=target)
  620. f = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
  621. h.setFormatter(f)
  622. logger.addHandler(h)
  623. logger. setLevel(general_log_level)
  624. if protocol_log_level is not None:
  625. logger_protocol.addHandler(h)
  626. logger_protocol.setLevel(protocol_log_level)
  627. def run_program(f, use_viewer=False, conn_factory=conn.CozmoConnection,
  628. connector=None, force_viewer_on_top=False,
  629. deprecated_filter="default", use_3d_viewer=False,
  630. show_viewer_controls=True,
  631. exit_on_connection_error=True):
  632. '''Connect to Cozmo and run the provided program/function f.
  633. Args:
  634. f (callable): The function to execute, accepts a connected
  635. :class:`cozmo.robot.Robot` as the parameter.
  636. use_viewer (bool): Specifies whether to display a view of Cozmo's camera
  637. in a window.
  638. conn_factory (callable): Override the factory function to generate a
  639. :class:`cozmo.conn.CozmoConnection` (or subclass) instance.
  640. connector (:class:`DeviceConnector`): Optional instance of a DeviceConnector
  641. subclass that handles opening the USB connection to a device.
  642. By default it will connect to the first Android or iOS device that
  643. has the Cozmo app running in SDK mode.
  644. force_viewer_on_top (bool): Specifies whether the window should be
  645. forced on top of all others (only relevant if use_viewer is True).
  646. Note that this is ignored if use_3d_viewer is True (as it's not
  647. currently supported on that windowing system).
  648. deprecated_filter (str): The filter for any DeprecationWarning messages.
  649. This is defaulted to "default" which shows the warning once per
  650. location. You can hide all deprecated warnings by passing in "ignore",
  651. see https://docs.python.org/3/library/warnings.html#warning-filter
  652. for more information.
  653. use_3d_viewer (bool): Specifies whether to display a 3D view of Cozmo's
  654. understanding of the world in a window. Note that if both this and
  655. `use_viewer` are set then the 2D camera view will render in an OpenGL
  656. window instead of a TkView window.
  657. show_viewer_controls (bool): Specifies whether to draw controls on the view.
  658. exit_on_connection_error (bool): Specify whether the program should exit on
  659. connection error or should an error be raised. Default to true.
  660. '''
  661. setup_basic_logging(deprecated_filter=deprecated_filter)
  662. # Wrap f (a function that takes in an already created robot)
  663. # with a function that accepts a cozmo.conn.CozmoConnection
  664. if asyncio.iscoroutinefunction(f):
  665. @functools.wraps(f)
  666. async def wrapper(sdk_conn):
  667. try:
  668. robot = await sdk_conn.wait_for_robot()
  669. await f(robot)
  670. except exceptions.SDKShutdown:
  671. pass
  672. except KeyboardInterrupt:
  673. logger.info('Exit requested by user')
  674. else:
  675. @functools.wraps(f)
  676. def wrapper(sdk_conn):
  677. try:
  678. robot = sdk_conn.wait_for_robot()
  679. f(robot)
  680. except exceptions.SDKShutdown:
  681. pass
  682. except KeyboardInterrupt:
  683. logger.info('Exit requested by user')
  684. try:
  685. if use_3d_viewer:
  686. connect_with_3dviewer(wrapper, conn_factory=conn_factory, connector=connector,
  687. enable_camera_view=use_viewer, show_viewer_controls=show_viewer_controls)
  688. elif use_viewer:
  689. connect_with_tkviewer(wrapper, conn_factory=conn_factory, connector=connector,
  690. force_on_top=force_viewer_on_top)
  691. else:
  692. connect(wrapper, conn_factory=conn_factory, connector=connector)
  693. except KeyboardInterrupt:
  694. logger.info('Exit requested by user')
  695. except exceptions.ConnectionError as e:
  696. if exit_on_connection_error:
  697. sys.exit("A connection error occurred: %s" % e)
  698. else:
  699. logger.error("A connection error occurred: %s" % e)
  700. raise