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()
示例#2
0
class UArm_SDK(object):
    def __init__(self):
        '''
        connect to UArm
        '''
        self.swift = SwiftAPI()

        self.swift.connect()
        self.swift.get_power_status()
        print(self.swift.get_device_info())

        self.swift.reset(wait=True)  # back to home position
        print('init complete')

        self.gripper_temp = 0  # keep track of gripper state

    def __del__(self):
        '''
        disconnect UArm
        '''
        self.swift.disconnect()
        print('uarm disconnected')

    def set_servo_angle(self, joint_angles, dt):
        '''
        set servo angle via SDK
        input:
            joint_angles, 5-vector: [theta1, theta2, theta3, theta4, pump state] in degrees
            dt, time step
        '''

        wait = True

        self.swift.set_servo_angle(servo_id=0,
                                   angle=joint_angles[0] + 90,
                                   speed=5000,
                                   wait=wait)
        time.sleep(dt / 4)
        self.swift.set_servo_angle(servo_id=1,
                                   angle=joint_angles[1],
                                   speed=5000,
                                   wait=wait)
        time.sleep(dt / 4)
        self.swift.set_servo_angle(servo_id=2,
                                   angle=joint_angles[2] - joint_angles[1],
                                   speed=5000,
                                   wait=wait)
        time.sleep(dt / 4)
        self.swift.set_servo_angle(servo_id=3,
                                   angle=180 - joint_angles[3],
                                   speed=5000,
                                   wait=wait)
        time.sleep(dt / 4)
        if joint_angles[4] > 0:
            self.swift.set_pump(on=True)
        elif joint_angles[4] == 0:
            self.swift.set_pump(on=False)
        else:
            print("ERROR")

    def control_uarm_via_traj(self, position, wrist_angle, pump_state, dt):
        '''
        set end effector position, wrist angle and pump state via SDK
        input:
            position, 3-vector: [px, py, pz]
            wrist_angle: wrist angle in rad
            pump_state: bool, 0 - off, 1 - on
        '''
        px, py, pz = position[0], position[1], position[2]
        # conver m to mm
        px *= 1000
        py *= 1000
        pz *= 1000

        # change end effector position
        e = self.swift.set_position(x=px, y=py, z=pz, speed=100000, wait=True)
        print(e)

        # change wrist angle
        self.swift.set_wrist(90 - wrist_angle * 180 / PI)

        if self.gripper_temp == 0 and pump_state == 1:
            # enable suction cup
            self.swift.set_pump(on=True, wait=True)
            print('pump on')
            self.gripper_temp = 1
        if self.gripper_temp == 1 and pump_state == 0:
            # disable suction cup
            self.swift.set_pump(on=False, wait=True)
            print('pump off')
            self.gripper_temp = 0

        time.sleep(dt)
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)
示例#5
0
import sys
import time
from datetime import datetime
from uarm.wrapper import SwiftAPI
sys.path.append(os.path.join(os.path.dirname(__file__), '../../..'))

speed = 20000

swift2 = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'})  # Wischer
swift2.waiting_ready()
swift1 = SwiftAPI(filters={'hwid': 'USB VID:PID=2341:0042'})  # Stift
swift1.waiting_ready()

# Stift Setting
swift1.set_position(150, 0, 10, speed=speed, timeout=20)
swift1.set_wrist(90)
time.sleep(2)
swift1.set_gripper(True)
time.sleep(2)
swift1.set_position(150, 0, 20, speed=speed, timeout=20)
time.sleep(2)

# Wischer Setting
swift2.set_position(150, 0, 0, speed=speed, timeout=20)
swift1.set_wrist(90)
time.sleep(2)
swift2.set_gripper(True)
swift2.set_position(110, 0, 20, speed=speed, timeout=20)
time.sleep(2)

示例#6
0
coords = open("output.txt", "r")
lines = coords.readlines()

for coord in lines:
    swift.reset()
    data = coord.split()

    x = data[1]
    y = data[0]

    x_out = -0.3498 * int(float(x)) / 1932 * 2016 + 524.2  #+0.01*int(y)
    y_out = -0.355 * int(
        float(y)) / 1449 * 1504 + 341 - 0.01 * (1400 - int(float(x)))

    swift.set_position(x=x_out, y=y_out, z=15)
    swift.set_wrist(90)
    time.sleep(0.2)
    #down = input("Down? ")
    swift.set_pump(True)
    swift.set_position(x=x_out, y=y_out, z=0)
    #up = input("Up? ")
    time.sleep(0.2)
    swift.set_position(x=x_out, y=y_out, z=30)
    ang1 = swift.get_servo_angle()[0]

    x = data[3]
    y = data[2]
    r = data[4]

    x_out = -0.3498 * int(float(x)) + 524.2  #+0.01*int(y)
    y_out = -0.355 * int(float(y)) + 341 - 0.01 * (1400 - int(float(x)))