Precise.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 Precise 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 
50 # ----------------------------------------------------
51 def pose_2_str(pose):
52  """Prints a pose target"""
53  [x,y,z,r,p,w] = pose_2_xyzrpw(pose)
54  return ('%.3f, %.3f, %.3f, %.3f, %.3f, %.3f' % (x,y,z,r,p,w))
55 
56 def angles_2_str(angles):
57  """Prints a joint target"""
58  str = ''
59  data = [',',',',',',',',',','']
60  for i in range(len(angles)):
61  #data[3] = data[3] + -0.00
62  str = str + ('%.3f%s ' % (angles[i],data[i]))
63  str = str[:-1]
64  return str
65 
66 # ----------------------------------------------------
67 # Object class that handles the robot instructions/syntax
68 class RobotPost(object):
69  """Robot post object"""
70  PROG_EXT = 'gpl' # set the program extension
71 
72  # other variables
73  # ROBOT_POST = 'unset'
74  ROBOT_NAME = ''
75  PROG_FILES = []
76 
77  nPROGS = 0
78  PROG = ''
79  TAB = ''
80  LOG = ''
81  nAxes = 6
82  FRAME = eye(4)
83  TOOL = eye(4)
84 
85 
86  def __init__(self, robotpost=None, robotname=None, robot_axes = 6, **kwargs):
87  #self.ROBOT_POST = robotpost
88  self.ROBOT_NAME = robotname
89  self.PROG = ''
90  self.LOG = ''
91  self.nAxes = robot_axes
92 
93  def ProgStart(self, progname):
94  self.nPROGS = self.nPROGS + 1
95  if self.nPROGS == 1:
96  self.addline('Module GPL')
97  self.addline(' Public Sub MAIN')
98  self.addline(' Dim prof1 As New Profile')
99  self.addline(' Dim loc1 As New Location')
100  self.addline(' prof1.Speed = 40')
101  self.addline(' prof1.Straight = True')
102  self.addline(' Controller.PowerEnabled = 1')
103  self.addline(' Robot.Attached = 1')
104  self.addline(' Robot.Home')
105 
106 
107  def ProgFinish(self, progname):
108  self.addline(' End Sub')
109  self.addline('End Module')
110 
111  def ProgSave(self, folder, progname, ask_user=True, show_result=True):
112  progname = 'Main' + '.' + self.PROG_EXT
113  if ask_user or not DirExists(folder):
114  filesave = getSaveFile(folder, progname, 'Save program as...')
115  if filesave is not None:
116  filesave = filesave.name
117  else:
118  return
119  else:
120  filesave = folder + '/' + progname
121  fid = open(filesave, "w")
122  fid.write(self.PROG)
123  fid.close()
124  print('SAVED: %s\n' % filesave)
125  self.PROG_FILES = filesave
126 
127  #---------------------- show result
128  if show_result:
129  if type(show_result) is str:
130  # Open file with provided application
131  import subprocess
132  p = subprocess.Popen([show_result, filesave])
133  elif type(show_result) is list:
134  import subprocess
135  p = subprocess.Popen(show_result + [filesave])
136  else:
137  # open file with default application
138  import os
139  os.startfile(filesave)
140 
141  if len(self.LOG) > 0:
142  mbox('Program generation LOG:\n\n' + self.LOG)
143 
144  def ProgSendRobot(self, robot_ip, remote_path, ftp_user, ftp_pass):
145  """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".
146  The connection parameters must be provided in the robot connection menu of RoboDK"""
147  UploadFTP(self.PROG_FILES, robot_ip, remote_path, ftp_user, ftp_pass)
148 
149  def MoveJ(self, pose, joints, conf_RLF=None):
150  """Add a joint movement"""
151  #poseabs = self.FRAME * pose * invH(self.TOOL)
152  self.addline(' Move.Loc(loc1.XYZValue(%s),prof1)' % angles_2_str(joints))
153 
154  def MoveL(self, pose, joints, conf_RLF=None):
155  """Add a linear movement"""
156  #poseabs = self.FRAME * pose * invH(self.TOOL)
157  self.addline(' Move.Loc(loc1.XYZValue(%s),prof1)' % angles_2_str(joints))
158 
159  def MoveC(self, pose1, joints1, pose2, joints2, conf_RLF_1=None, conf_RLF_2=None):
160  """Add a circular movement"""
161  #poseabs1 = self.FRAME * pose1 * invH(self.TOOL)
162  #poseabs2 = self.FRAME * pose2 * invH(self.TOOL)
163  #self.addline(' Move.Circle(loc1.XYZValue(%s),loc1.XYZValue(%s),prof1) ' % (pose_2_str(poseabs1),pose_2_str(poseabs2)))
164  self.addline(' Move.Circle(loc1.XYZValue(%s),loc1.XYZValue(%s),prof1) ' % (angles_2_str(joints1),angles_2_str(joints2)))
165  def setFrame(self, pose, frame_id = None, frame_name = None):
166  """Change the robot reference frame"""
167  self.FRAME = pose
168  #self.addline('BASE_FRAME ' + pose_2_str(pose))
169 
170  def setTool(self, pose, tool_id = None, tool_name = None):
171  """Change the robot TCP"""
172  self.TOOL = pose
173  #self.addline('TOOL_FRAME ' + pose_2_str(pose))
174 
175  def Pause(self, time_ms):
176  """Pause the robot program"""
177  self.addline(' Move.Delay(%s)' % (time_ms*0.001))
178 
179  def setSpeed(self, speed_mms):
180  """Changes the robot speed (in mm/s)"""
181  self.addline(' prof1.Speed = %0.2f' % speed_mms)
182 
183  def Acceleration(self, accel_ptg):
184  """Changes the robot acceleration (in %)"""
185  self.addline(' prof1.Accel = %0.2f' % accel_ptg)
186  self.addline(' prof1.Decel = %0.2f' % accel_ptg)
187 
188  def setSpeedJoints(self, speed_degs):
189  """Changes the robot joint speed (in deg/s)"""
190  self.addlog('setSpeedJoints not defined')
191 
192  def setAccelerationJoints(self, accel_degss):
193  """Changes the robot joint acceleration (in deg/s2)"""
194  self.addlog('setAccelerationJoints not defined')
195 
196  def setZoneData(self, zone_mm):
197  """Changes the zone data approach (makes the movement more smooth)"""
198  self.addlog('setZoneData not defined (%.1f mm)' % zone_mm)
199 
200  def setDO(self, io_var, io_value):
201  """Sets a variable (output) to a given value"""
202  if type(io_var) != str: # set default variable name if io_var is a number
203  io_var = 'OUT[%s]' % str(io_var)
204  if type(io_value) != str: # set default variable value if io_value is a number
205  if io_value > 0:
206  io_value = 'TRUE'
207  else:
208  io_value = 'FALSE'
209 
210  # at this point, io_var and io_value must be string values
211  self.addline('%s=%s' % (io_var, io_value))
212 
213  def waitDI(self, io_var, io_value, timeout_ms=-1):
214  """Waits for an input io_var to attain a given value io_value. Optionally, a timeout can be provided."""
215  if type(io_var) != str: # set default variable name if io_var is a number
216  io_var = 'IN[%s]' % str(io_var)
217  if type(io_value) != str: # set default variable value if io_value is a number
218  if io_value > 0:
219  io_value = 'TRUE'
220  else:
221  io_value = 'FALSE'
222 
223  # at this point, io_var and io_value must be string values
224  if timeout_ms < 0:
225  self.addline('WAIT FOR %s==%s' % (io_var, io_value))
226  else:
227  self.addline('WAIT FOR %s==%s TIMEOUT=%.1f' % (io_var, io_value, timeout_ms))
228 
229  def RunCode(self, code, is_function_call = False):
230  """Adds code or a function call"""
231  if is_function_call:
232  code.replace(' ','_')
233  if not code.endswith(')'):
234  code = code + '()'
235  self.addline(code)
236  else:
237  self.addline(code)
238 
239  def RunMessage(self, message, iscomment = False):
240  """Display a message in the robot controller screen (teach pendant)"""
241  if iscomment:
242  self.addline("' " + message)
243  else:
244  self.addline('Print "%s"' % message)
245 
246 # ------------------ private ----------------------
247  def addline(self, newline):
248  """Add a program line"""
249  self.PROG = self.PROG + newline + '\n'
250 
251  def addlog(self, newline):
252  """Add a log message"""
253  self.LOG = self.LOG + newline + '\n'
254 
255 # -------------------------------------------------
256 # ------------ For testing purposes ---------------
257 def Pose(xyzrpw):
258  [x,y,z,r,p,w] = xyzrpw
259  a = r*math.pi/180
260  b = p*math.pi/180
261  c = w*math.pi/180
262  ca = math.cos(a)
263  sa = math.sin(a)
264  cb = math.cos(b)
265  sb = math.sin(b)
266  cc = math.cos(c)
267  sc = math.sin(c)
268  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]])
269 
270 def test_post():
271  """Test the post with a basic program"""
272 
273  robot = RobotPost()
274 
275  robot.ProgStart("Program")
276 
277  robot.MoveJ(Pose([200, 200, 500, 180, 0, 180]), [-46.18419, -6.77518, -20.54925, 71.38674, 49.58727, -302.54752] )
278  robot.MoveL(Pose([200, 250, 15, 180, 0, -150]), [-41.62707, -8.89064, -30.01809, 60.62329, 49.66749, -258.98418] )
279  robot.MoveC(Pose([200, 200, 34, 180, 0, -150]), [-43.73892, -3.91728, -35.77935, 58.57566, 54.11615, -253.81122],Pose([200, 200, 15, 180, 0, -150]), [-43.73892, -3.91728, -35.77935, 58.57566, 54.11615, -253.81122])
280  #robot.setFrame(Pose([192.305408, 222.255946, 0.000000, 0.000000, 0.000000, 0.000000]),5,'Frame 5')
281  robot.setSpeed(30)
282  #robot.Delay(1000)
283 
284  #robot.ProgFinish("Program")
285  #robot.ProgSave(".","Program",True)
286  print(robot.PROG)
287  if len(robot.LOG) > 0:
288  mbox('Program generation LOG:\n\n' + robot.LOG)
289 
290  input("Press Enter to close...")
291 
292 if __name__ == "__main__":
293  """Function to call when the module is executed by itself: test"""
294  test_post()
def MoveJ(self, pose, joints, conf_RLF=None)
Definition: Precise.py:149
def setTool(self, pose, tool_id=None, tool_name=None)
Definition: Precise.py:170
def __init__(self, robotpost=None, robotname=None, robot_axes=6, kwargs)
Definition: Precise.py:86
def setDO(self, io_var, io_value)
Definition: Precise.py:200
def UploadFTP(program, robot_ip, remote_path, ftp_user, ftp_pass)
Definition: robodk.py:1312
def ProgSave(self, folder, progname, ask_user=True, show_result=True)
Definition: Precise.py:111
def ProgSendRobot(self, robot_ip, remote_path, ftp_user, ftp_pass)
Definition: Precise.py:144
def mbox(msg, b1='OK', b2='Cancel', frame=True, t=False, entry=None)
Definition: robodk.py:1458
def waitDI(self, io_var, io_value, timeout_ms=-1)
Definition: Precise.py:213
def RunCode(self, code, is_function_call=False)
Definition: Precise.py:229
def RunMessage(self, message, iscomment=False)
Definition: Precise.py:239
def setAccelerationJoints(self, accel_degss)
Definition: Precise.py:192
def getSaveFile(strdir='C:\\', strfile='file.txt', strtitle='Save file as ...')
Definition: robodk.py:1354
def MoveC(self, pose1, joints1, pose2, joints2, conf_RLF_1=None, conf_RLF_2=None)
Definition: Precise.py:159
def setFrame(self, pose, frame_id=None, frame_name=None)
Definition: Precise.py:165
def MoveL(self, pose, joints, conf_RLF=None)
Definition: Precise.py:154


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