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

Labjack U3 and python


  • Please log in to reply
68 replies to this topic

#1 david_b

david_b
  • Members
  • 38 posts

Posted 30 December 2013 - 04:39 PM

Hello ,

 

I just got one U3 HV and installed it under a raspberry pi. I managed to do some simple things with it but i would like if there are some python tutorials / examples using it/?

 

thanks in advance



#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 30 December 2013 - 05:19 PM

There are general Python quickstarts here:

 

http://labjack.com/s...ckpython/modbus

http://labjack.com/s...ython/low-level

 

There are also examples in the Examples directory of the LabJackPython download.

 

For U3 documentation take a look at the User's Guide:

 

http://labjack.com/s.../u3/users-guide



#3 david_b

david_b
  • Members
  • 38 posts

Posted 01 January 2014 - 05:50 PM

Hi LabJack and thank you for your reply.

I did some litle progress but i have some things unclear so i would have some other questions.

I will also attach a picture and try to explain my setup before post questions

 

20140102_023602.jpg

 

As you can see i am using a pi, an external powered usb hub and of course an u3 hv.

The device that is attached to u3 is actually 2 variable resistors that modify voltage between vs and gnd (2 resistors - 2 channels )

Now the questions / observations

 

1 - if i try to read ain0 and ain1 without connect my device i get

>>> import u3
>>> d = u3.U3()
>>> d.readRegister(0)
1.393296241760254
>>> d.readRegister(1)
1.3983793258666992
>>>

why i don't have 0 or 5V ? should i somehow initiate something before read or?

 

2 - I connect the device and i read again values and also move the resistors

 

on ain0 seems to have those values (resistor up and down) which seems normal i think 

>>> d.readRegister(0)
0.0005617141723632812
>>> d.readRegister(0)
4.656564712524414

on ain 1 i have this in same conditions

>>> d.readRegister(1)
4.656564712524414
>>> d.readRegister(1)
4.651481628417969

so it's act like i don't move the resistor. If i measure with digital voltmeter between gnd and ain1 i get correct values (about 0 and about 5v).

what could be the problem ?

 

thanks



#4 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 02 January 2014 - 03:20 PM

1.  No connection is not 0 volts, but rather undefined volts.  A floating reading is generally tough to predict and meaningless, but in the specific case of high-voltage channels on the U3-HV they sit at a predictable 1.4 volts with nothing connected.

 

http://labjack.com/s...s-guide/2.6.3.8

 

 

2.  Rather than your signal, try connecting a jumper wire to AIN1, and then connect the other end of the wire to GND or VS to see if you get 0V or ~5V.

 

There is a problem with your command.  Each analog input needs 2 registers to hold its 4-bytes of data, so the starting address for AIN1 is 2, not 1.  Try d.readRegister(2).  In the "Example" section on the following page see the second code box:

 

http://labjack.com/s...odbus/ud-modbus



#5 david_b

david_b
  • Members
  • 38 posts

Posted 03 January 2014 - 02:04 PM

I see,

 

Is there a "map" like ain0=0, ain1=2 etc...

 

ans also i want to set configurable pins to analog to do the same like ain0...

 

thanks



#6 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 03 January 2014 - 02:45 PM

Modbus on the U3 is using the old UD-Modbus map, which is not extensively documented.  There is a map at the bottom of the following page.  From this map, if a value uses 1 register, then the addresses increment by 1.  If a value uses 2 registers, then the addresses increment by 2.

 

http://labjack.com/s...odbus/ud-modbus

 

To read FIO4/AIN4, you just read from address 8, but you might get an error because that line is set to digital by default.  Try writing  value 255 to address 50590 to set all FIO to analog.



#7 david_b

david_b
  • Members
  • 38 posts

Posted 03 January 2014 - 02:52 PM

this mean that

ain0=0

ain1=2

ain2=4

ain3=6

?



#8 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 03 January 2014 - 02:55 PM

Yes, that is correct.  Just take the channel number x2.  So, for example, AIN14 starts at address 28.



#9 david_b

david_b
  • Members
  • 38 posts

