PYTHON   35
monitor generic
Guest on 1st February 2023 01:39:01 AM


  1. import re
  2.  
  3. import cozmo
  4.  
  5.  
  6. def print_prefix(evt):
  7.     robot.world.last_event = evt
  8.     print('-> ', evt.event_name, ' ', sep='', end='')
  9.  
  10.  
  11. def print_object(obj):
  12.     if isinstance(obj,cozmo.objects.LightCube):
  13.         cube_id = next(k for k,v in robot.world.light_cubes.items() if v==obj)
  14.         print('LightCube-',cube_id,sep='',end='')
  15.     else:
  16.         r = re.search('<(\w*)', obj.__repr__())
  17.         print(r.group(1), end='')
  18.  
  19.  
  20. def monitor_generic(evt, **kwargs):
  21.     print_prefix(evt)
  22.     if 'behavior_type_name' in kwargs:
  23.         print(kwargs['behavior_type_name'], '', end='')
  24.         print(' ', end='')
  25.     if 'obj' in kwargs:
  26.         print_object(kwargs['obj'])
  27.         print(' ', end='')
  28.     if 'action' in kwargs:
  29.         action = kwargs['action']
  30.         if isinstance(action, cozmo.anim.Animation):
  31.             print(action.anim_name, '', end='')
  32.         elif isinstance(action, cozmo.anim.AnimationTrigger):
  33.             print(action.trigger.name, '', end='')
  34.     print(set(kwargs.keys()))
  35.  
  36.  
  37. def monitor_EvtActionCompleted(evt, action, state, failure_code, failure_reason, **kwargs):
  38.     print_prefix(evt)
  39.     print_object(action)
  40.     if isinstance(action, cozmo.anim.Animation):
  41.         print('', action.anim_name, end='')
  42.     elif isinstance(action, cozmo.anim.AnimationTrigger):
  43.         print('', action.trigger.name, end='')
  44.     print('',state,end='')
  45.     if failure_code is not None:
  46.         print('',failure_code,failure_reason,end='')
  47.     print()
  48.  
  49.  
  50. def monitor_EvtObjectTapped(evt, *, obj, tap_count, tap_duration, tap_intensity, **kwargs):
  51.     print_prefix(evt)
  52.     print_object(obj)
  53.     print(' count=', tap_count,
  54.           ' duration=', tap_duration, ' intensity=', tap_intensity, sep='')
  55.  
  56.  
  57. def monitor_EvtObjectMovingStarted(evt, *, obj, acceleration, **kwargs):
  58.     print_prefix(evt)
  59.     print_object(obj)
  60.     print(' accleration=', acceleration, sep='')
  61.  
  62.  
  63. def monitor_EvtObjectMovingStopped(evt, *, obj, move_duration, **kwargs):
  64.     print_prefix(evt)
  65.     print_object(obj)
  66.     print(' move_duration=%3.1f secs' %move_duration)
  67.  
  68.  
  69. def monitor_face(evt, face, **kwargs):
  70.     print_prefix(evt)
  71.     name = face.name if face.name is not '' else '[unknown face]'
  72.     expr = face.expression if face.expression is not None else 'expressionless'
  73.     kw = set(kwargs.keys()) if len(kwargs) > 0 else '{}'
  74.     print(name, ' (%s) ' % expr, ' face_id=', face.face_id, '  ', kw, sep='')
  75.  
  76. dispatch_table = {
  77.   cozmo.action.EvtActionStarted        : monitor_generic,
  78.   cozmo.action.EvtActionCompleted      : monitor_EvtActionCompleted,
  79.   cozmo.behavior.EvtBehaviorStarted    : monitor_generic,
  80.   cozmo.behavior.EvtBehaviorStopped    : monitor_generic,
  81.   cozmo.anim.EvtAnimationsLoaded       : monitor_generic,
  82.   cozmo.anim.EvtAnimationCompleted     : monitor_EvtActionCompleted,
  83.   cozmo.objects.EvtObjectAppeared      : monitor_generic,
  84.   cozmo.objects.EvtObjectDisappeared   : monitor_generic,
  85.   cozmo.objects.EvtObjectMovingStarted : monitor_EvtObjectMovingStarted,
  86.   cozmo.objects.EvtObjectMovingStopped : monitor_EvtObjectMovingStopped,
  87.   cozmo.objects.EvtObjectObserved      : monitor_generic,
  88.   cozmo.objects.EvtObjectTapped        : monitor_EvtObjectTapped,
  89.   cozmo.faces.EvtFaceAppeared          : monitor_face,
  90.   cozmo.faces.EvtFaceObserved          : monitor_face,
  91.   cozmo.faces.EvtFaceDisappeared       : monitor_face,
  92. }
  93.  
  94. excluded_events = {    # Occur too frequently to monitor by default
  95.     cozmo.objects.EvtObjectObserved,
  96.     cozmo.faces.EvtFaceObserved,
  97. }
  98.  
  99.  
  100. def monitor(_robot, evt_class=None):
  101.     if not isinstance(_robot, cozmo.robot.Robot):
  102.         raise TypeError('First argument must be a Robot instance')
  103.     if evt_class is not None and not issubclass(evt_class, cozmo.event.Event):
  104.         raise TypeError('Second argument must be an Event subclass')
  105.     global robot
  106.     robot = _robot
  107.     if evt_class in dispatch_table:
  108.         robot.world.add_event_handler(evt_class,dispatch_table[evt_class])
  109.     elif evt_class is not None:
  110.         robot.world.add_event_handler(evt_class,monitor_generic)
  111.     else:
  112.         for k,v in dispatch_table.items():
  113.             if k not in excluded_events:
  114.                 robot.world.add_event_handler(k,v)
  115.  
  116.  
  117. def unmonitor(_robot, evt_class=None):
  118.     if not isinstance(_robot, cozmo.robot.Robot):
  119.         raise TypeError('First argument must be a Robot instance')
  120.     if evt_class is not None and not issubclass(evt_class, cozmo.event.Event):
  121.         raise TypeError('Second argument must be an Event subclass')
  122.     global robot
  123.     robot = _robot
  124.     try:
  125.         if evt_class in dispatch_table:
  126.             robot.world.remove_event_handler(evt_class,dispatch_table[evt_class])
  127.         elif evt_class is not None:
  128.             robot.world.remove_event_handler(evt_class,monitor_generic)
  129.         else:
  130.             for k,v in dispatch_table.items():
  131.                 robot.world.remove_event_handler(k,v)
  132.     except Exception:
  133.         pass

Raw Paste

Login or Register to edit or fork this paste. It's free.