Universal Robots Forum

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)

1 Like

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

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!