47 from robolink 
import *
    51 PYTHON_PATH = os.path.abspath(os.getenv(
'APPDATA') + 
'/../Local/Programs/Python/Python36-32/python.exe')
    54 DOBOT_PATH_DLL = os.path.abspath(os.getenv(
'HOMEPATH') + 
'/Desktop/DobotAPI/')
    56 PROGRAM_HEADER = 
'''# Dobot program generated from RoboDK post processor    58 import DobotDllType as dType    62     dType.DobotConnect.DobotConnect_NoError:  "DobotConnect_NoError",    63     dType.DobotConnect.DobotConnect_NotFound: "DobotConnect_NotFound",    64     dType.DobotConnect.DobotConnect_Occupied: "DobotConnect_Occupied"}    70 state = dType.ConnectDobot(api, "", 115200)[0]    71 print("Connect status:",CON_STR[state])    74 if (state != dType.DobotConnect.DobotConnect_NoError):    76     dType.DisconnectDobot(api)    77     raise Exception("Connection problems: " + CON_STR[state])    81 dType.SetQueuedCmdClear(api)    83 #Async Motion Params Setting    84 dType.SetHOMEParams(api, 200, 200, 200, 200, isQueued = 1)    85 dType.SetPTPJointParams(api, 200, 200, 200, 200, 200, 200, 200, 200, isQueued = 1)    86 dType.SetPTPCommonParams(api, 100, 100, isQueued = 1)    89 dType.SetHOMECmd(api, temp = 0, isQueued = 1)    91 # Main program definition    95     # Save the index command of the last command (set a short pause)    96     lastIndex = dType.SetWAITCmd(api, 0.1, isQueued = 1)[0]    98     # Start to Execute Queued Commands    99     dType.SetQueuedCmdStartExec(api)   101     # Wait until execution is done by verifying current command   102     while lastIndex > dType.GetQueuedCmdCurrentIndex(api)[0]:   106 ROBOT_DISCONNECT = 
'''   108 dType.DisconnectDobot(api)   113     """Prints a pose target"""   116     return (
'%.3f, %.3f, %.3f, %.3f' % (x,y,z, w*180.0/pi))
   119     """Prints a joint target"""   121     for i 
in range(len(joints)):
   122         str = str + (
'%.6f, ' % joints[i])
   129     """Robot post object"""   131     ROBOT_IP = 
'192.168.0.100';
   145     MAIN_PROG_NAME = 
None   150     def __init__(self, robotpost=None, robotname=None, robot_axes = 6, **kwargs):
   159         self.
addline(
'def %s():' % progname)
   163             self.
addline(
"'''Main procedure'''")
   165             self.
addline(
"'''Subprogram %s'''" % progname)
   166         self.
addline(
"print('Sending program %s ...')" % progname)
   172         self.
addline(
"print('Program %s sent')" % progname)
   173         self.
addline(
"sys.stdout.flush()")
   174         self.
addline(
"print('Running program %s on robot...')" % progname)
   175         self.
addline(
"sys.stdout.flush()")        
   178         self.
addline(
"# Stop executing commands")
   179         self.
addline(
"dType.SetQueuedCmdStopExec(api)")
   180         self.
addline(
"# Clear all queued commands")
   181         self.
addline(
"dType.SetQueuedCmdClear(api)")
   182         self.
addline(
"print('Program %s Finished')" % progname)
   183         self.
addline(
"sys.stdout.flush()")
   188     def ProgSave(self, folder, progname, ask_user=False, show_result=False):
   190         progname = progname + 
'.' + self.
PROG_EXT   192             filesave = 
getSaveFile(folder, progname, 
'Save program as...')
   193             if filesave 
is not None:
   194                 filesave = filesave.name
   198             filesave = folder + 
'/' + progname
   200         fid = open(filesave, 
"w")
   202         fid.write(PROGRAM_HEADER)
   205         fid.write(
'# Main program call: set as a loop\n')
   206         fid.write(
'while True:\n')        
   208         fid.write(
'    break\n')        
   209         fid.write(ROBOT_DISCONNECT)
   212         print(
'SAVED: %s\n' % filesave)
   217         selection_view = 
True   219         if selection_view 
and show_result:
   220             if type(show_result) 
is str:
   223                 p = subprocess.Popen([show_result, filesave])   
   224             elif type(show_result) 
