Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

Using LJPython and low-level functions for temp readings


  • Please log in to reply
4 replies to this topic

#1 RedSpanner

RedSpanner
  • Members
  • 37 posts

Posted 05 September 2012 - 02:59 PM

Hello, I have a number of EI1022 and an EI1034 sensors recording misc temperatures for a test setup connected to a ue9. I've written some exploratory code to better understand how the low-level functions operate. This code is: [codebox] import ue9 import LJ_Util print "Opening device...", # Opens a UE9 at IP Address 192.168.1.127 d = ue9.UE9( ethernet = True, ipAddress = "192.168.1.127") print "Done\n" #Set starting state: Pull FIO5 high to power both devices, and... # ...set MIO0 to Input (waiting to go to output state and drive SSR) d.feedback(FIOMask=0x20, FIODir=0x20, FIOState=0x20, MIOMask=0x01, MIODirection=0x00, MIOState=0x00) d.getCalibrationData() print 'Initialization ...Done\n' extTempArg = 0x0010 boxTempArg = 0x3F00 controlTempArg = 0x0003 boxTemp={} read = d.feedback(AINMask=boxTempArg) for i in read: volt = read.get(('AIN'+ str(i)), 'not found') corrVolt = d.binaryToCalibratedAnalogVoltage(volt, 0x00) Temp = LJ_Util.celsius_conversion(corrVolt) print Temp boxTemp.update(('AIN' + str(i)), Temp) print boxTemp [/codebox] When I run this code, here is the error I receive: [codebox] $ python Low-Level-Test.py Opening device... Done Initialization ...Done Traceback (most recent call last): File "Low-Level-Test.py", line 30, in <module> corrVolt = d.binaryToCalibratedAnalogVoltage(volt, 0x00) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ue9.py", line 1177, in binaryToCalibratedAnalogVoltage return (bits * slope) + offset TypeError: can't multiply sequence by non-int of type 'float' $ [/codebox] I thought I was using the calibration data correctly, but obviously not. Can you point me in the right direction to fix this? Thanks, Red

#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 05 September 2012 - 06:30 PM

In the UE9 class, the feedback method does the binaryToCalibratedAnalogVoltage conversions for you. "AIN0" to "AIN15" in the returned dictionary are the float voltages you want, so do not call binaryToCalibratedAnalogVoltage in your loop. Also, you will want to change your loop code since "i" in your for loop is a key in the "read" dictionary. Your "read.get" call is searching for invalid keys ("AINFIODir" and "AINAIN0" for example).

#3 RedSpanner

RedSpanner
  • Members
  • 37 posts

Posted 19 September 2012 - 08:54 AM

In the UE9 class, the feedback method does the binaryToCalibratedAnalogVoltage conversions for you. "AIN0" to "AIN15" in the returned dictionary are the float voltages you want, so do not call binaryToCalibratedAnalogVoltage in your loop.

Also, you will want to change your loop code since "i" in your for loop is a key in the "read" dictionary. Your "read.get" call is searching for invalid keys ("AINFIODir" and "AINAIN0" for example).

I now understand your first point, though I read your web site info differently... Based on the site, I interpreted the binarytocalibratedanalogvoltage function was necessary for low-level function calls using the feedback() or singleIO() methods. Does that mean this function is also NOT required for singleIO() method invocations?

I think I understand the issue with the read dictionary in my code, also. Even with the calibration code corrected, the script still does not function properly. I thought I could use the read.get() method to extract only the AINx voltage readings I want, but the 'for' loop processes every key, not just the selected ones, and so the default label ('not found') is returned for every key in the read dictionary. Can you suggest an alternative approach to extract the AINx readings from the read dictionary? I'm really scratching my head over that one...

#4 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 20 September 2012 - 01:16 PM

binaryToCalibratedAnalogVoltage is necessary for the singleIO method in the UE9 class. I would have expected it to be necessary for the feedback method as well, but that is not the case. If you were doing the low-level command/responses directly you need to convert analog input readings from binary to a float values, but the UE9 class' feedback implementation does it for you.

If you know what AIN you want from the returned dictionary you can get the value with something like "read['AIN0']" or "read.get('AIN0', 'Not Found')". Based on your code and assuming you only want readings from the AINMask, maybe something like this (not tested thoroughly):
print 'Initialization ...Done\n'

extTempArg = 0x0010
boxTempArg = 0x3F00
controlTempArg = 0x0003

boxTemp={}

read = d.feedback(AINMask=boxTempArg)
#Iterate through values 0 to 15 (AIN0 - 15)
for i in range(0, 16):
	#Check if the write mask bit is set for AINx
	if (boxTempArg >> i) & 1:
		volt = read.get('AIN'+str(i), 'not found')
		Temp = LJ_Util.celsius_conversion(volt)
		print Temp
		boxTemp['AIN'+str(i)]=Temp

print boxTemp


#5 RedSpanner

RedSpanner
  • Members
  • 37 posts

Posted 28 September 2012 - 10:34 AM

Thanks for this, it's a great reply, and had me scrambling to decipher the expression operator you used to bit-parse the AINMask. Very cool solution, plus it expanded my useful knowledge of Python. I also used the singleIO low-level function, with the calibration method as well. A combined code snippet showing both these things can be found below. It's not very elegant, but it works. I'll clean it up (hopefully, making it more 'Pythonic') when I move it to the final code block. [codebox] boxTempArg = 0x3F00 controlTempArg = 0x0003 boxTemp = {} read = d.feedback(AINMask=boxTempArg) print read # Iterate through values 0 to 15 (AIN0 - 15) for both sets of monitoring temperatures for i in range(0, 16): #Check if the write mask bit is set for AINx if (boxTempArg >> i) & 1: volt = read.get('AIN'+str(i), 'not found') Temp = LJ_Util.celsius_conversion(volt) print Temp boxTemp['AIN'+str(i)]=Temp # Read and convert external temperature xT = d.singleIO(IOType = 4, Channel = 4, BipGain = 0x01, Resolution = 17, SettlingTime = 5) print xT xV = d.binaryToCalibratedAnalogVoltage(xT.get('AIN4'), 0x01) print xV extTemp = LJ_Util.out_celsius_conversion(xV) print boxTemp print extTemp [/codebox] Note that the external temperature comes from an EI1034 wired up for a broader temperature range, as documented in the data sheet. This is why there is a different conversion routine call to module LJ_Util Thanks again for the help, folks


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users