def run(self): while self.alive: try: ports = get_ports(filters={'hwid': 'USB VID:PID=2341:0042'}) for port in ports: if port['device'] not in swifts.keys(): new_swift = SwiftAPI(port=port['device']) new_swift.waiting_ready() device_info = new_swift.get_device_info() print(new_swift.port, device_info) firmware_version = device_info['firmware_version'] if firmware_version and not firmware_version.startswith( ('0.', '1.', '2.', '3.')): new_swift.set_speed_factor(0.00001) new_swift.set_mode(mode=0) with lock: swifts[port['device']] = new_swift else: swift = swifts[port['device']] if not swift.connected: with lock: swifts.pop(port['device']) except Exception as e: pass time.sleep(0.001)
def Arm_Init(): """Robotic arm initiation""" swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}, cmd_pend_size=2, callback_thread_pool_size=1) swift.waiting_ready() device_info = swift.get_device_info() print(device_info) firmware_version = device_info['firmware_version'] if firmware_version and not firmware_version.startswith( ('0.', '1.', '2.', '3.')): swift.set_speed_factor(0.00005) swift.set_mode(0) return swift
def run(self): while self.alive: try: ports = get_ports(filters={'hwid': 'USB VID:PID=2341:0042'}) for port in ports: if port['device'] not in swifts.keys(): new_swift = SwiftAPI(port=port['device']) new_swift.waiting_ready() device_info = new_swift.get_device_info() print(new_swift.port, device_info) firmware_version = device_info['firmware_version'] if firmware_version and not firmware_version.startswith(('0.', '1.', '2.', '3.')): new_swift.set_speed_factor(0.00001) new_swift.set_mode(mode=0) with lock: pos = [150, 0, 150] for swift in swifts.values(): swift.flush_cmd() if len(swifts.values()) > 0: time.sleep(1) for swift in swifts.values(): pos = swift.get_position() if isinstance(pos, list): # print('sync pos:', pos) break # new_swift.reset(speed=speed) swifts[port['device']] = new_swift for swift in swifts.values(): swift.set_position(x=pos[0], y=pos[1], z=pos[2], speed=speed, wait=False) for swift in swifts.values(): if swift.connected: swift.flush_cmd(wait_stop=True) # if len(swifts) > 1: # time.sleep(3) else: swift = swifts[port['device']] if not swift.connected: with lock: swifts.pop(port['device']) except Exception as e: pass time.sleep(0.001)
import time from uarm.wrapper import SwiftAPI swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}) swift.waiting_ready(timeout=3) device_info = swift.get_device_info() print(device_info) firmware_version = device_info['firmware_version'] if firmware_version and not firmware_version.startswith( ('0.', '1.', '2.', '3.')): swift.set_speed_factor(0.0005) swift.set_mode(0) swift.reset(wait=True, speed=10000) swift.set_position(x=200, speed=10000 * 20) swift.set_position(y=100) swift.set_position(z=100) swift.flush_cmd(wait_stop=True) swift.set_polar(stretch=200, speed=10000 * 20) swift.set_polar(rotation=90) swift.set_polar(height=150) print(swift.set_polar(stretch=200, rotation=90, height=150, wait=True)) swift.flush_cmd() # time.sleep(1)
class uArmSwift: def __init__(self): self.swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}, cmd_pend_size=2, callback_thread_pool_size=1) if not self.swift.connected: print('lose connect') self.swift.waiting_ready() device_info = self.swift.get_device_info() print(device_info) firmware_version = device_info['firmware_version'] if firmware_version and not firmware_version.startswith( ('0.', '1.', '2.', '3.')): self.swift.set_speed_factor(0.00005) self.swift.set_mode(0) self.speed = 500000 self.swift.set_wrist(angle=90) self.wristAngle = self.swift.get_servo_angle(0, timeout=10) def set_position(self, x=100, y=0, z=100, wait=False): self.swift.set_position(x, y, z, speed=self.speed, wait=wait) def set_polar(self, stretch, rotation, height, wait=False): self.swift.set_polar(stretch, rotation, height, speed=self.speed, wait=wait) def set_servo_angle(self, num, angle, wait=False): if num < 0 and num > 3: print("num is wrong") self.swift.set_servo_angle(num, angle, wait, speed=self.speed, wait=wait) def set_wrist(self, angle=90, wait=False): # 第四电机 self.swift.set_wrist(angle, wait) def set_pump(self, on=False): self.swift.set_pump(on) def set_buzzer(self, freq=1000, duration=1, wait=False): self.swift.set_buzzer(freq, duration, wait) def get_position(self): return self.swift.get_position() def get_servo_angle(self, id=0): return self.swift.get_servo_angle(id, timeout=10) def is_moving(self): return self.swift.get_is_moving() def disconnect(self): self.swift.disconnect()
swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}, enable_handle_thread=False) # swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}, enable_write_thread=True) # swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}, enable_report_thread=True) # swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}, enable_handle_thread=True, enable_write_thread=True, enable_report_thread=True) swift.waiting_ready() # wait the rebot ready print(swift.get_device_info()) move_speed = 100 time.sleep(5) # <! must wait the effector check itself rtn = swift.get_mode(wait=True, timeout=10) # <! make sure the work mode is 5 print(rtn) if rtn != 5: swift.set_mode(5) time.sleep(5) # <! must wait the effector check itself swift.set_position(x=150, y=150, z=150, speed=move_speed, wait=True, timeout=10, cmd='G0') # print( swift.get_position() ) # print( swift.get_servo_angle() ) # print( swift.send_cmd_sync('P2243') ) while True: swift.set_position(x=400,
import socketio from uarm.wrapper import SwiftAPI sio = socketio.AsyncClient(reconnection=True) try: swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}, callback_thread_pool_size=2) swift.waiting_ready(timeout=10) device_info = swift.get_device_info() print(device_info) # swift.set_speed_factor(100) swift.set_mode(3) swift.reset(wait=True, speed=250) # firmware_version = device_info['firmware_version'] # if firmware_version and not firmware_version.startswith(('0.', '1.', '2.', '3.')): # # swift.set_speed_factor(0.0005) except Exception: swift = False print('swift uArm not connected') @sio.event async def connect(): print('connected to server')
# # Author: Vinman <*****@*****.**> <*****@*****.**> import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) from uarm.wrapper import SwiftAPI """ pressure test: set mode """ swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}, cmd_pend_size=2) swift.waiting_ready() print(swift.get_device_info()) mode = swift.get_mode() print('mode:', mode) mode_count = 4 count = 1 while swift.connected: mode = (mode + 1) % mode_count if swift.set_mode(mode) != mode: print('set mode {} failed, count={}'.format(mode, count)) count += 1 if count == 1000000: count = 1
def initialize(): global cartesian test_a = True test_b = True data_start() # saving data #convert cartesian coordinates to polar coordinates if(cartesian): print(Fore.BLUE + "Detecting cartesian coordinates, changing to polar") #test to check converting cartesian to polar if(test_converting(cartesian)): print(Fore.GREEN + "TEST OK") print(Style.RESET_ALL) else: print(Fore.RED + "TEST FAILED") print(Style.RESET_ALL) #debug outputs # print(default_stretch) # print(default_rotation) # print(default_height) change_variables_to_polar() print(Fore.GREEN + "Conversion from cartesian to polar successfull") print(Style.RESET_ALL) time.sleep(2) else: print(Fore.BLUE + "Default polar coordinates are set") print(Style.RESET_ALL) try: global serial_name global ser ser = serial.Serial('/dev/ttyUSB0',115200,timeout=2) #arduino serial arduino_reset() print(Fore.BLUE + "Arduino UNO\n") except: test_a = False print(Fore.RED + "FAILED CONNECT TO ARDUINO!") print(Style.RESET_ALL) #setting up uArm SwiftPro try: global swift #need to change sys.path.append(os.path.join(os.path.dirname(__file__), '../uArm-Python-SDK')) #link to uArm SwiftPro python3.x.x library swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}) swift.waiting_ready(timeout=5) swift.set_mode(0) device_info = swift.get_device_info() default_robot_position() print(Fore.BLUE) print(device_info) print("\n") except: test_b = False print(Fore.RED + "FAILED CONNECT TO uArm SwiftPro") print(Style.RESET_ALL) if test_a and test_b: print(Fore.GREEN + "DONE!\n") print(Style.RESET_ALL) return
class uArm(): def __init__(self): self.scope = 10 self.x0 = 160 self.y0 = 0 self.swift = SwiftAPI(filters={'hwid':'USB VID:PID=2341:0042'}) self.swift.waiting_ready(timeout=3) # self.swift.set_speed_factor(0.005) # if you change this, be prepared for different movements! self.swift.set_mode(mode=0) time.sleep(0.5) self.swift.set_servo_angle(angle=90) self.swift.set_wrist(angle=90) self.swift.set_position(x=200,y=0,z=20) # start it off with a salute self.swift.set_buzzer(frequency=1000, duration=1) # signal ready self.lstValidCharSet = ['?','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',\ '-','0','1','2','3','4','5','6','7','8','9'] self.lstLetter = [self.QuestionMark, self.LetterA, self.LetterB, self.LetterC, self.LetterD, self.LetterE, self.LetterF,\ self.LetterG, self.LetterH, self.LetterI, self.LetterJ, self.LetterK, self.LetterL, self.LetterM, self.LetterN,\ self.LetterO, self.LetterP, self.LetterQ, self.LetterR, self.LetterS, self.LetterT, self.LetterU, self.LetterV,\ self.LetterW, self.LetterX, self.LetterY, self.LetterZ, self.Hyphen, self.Number0, self.Number1, self.Number2,\ self.Number3, self.Number4, self.Number5, self.Number6, self.Number7, self.Number8, self.Number9] def __del__(self): input("PLEASE SUPPORT uARM ARM!!, then strike ENTER to continue ...") self.swift.set_buzzer(frequency=600, duration=2) self.swift.set_position(x=200,y=0,z=20) self.swift.flush_cmd() self.swift.disconnect() del self.swift self.swift = None def arm(self): """ Using this method to allow raw access to the uArm if required """ return self.swift def insert_pen(self): self.swift.set_buzzer(frequency=1000, duration=0.5) # signal ready self.swift.set_servo_angle(angle=90) time.sleep(0.5) self.swift.set_wrist(angle=90) time.sleep(0.5) self.swift.set_position(x=200,y=0,z=0) while (self.swift.get_is_moving()): continue input("Set pen in universal holder, then strike ENTER to continue ...") self.swift.set_position(x=200,y=0,z=10) return def pen_up(self): while (self.swift.get_is_moving()): continue x, y, z = self.swift.get_position() self.swift.set_position(x, y, 10) time.sleep(0.5) return 10 def pen_down(self): while (self.swift.get_is_moving()): continue x, y, z = self.swift.get_position() self.swift.set_position(x, y, 0) time.sleep(0.5) return 0 def setScope(self, strName): """ based upon the length of strName, determine the scope (char width) and starting X, Y positions assuming that the center of the page is 160,0 x extent is 110 - 210, y extent 80 - (-80) (x axis is PARALLEL to the arm, short edge of the paper) """ if type(strName) == str: strName = strName[:26] # going to truncate user input to a 26 characters max intLenName = len(strName) if (intLenName < 4): self.scope = 40.0 # keeping it real else: self.scope = math.floor(160.0/(intLenName * 1.1)) self.x0 = 160 - (0.5 * self.scope) self.y0 = self.scope * intLenName * 1.1 / 2 return def LetterSelect(self, c): """ given char c, return the plotting function index 0 resolves to the question mark character """ index = 0 if type(c) == str: if c == ' ': return self.SpaceBar else: c = c.upper() if c in self.lstValidCharSet: index = self.lstValidCharSet.index(c) - self.lstValidCharSet.index('A') + 1 # 0th item is '?' # if c in ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']: # index = ord(c) - ord('A') + 1 # using question mark as the 0th index item return self.lstLetter[index] # return the function to use
class uart: available_pixel = {} #rgb values of all the paints swift = None #robot arm object device_info = None firmware_version = None image = None #image you're trying to paint canvas = None #image of the canvas as you're working on it canvas_corners = None #points of the four corners of the canvas (in robot arm coords) ptransform = None #contains the warped image of M = None #transformation matrix xScale = None yScale = None # # __init__ # im = the image you're trying to paint # pixels = the dictionary of colors you have access to # initialized = a list of booleans determining which values you will initialize # [ True = available_pixel uses pixels parameter otherwise use defaults, # True = set swift to SwiftAPI object otherwise set them to None, # True = set image to a blank white 200x200 image, # True = calibrate canvas_corners using setFourCorners otherwise set to a preset # True = set ptransform using the webcam # ] # def __init__(self, im, pixels, initialized): if initialized[0]: self.available_pixel = pixels else: self.available_pixel = {'red':[255,0,0], 'green':[0,255,0], 'blue':[0,0,255],'magenta':[255,0,255], 'tomato':[255,99,71], 'lawn green':[124,252,0]} if initialized[1]: self.swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}) self.device_info = self.swift.get_device_info() self.firmware_version = self.device_info['firmware_version'] self.swift.set_mode(0) if initialized[2]: self.image = im if initialized[3] and initialized[1]: print("moving") self.swift.set_position(x=150, y=0, z=50, speed = 10000, cmd = "G0") # self.swift.set_wrist(20) # time.sleep(1) # self.swift.set_wrist(90) print("Setting four corners; input tl, tr, bl or br") self.canvas_corners = self.setFourCorners() else: self.swift.set_position(x=150, y=0, z=50, speed = 10000, cmd = "G0") self.canvas_corners = [ [263,50,103], #tl [263,-50,103],#tr [241,50,-12],#bl [241,-50,-12]]#br print("Setting four corners to default coordinates") if initialized[4]: _, cap = cv2.VideoCapture(0).read() self.ptransform = perspective.PerspectiveTransform(cap) self.M = self.get_m(200,200) self.xScale = self.get_scale(len(im[0]),[self.canvas_corners[0],self.canvas_corners[1]]) self.yScale = self.get_scale(len(im),[self.canvas_corners[0],self.canvas_corners[2]]) print("Arm all set up!") # # new xy to xyz function using algebra/geometry # def xy_to_xyz2(self, xy): #print("xy", xy) #print("xscale", self.xScale) #print("yscale", self.yScale) out = np.add(np.multiply(xy[0],self.xScale) + np.multiply(xy[1],self.yScale), self.canvas_corners[0]) print(out) return out # # GET SCALE # def get_scale(self, pix, corners): dif = np.subtract(corners[0], corners[1]) return -(dif/pix) # # HEAT MAP # def generate_heatmap(self): image = self.image.astype(dtype='int32') canvas = self.ptransform.warped.astype(dtype='int32') subtraction = np.subtract(image,canvas) print(subtraction) heatmap = np.full(im.shape,255, dtype='uint8') print(heatmap.shape) for i in range(subtraction.shape[0]): for j in range(subtraction.shape[1]): if (subtraction[i][j] < 0): heatmap[i][j][0] -= abs(subtraction[i][j]) heatmap[i][j][1] -= abs(subtraction[i][j]) elif (subtraction[i][j] > 0): heatmap[i][j][2] -= abs(subtraction[i][j]) heatmap[i][j][1] -= abs(subtraction[i][j]) return heatmap # # GETS CLOSEST COLOR # def get_closest_color(self, chosen_pixel): available_pixel = self.available_pixel distances = [] for key, value in available_pixel.items(): a1 = np.asarray(value) c1 = np.asarray(chosen_pixel) curr_dist = np.linalg.norm(a1 - c1) distances += [curr_dist] if(curr_dist == min(distances)): curr_key = key return curr_key # # move_to_file # def move_to_file(self, filename): var = [] count = 0 lines = open(filename, "r").read().split('\n') x,y,z,f,angle = 0 moveArm,moveWrist = False for i in range(len(lines)): for word in lines[i].split(' '): if(word is 'G0'): moveArm = True if(word[0] is 'X'): x = float(word[1:]) elif(word[0] is 'Y'): y = float(word[1:]) elif(word[0] is 'Z'): z = float(word[1:]) elif(word[0] is 'F'): f = float(word[1:]) elif(word is 'WA'): moveWrist = True angle = float(word[1:]) if(moveArm): self.swift.set_position(x=x, y=y, z=z, speed =f, cmd = "G0") moveArm = False time.sleep(1) if(moveWrist): self.swift.set_wrist(angle) moveWrist = False time.sleep(1) coordinates.close() # # SETTING FOUR CORNERS # def setFourCorners(self): speed_s = 10000 delay = 1 cmd_s = 'G0' todo = 4 coords = [[], [], [], []] while todo >0: key = input() if key == "tr": newCoord = self.swift.get_position() coords[1] = newCoord todo -= 1 print("Top right coordinate saved as ", newCoord) elif key == "tl": newCoord = self.swift.get_position() coords[0] = newCoord todo -= 1 print("Top left coordinate saved as", newCoord) elif key == "bl": newCoord = self.swift.get_position() coords[2] = newCoord todo -= 1 print("Bottom left coordinate saved as", newCoord) elif key == "br": newCoord = self.swift.get_position() coords[3] = newCoord todo -= 1 print("Bottom right coodirnate saved as", newCoord) return coords # # SAVED COORDS TO FILE # def saveCoordsToFile(self, fn): delay = 1 coords = [] while True: key = input() if key == "save": newCoord = swift.get_position() coords.append(newCoord) print("New coordinate saved as" + str(newCoord)) elif key == "done": break elif key.isdigit(): coords.append(int(key)) if os.path.exists(fn + ".uar"): os.remove(fn + ".uar") file = open(fn + ".uar", "w+") for c in coords: if not check(c): file.write("G0 X%f Y%f Z%f F5000\n" %(c[0], c[1], c[2])) else: self.set_wrist(c) file.write("WA " %(c)) coordinates.close() moveTo(fn + ".uar") return coords def check(inp): try: num_float = float(inp) return True except: return False # # GET M # def get_m(self, width, height): A = np.transpose(self.canvas_corners) print(A) B = [[0,0,1],[width,0,1],[0,height,1],[width,height,1]] B = np.transpose(B) print(B) pinvB = np.linalg.pinv(B) print(pinvB) M = np.matmul(A, np.linalg.pinv(B)) print(M) return M # # xytoxyz # def xy_to_xyz(self,xy): xyz = [xy[0],xy[1],1] xyz = np.transpose(xyz) return np.matmul(self.M,xyz) # # go to position # def go_to_position(self, xyz, f): print('going to : ', xyz) self.swift.set_position(x=xyz[0], y=xyz[1], z=xyz[2], speed = f, cmd = "G0") #: time.sleep(1) # # draw a line # # start and end: [x,y] def draw_line(self, start, end): startxyz = self.xy_to_xyz2(start) endxyz = self.xy_to_xyz2(end) start_pre = [startxyz[0]-20, startxyz[1], startxyz[2]] end_post = [endxyz[0]-20, endxyz[1], endxyz[2]] print("going to start pre") self.go_to_position(start_pre, 10000) print("going to start") self.go_to_position(startxyz, 5000) print("going to end") self.go_to_position(endxyz, 5000) print("going to end post") self.go_to_position(end_post, 10000) # # # draws a line, by moving across a list of points # def draw_line2(self, points): startxyz = self.xy_to_xyz2(points[0]) endxyz = self.xy_to_xyz2(points[-1]) start_pre = [startxyz[0]-5, startxyz[1], startxyz[2]] end_post = [endxyz[0]-5, endxyz[1], endxyz[2]] #print("going to start pre") self.go_to_position(start_pre, 10000) for point in points: point_xyz = self.xy_to_xyz2(point) self.go_to_position(point_xyz, 5000) #print("going to end post") self.go_to_position(end_post, 10000) # # # draws a line, by moving across a list of points # does NOT go to pre and post painting position # def draw_line3(self, points): startxyz = self.xy_to_xyz2(points[0]) endxyz = self.xy_to_xyz2(points[-1]) #print("going to start pre") #self.go_to_position(start_pre, 10000) for point in points: point_xyz = self.xy_to_xyz2(point) self.go_to_position(point_xyz, 5000)
swift = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'}) swift.waiting_ready() ret = swift.get_power_status() print('power status: {}'.format(ret)) ret = swift.get_device_info() print('device info: {}'.format(ret)) ret = swift.get_limit_switch() print('limit switch: {}'.format(ret)) ret = swift.get_gripper_catch() print('gripper catch: {}'.format(ret)) ret = swift.get_mode() print('mode: {}'.format(ret)) print('set mode:', swift.set_mode(1)) ret = swift.get_mode() print('mode: {}'.format(ret)) ret = swift.get_servo_attach(servo_id=2) print('servo attach: {}'.format(ret)) ret = swift.get_servo_angle() print('servo angle: {}'.format(ret)) def print_callback(ret, key=None): print('{}: {}'.format(key, ret)) swift.get_polar(wait=False, callback=functools.partial(print_callback, key='polar')) swift.get_position(wait=False, callback=functools.partial(print_callback, key='position')) swift.flush_cmd()