is list:
   226                 p = subprocess.Popen(show_result + [filesave])   
   230                 os.startfile(filesave)  
   232             if len(self.
LOG) > 0:
   233                 mbox(
'Program generation LOG:\n\n' + self.
LOG)
   235         if not selection_view:
   237             proc = subprocess.Popen([
'python', filesave])
   247         """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".   248         The connection parameters must be provided in the robot connection menu of RoboDK"""   258         process_env = os.environ.copy()
   259         process_env[
"PYTHONPATH"] = DOBOT_PATH_DLL + 
";"   260         process_env[
"PATH"] = DOBOT_PATH_DLL + 
":" + process_env[
"PATH"]
   262         print(
"POPUP: Starting process...")
   263         PROGRAM_FILE = os.path.abspath(self.
PROG_FILES)
   264         print(
"Python path: " + PYTHON_PATH)
   265         print(
"Program file: " + PROGRAM_FILE)
   269         process = subprocess.Popen([PYTHON_PATH, PROGRAM_FILE], shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=process_env, universal_newlines=
True, cwd=DOBOT_PATH_DLL)
   272         for stdout_line 
in iter(process.stdout.readline, 
""):
   273             print(
"POPUP: " + stdout_line.strip())
   276         process.stdout.close()
   277         return_code = process.wait()
   279             raise subprocess.CalledProcessError(return_code, [PYTHON_PATH, PROGRAM_FILE])
   281         if (return_code == 0):
   286             raise ProcessException(command, return_code, output)
   288     def MoveJ(self, pose, joints, conf_RLF=None):
   289         """Add a joint movement"""   291         if joints 
is not None:
   292             self.
addline(
"dType.SetPTPCmd(api, dType.PTPMode.PTPMOVJANGLEMode, %s, isQueued=1)" % 
joints_2_str(joints))
   295             self.
addline(
"dType.SetPTPCmd(api, dType.PTPMode.PTPMOVLXYZMode, %s, isQueued=1)" % 
pose_2_str(robot_pose))
   297     def MoveL(self, pose, joints, conf_RLF=None):
   298         """Add a linear movement"""   300         if joints 
is not None:
   301             self.
addline(
"dType.SetPTPCmd(api, dType.PTPMode.PTPMOVJANGLEMode, %s, isQueued=1)" % 
joints_2_str(joints))
   304             self.
addline(
"dType.SetPTPCmd(api, dType.PTPMode.PTPMOVLXYZMode, %s, isQueued=1)" % 
pose_2_str(robot_pose))
   306     def MoveC(self, pose1, joints1, pose2, joints2, conf_RLF_1, conf_RLF_2):
   307         """Add a circular movement"""   312     def setFrame(self, pose, frame_id=None, frame_name=None):
   313         """Change the robot reference frame"""   316     def setTool(self, pose, tool_id=None, tool_name=None):
   317         """Change the robot TCP"""   321         """Pause the robot program"""   323             self.
addline(
"dType.SetWAITCmd(api, 0.1, 1)")
   324             mbox(
"Robot paused, press OK to continue")
   326             self.
addline(
"dType.SetWAITCmd(api, %s, 1)" % (time_ms * 0.001))
   329         """Changes the robot speed (in mm/s)"""   334         """Changes the robot acceleration (in mm/s2)"""   335         self.addlog(
'Linear acceleration not supported')
   338         """Changes the robot joint speed (in deg/s)"""   343         """Changes the robot joint acceleration (in deg/s2)"""   348         """Changes the smoothing radius (aka CNT, APO or zone data) to make the movement smoother"""   356         """Sets a variable (digital output) to a given value"""   357         if type(io_var) != str:  
   358             io_var = 
'OUT[%s]' % str(io_var)
   359         if type(io_value) != str: 
   367         self.addlog(
'Digital IOs not managed by the robot (%s=%s)' % (io_var, io_value))
   369     def waitDI(self, io_var, io_value, timeout_ms=-1):
   370         """Waits for a variable (digital input) io_var to attain a given value io_value. Optionally, a timeout can be provided."""   371         if type(io_var) != str:  
   372             io_var = 
'IN[%s]' % str(io_var)
   373         if type(io_value) != str: 
   382             self.
addlog(
'Digital IOs not managed by the robot (WAIT FOR %s==%s)' % (io_var, io_value))
   385             self.
addlog(
'Digital IOs not managed by the robot (WAIT FOR %s==%s)' % (io_var, io_value))
   387     def RunCode(self, code, is_function_call = False):
   388         """Adds code or a function call"""   390             code.replace(
' ',
'_')
   391             if not code.endswith(
')'):
   398         """Display a message in the robot controller screen (teach pendant)"""   402             self.
