Arbitrary Waveform Generation on T7 using Python and Lua | LabJack
 

Arbitrary Waveform Generation on T7 using Python and Lua

4 posts / 0 new
Last post
Joe Passman
jpassman's picture
Arbitrary Waveform Generation on T7 using Python and Lua

I am trying to generate arbitrary waveforms on the DAC channels using what I've learned from this post on outputting a triangle wave (https://labjack.com/support/software/examples/lua-scripting/output-trian...) and this script that describes how to execute Lua from Python (https://github.com/labjack/labjack-ljm-python/blob/master/Examples/More/...). 

Here is my general thought process:

  • Use Python to generate an array that represents the waveform
  • Insert the array into a Lua script template
  • Iterate over the array to write the output voltage to DAC0

I am starting by trying to make a sinewave using the above technique. I have attached the following

  1. Python scripts (I use a function repository that calls in the python functions you wrote for Lua)
  2. expected waveform plot
  3. data on LJLogM

I suspect that maybe I am not setting the LJ.CheckInterval correctly. I am able to create a DC waveform (attached).

Any help would be appreciated!

 

LabJack Support
labjack support's picture
I do not think your interval

I do not think your interval is correct. You want to achieve a waveform with some frequency, it looks like currently 1Hz, which we can manipulate to say we want a period of 1/frequency=1s. You are currently using 200 data points to represent your period,1s per 200 points is the same as (1/200)s per point. You would want an update interval of (1/200)s = 5ms. You are already close to set up to get everything working right, you just need to use (1/sample_rate) for your interval instead of len(sinewave).

I would also suggest you use numpy.linspace rather than numpy.arange since the numpy docs recommend using linspace when dealing with non-integer steps:

https://numpy.org/doc/stable/reference/generated/numpy.arange.html

Finally, I also might recommend you use stream out mode rather than loading a Lua script if all you need to do is generate a periodic waveform. Stream out mode could reach faster update rates than is possible with lua and you would be able to keep everything in Python. If you install our beta version of LJM and run our most recent LJM Python installer you could use our new PeriodicStreamOut function (there is a PeriodicStreamOut example under Examples/More/Stream in our newest Python archive):

https://labjack.com/support/software/installers/ljm

https://labjack.com/support/software/examples/ljm/python

 

Joe Passman
jpassman's picture
Thanks for the response! I

Thanks for the response! I prefer doing it in Lua because I can then easily monitor the waveform in LJLogM after running the Python script.

I modified and attached my Lua scripts and output. I am still struggling to get a nice waveform and am scratching my head. 


However, I did heed your suggestion to use LJM Python (attached). Your periodic stream example works basically out of the box with my desired signal. However, I am scratching my head on how to loop this script continuously and monitor it at the same time using multithreading. I don't own an oscilloscope, so it would be nice to create the waveform and monitor it using the T7.

I am a bit of a noob, so any further help is appreciated.

LabJack Support
labjack support's picture
It looks like you are

It looks like you are currently looping over your entire waveform each interval; with what you have now, you are writing out your loop data as quickly as the T7 allows every interval.  instead, you should output a single sample each interval, maybe progressing through your waveform list using a counter variable that increments each interval then loops back to zero once you reach the last element of your waveform.

I also realized that you could also do everything in Lua rather than using Python since our Lua VM supports a sine function. You can save your lua script to flash memory, so you do not need to manually load the script to the device each time you start using the device. The only downside is that this would not allow you to make use of matplotlib or any other nice Python tools if you have further plans for your project down the line.

You could easily use our Lua script debugger in Kipling to load and save scripts:

https://labjack.com/support/software/applications/t-series/kipling/lua-s...

PeriodicStreamOut will loop over your waveform data until you stop stream, so you would not need to run any loops continuously, you just need to stop stream whenever you want to stop outputting your waveform. Stream out will actually maintain your waveform in the output buffer until you explicitly disable stream out using the device register "STREAM_OUT#_ENABLE", so you could even stop stream and restart stream and your waveform should stop and restart outputting your data (as long as you keep STREAM_OUT# in your scanlist).

You should not necessarily need to monitor the waveform if you use stream out since the output is just looping over your pre-loaded waveform data continuously and this is done with hardware timing, so it is pretty stable output. The best way to monitor stream out would be to stream an AIN connected to the DAC output alongside stream-out and plot it in Python. In that case, you would need to maintain some loop to continuously make calls to eStreamRead to read the AIN.