Sequence to write data from one channel to another under certain conditions


Olaf

Recommended Posts

Hello!

I am more or less new to DAQFactory (have done some simple data acquisition, but nothing complex yet).
After reading through many threads in this forum as well as spending some time with the user's guide, I still couldn't figure out how to do the following:
I want to read two channels from a Labjack UE9 with a frequency of 1000 Hz, so I think I should use the stream mode. So far, so good - acquiring the data at 1000 Hz works fine.
Now I would like to copy the values from one of these channels to one of two new channels (for this, I have created two V channels called "T_Pre" and "T_Post"), but only if the signal from the other UE9 channel is between two specific values (variables set by scrollbars). This must work during the ongoing stream acquisition. Only the values from the V channels have to be stored; the stream data can be discarded afterwards.
Here's what I came up with (and what does not work):

//initialization
using ("device.labjack")
include ("C:\Program Files (x86)\LabJack\Drivers\LabJackUD.h")
//setup stream, set scan rate
AddRequest(0, LJ_ioPUT_CONFIG, LJ_chSTREAM_SCAN_FREQUENCY, Var.scanrt, 0, 0)
var.streamtext="running..."
//clear channel history
UE9_12.ClearHistory()
UE9_11.ClearHistory(1)
//setup channels to stream
AddRequest(0, LJ_ioCLEAR_STREAM_CHANNELS, 0, 0, 0, 0)
AddRequest(0, LJ_ioADD_STREAM_CHANNEL, 12, 0, 0, 0)
AddRequest(0, LJ_ioADD_STREAM_CHANNEL, 11, 0, 0, 0)

//start the stream
GoOne(0)
var.streaming=1
//set variable "scanrate" to actual scanrate
global scanrate = 0
eGet (0, LJ_ioSTART_STREAM, 0, @scanrate, 0)
 
while(var.stop==0)
 
 //write data to VChannels, if input UE9_11 is between certain values
if (UE9_11[0]>var.kw1min)
    if (UE9_11[0]<var.kw1max)
       T_Pre[0]=UE9_12[0]
    endif
endif
 
if (UE9_11[0]>var.kw2min)
    if (UE9_11[0]<var.kw2max)
       T_Post[0]=UE9_12[0]
    endif
endif
 
endwhile
 
ePut(0, LJ_ioSTOP_STREAM, 0, 0, 0)
var.streaming=0
var.stop=0

The sequence is started by a button. Another Button changes "var.stop" to 1 if pressed, which stops the sequence as intended. The problem is probably somewhere in the "while" loop.

I'm quite sure that I made a simple beginner's mistake, so it would be very kind if anyone could point me in the right direction.

Thanks in advance!

Link to comment
Share on other sites

What you have won't work because of the way stream works.  Stream sends data in blocks, and so it is hard to process every data point as it comes in.  Also, your while() loop lacks a delay(), which will cause the CPU power to spike.  You should pretty much always have a delay() inside a while() loop.  There are exceptions, but this is a good general rule.

Now to your question, it is probably easier to just use the filter() function on the whole incoming data.  You can make a V channel that is a calculated v channel (meaning there is an Expression associated with it).  That expression would be something like:

filter(UE9_11, UE9_11 > var.kw1min)

Then whenever you reference that V channel it will perform the filter, which will remove all values that don't match the criteria.  Filter() is an internal function so is pretty darn fast.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.