44 SCRIPT_URP =
'''<URProgram createdIn="3.0.0" lastSavedIn="3.0.0" name="%s" directory="/" installation="default"> 46 <MainProgram runOnlyOnce="false" motionType="MoveJ" speed="1.0471975511965976" acceleration="1.3962634015954636" useActiveTCP="false"> 51 <file resolves-to="file">%s</file> 60 """Get a safe program name""" 61 for c
in r'-[]/\;,><&*:%=+@!#^()|?^':
62 progname = progname.replace(c,
'')
63 if len(progname) <= 0:
65 if progname[0].isdigit():
66 progname =
'P' + progname
77 """Calculate the p[x,y,z,rx,ry,rz] position for a pose target""" 78 def saturate_1(value):
79 return min(max(value,-1.0),1.0)
81 angle =
acos( saturate_1((pose[0,0]+pose[1,1]+pose[2,2]-1)/2) )
82 rxyz = [pose[2,1]-pose[1,2], pose[0,2]-pose[2,0], pose[1,0]-pose[0,1]]
88 rxyz =
mult3(rxyz, angle)
89 return [pose[0,3], pose[1,3], pose[2,3], rxyz[0], rxyz[1], rxyz[2]]
92 """Prints a pose target""" 95 return (
'p[%.6f, %.6f, %.6f, %.6f, %.6f, %.6f]' % (x*MM_2_M,y*MM_2_M,z*MM_2_M,w,p,r))
98 """Prints a joint target""" 102 return (
'[%.6f, %.6f, %.6f, %.6f, %.6f, %.6f]' % (angles[0]*d2r, angles[1]*d2r, angles[2]*d2r, angles[3]*d2r, angles[4]*d2r, angles[5]*d2r))
104 return 'this post only supports 6 joints' 109 """Robot post object""" 115 BLEND_RADIUS_M = 0.001
121 ROBOT_NAME =
'generic' 123 MAIN_PROGNAME =
'unknown' 131 def __init__(self, robotpost=None, robotname=None, robot_axes = 6, **kwargs):
142 self.
addline(
' # Subprogram %s' % progname)
143 self.
addline(
' def %s():' % progname)
150 self.
addline(
' # End of main program')
157 def ProgSave(self, folder, progname, ask_user = False, show_result = False):
160 progname = progname +
'.' + self.
PROG_EXT 162 filesave =
getSaveFile(folder, progname,
'Save program as...')
163 if filesave
is not None:
164 filesave = filesave.name
168 filesave = folder +
'/' + progname
170 fid = open(filesave,
"w")
172 fid.write(
' # Default parameters:\n')
173 fid.write(
' global speed_ms = %.3f\n' % self.
SPEED_MS)
174 fid.write(
' global speed_rads = %.3f\n' % self.
SPEED_RADS)
175 fid.write(
' global accel_mss = %.3f\n' % self.
ACCEL_MSS)
176 fid.write(
' global accel_radss = %.3f\n' % self.
ACCEL_RADSS)
177 fid.write(
' global blend_radius_m = %.3f\n' % self.
BLEND_RADIUS_M)
179 fid.write(
' # Add any suprograms here\n')
183 fid.write(
' # Main program:\n')
184 for line
in self.
PROG:
188 print(
'SAVED: %s\n' % filesave)
210 if type(show_result)
is str:
213 p = subprocess.Popen([show_result, filesave])
214 elif type(show_result)
is list:
216 p = subprocess.Popen(show_result + [filesave])
220 os.startfile(filesave)
221 if len(self.
LOG) > 0:
222 mbox(
'Program generation LOG:\n\n' + self.
LOG)
225 """Send a program to the robot using the provided parameters. This method is executed right after ProgSave if we selected the option "Send Program to Robot". 226 The connection parameters must be provided in the robot connection menu of RoboDK""" 232 print(
"POPUP: Connecting to robot...")
234 robot_socket = socket.create_connection((robot_ip, 30002))
235 print(
"POPUP: Sending program")
237 robot_socket.send(progfile.read())
238 print(
"POPUP: Sending program...")
241 received = robot_socket.recv(4096)
250 idx_error = received.index(b
'error')
254 idx_error_end = min(idx_error + 20, len(received))
256 idx_error_end = received.index(b
'\0',idx_error)
259 print(
"POPUP: Robot response: <strong>" + received[idx_error:idx_error_end].decode(
"utf-8") +
"</strong>")
263 print(
"POPUP: Program sent. The program should be running on the robot.")
266 print(
"POPUP: Problems running program...")
273 blend_radius =
'blend_radius_m';
274 current_pos = pose.Pos()
280 blend_radius =
'%.3f' % (max(0.4*distance*0.001, 0.001))
284 def MoveJ(self, pose, joints, conf_RLF=None):
285 """Add a joint movement""" 291 self.
addline(
'movej(%s,accel_radss,speed_rads,0,%s)' % (
angles_2_str(joints), blend_radius))
293 def MoveL(self, pose, joints, conf_RLF=None):
294 """Add a linear movement""" 306 self.
addline(
'movel(%s,accel_mss,speed_ms,0,%s)' % (target, blend_radius))
309 def MoveC(self, pose1, joints1, pose2, joints2, conf_RLF_1=None, conf_RLF_2=None):
310 """Add a circular movement""" 315 radius = a*b*c/
sqrt(pow(a*a+b*b+c*c,2)-2*(pow(a,4)+pow(b,4)+pow(c,4)))
318 def distance_p1_p02(p0,p1,p2):
321 return dot(v02,v01)/
dot(v02,v02)
327 self.
MoveL(pose2, joints2, conf_RLF_2)
331 if radius < 1
or radius > 100000:
332 self.
MoveL(pose2, joints2, conf_RLF_2)
335 d_p1_p02 = distance_p1_p02(p0, p1, p2)
337 self.
MoveL(pose2, joints2, conf_RLF_2)
345 def setFrame(self, pose, frame_id=None, frame_name=None):
346 """Change the robot reference frame""" 353 def setTool(self, pose, tool_id=None, tool_name=None):
354 """Change the robot TCP""" 358 """Pause the robot program""" 360 self.
addline(
'halt() # reimplement this function to force stop')
362 self.
addline(
'sleep(%.3f)' % (time_ms*0.001))
365 """Changes the robot speed (in mm/s)""" 370 """Changes the robot acceleration (in mm/s2)""" 375 """Changes the robot joint speed (in deg/s)""" 380 """Changes the robot joint acceleration (in deg/s2)""" 385 """Changes the zone data approach (makes the movement more smooth)""" 392 """Sets a variable (output) to a given value""" 393 if type(io_value) != str:
399 if type(io_var) != str:
400 newline =
'set_standard_digital_out(%s, %s)' % (str(io_var), io_value)
402 newline =
'%s = %s' % (io_var, io_value)
406 def waitDI(self, io_var, io_value, timeout_ms=-1):
407 """Waits for an input io_var to attain a given value io_value. Optionally, a timeout can be provided.""" 408 if type(io_var) != str:
409 io_var =
'get_standard_digital_in(%s)' % str(io_var)
410 if type(io_value) != str:
418 self.
addline(
'while (%s != %s):' % (io_var, io_value))
422 def RunCode(self, code, is_function_call = False):
423 """Adds code or a function call""" 425 code.replace(
' ',
'_')
426 if not code.endswith(
')'):
434 """Show a message on the controller screen""" 438 self.
addline(
'popup("%s","Message",False,False,blocking=True)' % message)
442 """Add a program line""" 444 self.
PROG.append(self.
TAB + newline +
'\n')
446 self.
SUBPROG.append(self.
TAB + newline +
'\n')
449 """Add a log message""" 455 [x,y,z,r,p,w] = xyzrpw
465 return Mat([[cb*ca, ca*sc*sb - cc*sa, sc*sa + cc*ca*sb, x],[cb*sa, cc*ca + sc*sb*sa, cc*sb*sa - ca*sc, y],[-sb, cb*sc, cc*cb, z],[0,0,0,1]])
468 """Test the post with a basic program""" 470 robot =
RobotPost(
'Universal Robotics',
'Generic UR robot')
472 robot.ProgStart(
"Program")
473 robot.RunMessage(
"Program generated by RoboDK",
True)
474 robot.setFrame(
Pose([807.766544, -963.699898, 41.478944, 0, 0, 0]))
475 robot.setTool(
Pose([62.5, -108.253175, 100, -60, 90, 0]))
477 robot.setAcceleration(3000)
478 robot.MoveJ(
Pose([200, 200, 500, 180, 0, 180]), [-46.18419, -6.77518, -20.54925, 71.38674, 49.58727, -302.54752] )
479 robot.MoveL(
Pose([200, 250, 348.734575, 180, 0, -150]), [-41.62707, -8.89064, -30.01809, 60.62329, 49.66749, -258.98418] )
480 robot.MoveL(
Pose([200, 200, 262.132034, 180, 0, -150]), [-43.73892, -3.91728, -35.77935, 58.57566, 54.11615, -253.81122] )
481 robot.RunMessage(
"Setting air valve 1 on")
482 robot.RunCode(
"TCP_On",
True)
484 robot.MoveL(
Pose([200, 250, 348.734575, 180, 0, -150]), [-41.62707, -8.89064, -30.01809, 60.62329, 49.66749, -258.98418] )
485 robot.MoveL(
Pose([250, 300, 278.023897, 180, 0, -150]), [-37.52588, -6.32628, -34.59693, 53.52525, 49.24426, -251.44677] )
486 robot.MoveL(
Pose([250, 250, 191.421356, 180, 0, -150]), [-39.75778, -1.04537, -40.37883, 52.09118, 54.15317, -246.94403] )
487 robot.RunMessage(
"Setting air valve off")
488 robot.RunCode(
"TCP_Off",
True)
490 robot.MoveL(
Pose([250, 300, 278.023897, 180, 0, -150]), [-37.52588, -6.32628, -34.59693, 53.52525, 49.24426, -251.44677] )
491 robot.MoveL(
Pose([250, 200, 278.023897, 180, 0, -150]), [-41.85389, -1.95619, -34.89154, 57.43912, 52.34162, -253.73403] )
492 robot.MoveL(
Pose([250, 150, 191.421356, 180, 0, -150]), [-43.82111, 3.29703, -40.29493, 56.02402, 56.61169, -249.23532] )
493 robot.ProgFinish(
"Program")
495 for line
in robot.PROG:
497 if len(robot.LOG) > 0:
498 mbox(
'Program generation LOG:\n\n' + robot.LOG)
500 input(
"Press Enter to close...")
502 if __name__ ==
"__main__":
503 """Function to call when the module is executed by itself: test"""
def setAcceleration(self, accel_mmss)
def ProgStart(self, progname)
def __init__(self, robotpost=None, robotname=None, robot_axes=6, kwargs)
def blend_radius_check(self, pose)
def MoveJ(self, pose, joints, conf_RLF=None)
def setDO(self, io_var, io_value)
def RunCode(self, code, is_function_call=False)
def ProgFinish(self, progname)
def get_safe_name(progname)
def ProgSave(self, folder, progname, ask_user=False, show_result=False)
def waitDI(self, io_var, io_value, timeout_ms=-1)
def MoveL(self, pose, joints, conf_RLF=None)
def MoveC(self, pose1, joints1, pose2, joints2, conf_RLF_1=None, conf_RLF_2=None)
def setAccelerationJoints(self, accel_degss)
def mbox(msg, b1='OK', b2='Cancel', frame=True, t=False, entry=None)
def setSpeedJoints(self, speed_degs)
def setZoneData(self, zone_mm)
def setTool(self, pose, tool_id=None, tool_name=None)
def setFrame(self, pose, frame_id=None, frame_name=None)
def addlog(self, newline)
def addline(self, newline)
def RunMessage(self, message, iscomment=False)
def ProgSendRobot(self, robot_ip, remote_path, ftp_user, ftp_pass)
def setSpeed(self, speed_mms)
def getSaveFile(strdir='C:\\', strfile='file.txt', strtitle='Save file as ...')
def circle_radius(p0, p1, p2)