Wait using s-function - matlab

I am using a s-function built in Simulink and I need to implement a waiting time. For example I need to do this :
send the first frame
wait 20 ms
send the second frame
wait 20 ms
send third frame
How could I establish this waiting time between 2 frames. I am using C language and a Level-1 Matlab S-function.

First of all, it is very difficult to get millisecond accuracy. it depends a lot on your hardware, OS, proccesses running..
you could try to achieve it by simply using the pause command
send the first frame
pause(0.020)
send the second frame
pause(0.020)
send third frame
or using timer objects http://www.mathworks.com/help/matlab/ref/timerclass.html
both solutions are not accurate. the best solution would be to base your timing in external events. is there any event triggered after sending each frame?

Related

Callbacks: Difference between DAQmxRegisterDoneEvent() and DAQmxEveryNSamplesEvent

Trying to figure out how callback wrappers are called specifically. Our code deals with a slowTask and an onTask. During a slowTask, I deal with the following two lines (specific to this question):
DAQmxCfgSampClkTiming(slowTask, "OnboardClock", GUI_RATE,
DAQmx_Val_Rising, DAQmx_Val_ContSamps, 1);
DAQmxRegisterEveryNSamplesEvent(slowTask, DAQmx_Val_Acquired_Into_Buffer, 1,
0, EveryNCallbackWrapper, this);
I understand that here, everytime the buffer fills up with one sample, EveryNCallbackWrapper will be called.
For an onTask, I have a hardtime understanding how the callback gets called. I consulted the NI documentation but couldn't quite get it.
DAQmxCfgSampClkTiming(onTask, "OnboardClock", ON_RATE, DAQmx_Val_Rising,
DAQmx_Val_FiniteSamps, 100);
DAQmxRegisterDoneEvent(onTask, 0, DoneCallbackWrapper, this);
This one boggles my mind a bit more. I believe that whenever onTask is triggered (with a hardware trigger), the DAQ starts taking and digitizing analog measurements at ON_RATE samples/second and once 100 samples are taken/read into the DAQs buffer, the DoneCallbackWrapper() is called. Depending on how long this hardware trigger stays high, this wrapper will be called every time the DAQ reads 100 samples (while trigger is high) OR will the callback be called only once after 100 samples were read?
The callback is called only once after 100 samples were read
Because slowTask uses DAQmx_Val_ContSamps, the program asks for an infinite (aka continuous) acquisition where data is streamed to the host. Using the EveryNSamples callback allows the program to access and process the newest data that was sent by the device.
Because onTask uses DAQmx_Val_FiniteSamps, the program asks for a single acquisition of 100 samples. Using the Done event allows the program to access and process the complete and full acquisition.
In your comment update, the program uses
DAQmxCfgDigEdgeStartTrig(onTask, "/PXI2Slot4/PXI_Trig0", DAQmx_Val_Rising));
to configure a digital edge start trigger for onTask. When that trigger line has a rising edge, the onTask acquisition begins, captures 100 samples, stops, and invokes the callback.
If the program needs to acquire 100 samples for onTask for every rising edge on /PXI2Slot4/PXI_Trig0, you can use the retriggerable property on the NI 63xx series devices that allows the same task to re-run for each trigger event.
More details are in the X Series User Manual:
The AI Start Trigger is also configurable as retriggerable. The timing engine generates the sample and convert clocks for the configured acquisition in response to each pulse on an AI Start Trigger signal.
The timing engine ignores the AI Start Trigger signal while the clock generation is in progress. After the clock generation is finished, the counter waits for another Start Trigger to begin another clock generation. Figure 4-22 shows a retriggerable analog input with three AI channels and four samples per trigger

how to store incoming data and then output it after a certain delay in simulink

I have an incoming data and I would like to store it and then Output this data but after a certain delay, after some milliseconds i Output this data.
I used the Queue block inside an enabled Subsystem and the Trigger Signal is the clock divided by 10, So i have evrery time .. every 0.1 second i Output the values from the block,.. but the data is accumulated, not delayed. any idea why?
Here is the Picture of this Operation
and
EDIT: You now show how are you storing the signal. And The queue block is used wrongly.
If you want just to delay a signal, and output it delayed, then use my answer below. I am unsure what you mean by store it by N time and then output it. Simulink is "continuous" thus you can not output it "in one go" after N time, that makes no sense. The closest thing to that is to delay the singal, and for that, you dont need that enabled subsystem, you just need the Transport delay block.
ORIGINAL
What about the Transport Delay block?
It looks like this:
and It allows you to set the delay time in seconds, isntead of in ticks (as z^-1 does).

How to run a multi-queue code using OpenCL?

