Universal Robots Forum

Convert unsigned value of modbus 270 to 275 to signed value

how to convert unsigned value of modbus address from 270 to 275 (ur joint pose in mrad) to signed value?

In what condition,read value of modbus should minus -6283.

@willie.lou

Modbus signals are always read as unsigned.
If you expect a signed 16-bit integer to be sent, you should test if the integer is greater than 32768, and if so, you should convert it to the negative inverse.

Check out the descriptive text in the beginning of the Modbus Server article, as this explains this conversion.

modbus address between 270 to 275 is UR joint position in mili radian. As max degree allow to turn would be ±360 or ±6.283 rad or ±6283 m rad.
So unsigned value between modbus address 270 to 275 would never more than 32768.

I try derive formula and observe that in the case of
joint pose in -ve,than it should
val=(val-6283)/1000 radian
however in the case of joint pose in +ve,then
val=(val/1000) radian

I would like to get exactly same value get_actual_joint_position()

if joint in +ve
base=modbus[260]/1000
shoulder=modbus[261]/1000
elbow=modbus[262]/1000
w1=modbus[263]/1000
w2=modbus[264]/1000
w3=modbus[265]/1000

if joint in -ve
base=(modbus[260]-6283)/1000
shoulder=(modbus[261]-6283)/1000
elbow=(modbus[262]-6283)/1000
w1=(modbus[263]-6283)/1000
w2=(modbus[264]-6283)/1000
w3=(modbus[265]-6283)/1000

p[base,shoulder,elbow,w1,w2,w3]

however in run time,doesnt know either the joint in +ve or -ve.please advice.

Is this python code? If so, how you should read signed values from modbus depends on the modbus library you are using. Still, assuming you got an unsigned 16 bit integer that should have been interpreted as twos-complement, you can get the actual value like this (in Python):

def reinterpredSigned16(val):
  if val & 0x8000: return val - 0x10000
  return val
```

I was thrown off by the ``f joint in -veand modbus[xxx]. After more reading it seems more likely that you’re asking about URScript after all.

You can write the same function in URScript with some modifications:

def reinterpredSigned16(val):
  if val >= 32768
    return val - 65536
  end
  return val
end

However, in URScript you might as well just use get_actual_joint_position when available.