Mitsubishi.py
Go to the documentation of this file.
1 # Copyright 2017 - RoboDK Software S.L. - http://www.robodk.com/
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
6 # Unless required by applicable law or agreed to in writing, software
7 # distributed under the License is distributed on an "AS IS" BASIS,
8 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 # See the License for the specific language governing permissions and
10 # limitations under the License.
11 
12 # ----------------------------------------------------
13 # This file is a POST PROCESSOR for Robot Offline Programming to generate programs
14 # for a generic robot with RoboDK
15 #
16 # To edit/test this POST PROCESSOR script file:
17 # Select "Program"->"Add/Edit Post Processor", then select your post or create a new one.
18 # You can edit this file using any text editor or Python editor. Using a Python editor allows to quickly evaluate a sample program at the end of this file.
19 # Python should be automatically installed with RoboDK
20 #
21 # You can also edit the POST PROCESSOR manually:
22 # 1- Open the *.py file with Python IDLE (right click -> Edit with IDLE)
23 # 2- Make the necessary changes
24 # 3- Run the file to open Python Shell: Run -> Run module (F5 by default)
25 # 4- The "test_post()" function is called automatically
26 # Alternatively, you can edit this file using a text editor and run it with Python
27 #
28 # To use a POST PROCESSOR file you must place the *.py file in "C:/RoboDK/Posts/"
29 # To select one POST PROCESSOR for your robot in RoboDK you must follow these steps:
30 # 1- Open the robot panel (double click a robot)
31 # 2- Select "Parameters"
32 # 3- Select "Unlock advanced options"
33 # 4- Select your post as the file name in the "Robot brand" box
34 #
35 # To delete an existing POST PROCESSOR script, simply delete this file (.py file)
36 #
37 # ----------------------------------------------------
38 # More information about RoboDK Post Processors and Offline Programming here:
39 # http://www.robodk.com/help#PostProcessor
40 # http://www.robodk.com/doc/en/PythonAPI/postprocessor.html
41 # ----------------------------------------------------
42 
43 
44 # ----------------------------------------------------
45 # Import RoboDK tools
46 from robodk import *
47 
48 # ----------------------------------------------------
49 def pose_2_str(pose):
50  """Prints a pose target"""
51  [x,y,z,r,p,w] = pose_2_xyzrpw(pose)
52  return ('(%.3f,%.3f,%.3f,%.3f,%.3f,%.3f)' % (x,y,z,r,p,w))
53 
54 def angles_2_str(angles):
55  """Prints a joint target"""
56  str = ''
57  for i in range(len(angles)):
58  str = str + ('%.6f,' % (angles[i]))
59  str = str[:-1]
60  return "(%s)" % str
61 
62 # ----------------------------------------------------
63 # Object class that handles the robot instructions/syntax
64 class RobotPost(object):
65  """Robot post object"""
66  PROG_EXT = 'prg' # set the program extension
67 
68  # other variables
69  ROBOT_POST = ''
70  ROBOT_NAME = ''
71  PROG_FILES = []
72 
73  PROG = ''
74  LOG = ''
75  FOOTER = ''
76  STRUCT_FLAG = '(7,0)'
77  lineno = 0
78  nAxes = 6
79  nTargets = 0
80  POSE_LAST = None
81  MSG_VAR = None
82 
83 
84  def __init__(self, robotpost=None, robotname=None, robot_axes = 6, **kwargs):
85  self.ROBOT_POST = robotpost
86  self.ROBOT_NAME = robotname
87  self.PROG = ''
88  self.LOG = ''
89  self.nAxes = robot_axes
90 
91  def ProgStart(self, progname):
92  #self.addline('PROC %s()' % progname)
93  return
94 
95  def ProgFinish(self, progname):
96  self.addline('End')
97 
98  def ProgSave(self, folder, progname, ask_user=False, show_result=False):
99  progname = progname + '.' + self.PROG_EXT
100  if ask_user or not DirExists(folder):
101  filesave = getSaveFile(folder, progname, 'Save program as...')
102  if filesave is not None:
103  filesave = filesave.name
104  else:
105  return
106  else:
107  filesave = folder + '/' + progname
108  fid = open(filesave, "w")
109  fid.write(self.PROG)
110  fid.write(self.FOOTER)
111  fid.close()
112  print('SAVED: %s\n' % filesave)
113  self.PROG_FILES = filesave
114 
115  #---------------------- show result
116  if show_result:
117  if type(show_result) is str:
118  # Open file with provided application
119  import subprocess
120  p = subprocess.Popen([show_result, filesave])
121  elif type(show_result) is list:
122  import subprocess
123  p = subprocess.Popen(show_result + [filesave])
124  else:
125  # open file with default application
126  import os
127  os.startfile(filesave)
128 
129  if len(self.LOG) > 0:
130  mbox('Program generation LOG:\n\n' + self.LOG)
131 
132  def ProgSendRobot(self, robot_ip, remote_path, ftp_user, ftp_pass):
133  """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".
134  The connection parameters must be provided in the robot connection menu of RoboDK"""
135  UploadFTP(self.PROG_FILES, robot_ip, remote_path, ftp_user, ftp_pass)
136 
137  def MoveJ(self, pose, joints, conf_RLF=None):
138  """Add a joint movement"""
139  self.POSE_LAST = pose
140  self.nTargets = self.nTargets + 1
141  self.addline('J%i = %s' % (self.nTargets, angles_2_str(joints)))
142  self.addline('Mov J%i' % self.nTargets)
143  # J1=(0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00)
144  self.addfooter('J%i=%s' % (self.nTargets, angles_2_str([0]*6)))
145 
146  def MoveL(self, pose, joints, conf_RLF=None):
147  """Add a linear movement"""
148  self.POSE_LAST = pose
149  self.nTargets = self.nTargets + 1
150  self.addline('P%i = %s' % (self.nTargets, pose_2_str(pose)))
151  self.addline('Mvs P%i' % self.nTargets)
152 
153  # P1=(+0.00,+0.00,+0.00,+0.00,+0.00,+0.00,+0.00,+0.00)(,)
154  self.addfooter('P%i=%s%s' % (self.nTargets, pose_2_str(Pose([0]*6)), self.STRUCT_FLAG))
155 
156  def MoveC(self, pose1, joints1, pose2, joints2, conf_RLF_1=None, conf_RLF_2=None):
157  """Add a circular movement"""
158  self.nTargets = self.nTargets + 1
159  pi = self.nTargets
160  self.addline('P%i = %s' % (self.nTargets, pose_2_str(POSE_LAST)))
161  self.addfooter('P%i=%s%s' % (self.nTargets, pose_2_str(Pose([0]*6)), self.STRUCT_FLAG))
162  self.nTargets = self.nTargets + 1
163  self.addline('P%i = %s' % (self.nTargets, pose_2_str(pose1)))
164  self.addfooter('P%i=%s%s' % (self.nTargets, pose_2_str(Pose([0]*6)), self.STRUCT_FLAG))
165  self.nTargets = self.nTargets + 1
166  self.addline('P%i = %s' % (self.nTargets, pose_2_str(pose2)))
167  self.addline('Mvc P%i, P%i, P%i' % (pi, pi+1, pi+2))
168  self.addfooter('P%i=%s%s' % (self.nTargets, pose_2_str(Pose([0]*6)), self.STRUCT_FLAG))
169 
170  def setFrame(self, pose, frame_id=None, frame_name=None):
171  """Change the robot reference frame"""
172  self.addline('Base %s' % pose_2_str(pose))
173 
174  def setTool(self, pose, tool_id=None, tool_name=None):
175  """Change the robot TCP"""
176  self.addline('Tool %s' % pose_2_str(pose))
177 
178  def Pause(self, time_ms):
179  """Pause the robot program"""
180  if time_ms < 0:
181  self.addline('Hlt')
182  else:
183  self.addline('Dly %.3f' % (time_ms*0.001))
184 
185  def setSpeed(self, speed_mms):
186  """Changes the robot speed (in mm/s)"""
187  # Assume 5000 mm/s as 100%
188  speed_percent = min(speed_mms*100/5000, 100)
189  self.addline('Ovrd %.1f' % speed_percent)
190 
191  def setAcceleration(self, accel_mmss):
192  """Changes the robot acceleration (in mm/s2)"""
193  self.addlog('setAcceleration not defined')
194 
195  def setSpeedJoints(self, speed_degs):
196  """Changes the robot joint speed (in deg/s)"""
197  self.addlog('setSpeedJoints not defined')
198 
199  def setAccelerationJoints(self, accel_degss):
200  """Changes the robot joint acceleration (in deg/s2)"""
201  self.addlog('setAccelerationJoints not defined')
202 
203  def setZoneData(self, zone_mm):
204  """Changes the smoothing radius (aka CNT, APO or zone data) to make the movement smoother"""
205  if zone_mm > 0:
206  self.addline('Cnt 1, %.1f' % zone_mm)
207  else:
208  self.addline('Cnt 0')
209 
210  def setDO(self, io_var, io_value):
211  """Sets a variable (digital output) to a given value"""
212  if type(io_var) != str: # set default variable name if io_var is a number
213  io_var = 'OUT(%s)' % str(io_var)
214  if type(io_value) != str: # set default variable value if io_value is a number
215  if io_value > 0:
216  io_value = '1'
217  else:
218  io_value = '0'
219 
220  # at this point, io_var and io_value must be string values
221  self.addline('%s=%s' % (io_var, io_value))
222 
223  def waitDI(self, io_var, io_value, timeout_ms=-1):
224  """Waits for a variable (digital input) io_var to attain a given value io_value. Optionally, a timeout can be provided."""
225  if type(io_var) != str: # set default variable name if io_var is a number
226  io_var = 'IN(%s)' % str(io_var)
227  if type(io_value) != str: # set default variable value if io_value is a number
228  if io_value > 0:
229  io_value = '1'
230  else:
231  io_value = '0'
232 
233  # at this point, io_var and io_value must be string values
234  if timeout_ms < 0:
235  self.addline('Wait %s=%s' % (io_var, io_value))
236  else:
237  self.addline('Wait %s=%s Timeout=%.1f' % (io_var, io_value, timeout_ms))
238 
239  def RunCode(self, code, is_function_call = False):
240  """Adds code or a function call"""
241  if is_function_call:
242  code.replace(' ','_')
243  self.addline('CallP "%s"' % code)
244  else:
245  self.addline(code)
246 
247  def RunMessage(self, message, iscomment = False):
248  """Display a message in the robot controller screen (teach pendant)"""
249  if iscomment:
250  self.addline('\'' + message)
251  else:
252  if self.MSG_VAR is None:
253  self.MSG_VAR = 'CMESSAGE'
254  self.addline('%s$ = "%s"' % (self.MSG_VAR, message))
255 
256 # ------------------ private ----------------------
257  def addline(self, newline):
258  """Add a program line"""
259  self.lineno += 1
260  self.PROG += '%d %s' % (self.lineno, newline) + '\n'
261 
262  def addlog(self, newline):
263  """Add a log message"""
264  self.LOG = self.LOG + newline + '\n'
265 
266  def addfooter(self, newline):
267  """Add a program line"""
268  self.FOOTER = self.FOOTER + newline + '\n'
269 # -------------------------------------------------
270 # ------------ For testing purposes ---------------
271 def Pose(xyzrpw):
272  [x,y,z,r,p,w] = xyzrpw
273  a = r*math.pi/180
274  b = p*math.pi/180
275  c = w*math.pi/180
276  ca = math.cos(a)
277  sa = math.sin(a)
278  cb = math.cos(b)
279  sb = math.sin(b)
280  cc = math.cos(c)
281  sc = math.sin(c)
282  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]])
283 
284 def test_post():
285  """Test the post with a basic program"""
286 
287  robot = RobotPost()
288 
289  robot.ProgStart("Program")
290  robot.RunMessage("Program generated by RoboDK using a custom post processor", True)
291  robot.setFrame(Pose([807.766544, -963.699898, 41.478944, 0, 0, 0]))
292  robot.setTool(Pose([62.5, -108.253175, 100, -60, 90, 0]))
293  robot.MoveJ(Pose([200, 200, 500, 180, 0, 180]), [-46.18419, -6.77518, -20.54925, 71.38674, 49.58727, -302.54752] )
294  robot.MoveL(Pose([200, 250, 348.734575, 180, 0, -150]), [-41.62707, -8.89064, -30.01809, 60.62329, 49.66749, -258.98418] )
295  robot.MoveL(Pose([200, 200, 262.132034, 180, 0, -150]), [-43.73892, -3.91728, -35.77935, 58.57566, 54.11615, -253.81122] )
296  robot.RunMessage("Setting air valve 1 on")
297  robot.RunCode("TCP_On", True)
298  robot.Pause(1000)
299  robot.MoveL(Pose([200, 250, 348.734575, 180, 0, -150]), [-41.62707, -8.89064, -30.01809, 60.62329, 49.66749, -258.98418] )
300  robot.MoveL(Pose([250, 300, 278.023897, 180, 0, -150]), [-37.52588, -6.32628, -34.59693, 53.52525, 49.24426, -251.44677] )
301  robot.MoveL(Pose([250, 250, 191.421356, 180, 0, -150]), [-39.75778, -1.04537, -40.37883, 52.09118, 54.15317, -246.94403] )
302  robot.RunMessage("Setting air valve off")
303  robot.RunCode("TCP_Off", True)
304  robot.Pause(1000)
305  robot.MoveL(Pose([250, 300, 278.023897, 180, 0, -150]), [-37.52588, -6.32628, -34.59693, 53.52525, 49.24426, -251.44677] )
306  robot.MoveL(Pose([250, 200, 278.023897, 180, 0, -150]), [-41.85389, -1.95619, -34.89154, 57.43912, 52.34162, -253.73403] )
307  robot.MoveL(Pose([250, 150, 191.421356, 180, 0, -150]), [-43.82111, 3.29703, -40.29493, 56.02402, 56.61169, -249.23532] )
308  robot.MoveJ(None, [-46.18419, -6.77518, -20.54925, 71.38674, 49.58727, -302.54752] )
309  robot.ProgFinish("Program")
310  # robot.ProgSave(".","Program",True)
311  print(robot.PROG)
312  print(robot.FOOTER)
313  if len(robot.LOG) > 0:
314  mbox('Program generation LOG:\n\n' + robot.LOG)
315 
316  input("Press Enter to close...")
317 
318 if __name__ == "__main__":
319  """Function to call when the module is executed by itself: test"""
320  test_post()
def MoveJ(self, pose, joints, conf_RLF=None)
Definition: Mitsubishi.py:137
def ProgSendRobot(self, robot_ip, remote_path, ftp_user, ftp_pass)
Definition: Mitsubishi.py:132
def RunCode(self, code, is_function_call=False)
Definition: Mitsubishi.py:239
def MoveC(self, pose1, joints1, pose2, joints2, conf_RLF_1=None, conf_RLF_2=None)
Definition: Mitsubishi.py:156
def __init__(self, robotpost=None, robotname=None, robot_axes=6, kwargs)
Definition: Mitsubishi.py:84
def UploadFTP(program, robot_ip, remote_path, ftp_user, ftp_pass)
Definition: robodk.py:1312
def MoveL(self, pose, joints, conf_RLF=None)
Definition: Mitsubishi.py:146
def RunMessage(self, message, iscomment=False)
Definition: Mitsubishi.py:247
def mbox(msg, b1='OK', b2='Cancel', frame=True, t=False, entry=None)
Definition: robodk.py:1458
def setTool(self, pose, tool_id=None, tool_name=None)
Definition: Mitsubishi.py:174
def waitDI(self, io_var, io_value, timeout_ms=-1)
Definition: Mitsubishi.py:223
def ProgSave(self, folder, progname, ask_user=False, show_result=False)
Definition: Mitsubishi.py:98
def getSaveFile(strdir='C:\\', strfile='file.txt', strtitle='Save file as ...')
Definition: robodk.py:1354
def setFrame(self, pose, frame_id=None, frame_name=None)
Definition: Mitsubishi.py:170


ros_robodk_post_processors
Author(s): Victor Lamoine - Institut Maupertuis
autogenerated on Sun Jun 7 2020 03:50:22