-
Notifications
You must be signed in to change notification settings - Fork 36
/
Universal_Robots_Sync.py
799 lines (689 loc) · 31.1 KB
/
Universal_Robots_Sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# Copyright 2017 - RoboDK Software S.L. - http://www.robodk.com/
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------
# This file is a POST PROCESSOR for Robot Offline Programming to generate programs
# for a Universal Robot with RoboDK
#
# To edit/test this POST PROCESSOR script file:
# Select "Program"->"Add/Edit Post Processor", then select your post or create a new one.
# 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.
# Python should be automatically installed with RoboDK
#
# You can also edit the POST PROCESSOR manually:
# 1- Open the *.py file with Python IDLE (right click -> Edit with IDLE)
# 2- Make the necessary changes
# 3- Run the file to open Python Shell: Run -> Run module (F5 by default)
# 4- The "test_post()" function is called automatically
# Alternatively, you can edit this file using a text editor and run it with Python
#
# To use a POST PROCESSOR file you must place the *.py file in "C:/RoboDK/Posts/"
# To select one POST PROCESSOR for your robot in RoboDK you must follow these steps:
# 1- Open the robot panel (double click a robot)
# 2- Select "Parameters"
# 3- Select "Unlock advanced options"
# 4- Select your post as the file name in the "Robot brand" box
#
# To delete an existing POST PROCESSOR script, simply delete this file (.py file)
#
# ----------------------------------------------------
# More information about RoboDK Post Processors and Offline Programming here:
# http://www.robodk.com/help#PostProcessor
# http://www.robodk.com/doc/en/PythonAPI/postprocessor.html
# ----------------------------------------------------
DEFAULT_HEADER_SCRIPT = """
#--------------------------
# Add any default subprograms here
# For example, to drive a gripper as a program call:
# def Gripper_Open():
# ...
# end
#
# Example to synchronize motion with another robot:
global sync_count = 0
def Synchronize():
# Use the following digital output to signal the state of the robot:
DO_SYNC = 1
# Use the following digital input to get the state of another robot:
DI_SYNC = 1
if (get_standard_digital_out(DO_SYNC) == get_standard_digital_in(DI_SYNC)):
set_standard_digital_out(DO_SYNC, not (get_standard_digital_out(DI_SYNC)))
sleep(0.1)
thread Thread_wait_1():
while (True):
sleep(0.01)
end
end
if (get_standard_digital_out(DO_SYNC) != get_standard_digital_in(DI_SYNC)):
global thread_handler_1=run Thread_wait_1()
while (get_standard_digital_out(DO_SYNC) != get_standard_digital_in(DI_SYNC)):
sync()
end
kill thread_handler_1
end
else:
if (get_standard_digital_out(DO_SYNC) != get_standard_digital_in(DI_SYNC)):
set_standard_digital_out(DO_SYNC, not (get_standard_digital_out(DO_SYNC)))
end
end
end
# Example to drive a spray gun:
def SprayOn(value):
# use the value as an output:
DO_SPRAY = 5
if value == 0:
set_standard_digital_out(DO_SPRAY, False)
else:
set_standard_digital_out(DO_SPRAY, True)
end
end
#
# Example to move an external axis
def MoveAxis(value):
# use the value as an output:
DO_AXIS_1 = 1
DI_AXIS_1 = 1
if value <= 0:
set_standard_digital_out(DO_AXIS_1, False)
# Wait for digital input to change state
#while (get_standard_digital_in(DI_AXIS_1) != False):
# sync()
#end
else:
set_standard_digital_out(DO_AXIS_1, True)
# Wait for digital input to change state
#while (get_standard_digital_in(DI_AXIS_1) != True):
# sync()
#end
end
end
#--------------------------
"""
#SCRIPT_URP = '''<URProgram name="%s">
# <children>
# <MainProgram runOnlyOnce="false" motionType="MoveJ" speed="1.0471975511965976" acceleration="1.3962634015954636" useActiveTCP="false">
# <children>
# <Script type="File">
# <cachedContents>%s
#</cachedContents>
# <file resolves-to="file">%s</file>
# </Script>
# </children>
# </MainProgram>
# </children>
#</URProgram>'''
SCRIPT_URP = '''<URProgram createdIn="3.0.0" lastSavedIn="3.0.0" name="%s" directory="/" installation="default">
<children>
<MainProgram runOnlyOnce="true" motionType="MoveJ" speed="1.0471975511965976" acceleration="1.3962634015954636" useActiveTCP="false">
<children>
<Script type="File">
<cachedContents>%s
</cachedContents>
<file resolves-to="file">%s</file>
</Script>
</children>
</MainProgram>
</children>
</URProgram>'''
def get_safe_name(progname):
"""Get a safe program name"""
for c in r'-[]/\;,><&*:%=+@!#^()|?^':
progname = progname.replace(c,'')
if len(progname) <= 0:
progname = 'Program'
if progname[0].isdigit():
progname = 'P' + progname
return progname
# ----------------------------------------------------
# Import RoboDK tools
from robodk import *
# ----------------------------------------------------
import socket
import struct
# UR information for real time control and monitoring
# Byte shifts for the real time packet:
UR_GET_RUNTIME_MODE = 132*8-4
RUNTIME_CANCELLED = 0
RUNTIME_READY = 1
RUNTIME_BUSY = 2
RUNTIME_MODE_MSG = []
RUNTIME_MODE_MSG.append("Operation cancelled") #0
RUNTIME_MODE_MSG.append("Ready") #1
RUNTIME_MODE_MSG.append("Running") #2 # Running or Jogging
# Get packet size according to the byte array
def UR_packet_size(buf):
if len(buf) < 4:
return 0
return struct.unpack_from("!i", buf, 0)[0]
# Check if a packet is complete
def UR_packet_check(buf):
msg_sz = UR_packet_size(buf)
if len(buf) < msg_sz:
print("Incorrect packet size %i vs %i" % (msg_sz, len(buf)))
return False
return True
# Get specific information from a packet
def UR_packet_value(buf, offset, nval=6):
if len(buf) < offset+nval:
print("Not available offset (maybe older Polyscope version?): %i - %i" % (len(buf), offset))
return None
format = '!'
for i in range(nval):
format+='d'
return list(struct.unpack_from(format, buf, offset)) #return list(struct.unpack_from("!dddddd", buf, offset))
ROBOT_PROGRAM_ERROR = -1
ROBOT_NOT_CONNECTED = 0
ROBOT_OK = 1
def UR_SendProgramRobot(robot_ip, data):
print("POPUP: Connecting to robot...")
sys.stdout.flush()
robot_socket = socket.create_connection((robot_ip, 30002))
print("POPUP: Sending program..")
sys.stdout.flush()
robot_socket.send(data)
print("POPUP: Sending program...")
sys.stdout.flush()
pause(1)
received = robot_socket.recv(4096)
robot_socket.close()
if received:
#print("POPUP: Program running")
#print(str(received))
sys.stdout.flush()
idx_error = -1
try:
idx_error = received.index(b'error')
except:
pass
if idx_error >= 0:
idx_error_end = min(idx_error + 20, len(received))
try:
idx_error_end = received.index(b'\0',idx_error)
except:
pass
print("POPUP: Robot response: <strong>" + received[idx_error:idx_error_end].decode("utf-8") + "</strong>")
sys.stdout.flush()
pause(5)
return ROBOT_PROGRAM_ERROR
else:
print("POPUP: Program sent. The program should be running on the robot.")
sys.stdout.flush()
return ROBOT_OK
else:
print("POPUP: Robot connection problems...")
sys.stdout.flush()
pause(2)
return ROBOT_NOT_CONNECTED
# Monitor thread to retrieve information from the robot
def UR_Wait_Ready(robot_ip, percent_cmpl):
RUNTIME_MODE_LAST = -1
while True:
print("Connecting to robot %s:%i" % (robot_ip, 30003))
rt_socket = socket.create_connection((robot_ip, 30003))
print("Connected")
buf = b''
while True:
more = rt_socket.recv(4096)
if more:
buf = buf + more
if UR_packet_check(buf):
packet_len = UR_packet_size(buf)
packet, buf = buf[:packet_len], buf[packet_len:]
RUNTIME_MODE = round(UR_packet_value(packet, UR_GET_RUNTIME_MODE, 1)[0])
if RUNTIME_MODE_LAST != RUNTIME_MODE:
RUNTIME_MODE_LAST = RUNTIME_MODE
if RUNTIME_MODE < len(RUNTIME_MODE_MSG):
print("POPUP: Robot " + RUNTIME_MODE_MSG[RUNTIME_MODE] + " (transfer %.1f%% completed)" % percent_cmpl)
sys.stdout.flush()
else:
print("POPUP: Robot Status Unknown (%.i)" % RUNTIME_MODE + " (transfer %.1f%% completed)" % percent_cmpl)
sys.stdout.flush()
if RUNTIME_MODE == RUNTIME_READY:
rt_socket.close()
return True
rt_socket.close()
return False
def pose_2_ur(pose):
"""Calculate the p[x,y,z,rx,ry,rz] position for a pose target"""
def saturate_1(value):
return min(max(value,-1.0),1.0)
angle = acos( saturate_1((pose[0,0]+pose[1,1]+pose[2,2]-1)/2) )
rxyz = [pose[2,1]-pose[1,2], pose[0,2]-pose[2,0], pose[1,0]-pose[0,1]]
if angle == 0:
rxyz = [0,0,0]
else:
rxyz = normalize3(rxyz)
rxyz = mult3(rxyz, angle)
return [pose[0,3], pose[1,3], pose[2,3], rxyz[0], rxyz[1], rxyz[2]]
def pose_2_str(pose):
"""Prints a pose target"""
[x,y,z,w,p,r] = pose_2_ur(pose)
MM_2_M = 0.001
return ('p[%.6f, %.6f, %.6f, %.6f, %.6f, %.6f]' % (x*MM_2_M,y*MM_2_M,z*MM_2_M,w,p,r))
def angles_2_str(angles):
"""Prints a joint target"""
njoints = len(angles)
d2r = pi/180.0
if njoints == 6:
return ('[%.6f, %.6f, %.6f, %.6f, %.6f, %.6f]' % (angles[0]*d2r, angles[1]*d2r, angles[2]*d2r, angles[3]*d2r, angles[4]*d2r, angles[5]*d2r))
else:
return 'this post only supports 6 joints'
def circle_radius(p0,p1,p2):
a = norm(subs3(p0,p1))
b = norm(subs3(p1,p2))
c = norm(subs3(p2,p0))
radius = a*b*c/sqrt(pow(a*a+b*b+c*c,2)-2*(pow(a,4)+pow(b,4)+pow(c,4)))
return radius
#def distance_p1_p02(p0,p1,p2):
# v01 = subs3(p1, p0)
# v02 = subs3(p2, p0)
# return dot(v02,v01)/dot(v02,v02)
# ----------------------------------------------------
# Object class that handles the robot instructions/syntax
class RobotPost(object):
"""Robot post object"""
MAX_LINES_X_PROG = 1000 # Maximum number of lines per program. If the number of lines is exceeded, the program will be executed step by step by RoboDK
PROG_EXT = 'script' # set the program extension
SPEED_MS = 0.3 # default speed for linear moves in m/s
SPEED_RADS = 0.75 # default speed for joint moves in rad/s
ACCEL_MSS = 3 # default acceleration for lineaer moves in m/ss
ACCEL_RADSS = 1.2 # default acceleration for joint moves in rad/ss
BLEND_RADIUS_M = 0.001 # default blend radius in meters (corners smoothing)
MOVEC_MIN_RADIUS = 1 # minimum circle radius to output (in mm). It does not take into account the Blend radius
MOVEC_MAX_RADIUS = 10000 # maximum circle radius to output (in mm). It does not take into account the Blend radius
USE_MOVEP = False
#--------------------------------
REF_FRAME = eye(4) # default reference frame (the robot reference frame)
LAST_POS_ABS = None # last XYZ position
# other variables
ROBOT_POST = 'unset'
ROBOT_NAME = 'generic'
PROG_FILES = []
MAIN_PROGNAME = 'unknown'
nPROGS = 0
PROG = []
PROG_LIST = []
VARS = []
VARS_LIST = []
SUBPROG = []
TAB = ''
LOG = ''
def __init__(self, robotpost=None, robotname=None, robot_axes = 6, **kwargs):
self.ROBOT_POST = robotpost
self.ROBOT_NAME = robotname
for k,v in kwargs.items():
if k == 'lines_x_prog':
self.MAX_LINES_X_PROG = v
def ProgStart(self, progname):
progname = get_safe_name(progname)
self.nPROGS = self.nPROGS + 1
if self.nPROGS <= 1:
self.TAB = ''
# Create global variables:
self.vars_update()
self.MAIN_PROGNAME = progname
else:
self.addline('# Subprogram %s' % progname)
self.addline('def %s():' % progname)
self.TAB = ' '
def ProgFinish(self, progname):
progname = get_safe_name(progname)
self.TAB = ''
if self.nPROGS <= 1:
self.addline('# End of main program')
else:
self.addline('end')
self.addline('')
def ProgSave(self, folder, progname, ask_user = False, show_result = False):
progname = get_safe_name(progname)
progname = progname + '.script'# + self.PROG_EXT
if ask_user or not DirExists(folder):
filesave = getSaveFile(folder, progname, 'Save program as...')
if filesave is not None:
filesave = filesave.name
else:
return
else:
filesave = folder + '/' + progname
self.prog_2_list()
fid = open(filesave, "w")
# Create main program call:
fid.write('def %s():\n' % self.MAIN_PROGNAME)
# Add global parameters:
fid.write(' # Global parameters:\n')
for line in self.VARS_LIST[0]:
fid.write(' ' + line+ '\n')
#fid.write(' \n')
fid.write(' ')
# Add a custom header if desired:
fid.write(DEFAULT_HEADER_SCRIPT)
fid.write(' \n')
# Add the suprograms that are being used in RoboDK
for line in self.SUBPROG:
fid.write(' ' + line + '\n')
fid.write(' \n')
# Add the main code:
fid.write(' # Main program:\n')
for prog in self.PROG_LIST:
for line in prog:
fid.write(' ' + line + '\n')
fid.write('end\n\n')
fid.write('%s()\n' % self.MAIN_PROGNAME)
fid.close()
print('SAVED: %s\n' % filesave) # tell RoboDK the path of the saved file
self.PROG_FILES = filesave
#---------------------------- SAVE URP (GZIP compressed XML file)-------------------------
filesave_urp = filesave[:-8]+'.urp'
fid = open(filesave, "r")
prog_final = fid.read()
fid.close()
self.PROG_XML = SCRIPT_URP % (self.MAIN_PROGNAME, prog_final, self.MAIN_PROGNAME+'.script')
# Comment next line to force transfer of the SCRIPT file
#self.PROG_FILES = filesave_urp
import gzip
with gzip.open(filesave_urp, 'wb') as fid_gz:
fid_gz.write(self.PROG_XML.encode('utf-8'))
#print('SAVED: %s\n' % filesave_urp) # tell RoboDK the path of the saved file
#------------------------------------------------------------------------------------------
# open file with default application
if show_result:
if type(show_result) is str:
# Open file with provided application
import subprocess
p = subprocess.Popen([show_result, filesave])
elif type(show_result) is list:
import subprocess
p = subprocess.Popen(show_result + [filesave])
else:
# open file with default application
import os
os.startfile(filesave)
if len(self.LOG) > 0:
mbox('Program generation LOG:\n\n' + self.LOG)
#if len(self.PROG_LIST) > 1:
# mbox("Warning! The program " + progname + " is too long and directly running it on the robot controller might be slow. It is better to run it form RoboDK.")
def ProgSendRobot(self, robot_ip, remote_path, ftp_user, ftp_pass):
"""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".
The connection parameters must be provided in the robot connection menu of RoboDK"""
#UploadFTP(self.PROG_FILES, robot_ip, remote_path, ftp_user, ftp_pass)
#return
nprogs = len(self.PROG_LIST)
for i in range(nprogs):
# Prepare next program execution:
send_str = ''
send_str += ('def %s():\n' % self.MAIN_PROGNAME)
# Add global parameters:
send_str += (' # Global parameters:\n')
for line in self.VARS_LIST[i]:
send_str += (' ' + line + '\n')
send_str += (' \n')
# Add a custom header if desired:
send_str += (DEFAULT_HEADER_SCRIPT)
send_str += (' \n')
for line in self.SUBPROG:
send_str += ' ' + line+ '\n'
send_str += (' \n')
# Add the main code:
send_str += (' # Main program:\n')
for line in self.PROG_LIST[i]:
send_str += ' ' + line
send_str += '\n'
send_str += 'end\n\n'
send_str += '%s()\n' % self.MAIN_PROGNAME
send_bytes = str.encode(send_str)
# Wait until the robot is ready:
while i > 0 and not UR_Wait_Ready(robot_ip, i*100.0/nprogs):
print("POPUP: Connect robot to run the program program...")
sys.stdout.flush()
pause(2)
# Send script to the robot:
#print(send_str)
#input("POPUP: Enter to continue")
while ROBOT_NOT_CONNECTED == UR_SendProgramRobot(robot_ip, send_bytes):
print("POPUP: Connect robot to transfer program...")
sys.stdout.flush()
pause(2)
def blend_radius_check(self, pose_abs, ratio_check=0.4):
# check that the blend radius covers 40% of the move (at most)
blend_radius = 'blend_radius_m';
#return blend_radius
current_pos = pose_abs.Pos()
if self.LAST_POS_ABS is None:
blend_radius = '0'
else:
distance = norm(subs3(self.LAST_POS_ABS, current_pos)) # in mm
if ratio_check*distance < self.BLEND_RADIUS_M*1000:
blend_radius = '%.3f' % (round(ratio_check*distance*0.001,3))
#self.LAST_POS_ABS = current_pos
return blend_radius
def MoveJ(self, pose, joints, conf_RLF=None):
"""Add a joint movement"""
if pose is None:
blend_radius = "0"
self.LAST_POS_ABS = None
else:
pose_abs = self.REF_FRAME*pose
blend_radius = self.blend_radius_check(pose_abs)
self.LAST_POS_ABS = pose_abs.Pos()
if len(joints) < 6:
self.RunMessage('Move axes to: ' + angles_2_str(joints))
else:
self.addline('movej(%s,accel_radss,speed_rads,0,%s)' % (angles_2_str(joints), blend_radius))
def MoveL(self, pose, joints, conf_RLF=None):
"""Add a linear movement"""
# Movement in joint space or Cartesian space should give the same result:
# pose_wrt_base = self.REF_FRAME*pose
# self.addline('movel(%s,accel_mss,speed_ms,0,blend_radius_m)' % (pose_2_str(pose_wrt_base)))
if pose is None:
blend_radius = "0"
self.LAST_POS = None
target = angles_2_str(joints)
else:
pose_abs = self.REF_FRAME*pose
blend_radius = self.blend_radius_check(pose_abs)
target = pose_2_str(pose_abs)
self.LAST_POS_ABS = pose_abs.Pos()
if self.USE_MOVEP:
self.addline('movep(%s,accel_mss,speed_ms,%s)' % (target, blend_radius))
else:
self.addline('movel(%s,accel_mss,speed_ms,0,%s)' % (angles_2_str(joints), blend_radius))
def MoveC(self, pose1, joints1, pose2, joints2, conf_RLF_1=None, conf_RLF_2=None):
"""Add a circular movement"""
pose1_abs = self.REF_FRAME*pose1
pose2_abs = self.REF_FRAME*pose2
p0 = self.LAST_POS_ABS
p1 = pose1_abs.Pos()
p2 = pose2_abs.Pos()
if p0 is None:
self.MoveL(pose2, joints2, conf_RLF_2)
return
radius = circle_radius(p0, p1, p2)
print("MoveC Radius: " + str(radius) + " mm")
if radius < self.MOVEC_MIN_RADIUS or radius > self.MOVEC_MAX_RADIUS:
self.MoveL(pose2, joints2, conf_RLF_2)
return
#blend_radius = self.blend_radius_check(pose1_abs, 0.2)
blend_radius = '%.3f' % (0.001*radius) #'0'
#blend_radius = '0'
self.LAST_POS_ABS = pose2_abs.Pos()
#self.addline('movec(%s,%s,accel_mss,speed_ms,%s)' % (angles_2_str(joints1),angles_2_str(joints2), blend_radius))
self.addline('movec(%s,%s,accel_mss,speed_ms,%s)' % (pose_2_str(pose1_abs),pose_2_str(pose2_abs), blend_radius))
def setFrame(self, pose, frame_id=None, frame_name=None):
"""Change the robot reference frame"""
# the reference frame is not needed if we use joint space for joint and linear movements
# the reference frame is also not needed if we use cartesian moves with respect to the robot base frame
# the cartesian targets must be pre-multiplied by the active reference frame
self.REF_FRAME = pose
self.addline('# set_reference(%s)' % pose_2_str(pose))
def setTool(self, pose, tool_id=None, tool_name=None):
"""Change the robot TCP"""
self.addline('set_tcp(%s)' % pose_2_str(pose))
#self.addline('set_payload(1.4, [-0.1181, -0.1181, 0.03])')
#self.addline('set_gravity([0.0, 0.0, 9.82]))')
def Pause(self, time_ms):
"""Pause the robot program"""
if time_ms <= 0:
self.addline('halt() # reimplement this function to force stop')
else:
self.addline('sleep(%.3f)' % (time_ms*0.001))
def setSpeed(self, speed_mms):
"""Changes the robot speed (in mm/s)"""
if speed_mms < 999.9:
self.USE_MOVEP = True
else:
self.USE_MOVEP = False
self.SPEED_MS = speed_mms/1000.0
self.addline('speed_ms = %.3f' % self.SPEED_MS)
def setAcceleration(self, accel_mmss):
"""Changes the robot acceleration (in mm/s2)"""
self.ACCEL_MSS = accel_mmss/1000.0
self.addline('accel_mss = %.3f' % self.ACCEL_MSS)
def setSpeedJoints(self, speed_degs):
"""Changes the robot joint speed (in deg/s)"""
self.SPEED_RADS = speed_degs*pi/180
self.addline('speed_rads = %.3f' % self.SPEED_RADS)
def setAccelerationJoints(self, accel_degss):
"""Changes the robot joint acceleration (in deg/s2)"""
self.ACCEL_RADSS = accel_degss*pi/180
self.addline('accel_radss = %.3f' % self.ACCEL_RADSS)
def setZoneData(self, zone_mm):
"""Changes the zone data approach (makes the movement more smooth)"""
if zone_mm < 0:
zone_mm = 0
self.BLEND_RADIUS_M = zone_mm / 1000.0
self.addline('blend_radius_m = %.3f' % self.BLEND_RADIUS_M)
def setDO(self, io_var, io_value):
"""Sets a variable (output) to a given value"""
if type(io_value) != str: # set default variable value if io_value is a number
if io_value > 0:
io_value = 'True'
else:
io_value = 'False'
if type(io_var) != str: # set default variable name if io_var is a number
newline = 'set_standard_digital_out(%s, %s)' % (str(io_var), io_value)
else:
newline = '%s = %s' % (io_var, io_value)
self.addline(newline)
def waitDI(self, io_var, io_value, timeout_ms=-1):
"""Waits for an input io_var to attain a given value io_value. Optionally, a timeout can be provided."""
if type(io_var) != str: # set default variable name if io_var is a number
io_var = 'get_standard_digital_in(%s)' % str(io_var)
if type(io_value) != str: # set default variable value if io_value is a number
if io_value > 0:
io_value = 'True'
else:
io_value = 'False'
# at this point, io_var and io_value must be string values
#if timeout_ms < 0:
self.addline('while (%s != %s):' % (io_var, io_value))
self.addline(' sync()')
self.addline('end')
def RunCode(self, code, is_function_call = False):
"""Adds code or a function call"""
if is_function_call:
code = get_safe_name(code)
if code.lower() == "usemovel":
self.USE_MOVEP = False
return
elif code.lower() == "usemovep":
self.USE_MOVEP = False
return
elif code.lower().startswith("sync"):
# Trigger the call Synchronize, defined in the header
code = "Synchronize()"
self.addline(code)
return
if not code.endswith(')'):
code = code + '()'
#self.addline(code)
else:
#self.addline(code)
self.addline('# ' + code) #generate custom code as a comment
def RunMessage(self, message, iscomment = False):
"""Show a message on the controller screen"""
if iscomment:
self.addline('# ' + message)
else:
self.addline('popup("%s","Message",False,False,blocking=True)' % message)
# ------------------ private ----------------------
def vars_update(self):
# Generate global variables for this program
self.VARS = []
self.VARS.append('global speed_ms = %.3f' % self.SPEED_MS)
self.VARS.append('global speed_rads = %.3f' % self.SPEED_RADS)
self.VARS.append('global accel_mss = %.3f' % self.ACCEL_MSS)
self.VARS.append('global accel_radss = %.3f' % self.ACCEL_RADSS)
self.VARS.append('global blend_radius_m = %.3f' % self.BLEND_RADIUS_M)
def prog_2_list(self):
if len(self.PROG) > 1:
self.PROG_LIST.append(self.PROG)
self.PROG = []
self.VARS_LIST.append(self.VARS)
self.VARS = []
self.vars_update()
def addline(self, newline):
"""Add a program line"""
if self.nPROGS <= 1:
if len(self.PROG) > self.MAX_LINES_X_PROG:
self.prog_2_list()
self.PROG.append(self.TAB + newline)
else:
self.SUBPROG.append(self.TAB + newline)
def addlog(self, newline):
"""Add a log message"""
self.LOG = self.LOG + newline + '\n'
# -------------------------------------------------
# ------------ For testing purposes ---------------
def Pose(xyzrpw):
[x,y,z,r,p,w] = xyzrpw
a = r*math.pi/180
b = p*math.pi/180
c = w*math.pi/180
ca = math.cos(a)
sa = math.sin(a)
cb = math.cos(b)
sb = math.sin(b)
cc = math.cos(c)
sc = math.sin(c)
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]])
def test_post():
"""Test the post with a basic program"""
robot = RobotPost('Universal Robotics', 'Generic UR robot')
robot.ProgStart("Program")
robot.RunMessage("Program generated by RoboDK", True)
robot.setFrame(Pose([807.766544, -963.699898, 41.478944, 0, 0, 0]))
robot.setTool(Pose([62.5, -108.253175, 100, -60, 90, 0]))
robot.setSpeed(100) # set speed to 100 mm/s
robot.setAcceleration(3000) # set speed to 3000 mm/ss
robot.MoveJ(Pose([200, 200, 500, 180, 0, 180]), [-46.18419, -6.77518, -20.54925, 71.38674, 49.58727, -302.54752] )
robot.MoveL(Pose([200, 250, 348.734575, 180, 0, -150]), [-41.62707, -8.89064, -30.01809, 60.62329, 49.66749, -258.98418] )
robot.MoveL(Pose([200, 200, 262.132034, 180, 0, -150]), [-43.73892, -3.91728, -35.77935, 58.57566, 54.11615, -253.81122] )
robot.RunMessage("Setting air valve 1 on")
robot.RunCode("TCP_On", True)
robot.Pause(1000)
robot.MoveL(Pose([200, 250, 348.734575, 180, 0, -150]), [-41.62707, -8.89064, -30.01809, 60.62329, 49.66749, -258.98418] )
robot.MoveL(Pose([250, 300, 278.023897, 180, 0, -150]), [-37.52588, -6.32628, -34.59693, 53.52525, 49.24426, -251.44677] )
robot.MoveL(Pose([250, 250, 191.421356, 180, 0, -150]), [-39.75778, -1.04537, -40.37883, 52.09118, 54.15317, -246.94403] )
robot.RunMessage("Setting air valve off")
robot.RunCode("TCP_Off", True)
robot.Pause(1000)
robot.MoveL(Pose([250, 300, 278.023897, 180, 0, -150]), [-37.52588, -6.32628, -34.59693, 53.52525, 49.24426, -251.44677] )
robot.MoveL(Pose([250, 200, 278.023897, 180, 0, -150]), [-41.85389, -1.95619, -34.89154, 57.43912, 52.34162, -253.73403] )
robot.MoveL(Pose([250, 150, 191.421356, 180, 0, -150]), [-43.82111, 3.29703, -40.29493, 56.02402, 56.61169, -249.23532] )
robot.ProgFinish("Program")
# robot.ProgSave(".","Program",True)
for line in robot.PROG:
print(line[:-1])
if len(robot.LOG) > 0:
mbox('Program generation LOG:\n\n' + robot.LOG)
input("Press Enter to close...")
if __name__ == "__main__":
"""Function to call when the module is executed by itself: test"""
test_post()