QTimer working slow with BCI2000

Forum for discussion on different user applications
Locked
ksp6
Posts: 12
Joined: 22 Mar 2013, 11:29

QTimer working slow with BCI2000

Post by ksp6 » 22 Mar 2013, 12:10

Hi,

We are trying to study the changes in EEG power levels with different levels of Cognitive Load. For the same, I am trying to develop a new Application in BCI2000. It is the n-Back Test which is widely used to impose Working Memory Load on the subject.

So far I have developed the nBackTest same in C++ using the Qt framework and it works fine. You can check the video at http_nospam_youtu.be/pJvp2SNvOrk (watch in HD to see the text clearly)

I wanted to integrate the same in BCI2000 and I thought I would exploit class inheritance features in C++ for the same.
So in the BCI2000 ApplicationFilter, I inherit my nBackTest class, create objects of the same and call the respective methods. One of these
methods in the nBackTest class is QTimer Slot which is called every 500 ms to update the display and show characters.

However, I notice a significant Time lag in the timer slot function call when implemented with BCI2000. I have compiled BCI2000 using system Qt (v4.8.4) and I checked the operator source code which also uses QTimer slots to handle timing. You can check video at http_nospam_youtu.be/trg836Di8YY

I have tried QTimerEvent, Single Shot timer but could not resolve this issue. It would be great if someone could help me resolve this issue.

Regards,
Kedar.

mellinger
Posts: 1163
Joined: 12 Feb 2003, 11:06

Re: QTimer working slow with BCI2000

Post by mellinger » 22 Mar 2013, 13:12

Hi,

Qt timers run from the event loop of the thread they are created in. They are not executed in an asynchronous manner. Thus, they will only be executed as often as the main event loop is being called.

In a BCI2000 application module, the main thread is blocked until a packet of data arrives. Then, any processing such as video or audio output is done, and the thread is blocked again. From time to time, the main thread also checks whether there are any events pending in the Qt message queue, and will call Qt's pending event handler to handle them.

This blocking behavior of BCI2000 is on purpose. It ensures minimal time lag between reception of a data packet, and video resp. audio output.

If you need to do any processing that is not synchronized to BCI2000 data packets, then you will need to run your code in a separate thread, which you start from the task filter's StartRun() function, and stop from the task filter's StopRun() function.

HTH,
Juergen

ksp6
Posts: 12
Joined: 22 Mar 2013, 11:29

Re: QTimer working slow with BCI2000

Post by ksp6 » 22 Mar 2013, 14:03

Hi Juergen,

Thanks for your reply. I am using the StartRun() and StopRun() functions in BCI2000. Here's a short snippet.

Code: Select all

void CognitiveLoadTaskFilter::StartRun()
{
  // The user has just pressed "Start" (or "Resume")
  bciout << "Hello World!" << endl;
  // mTaskObj is an object of nBackTest Class which I inherit along with ApplicationBase
  mTaskObj->startTask();
}
Thanks.
-Kedar

gschalk
Posts: 615
Joined: 28 Jan 2003, 12:37

Re: QTimer working slow with BCI2000

Post by gschalk » 22 Mar 2013, 17:51

Hi,

What Juergen is saying is that it is not a good idea to do asynchronous sequencing (using some timer function), as functions in BCI2000 often need to be synchronized with the arrival of EEG blocks.

Thus, put your sequencing code in the Process() function, which is called every time a block of EEG arrives. Then, you won't need your own timer/callback/etc. functions.

Gerv

ksp6
Posts: 12
Joined: 22 Mar 2013, 11:29

Re: QTimer working slow with BCI2000

Post by ksp6 » 25 Mar 2013, 07:48

I get your point now. I synchronized with Process() and now it works fine. I also replaced the QTimer with QTime to handle timing.
Thanks for the help.

- Kedar.

Locked

Who is online

Users browsing this forum: No registered users and 1 guest