URScript initialise array of given length

I want to create a function in my installation node that gets a string of comma separated values and returns them as an array.

The only thing I’m missing is how to initialise an array of unknown size.
Like when on any programming language you do something like
new_array[10]

Is this at all possible on URScripts?

1 Like

I personally have come across a couple weird ways to create arrays of different sizes. Here they are in order of greasiness to most applicable to your situation. URScript isn’t the greatest at handling that.

list of length 32:

L = 0
L = integer_to_binary_list(L)

creates a list of False that is 32 items long.

list of variable length:

this involves registering a function on an xmlrpc server that returns the array:

python3 example

set this server up directly on the robot or start it with a URCap

from xmlrpc.server import SimpleXMLRPCServer

def create_array(length):
   return [0]*length

server = SimpleXMLRPCServer(('localhost', XXXX)
server.register_function(create_array, "create_array")
server.serve_forever()

and then on the robot side:

proxy = rpc_factory("xmlrpc", "http://localhost:XXXX")

array = proxy.create_array(100)

unfortunately right now it seems that the array length limit coming through XMLRPC is 511. I’m currently trying to get an answer on why that changed in software version 5.6.

Good luck and hopefully there are some other ideas floating around!

1 Like

I can barely believe the madness I just read :smile:

Very ingenious work around.
I am developing a cap and was trying to avoid polyscope for some of the communication but I guess I’ll have to dive in there…

Huge thanks for the reply!

So we have xmlrpc functions available that allow you to get back a list of a given length, padded with a starting value that you can define which works great if you have an rpc server running.

One of our software guys wrote a bit of node code that auto-generates a script file for getting back a list of any length, up to 250 elements currently, that you can pad with a value you specify. Our robot programmers have switched to using this over the rpc call as its faster and reduces one of the calls that they have to make to the rpc server.

I attached the file, when you see it you will probably chuckle as it’s a bit “heavy” but in lieu of anything native being provided it works really well. We use it for creating lists of numbers, booleans or poses.

You could also build on top of this new functions for simply getting a specific type of list with a preset default

For instance

def getDynamicArrayOfPoses(size):
  return generated_getArrayOfSize(size, p[0,0,0,0,0,0])
end

dynamicArray.script.zip (1.8 KB)

3 Likes

Thanks Matt! Clever, and the guys and I got a good chuckle out of that script in the office. It’s amazing what we have to resort to haha. I’m still surprised it’s not a feature.

1 Like

Yeah, I wish it was a native function but I also wish we could store strings in lists and could also have lists of lists…but you know, I feel like the only one at times that wants these language features

1 Like

This is what I was fishing for actually.
I’m getting a message from a socket server and I’d like to split it into different strings and shove them on a list…

Very mysterious omission actually…

As insane as this is.
It’s probably the most usable answer.

I did chuckle! hehe

Thank you!

Good afternoon, I would like to know how you managed to start the python code you wrote, have you written a ur script? you installed a UR cap and if yes which? I am trying to make a dynamic array and I tried many things with no satisfactory results.
Would be great to get a solution thank you very much!

Ah yes, my favourite thread!

Let’s say you want to have this Python server running on the robot. There are at least 3 ways we could do this.

  1. Start the server with the URCap MyDaemon example (Java gives me bad feels so I don’t do this)
  2. System V scripts (michael_scott_nonono.gif)
  3. Start the server on boot in the underlying Debian Jessie OS using systemd.

Method 3 (The sane way):

  1. Create the python code
  2. Create a service file and enable the service using systemctl
  3. Now you have a python 2.7 xmlrpc server that starts at boot and hosts a function that returns an array of whatever length you want (Up to length 512 of course)

Step 1: Python 2.7 Server code (Robot uses 2.7)

$ mkdir /home/scripts
$ nano /home/scripts/my_xmlrpc_server.py
#from xmlrpclib.SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCServer

def create_array(length):
    # Could also use False instead of 0 if you're doing booleans
    return [0]*length

ip = 'localhost'
port = 42069  # Nice

server = SimpleXMLRPCServer((ip, port))
server.register_function(create_array, "create_array")

server.serve_forever()
print "Started server on {}:{}".format(ip, port)

Step 2: Service File

Create service file

$ nano /lib/systemd/system/MyServer.service 

Enter details

[Unit]
Description=Awesome Array Creation Server
After=multi-user.target

[Service]
ExecStart=/usr/bin/python /home/scripts/my_xmlrpc_server.py

[Install]
WantedBy=multi-user.target

Check for new service files, Enable/Start service + Check status

$ systemctl daemon-reload  # This line makes the system check for new service files
$ systemctl enable MyServer.service
$ systemctl start MyServer.service
$ systemctl status MyServer.service  # This should spit out the current status of the server or any exceptions on start

Need to change the Python and restart?

$ systemctl restart MyServer.service

Kill it with fire?

$ systemctl disable MyServer.service

Hello first of all thank you very much for your rapid response.
It seemed to work but at the end I get an failure message, do you know why this happens? have I got something wrong or do you have any additional tips to try and solve them?
Thank you



Yea I should have run that Python first, it’s the import that’s the problem as seen 3rd from last line in your status readout. Try to dig up the example code for starting the server in Python 2.7 and it should point you in the right direction. I might get to it in the next day or two but this is recreational for me.

edit:
https://docs.python.org/2.7/library/simplexmlrpcserver.html
I changed my example code to match this

I agree, writing a custom server in python 2… kicking off it off, then running sockets to get access to a means to create an array of said length is insane. UR really needs to figure this out instead of passing the pain along to their customers.

What tree do we need to collectively bark up to motivate UR management to throw money at this problem?