class MyArm(): def __init__(self, speed=1, logging=False): self.speed = speed * 10000 self.timout = 20 if logging: logger_init(logging.INFO) self.robot = SwiftAPI() time.sleep(2) def goto(self, x, y, z, wait=False): print('goto', x, y, z) self.robot.set_position(x, y, z, speed=self.speed, timeout=self.timout, wait=wait, relative=False) def step(self, x=None, y=None, z=None, wait=False): if x is None: x = 0 if y is None: y = 0 if z is None: z = 0 print('goto', x, y, z) self.robot.set_position(x, y, z, speed=self.speed, timeout=self.timout, wait=wait, relative=True) def grab(self, value=True): self.robot.set_pump(value, self.timout)
cap.set(3, int(1280 * RESCALE_FACTOR)) cap.set(4, int(720 * RESCALE_FACTOR)) # could not open port 'COM4': PermissionError(13, 'Access is denied.', None, 5) accessed = False while not accessed: try: swift = SwiftAPI() accessed = True except Exception as e: time.sleep(0.2) print('device info: ') print(swift.get_device_info()) swift.set_position(x=200, y=0, z=31, relative=False, speed=20000, wait=True) swift.set_pump(True) for i in range(15): _, frame = cap.read() # find air hockey table hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lower_table = np.array([0, 0, 128]) upper_table = np.array([180, 55, 255]) mask_table = cv2.inRange(hsv, lower_table, upper_table) _, contours, _ = cv2.findContours(mask_table, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) fieldx = 0 fieldy = 0 fieldw = 0
x = pos[0] swift.set_position(x=x + x1) if event.code == 16: #左边,左和右 y1 = 5 if event.value < 0 else -5 pos = swift.get_position() y = pos[1] swift.set_position(y=y + y1) if event.code == 308: #右边,上-三角形 z = 35 swift.set_position(z=z) if event.code == 304: #右边,下-X z = 2 swift.set_position(z=z) if event.code == 307: #右边,左-正方形 swift.set_pump(on=True) if event.code == 305: #右边,左-圆圈 swift.set_pump(on=False) if event.code == 315: #Start键,退出 print('break') break # while True: # sleep(1) except KeyboardInterrupt as e: print('KeyboardInterrupt', e) finally: # print('ret5: ' + test_ports['swift_service']['handle'].call('set cmd_sync G0 X80 Y0 Z60')) swift.set_pump(on=False) swift.set_position(x=80, y=0, z=30, speed=1800, wait=True)
def robot_pump(swift: SwiftAPI, pump_state): swift.set_pump(on=pump_state)
print('R links') if event.code == 'ABS_RX' and event.state > 10000: w -= 1 swift.set_wrist(w, wait=True) print('R rechts') if event.code == 'ABS_RY' and event.state < -20000: swift.set_position(z=-1, speed=20000, relative=True, wait=True) print('R unten') if event.code == 'ABS_RY' and event.state > 10000: swift.set_position(z=+1, speed=20000, relative=True, wait=True) print('R oben') if event.code == 'BTN_SOUTH' and event.state == 1: swift.set_pump(False) print('A Button') if event.code == 'BTN_WEST' and event.state == 1: swift.set_gripper(True, timeout=10) print('X Button - Start Position') if event.code == 'BTN_EAST' and event.state == 1: swift.set_gripper(False, timeout=10) print('B Button') if event.code == 'BTN_NORTH' and event.state == 1: swift.set_pump(True) print('Y Button') if event.code == 'BTN_START' and event.state == 1: print('Back Button') if event.code == 'BTN_SELECT' and event.state == 1: swift.set_position(150, 0, 150, speed=20000, timeout=20)