Guest User

Untitled

a guest
Nov 24th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import print_function
  4. import sys
  5. import time
  6. import re
  7. # import pyuarm
  8. import yaml
  9. from pprint import pformat
  10.  
  11. from uf.utils.log import logger_init, logging
  12. from uf.wrapper.swift_api import SwiftAPI
  13. from uf.wrapper.uarm_api import UarmAPI
  14.  
  15. SPEED_DEFAULT = 200
  16. Z_DEFAULT = 150
  17. SIMPLE_RESPONSE_REGEX = r"ok V(?P<response>\S+)"
  18. TEMP_RESPONSE_REGEX = r"ok T:(?P<response>\S+)"
  19.  
  20. def main():
  21. config = {}
  22. with open("config.yaml", 'r') as ymlfile:
  23. config = yaml.load(ymlfile)
  24. debug = config.get('debug')
  25. if debug:
  26. logger_init(logging.DEBUG)
  27.  
  28. arm = None
  29.  
  30. try:
  31. arm = get_api(config)
  32. except Exception as exc:
  33. if debug:
  34. import pudb; pudb.set_trace()
  35. raise exc
  36. print("Error connecting to the arm", file=sys.stderr)
  37. exit(1)
  38.  
  39. time.sleep(2)
  40.  
  41. device_info = arm.get_device_info()
  42. logging.info('device info:\n%s' % pformat(device_info))
  43. fw_version = tuple(int(number) for number in device_info[2].split('.'))
  44. logging.info('firmware version:\n%s' % (fw_version, ))
  45.  
  46. mode_response = arm.send_cmd_sync("M2400 S1")
  47. logging.info('mode response:\n%s' % mode_response)
  48.  
  49. if not high5(arm, config):
  50. print("Error excuting one of the operations.", file=sys.stderr)
  51. exit(2)
  52.  
  53. arm.set_servo_detach(wait=True)
  54.  
  55. def get_api(config):
  56. device = config.get('device', 'swift')
  57. if device == 'swift':
  58. logging.info('setup swift ...')
  59. api = SwiftAPI()
  60. elif device == 'metal':
  61. logging.info('setup metal ...')
  62. api = UarmAPI()
  63. return api
  64.  
  65. def high5(arm, config):
  66. """
  67. Perform high-five actions on the uarm using the API instance `arm`
  68. """
  69.  
  70. wait = config.get('wait', False)
  71. z = config.get('z', Z_DEFAULT)
  72. speed = config.get('speed', SPEED_DEFAULT)
  73.  
  74. if not home_arm(arm, config):
  75. # fails fast instead of failing at the end
  76. return
  77.  
  78. movements = [
  79. # raise arm
  80. {'x':150, 'y':0, 'z':z, 'speed':speed, 'wait':wait},
  81. ]
  82. direction = config.get('direction', 'cw')
  83. if direction == 'cw':
  84. movements.extend([
  85. # backswing
  86. {'x':150, 'y':70, 'z':z, 'speed':speed, 'wait':wait},
  87. # in
  88. {'x':150, 'y':-70, 'z':z, 'speed':speed, 'wait':wait},
  89. ])
  90. else:
  91. movements.extend([
  92. # backswing
  93. {'x':150, 'y':-70, 'z':z, 'speed':speed, 'wait':wait},
  94. # in
  95. {'x':150, 'y':70, 'z':z, 'speed':speed, 'wait':wait},
  96. ])
  97.  
  98. for movement in movements:
  99. if not perform_movement(arm, **movement):
  100. return
  101. time.sleep(0.1)
  102. while arm.get_is_moving():
  103. time.sleep(0.1)
  104.  
  105. time.sleep(0.3)
  106.  
  107. if not home_arm(arm, config):
  108. return
  109.  
  110. return True
  111.  
  112.  
  113. def home_arm(arm, config={}):
  114. wait = config.get('wait', False)
  115. speed = config.get('speed', SPEED_DEFAULT)
  116. z = config.get('z', Z_DEFAULT)
  117. if not perform_movement(arm, x=150, y=0, z=z, speed=speed, wait=wait):
  118. return
  119. return True
  120.  
  121. def perform_movement(arm, **movement):
  122. logging.info('set ' + ' '.join([
  123. "%s:%s" % (key.upper(), value) for key, value in movement.items()
  124. ]))
  125. if not arm.set_position(**movement):
  126. return
  127. time.sleep(0.1)
  128. while arm.get_is_moving():
  129. time.sleep(0.1)
  130. return True
  131.  
  132. def send_cmd_sync_ok(arm, command, response_regex=None):
  133. """
  134. Send a command and wait for it to complete. Optionally parse the response.
  135. """
  136. logging.debug("sending command \"%s\"" % command)
  137. response = arm.send_cmd_sync(command)
  138. logging.debug("command response \"%s\"" % response)
  139. if not response.startswith("ok"):
  140. raise RuntimeError("command \"%s\" failed: %s" % (command, response))
  141. if response_regex:
  142. response_match = re.search(response_regex, response)
  143. if response_match and 'response' in response_match.groupdict():
  144. response = response_match.groupdict()['response']
  145. else:
  146. raise ValueError(
  147. "response \"%s\" for command \"%s\" did not match regex \"%s\"" %
  148. (response, command, response_regex)
  149. )
  150. return response
  151.  
  152. if __name__ == "__main__":
  153. main()
Add Comment
Please, Sign In to add comment