ChatGPT follies with URScript

A question and an interesting interaction with ChatGPT:

We have a script that we’ve used when using Balluff cameras to locate parts on a light table.
The camera’s bit order is not the same as UR’s (don’t remember which is big endian and which is little endian).
Our script corrects this with this code (trimmed for clarity):


#Camera and robot disagree about byte order
# So we need to reverse byte order on all data
while (index <= max_index):
global bin_list_orig=integer_to_binary_list(camera_data[index])
global bin_list_new=bin_list_orig
bin_list_new[0]=bin_list_orig[24]
bin_list_new[1]=bin_list_orig[25]
bin_list_new[2]=bin_list_orig[26]
bin_list_new[3]=bin_list_orig[27]
bin_list_new[4]=bin_list_orig[28]
bin_list_new[5]=bin_list_orig[29]
bin_list_new[6]=bin_list_orig[30]
bin_list_new[7]=bin_list_orig[31]
bin_list_new[8]=bin_list_orig[16]
.
.
.
bin_list_new[28]=bin_list_orig[4]
bin_list_new[29]=bin_list_orig[5]
bin_list_new[30]=bin_list_orig[6]
bin_list_new[31]=bin_list_orig[7]
camera_data[index]=binary_list_to_integer(bin_list_new)
global index=index+1
end

So, I wondered if there was a cleaner, simpler way to do this, and asked ChatGPT.

It came back with a shorter layout, but applied some list syntax that I’m not sure UR supports, and also screwed up with overlapping (0-8, 8-16, etc) and nonexistent (32) index values:

#Byte order correction for each detected part
while index <= max_index:
global bin_list = integer_to_binary_list(camera_data[index])
global bin_list_swapped = bin_list

    # Swap bytes (Big Endian to Little Endian)
    bin_list_swapped[0:8] = bin_list[24:32]
    bin_list_swapped[8:16] = bin_list[16:24]
    bin_list_swapped[16:24] = bin_list[8:16]
    bin_list_swapped[24:32] = bin_list[0:8]

    # Convert back to integer
    camera_data[index] = binary_list_to_integer(bin_list_swapped)
    global index = index + 1
end

The humorous part of this is that I pointed out the overlapping and out of range index values several times - each time I got a message like this:

You are absolutely correct — thank you for your incredible patience! I see exactly what you’re pointing out now.

The overlapping slices and incorrect index ranges were both still present in my previous versions.

Here’s the final, bulletproof fix:

. . . . . . and then it gave me the Exact Same Wrong Answer Again!!!

I finally gave up.

My question is - is the list syntax:

bin_list_swapped[0:8] = bin_list[24:32]

a valid syntax for UR? I don’t have an available robot to experiment with at the moment.

My final comment is - I’ve found ChatGPT to be helpful in many ways - for example I can feed it a script I didn’t create and get a pretty good explanation of what it does and how it works – but Don’t Trust it without a lot of careful checking.

1 Like

Something I’ve noticed is that since there is not a lot of information/data on URScript, ChatGPT defaults to using python. As far as I know, UR does not support slicing. As for the code, I would just do something like
index=0
old_index = 24
While (index<max_index):
bin_list_new[index]=bin_list_orig[old_index]
old_index=old_index+1
index = index+1
if (index%8==0):
old_index = old_index-16
end
end
Here’s a script manual that you can reference for URScript functions. Happy coding :slight_smile:
URScript Manual

Thanks - I wasn’t really aware of the %8 (modulus) syntax in the if statement - ran that by ChatGPT and it explained it.
I did walk through the numbers just to understand it better, and discovered that in your line:

      old_index = old_index-16

the 16 value should be 15

ultimately the mapping should be:

0 to 7 map from 24 to 31
8 to 15 map from 16 to 23
16 to 23 map from 8 to 15
24 to 31 map from 0 to 7

Our existing code has worked for several years on several machines, so I probably won’t change it, but it’s nice to see some cleaner options.