|
| 1 | +from robolink import * # API to communicate with RoboDK |
| 2 | +from robodk import * # basic matrix operations |
| 3 | + |
| 4 | +# Start the with RoboDK |
| 5 | +RDK = Robolink() |
| 6 | + |
| 7 | +# Select the robot |
| 8 | +ROBOT = RDK.ItemUserPick('Select a robot', ITEM_TYPE_ROBOT) |
| 9 | + |
| 10 | +FRAME = RDK.Item('Path Reference', ITEM_TYPE_FRAME) |
| 11 | +TOOL = RDK.Item('Tool Reference', ITEM_TYPE_TOOL) |
| 12 | +if not FRAME.Valid() or not TOOL.Valid(): |
| 13 | + raise Exception("Select appropriate FRAME and TOOL references") |
| 14 | + |
| 15 | +# Check if the user selected a robot |
| 16 | +if not ROBOT.Valid(): |
| 17 | + quit() |
| 18 | + |
| 19 | +# csv_file = 'C:/Users/Albert/Desktop/Var_P.csv' |
| 20 | +csv_file = getOpenFile(RDK.getParam('PATH_OPENSTATION')) |
| 21 | + |
| 22 | +# Load P_Var.CSV data as a list of poses, including links to reference and tool frames |
| 23 | +def load_targets(strfile): |
| 24 | + csvdata = LoadList(strfile, ',', 'utf-8') |
| 25 | + poses = [] |
| 26 | + idxs = [] |
| 27 | + for i in range(0, len(csvdata)): |
| 28 | + x,y,z,rx,ry,rz = csvdata[i][0:6] |
| 29 | + poses.append(transl(x,y,z)*rotz(rz*pi/180)*roty(ry*pi/180)*rotx(rx*pi/180)) |
| 30 | + idxs.append(csvdata[i][6]) |
| 31 | + return poses, idxs |
| 32 | + |
| 33 | +# Load and display Targets from the CSV file |
| 34 | +def load_targets_GUI(strfile): |
| 35 | + poses, idxs = load_targets(strfile) |
| 36 | + program_name = getFileName(strfile) |
| 37 | + program_name = program_name.replace('-','_').replace(' ','_') |
| 38 | + program = RDK.Item(program_name, ITEM_TYPE_PROGRAM) |
| 39 | + if program.Valid(): |
| 40 | + program.Delete() |
| 41 | + |
| 42 | + program = RDK.AddProgram(program_name, ROBOT) |
| 43 | + program.setFrame(FRAME) |
| 44 | + program.setTool(TOOL) |
| 45 | + ROBOT.MoveJ(ROBOT.JointsHome()) |
| 46 | + |
| 47 | + for pose, idx in zip(poses, idxs): |
| 48 | + name = '%s-%i' % (program_name, idx) |
| 49 | + target = RDK.Item(name, ITEM_TYPE_TARGET) |
| 50 | + if target.Valid(): |
| 51 | + target.Delete() |
| 52 | + target = RDK.AddTarget(name, FRAME, ROBOT) |
| 53 | + target.setPose(pose) |
| 54 | + |
| 55 | + try: |
| 56 | + program.MoveJ(target) |
| 57 | + except: |
| 58 | + print('Warning: %s can not be reached. It will not be added to the program' % name) |
| 59 | + |
| 60 | + |
| 61 | +def load_targets_move(strfile): |
| 62 | + poses, idxs = load_targets(strfile) |
| 63 | + |
| 64 | + ROBOT.setFrame(FRAME) |
| 65 | + ROBOT.setTool(TOOL) |
| 66 | + |
| 67 | + ROBOT.MoveJ(ROBOT.JointsHome()) |
| 68 | + |
| 69 | + for pose, idx in zip(poses, idxs): |
| 70 | + try: |
| 71 | + ROBOT.MoveJ(pose) |
| 72 | + except: |
| 73 | + RDK.ShowMessage('Target %i can not be reached' % idx, False) |
| 74 | + |
| 75 | + |
| 76 | +# Force just moving the robot after double clicking |
| 77 | +#load_targets_move(csv_file) |
| 78 | +#quit() |
| 79 | + |
| 80 | +# Recommended mode of operation: |
| 81 | +# 1-Double click the python file creates a program in RoboDK station |
| 82 | +# 2-Generate program generates the program directly |
| 83 | + |
| 84 | +MAKE_GUI_PROGRAM = False |
| 85 | + |
| 86 | +ROBOT.setFrame(FRAME) |
| 87 | +ROBOT.setTool(TOOL) |
| 88 | + |
| 89 | + |
| 90 | +if RDK.RunMode() == RUNMODE_SIMULATE: |
| 91 | + MAKE_GUI_PROGRAM = True |
| 92 | + # MAKE_GUI_PROGRAM = mbox('Do you want to create a new program? If not, the robot will just move along the tagets', 'Yes', 'No') |
| 93 | + |
| 94 | +else: |
| 95 | + # if we run in program generation mode just move the robot |
| 96 | + MAKE_GUI_PROGRAM = False |
| 97 | + |
| 98 | + |
| 99 | +if MAKE_GUI_PROGRAM: |
| 100 | + RDK.Render(False) # Faster if we turn render off |
| 101 | + load_targets_GUI(csv_file) |
| 102 | +else: |
| 103 | + load_targets_move(csv_file) |
0 commit comments