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


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