Home Programming

Discussion

Left ArrowBack to discussions page
matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,267 Handy
edited May 2017 in Programming
I thought that we might want a single thread where everyone can post up any handy little helper functions that they have developed over time that make programming and using the UR robots easier.  If you have any functions that you are willing to share please post the code below along with an example of how to use the code.  If you have any questions about how to use something please feel free to reach out to the author or post a question on here so that everyone can learn from each other.

Comments

  • JoseJose Posts: 11 Apprentice
    Hi,
    where I can find the list of commands like "set speed" , "popup" ... ?
  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,267 Handy
    The dashboard server commands can be found in this document https://www.universal-robots.com/how-tos-and-faqs/how-to/ur-how-tos/dashboard-server-port-29999-15690/

    the other commands thay that I use regularly are sprinkled throughout the UR support website.  
  • JoseJose Posts: 11 Apprentice
    Thanks for your reply.
  • klacourtklacourt Posts: 12 Apprentice
    It appears the close popup command does not work for the "Protective Stop" popup?  I am running 3.5.3.10825 with no luck...
  • klacourtklacourt Posts: 12 Apprentice
    AHH!  Just found "unlock protective stop" is a dashboard command!  My reference must have been old.
  • lakshmip001lakshmip001 Partner Posts: 41 Apprentice
    Hi, I am controlling UR5 using PC and sometimes results in an error getinverse(): unable to find solution displayed on UR teach pendant. How can I read popup message from pc and then close the popup.
  • JstewartJstewart Posts: 62 Apprentice
    This function allows you to easily change the speed of the running robot by changing the current value of the speed slider programmatically.
    def runSlow(speed):
    
    if speed == 0:
      speed = 0.001
    end
    socket_open("127.0.0.1",30003)
    socket_send_string("set speed")
    socket_send_string(speed)
    socket_send_byte(10)
    socket_close()
    
    end
    
    
    This function takes a float between 0 and 1 as an argument, one thing to note is if you actually send 0 it pauses the program but I have not found a way to easily recover from this so I am using the if statement to verify that the speed is not 0, if so it sets it to 0.001 which basically stops motion but allows the robot program to continue to run.

    Usage in a program would be

    runSlow(0.2)

    @matthewd92 Is it normal for this to lag when changing the speed? I noticed that whenever i set my waypoints and set the speeds right before them.




  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,267 Handy
    edited November 2019
    Here is something that we get asked about a lot, which is creating planes in URScript.  One word of caution, these calculations result in a plane that is configured with the first point as the origin, +Y axis defined by the second point and the +X axis defined by the third point, this is not the same way that UR calculates planes in current polyscope (newer than 3.7 or e-series) but we still use it everyday in production environments.  We did not develop this code so I will not take credit for it but it has been thoroughly tested and used by us and works as expected.

    <div>#**</div><div>#&nbsp;@api&nbsp;global_plane_finder(p1,p2,p3)&nbsp;global_plane_finder</div><div>#&nbsp;@apiGroup&nbsp;Private&nbsp;Scripts</div><div>#&nbsp;@apiName&nbsp;global_plane_finder</div><div>#&nbsp;@apiDescription&nbsp;This&nbsp;method&nbsp;is&nbsp;used&nbsp;to&nbsp;calculate&nbsp;the&nbsp;plane&nbsp;on&nbsp;which&nbsp;the&nbsp;welds&nbsp;have&nbsp;to&nbsp;be&nbsp;made.</div><div>#*</div><br><div>def&nbsp;global_plane_finder(p1,&nbsp;p2,&nbsp;p3):</div><div>&nbsp;&nbsp;V12&nbsp;=&nbsp;[p2[0]&nbsp;-&nbsp;p1[0],&nbsp;p2[1]&nbsp;-&nbsp;p1[1],&nbsp;p2[2]&nbsp;-&nbsp;p1[2]]</div><div>&nbsp;&nbsp;V13&nbsp;=&nbsp;[p3[0]&nbsp;-&nbsp;p1[0],&nbsp;p3[1]&nbsp;-&nbsp;p1[1],&nbsp;p3[2]&nbsp;-&nbsp;p1[2]]</div><div>&nbsp;&nbsp;VZ&nbsp;=&nbsp;mycross(V12,&nbsp;V13)</div><div>&nbsp;&nbsp;Tmat&nbsp;=&nbsp;pose2Tmat(p1)</div><div>&nbsp;&nbsp;VZofp1&nbsp;=&nbsp;[Tmat[2],&nbsp;Tmat[6],&nbsp;Tmat[10]]</div><div>&nbsp;&nbsp;value&nbsp;=&nbsp;mydot(VZ,&nbsp;VZofp1)</div><div>&nbsp;&nbsp;if&nbsp;(value&nbsp;<&nbsp;-0.00001):</div><div>&nbsp;&nbsp;&nbsp;&nbsp;VZ&nbsp;=&nbsp;mycross(V13,&nbsp;V12)</div><div>&nbsp;&nbsp;end</div><div>&nbsp;&nbsp;VZ&nbsp;=&nbsp;myunitised(VZ)</div><div>&nbsp;&nbsp;VY&nbsp;=&nbsp;myunitised(V12)</div><div>&nbsp;&nbsp;VX&nbsp;=&nbsp;mycross(VY,&nbsp;VZ)</div><div>&nbsp;&nbsp;output&nbsp;=&nbsp;[VX[0],&nbsp;VY[0],&nbsp;VZ[0],&nbsp;p1[0],&nbsp;VX[1],&nbsp;VY[1],</div><div>&nbsp;&nbsp;VZ[1],&nbsp;p1[1],&nbsp;VX[2],&nbsp;VY[2],&nbsp;VZ[2],&nbsp;p1[2],&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;1]</div><div>&nbsp;&nbsp;local&nbsp;pose&nbsp;=&nbsp;Tmat2pose(output)</div><div>&nbsp;&nbsp;return&nbsp;pose</div><div>end</div><br><br><div>def&nbsp;mycross(a,&nbsp;b):</div><div>&nbsp;&nbsp;output&nbsp;=&nbsp;[0,&nbsp;0,&nbsp;0]</div><div>&nbsp;&nbsp;output[0]&nbsp;=&nbsp;a[1]&nbsp;*&nbsp;b[2]&nbsp;-&nbsp;a[2]&nbsp;*&nbsp;b[1]</div><div>&nbsp;&nbsp;output[1]&nbsp;=&nbsp;a[2]&nbsp;*&nbsp;b[0]&nbsp;-&nbsp;a[0]&nbsp;*&nbsp;b[2]</div><div>&nbsp;&nbsp;output[2]&nbsp;=&nbsp;a[0]&nbsp;*&nbsp;b[1]&nbsp;-&nbsp;a[1]&nbsp;*&nbsp;b[0]</div><div>&nbsp;&nbsp;return&nbsp;output</div><div>end</div><br><br><div>def&nbsp;Tmat2pose(Tmat):</div><div>&nbsp;&nbsp;output&nbsp;=&nbsp;p[0,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;0]</div><div>&nbsp;&nbsp;output[0]&nbsp;=&nbsp;Tmat[3]</div><div>&nbsp;&nbsp;output[1]&nbsp;=&nbsp;Tmat[7]</div><div>&nbsp;&nbsp;output[2]&nbsp;=&nbsp;Tmat[11]</div><div>&nbsp;&nbsp;sy&nbsp;=&nbsp;mynorm([Tmat[0],&nbsp;Tmat[4],&nbsp;0])</div><div>&nbsp;&nbsp;if&nbsp;(sy&nbsp;>&nbsp;0.00001):</div><div>&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&nbsp;atan2(Tmat[9],&nbsp;Tmat[10])</div><div>&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;=&nbsp;atan2(-&nbsp;Tmat[8],&nbsp;sy)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;z&nbsp;=&nbsp;atan2(Tmat[4],&nbsp;Tmat[0])</div><div>&nbsp;&nbsp;else:</div><div>&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&nbsp;atan2(-&nbsp;Tmat[6],&nbsp;Tmat[5])</div><div>&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;=&nbsp;atan2(-&nbsp;Tmat[8],&nbsp;sy)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;z&nbsp;=&nbsp;0</div><div>&nbsp;&nbsp;end</div><div>&nbsp;&nbsp;rotvec&nbsp;=&nbsp;rpy2rotvec([x,&nbsp;y,&nbsp;z])</div><div>&nbsp;&nbsp;output[3]&nbsp;=&nbsp;rotvec[0]</div><div>&nbsp;&nbsp;output[4]&nbsp;=&nbsp;rotvec[1]</div><div>&nbsp;&nbsp;output[5]&nbsp;=&nbsp;rotvec[2]</div><div>&nbsp;&nbsp;return&nbsp;output</div><div>end</div><br><br><div>def&nbsp;pose2Tmat(pose):</div><div>&nbsp;&nbsp;if&nbsp;(pose[3])&nbsp;==&nbsp;0&nbsp;and&nbsp;(pose[4]&nbsp;==&nbsp;0)&nbsp;and&nbsp;(pose[5]&nbsp;==&nbsp;(0)):</div><div>&nbsp;&nbsp;&nbsp;&nbsp;Rmat&nbsp;=&nbsp;[1,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;1,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;1]</div><div>&nbsp;&nbsp;else:</div><div>&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&nbsp;pose[3]</div><div>&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;=&nbsp;pose[4]</div><div>&nbsp;&nbsp;&nbsp;&nbsp;z&nbsp;=&nbsp;pose[5]</div><div>&nbsp;&nbsp;&nbsp;&nbsp;ang&nbsp;=&nbsp;mynorm([x,&nbsp;y,&nbsp;z])</div><div>&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&nbsp;x/ang</div><div>&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;=&nbsp;y/ang</div><div>&nbsp;&nbsp;&nbsp;&nbsp;z&nbsp;=&nbsp;z/ang</div><div>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;sin(ang)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;=&nbsp;cos(ang)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;=&nbsp;1&nbsp;-&nbsp;c</div><div>&nbsp;&nbsp;&nbsp;&nbsp;Rmat&nbsp;=&nbsp;[c&nbsp;+&nbsp;(t&nbsp;*&nbsp;x&nbsp;*&nbsp;x),&nbsp;(t&nbsp;*&nbsp;x&nbsp;*&nbsp;y)&nbsp;-&nbsp;(s&nbsp;*&nbsp;z),&nbsp;(t&nbsp;*&nbsp;x&nbsp;*&nbsp;z)&nbsp;+&nbsp;(s&nbsp;*&nbsp;y),&nbsp;(t&nbsp;*&nbsp;x&nbsp;*&nbsp;y)&nbsp;+&nbsp;(s&nbsp;*&nbsp;z),</div><div>&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;+&nbsp;(t&nbsp;*&nbsp;y&nbsp;*&nbsp;y),&nbsp;(t&nbsp;*&nbsp;y&nbsp;*&nbsp;z)&nbsp;-&nbsp;(s&nbsp;*&nbsp;x),&nbsp;(t&nbsp;*&nbsp;x&nbsp;*&nbsp;z)&nbsp;-&nbsp;(s&nbsp;*&nbsp;y),&nbsp;(t&nbsp;*&nbsp;y&nbsp;*&nbsp;z)&nbsp;+&nbsp;(s&nbsp;*&nbsp;x),&nbsp;c&nbsp;+&nbsp;(t&nbsp;*&nbsp;z&nbsp;*&nbsp;z)]</div><div>&nbsp;&nbsp;end</div><div>&nbsp;&nbsp;Tmat&nbsp;=&nbsp;[Rmat[0],&nbsp;Rmat[1],&nbsp;Rmat[2],&nbsp;pose[0],&nbsp;Rmat[3],&nbsp;Rmat[4],</div><div>&nbsp;&nbsp;Rmat[5],&nbsp;pose[1],&nbsp;Rmat[6],&nbsp;Rmat[7],&nbsp;Rmat[8],&nbsp;pose[2]]</div><div>&nbsp;&nbsp;return&nbsp;Tmat</div><div>end</div><br><br><div>def&nbsp;mynorm(a):</div><div>&nbsp;&nbsp;base&nbsp;=&nbsp;sqrt(a[0]&nbsp;*&nbsp;a[0]&nbsp;+&nbsp;a[1]&nbsp;*&nbsp;a[1]&nbsp;+&nbsp;a[2]&nbsp;*&nbsp;a[2])</div><div>&nbsp;&nbsp;return&nbsp;base</div><div>end</div><br><br><div>def&nbsp;myunitised(a):</div><div>&nbsp;&nbsp;output&nbsp;=&nbsp;[0,&nbsp;0,&nbsp;0]</div><div>&nbsp;&nbsp;base&nbsp;=&nbsp;mynorm(a)</div><div>&nbsp;&nbsp;output[0]&nbsp;=&nbsp;a[0]&nbsp;/&nbsp;base</div><div>&nbsp;&nbsp;output[1]&nbsp;=&nbsp;a[1]&nbsp;/&nbsp;base</div><div>&nbsp;&nbsp;output[2]&nbsp;=&nbsp;a[2]&nbsp;/&nbsp;base</div><div>&nbsp;&nbsp;return&nbsp;output</div><div>end</div><br><br><div>def&nbsp;mydot(a,&nbsp;b):</div><div>&nbsp;&nbsp;output&nbsp;=&nbsp;a[0]&nbsp;*&nbsp;b[0]&nbsp;+&nbsp;a[1]&nbsp;*&nbsp;b[1]&nbsp;+&nbsp;a[2]&nbsp;*&nbsp;b[2]</div><div>&nbsp;&nbsp;return&nbsp;output</div><div>end</div>

  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,267 Handy
    Jstewart said:
    This function allows you to easily change the speed of the running robot by changing the current value of the speed slider programmatically.
    def runSlow(speed):
    
    if speed == 0:
      speed = 0.001
    end
    socket_open("127.0.0.1",30003)
    socket_send_string("set speed")
    socket_send_string(speed)
    socket_send_byte(10)
    socket_close()
    
    end
    
    
    This function takes a float between 0 and 1 as an argument, one thing to note is if you actually send 0 it pauses the program but I have not found a way to easily recover from this so I am using the if statement to verify that the speed is not 0, if so it sets it to 0.001 which basically stops motion but allows the robot program to continue to run.

    Usage in a program would be

    runSlow(0.2)

    @matthewd92 Is it normal for this to lag when changing the speed? I noticed that whenever i set my waypoints and set the speeds right before them.




    Yes, there can be a lag between issuing the command and the speed slider being adjusted, it is usually very small but I have seen it have as much as 1/2 second or so lag.  We use this primarily at the start of a program for instance when we are debugging to make sure that it runs slow, allows us to move the speed slider back to 100% when we use the move screen but not have to worry that we might start a buggy program at speed.

    We have also used in before in conjunction with a laser scanner, instead of putting the robot in a safeguard stop when someone entered the cell, we placed the robot in reduced mode and then fired a set speed command to chop the speed slider to 1%, this effectively "stops" the robot, its still moving but extremely slowly.  This was so the program could continue to run even when someone walked by so that we could monitor the machines in the cell and catch their completion status as it was only broadcast once and missing it meant we never knew the machines finished.  We no longer follow this practice but rather wire machines like that up to other systems that can catch those signals such as a PLC and then the robot communicates with the PLC to get the information it is looking for.
Sign In or Register to comment.
Left ArrowBack to discussions page