Posted 03 January 2014 - 02:56 PM

so if i put all in analog then i will have only ain's right?



#10 david_b

david_b
  • Members
  • 38 posts

Posted 03 January 2014 - 03:23 PM

i connected a wire between ain0 and gnd/vs and i get this

>>> d.readRegister(0)
0.0005617141723632812
>>> d.readRegister(0)
5.027621269226074

i did the same for ain1 and get this

>>> d.readRegister(2)
0.00054931640625
>>> d.readRegister(2)
5.026224136352539

ain2

>>> d.readRegister(4)
-0.007281303405761719
>>> d.readRegister(4)
5.022006988525391

ain3

>>> d.readRegister(6)
-0.004519462585449219
>>> d.readRegister(6)
5.026250839233398

so till now seems fine



#11 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 03 January 2014 - 03:30 PM

so if i put all in analog then i will have only ain's right?

 

50590 is the address for the FIOAnalog bitmask.  The lower 8 bits control whether FIO0-7/AIN0-7 are set to analog (true=1) or digital (false=0).  On a U3-HV, the lowest 4 bits do not matter as AIN0-AIN3 are always analog on an HV, but for clarity it makes sense to always pass 1 for these bits.  The next 4 bits can be set however you want to control FIO4-7/AIN4-7.  For example, pass b00111111 (decimal 63) to set FIO4 & FIO5 to analog and FIO6 & FIO7 to digital.



#12 david_b

david_b
  • Members
  • 38 posts

Posted 17 January 2014 - 04:47 AM

Hi again,

 

I did some progress and now i have few specific questions

 

What i need to monitor with labjack is:

 

1 - 2 x digital inputs which i want a 0 if voltage is about 0 and i want 1 if voltage is above 5V

2 - 2 x digital inputs, where i will receive some impulse and i need to measure time between impulses

3 - 8 x analog inputs where i will monitor voltage between 0 and 5 V

4 - 4 x digital (i am not sure) where i want to monitor 4 buttons (if the button is pressed (circuit closed) i have 1 and 0 if circuit is open (button unpressed) )

5 - 5 x digital outputs where i will command 5 relays

 

Any guidance will be highly appreciated

 

thank you in advance



#13 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 17 January 2014 - 09:48 AM

I will start with hardware related things, then someone else can jump in if Python specific help is needed.

 

1.  Normal thresholds for digital inputs are <0.8V is low and >2.0V is high:

 

http://labjack.com/s...uide/appendix-a

 

What is your max voltage?

 

2.  What is the range of time between impulses ... the max and min period?  What is the min width of the impulse?

 

3.  The U3-HV has 4 analog inputs with a +/-10V range (AIN0-AIN3).  For more see:

 

http://labjack.com/s...s-guide/2.6.3.6

 

4.  Mechanical buttons/switches?  Do you just want to poll to determine if the switch is open or closed at any time, or do you need a way to catch a very brief button press and make sure you don't miss it?

 

5.  Solid-state relays?

 

http://labjack.com/s...trolling-relays



#14 david_b

david_b
  • Members
  • 38 posts

Posted 19 January 2014 - 06:47 AM

1 - max voltage is 5V

 

2 - i am not sure yet, i will came back after i will have some measurements

 

3 - so i will need to configure another 4 to be analog inputs

 

4 - i would rather prefer a way to catch the button press

 

5 - yes



#15 david_b

david_b
  • Members
  • 38 posts

Posted 20 January 2014 - 01:59 AM

I came back with answer for 2

 

is 17Hz impulse and i need to monitor it and take actions if it is increased or decreased with till 50%



#16 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 20 January 2014 - 03:37 PM

1.  Use any digital input.  Sounds straightforward:

 

http://labjack.com/s...s-guide/2.8.1.1

 

 

2.  I don't really understand what you are saying about the pulses.  What is the range of time between pulses?  That is, the max and min period?  What is the min width of the pulses?

 

 

3.  Yes, you will need to use some FIO or EIO as analog inputs, and you will need to divide down your signal using resistors or the LJTick-Divider.

 

 