addline(
'print("%s")' % message)
   406         """Add a program line"""   410         """Add a log message"""   411         self.
LOG = self.
LOG + newline + 
'\n'   416     [x,y,z,r,p,w] = xyzrpw
   426     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]])
   429     """Test the post with a basic program"""   433     robot.ProgStart(
"Program")
   434     robot.RunMessage(
"Program generated by RoboDK using a custom post processor", 
True)
   435     robot.setFrame(
Pose([807.766544, -963.699898, 41.478944, 0, 0, 0]))
   436     robot.setTool(
Pose([62.5, -108.253175, 100, -60, 90, 0]))
   437     robot.MoveJ(
Pose([200, 200, 500, 180, 0, 180]), [-46.18419, -6.77518, -20.54925, 71.38674, 49.58727, -302.54752] )
   438     robot.MoveL(
Pose([200, 250, 348.734575, 180, 0, -150]), [-41.62707, -8.89064, -30.01809, 60.62329, 49.66749, -258.98418] )
   439     robot.MoveL(
Pose([200, 200, 262.132034, 180, 0, -150]), [-43.73892, -3.91728, -35.77935, 58.57566, 54.11615, -253.81122] )
   440     robot.RunMessage(
"Setting air valve 1 on")
   441     robot.RunCode(
"TCP_On", 
True)
   443     robot.MoveL(
Pose([200, 250, 348.734575, 180, 0, -150]), [-41.62707, -8.89064, -30.01809, 60.62329, 49.66749, -258.98418] )
   444     robot.MoveL(
Pose([250, 300, 278.023897, 180, 0, -150]), [-37.52588, -6.32628, -34.59693, 53.52525, 49.24426, -251.44677] )
   445     robot.MoveL(
Pose([250, 250, 191.421356, 180, 0, -150]), [-39.75778, -1.04537, -40.37883, 52.09118, 54.15317, -246.94403] )
   446     robot.RunMessage(
"Setting air valve off")
   447     robot.RunCode(
"TCP_Off", 
True)
   449     robot.MoveL(
Pose([250, 300, 278.023897, 180, 0, -150]), [-37.52588, -6.32628, -34.59693, 53.52525, 49.24426, -251.44677] )
   450     robot.MoveL(
Pose([250, 200, 278.023897, 180, 0, -150]), [-41.85389, -1.95619, -34.89154, 57.43912, 52.34162, -253.73403] )
   451     robot.MoveL(
Pose([250, 150, 191.421356, 180, 0, -150]), [-43.82111, 3.29703, -40.29493, 56.02402, 56.61169, -249.23532] )
   452     robot.MoveJ(
None, [-46.18419, -6.77518, -20.54925, 71.38674, 49.58727, -302.54752] )
   453     robot.ProgFinish(
"Program")
   456     if len(robot.LOG) > 0:
   457         mbox(
'Program generation LOG:\n\n' + robot.LOG)
   459     input(
"Press Enter to close...")
   461 if __name__ == 
"__main__":
   462     """Function to call when the module is executed by itself: test""" def setSpeedJoints(self, speed_degs)
def ProgFinish(self, progname)
def ProgStart(self, progname)
def __init__(self, robotpost=None, robotname=None, robot_axes=6, kwargs)
def addline(self, newline)
def addlog(self, newline)
def ProgSendRobot(self, robot_ip, remote_path, ftp_user, ftp_pass)
def MoveC(self, pose1, joints1, pose2, joints2, conf_RLF_1, conf_RLF_2)
def waitDI(self, io_var, io_value, timeout_ms=-1)
def setZoneData(self, zone_mm)
def RunCode(self, code, is_function_call=False)
def ProgSave(self, folder, progname, ask_user=False, show_result=False)
def setSpeed(self, speed_mms)
def setTool(self, pose, tool_id=None, tool_name=None)
def MoveJ(self, pose, joints, conf_RLF=None)
def mbox(msg, b1='OK', b2='Cancel', frame=True, t=False, entry=None)
def setAccelerationJoints(self, accel_degss)
def RunMessage(self, message, iscomment=False)
def setFrame(self, pose, frame_id=None, frame_name=None)
def setAcceleration(self, accel_mmss)
def getSaveFile(strdir='C:\\', strfile='file.txt', strtitle='Save file as ...')
def MoveL(self, pose, joints, conf_RLF=None)
def setDO(self, io_var, io_value)