Mitsubishi_Movemaster_EX.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, ref_frame):
50  """Prints a pose target"""
51  [x,y,z,r,p,w] = pose_2_xyzrpw(ref_frame*pose)
52  return ('%.3f,%.3f,%.3f,%.3f,%.3f' % (x,y,z,p,w))
53 
54 def joints_2_str(angles):
55  """Prints a joint target"""
56  str = ''
57  for i in range(len(angles)):
58  if i == 3:
59  continue
60  str = str + ('%.6f,' % (angles[i]))
61  str = str[:-1]
62  return "%s" % str
63 
64 # ----------------------------------------------------
65 # Object class that handles the robot instructions/syntax
66 class RobotPost(object):
67  """Robot post object"""
68  PROG_EXT = 'prg' # set the program extension
69 
70  # other variables
71  ROBOT_POST = ''
72  ROBOT_NAME = ''
73  PROG_FILES = []
74 
75  PROG = ''
76  LOG = ''
77  nAxes = 6
78  REF_FRAME = eye(4)
79 
80 
81  def __init__(self, robotpost=None, robotname=None, robot_axes = 6, **kwargs):
82  self.ROBOT_POST = robotpost
83  self.ROBOT_NAME = robotname
84  self.PROG = ''
85  self.LOG = ''
86  self.nAxes = robot_axes
87 
88  def ProgStart(self, progname):
89  #self.addline('PROC %s()' % progname)
90  return
91 
92  def ProgFinish(self, progname):
93  self.addline('RT')
94 
95  def ProgSave(self, folder, progname, ask_user=False, show_result=False):
96  progname = progname + '.' + self.PROG_EXT
97  if ask_user or not DirExists(folder):
98  filesave = getSaveFile(folder, progname, 'Save program as...')
99  if filesave is not None:
100  filesave = filesave.name
101  else:
102  return
103  else:
104  filesave = folder + '/' + progname
105  fid = open(filesave, "w")
106  fid.write(self.PROG)
107  fid.close()
108  print('SAVED: %s\n' % filesave)
109  self.PROG_FILES = filesave
110 
111  #---------------------- show result
112  if show_result:
113  if type(show_result) is str:
114  # Open file with provided application
115  import subprocess
116  p = subprocess.Popen([show_result, filesave])
117  elif type(show_result) is list:
118  import subprocess
119  p = subprocess.Popen(show_result + [filesave])
120  else:
121  # open file with default application
122  import os
123  os.startfile(filesave)
124 
125  if len(self.LOG) > 0:
126  mbox('Program generation LOG:\n\n' + self.LOG)
127 
128  def ProgSendRobot(self, robot_ip, remote_path, ftp_user, ftp_pass):
129  """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".
130  The connection parameters must be provided in the robot connection menu of RoboDK"""
131  UploadFTP(self.PROG_FILES, robot_ip, remote_path, ftp_user, ftp_pass)
132 
133  def MoveJ(self, pose, joints, conf_RLF=None):
134  """Add a joint movement"""
135  self.POSE_LAST = pose
136  self.addline('MJ %s' % joints_2_str(joints))
137 
138  def MoveL(self, pose, joints, conf_RLF=None):
139  """Add a linear movement"""
140  self.POSE_LAST = pose
141  self.addline('MP %s' % pose_2_str(pose,self.REF_FRAME))
142 
143  def MoveC(self, pose1, joints1, pose2, joints2, conf_RLF_1=None, conf_RLF_2=None):
144  """Add a circular movement"""
145  self.addlog('Circular move not possible')
146 
147  def setFrame(self, pose, frame_id=None, frame_name=None):
148  """Change the robot reference frame"""
149  self.REF_FRAME = pose
150  #elf.addline('Base %s' % pose_2_str(pose))
151 
152  def setTool(self, pose, tool_id=None, tool_name=None):
153  """Change the robot TCP"""
154  self.addline('TL %.0f' % pose.Pos()[2])
155 
156  def Pause(self, time_ms):
157  """Pause the robot program"""
158  if time_ms < 0:
159  self.addline('Stop')
160  else:
161  self.addline('TI %.0f' % (time_ms*0.01))
162 
163  def setSpeed(self, speed_mms):
164  """Changes the robot speed (in mm/s)"""
165  # Assume 5000 mm/s as 9/9
166  speed_percent = min(speed_mms*9/5000, 9)
167  self.addline('SP %.0f' % speed_percent)
168 
169  def setAcceleration(self, accel_mmss):
170  """Changes the robot acceleration (in mm/s2)"""
171  self.addlog('setAcceleration not defined')
172 
173  def setSpeedJoints(self, speed_degs):
174  """Changes the robot joint speed (in deg/s)"""
175  self.addlog('setSpeedJoints not defined')
176 
177  def setAccelerationJoints(self, accel_degss):
178  """Changes the robot joint acceleration (in deg/s2)"""
179  self.addlog('setAccelerationJoints not defined')
180 
181  def setZoneData(self, zone_mm):
182  """Changes the smoothing radius (aka CNT, APO or zone data) to make the movement smoother"""
183  self.addlog('Rounding not possible')
184 
185  def setDO(self, io_var, io_value):
186  """Sets a variable (digital output) to a given value"""
187  if type(io_var) != str: # set default variable name if io_var is a number
188  io_var = 'OUT(%s)' % str(io_var)
189  if type(io_value) != str: # set default variable value if io_value is a number
190  if io_value > 0:
191  io_value = '1'
192  else:
193  io_value = '0'
194 
195  # at this point, io_var and io_value must be string values
196  self.addline('%s=%s' % (io_var, io_value))
197 
198  def waitDI(self, io_var, io_value, timeout_ms=-1):
199  """Waits for a variable (digital input) io_var to attain a given value io_value. Optionally, a timeout can be provided."""
200  if type(io_var) != str: # set default variable name if io_var is a number
201  io_var = 'IN(%s)' % str(io_var)
202  if type(io_value) != str: # set default variable value if io_value is a number
203  if io_value > 0:
204  io_value = '1'
205  else:
206  io_value = '0'
207 
208  # at this point, io_var and io_value must be string values
209  if timeout_ms < 0:
210  self.addline('Wait %s=%s' % (io_var, io_value))
211  else:
212  self.addline('Wait %s=%s Timeout=%.1f' % (io_var, io_value, timeout_ms))
213 
214  def RunCode(self, code, is_function_call = False):
215  """Adds code or a function call"""
216  if is_function_call:
217  code.replace(' ','_')
218  self.addline('GS %s' % code)
219  else:
220  self.addline(code)
221 
222  def RunMessage(self, message, iscomment = False):
223  """Display a message in the robot controller screen (teach pendant)"""
224  if iscomment:
225  self.addline('\' ' + message)
226  else:
227  self.addlog('Show message on teach pendant not implemented (%s)' % message)
228 
229 # ------------------ private ----------------------
230  def addline(self, newline):
231  """Add a program line"""
232  self.PROG = self.PROG + newline + '\n'
233 
234  def addlog(self, newline):
235  """Add a log message"""
236  self.LOG = self.LOG + newline + '\n'
237 
238 # -------------------------------------------------
239 # ------------ For testing purposes ---------------
240 def Pose(xyzrpw):
241  [x,y,z,r,p,w] = xyzrpw
242  a = r*math.pi/180
243  b = p*math.pi/180
244  c = w*math.pi/180
245  ca = math.cos(a)
246  sa = math.sin(a)
247  cb = math.cos(b)
248  sb = math.sin(b)
249  cc = math.cos(c)
250  sc = math.sin(c)
251  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]])
252 
253 def test_post():
254  """Test the post with a basic program"""
255 
256  robot = RobotPost()
257 
258  robot.ProgStart("Program")
259  robot.RunMessage("Program generated by RoboDK using a custom post processor", True)
260  robot.setFrame(Pose([807.766544, -963.699898, 41.478944, 0, 0, 0]))
261  robot.setTool(Pose([62.5, -108.253175, 100, -60, 90, 0]))
262  robot.MoveJ(Pose([200, 200, 500, 180, 0, 180]), [-46.18419, -6.77518, -20.54925, 71.38674, 49.58727, -302.54752] )
263  robot.MoveL(Pose([200, 250, 348.734575, 180, 0, -150]), [-41.62707, -8.89064, -30.01809, 60.62329, 49.66749, -258.98418] )
264  robot.MoveL(Pose([200, 200, 262.132034, 180, 0, -150]), [-43.73892, -3.91728, -35.77935, 58.57566, 54.11615, -253.81122] )
265  robot.RunMessage("Setting air valve 1 on")
266  robot.RunCode("TCP_On", True)
267  robot.Pause(1000)
268  robot.MoveL(Pose([200, 250, 348.734575, 180, 0, -150]), [-41.62707, -8.89064, -30.01809, 60.62329, 49.66749, -258.98418] )
269  robot.MoveL(Pose([250, 300, 278.023897, 180, 0, -150]), [-37.52588, -6.32628, -34.59693, 53.52525, 49.24426, -251.44677] )
270  robot.MoveL(Pose([250, 250, 191.421356, 180, 0, -150]), [-39.75778, -1.04537, -40.37883, 52.09118, 54.15317, -246.94403] )
271  robot.RunMessage("Setting air valve off")
272  robot.RunCode("TCP_Off", True)
273  robot.Pause(1000)
274  robot.MoveL(Pose([250, 300, 278.023897, 180, 0, -150]), [-37.52588, -6.32628, -34.59693, 53.52525, 49.24426, -251.44677] )
275  robot.MoveL(Pose([250, 200, 278.023897, 180, 0, -150]), [-41.85389, -1.95619, -34.89154, 57.43912, 52.34162, -253.73403] )
276  robot.MoveL(Pose([250, 150, 191.421356, 180, 0, -150]), [-43.82111, 3.29703, -40.29493, 56.02402, 56.61169, -249.23532] )
277  robot.MoveJ(None, [-46.18419, -6.77518, -20.54925, 71.38674, 49.58727, -302.54752] )
278  robot.ProgFinish("Program")
279  # robot.ProgSave(".","Program",True)
280  print(robot.PROG)
281  if len(robot.LOG) > 0:
282  mbox('Program generation LOG:\n\n' + robot.LOG)
283 
284  input("Press Enter to close...")
285 
286 if __name__ == "__main__":
287  """Function to call when the module is executed by itself: test"""
288  test_post()
def __init__(self, robotpost=None, robotname=None, robot_axes=6, kwargs)
def MoveC(self, pose1, joints1, pose2, joints2, conf_RLF_1=None, conf_RLF_2=None)
def ProgSendRobot(self, robot_ip, remote_path, ftp_user, ftp_pass)
def ProgSave(self, folder, progname, ask_user=False, show_result=False)
def UploadFTP(program, robot_ip, remote_path, ftp_user, ftp_pass)
Definition: robodk.py:1312
def mbox(msg, b1='OK', b2='Cancel', frame=True, t=False, entry=None)
Definition: robodk.py:1458
def getSaveFile(strdir='C:\\', strfile='file.txt', strtitle='Save file as ...')
Definition: robodk.py:1354


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