For example, I'm doing some image processing work on every frame of a video.
Every frame's processing using 200ms including writing, processing and reading.
And the fps is 25, in that case every two frames' distance is 40ms. Then the processing is too slow to show continuous result.
So here is my idea, I use multi-queues for this work.
In CPU part,
while(video is not over)
{
1. read the frame0;
processing the **frame0** using **queue0**;
wait 40 ms;
2. read the frame1;
processing the frame1 using **queue1**;
wait 40 ms;
3.4.5.
...(after 5 frames(just about the 200ms's processing time))
6. download the **frame0**'s result.
7. read the frame5;
processing the frame5 using **queue0**;
wait 40 ms;
...
}
The code means that, I use different queues for reading and processing the same frame in a video.
However, my experiment result is faster, but just 2 times faster, but not in my imaginary speed.
Can anyone tell me how to deal it? THX!
Assuming you have one Device, here are some thoughts on this point:
Main reason to have multiple Command Queues (CQ) per single OpenCL Device is the ability to execute kernels & do IO operations simultaneously.
Usually one CQ is enought to load single Device at ~100%. Though, your multi-CQ idea is good (in my opinion), as you're constantly feeding GPU with workload.
Look at kernel execution time. May be, it's big enough, so that your Device is constantly executing kernels & can't go any faster.
I think, you don't need to wait for 40ms. Good solution is to process frames in queue, in which they are put to eliminate the difference between bitstream & display order.
If you have too many CQ, your OpenCL driver thread will be busy maintaining them, so that performance may decrease.

How can I parallelize input and display in MATLAB?

I'm using Psychtoolbox in MATLAB to run a behavioral psychology paradigm. As part of the paradigm, users have to view a visual stimulus and respond to it using some input mechanism. For a keyboard, this works as follows:
show stimulus
poll keyboard for response
if no response detected, loop back to 1
if response detected, break and move on with script
This works fine for a keyboard, as step 2 takes between 1-2 ms. The problem comes when I use an alternate input mechanism; in that case, step 2 takes ~20 ms. (I need this alternate input to run the study, and that should be considered immutable fact.) As the stimulus changes with a very short timespan, this added delay breaks the task.
My current thought is to try to use the parallel processing, such that one thread shows the stimulus, and another thread polls the keyboard. I'm currently using the Parallel Computing Toolbox to do this. The problem I'm having is that I don't know how to direct keyboard input to a "parallelized" thread. Does anyone know (1) whether it's possible to direct keyboard input to a thread / have a thread send a visual signal to a monitor, and if yes, (2) how to do it?
Also, if anyone has any better ideas as to how to approach this problem, I'm all ears.
According to this MATLAB newsgroup thread, it appears that threads can't modify graphics objects. Only the desktop MATLAB client can do that. This means that you can't handle updating of graphics from a thread, and I can confirm this as I tried it and wasn't able to modify figures or even the root object from a thread.
However, I think you may be able to do the main graphics updating in MATLAB while a thread handles polling for your input. Here's a sample function for continuously updating a display until a thread waiting for input from KbCheck is finished running:
function varargout = plot_until_input
obj = createJob(); %# Create a job
task = createTask(obj,#get_input,4,{deviceNumber}); %# Create a task
submit(obj); %# Submit the job
waitForState(task,'running'); %# Wait for the task to start running
%# Initialize your stimulus display here
while ~strcmp(get(task,'State'),'finished') %# Loop while the task is running
%# Update your stimulus display here
end
varargout = get(task,'OutputArguments'); %# Get the outputs from the task
destroy(obj); %# Remove the job from memory
%#---Nested functions below---
function [keyIsDown,secs,keyCode,deltaSecs] = get_input(deviceNumber)
keyIsDown = false;
while ~keyIsDown %# Keep looping until a key is pressed
[keyIsDown,secs,keyCode,deltaSecs] = KbCheck(deviceNumber);
end
end
end
I was able to successfully run the above function with some simple plotting routines and replacing the code in get_input with a simple pause statement and a return value. I'm unsure whether KbCheck will work in a thread, but hopefully you will be able to adapt this for your needs.
Here's the documentation for the Parallel Computing Toolbox functions used in the above code: createJob, createTask, submit, waitForState, destroy.
I don't know of a way how you could do this with parallel processing.
However, a feature you might be able to use is the timer object. You would set up the timer object to poll the input mechanism, and, if anything is detected, change the value of a global variable. Then, you start your stimulus routine. In the while-loop in which you're updating the display, you keep checking the global variable for a change from the timer object.
You have to tackle the 20ms latency in your input device. If it's too slow then get another input device. You can get good sub-millisecond timing with proper response boxes.
All this talk about threading is misguided and not applicable to the PTB framework.

Taking continuous inputs in MATLAB

I want to take data from bluetooth into Matlab every 10 to 15 seconds. I figured out a way to transfer data to Matlab via bluetooth. The problem I am facing is that I want Matlab to execute a set of commands after a time interval to take input from bluetooth. How can I do this?
You could create a TIMER OBJECT, that you set to execute a function that collects and processes data every 10 to 15 seconds.