4.  Sounds like you want to use a counter, or a timer configured in mode 5 or 6:

 

http://labjack.com/s...users-guide/2.9

 

http://labjack.com/s...s-guide/2.8.1.3



#17 david_b

david_b
  • Members
  • 38 posts

Posted 21 January 2014 - 02:47 AM

I will try to explain another way

 

I have 2 wheels and there is a device on each wheel. When the wheel speed is about 5km/h i get an impulse with 17hz frequency .  If the speed is increased then i will get more that 17hz, also i will get less if the speed is decreased. I need to monitor this and take some actions when the speed is changed.

 

I don t know if i am clear enough...



#18 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 21 January 2014 - 11:51 AM

2.  I suggest you use a timer configured for 32-bit Period Measurement (mode 2).



#19 david_b

david_b
  • Members
  • 38 posts

Posted 23 January 2014 - 06:23 AM

Thanks for the answers

do you thing this approach is ok ? and also if you have suggestion where i haven t filled up ?


1 - use FIO4 and FIO5

2 - use what

3 - use AIN0 / AIN1 / AIN2 / AIN3 / EIO0 / EIO1 / EIO2 / EIO3 / EIO4

4 - as far as i see l3 has only 2 timers and i need 5 buttons to monitor

5 - use EIO5 / EIO6 / EIO7 ...and 2 more?

Thanks again



#20 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 23 January 2014 - 04:02 PM

3.  Lets say you put the 8x 0-5V analog signals on AIN0-AIN3 and FIO4-FIO7.  FIO4-FIO7 will need divider resistors or you will need 2x LJTick-Divider-UNI10V.

 

2.  You want 2 timers using the 32-bit period input mode.  These could be on EIO0 & EIO1 if you set pinoffset=8.

 

1.  2x digital inputs.  Use EIO2 & EIO3.

 

4.  4x buttons.  Use EIO4-EIO7.

 

5.  5x digital outputs to control SSRs.  Use LJTick-RelayDrivers on CIO0-CIO3 for 4 SSRs, and use DAC0 to control 1 more SSR.

 

 

 

4.  You are correct that the U3 does not have enough timers/counters for everything you need, so I suggest you use normal digital inputs to monitor the buttons.  You can sample fast enough that you do not miss button presses, and you can also put a little RC circuit on the button signal to make it last longer if needed.

 

5.  You could control the SSRs directly with the digital outputs, but this will result in small errors on your analog input readings as mentioned in the Controlling Relays app note.  Thus I recommend you use 2x LJTick-RelayDrivers to avoid this.



#21 david_b

david_b
  • Members
  • 38 posts

Posted 02 February 2014 - 04:48 PM

Hi again

 

I managed to do dividers and also to get all devices connected. I made a schematic of how i connected them.

 

z913.png

 

 

 

Meanwhile as you can see  i replaced raspberry pi with a Intel Nuc :)

 

qvz6.jpg



#22 david_b

david_b
  • Members
  • 38 posts

Posted 03 February 2014 - 11:27 AM

i have a problem...

i tried python ljm and get this

[email protected]:/var/storage/Python_LJM# python
Python 2.7.3 (default, Jan  2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import labjack.ljm
<class 'labjack.ljm.ljm.LJMError'>: Cannot load the LJM library libLabJackM.so. libLabJackM.so: cannot open shared object file: No such file or directory
>>>



#23 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 03 February 2014 - 11:56 AM

I am not a Python expert, but realize that LJM is for the T7.  For the U3/U6/UE9 you want LabJackPython.

 

http://labjack.com/s...t/labjackpython



#24 david_b

david_b
  • Members
  • 38 posts

Posted 03 February 2014 - 11:58 AM

yeah.. now i saw me also... i will continue on the way i started



#25 david_b

david_b
  • Members
  • 38 posts

Posted 03 February 2014 - 12:07 PM

I have looked here

http://labjack.com/s...ers-guide/5.2.3

 

but i didn t understand how to set up a FIO to analog or digital, i mean i haven t catch the logic....

 

i want to set fio4-7 to analog

 

Is there other doc / explanation also?

 

thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users