Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

CR1000 OS Math Bug?


kcopeland Dec 13, 2016 05:08 PM

Hello-

I'm trying to wrap my head around this.  I'm doing computation in the data logger and I don't know how to explain two different answers I'm getting by doing essentially the same thing.  See code:

Public reg41 As Long = 16754
Public reg42 As Long  = 15729
Public test_216 As Long
Public test_mult As Long
Public test_add As Long
Public sr_32bitdata As Long
Public sr_mantissa
Public sr_exponent
Public sr_signbit
Public sr_sensitivity


'Main Program
BeginProg
 Scan (1,Sec,0,0)
   test_216 = 2^16
   test_mult = reg41 * test_216
   test_add = test_mult + reg42
  
    sr_32bitdata = (reg41 * (2^16)) + reg42


    sr_signbit = IIF( sr_32bitdata < 2147483647, 1, -1)
    sr_exponent = Floor(sr_32bitdata/ (2^23)) - 127
    sr_mantissa = ((sr_32bitdata - ((sr_exponent+127)*(2^23)))/ (2^23)) + 1
    sr_sensitivity = sr_signbit * sr_mantissa * (2^sr_exponent) 

 NextScan
EndProg

The Test_Add variable and the sr_32bitdata should give exactly the same answer.  But when I combine all the math into the sr_32bitdata variable, I get the wrong answer.  What am I missing?  I'm using OS29 on a Cr1000


JDavis Dec 13, 2016 08:05 PM

I don't have a datalogger onhand at the momment to check your question.

I did notice what you are doing in the latter part of the program. For that part, I recommend getting the bits into the right locations in a Long, then using Movebytes to get the bits into a Float. Your method can run into rounding errors. To get the bits into a Long, use AND bit masks, then OR them together.


kcopeland Dec 13, 2016 08:09 PM

Thanks Jacob.  That is originally how I was doing my conversion and we were having issues with that conversion.  So we went to the manufacturer manual and are following their guidelines for how to convert the data we are receiving in the registers. 

http://www.hukseflux.com/sites/default/files/product_manual/SR05_manual_v1610.pdf

Page 72, in case you want to check it out.


aps Dec 14, 2016 09:47 AM

That sensor codes the floats a IEEE754 32 bit values which is the same format the datalogger uses.   This means you could read the data as a long and use the Movebytes command to convert that directly into a float in the logger, with no errors, providing the bytes are in the right order.   There are also options to read the registers that are stored as floats from the sensor directly using the Modbus instruction option to read floats directly into float values.   If you take that approach you need to use multiple Modbus commands to get the different data into the logger but this is a consequence of the manufactures of these types of sensors not storing data of the same datatypes on sequential registers, they seem to mix up the different datatypes in almost a random fashion which is rather inconvenient.

A colleague of mine has written a program to read data out of the SR05.  He may post a copy on here later.

Log in or register to post/reply in the forum.