MNE-CPP  beta 1.0
neuromag.cpp
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "neuromag.h"
42 #include "neuromagproducer.h"
43 
45 
46 #include <utils/ioutils.h>
47 
48 
49 //*************************************************************************************************************
50 //=============================================================================================================
51 // QT INCLUDES
52 //=============================================================================================================
53 
54 #include <QtCore/QtPlugin>
55 #include <QtCore/QTextStream>
56 #include <QtCore/QFile>
57 
58 #include <QList>
59 #include <QDebug>
60 
61 
62 //*************************************************************************************************************
63 //=============================================================================================================
64 // USED NAMESPACES
65 //=============================================================================================================
66 
67 using namespace MneRtClientPlugin;
68 using namespace UTILSLIB;
69 
70 
71 //*************************************************************************************************************
72 //=============================================================================================================
73 // DEFINE MEMBER METHODS
74 //=============================================================================================================
75 
77 : m_sNeuromagClientAlias("mne-x")
78 , m_pRtCmdClient(NULL)
79 , m_bCmdClientIsConnected(false)
80 , m_sNeuromagIP("172.21.16.88")//("127.0.0.1")
81 , m_pNeuromagProducer(new NeuromagProducer(this))
82 , m_iBufferSize(-1)
83 , m_pRawMatrixBuffer_In(0)
84 , m_bIsRunning(false)
85 {
86 
87 }
88 
89 
90 //*************************************************************************************************************
91 
93 {
94  if(m_pNeuromagProducer->isRunning() || this->isRunning())
95  stop();
96 }
97 
98 
99 //*************************************************************************************************************
100 
101 QSharedPointer<IPlugin> Neuromag::clone() const
102 {
103  QSharedPointer<Neuromag> pNeuromagClone(new Neuromag());
104  return pNeuromagClone;
105 }
106 
107 
108 //*************************************************************************************************************
109 
111 {
112  // Start NeuromagProducer
113  m_pNeuromagProducer->start();
114 
115  //init channels when fiff info is available
116  connect(this, &Neuromag::fiffInfoAvailable, this, &Neuromag::initConnector);
117 
118  //Try to connect the cmd client on start up using localhost connection
119  this->connectCmdClient();
120 }
121 
122 
123 //*************************************************************************************************************
124 
126 {
127 
128 }
129 
130 
131 //*************************************************************************************************************
132 //=============================================================================================================
133 // Create measurement instances and config them
134 //=============================================================================================================
135 
136 void Neuromag::initConnector()
137 {
138  if(m_pFiffInfo)
139  {
140  m_pRTMSA_Neuromag = PluginOutputData<NewRealTimeMultiSampleArray>::create(this, "RtClient", "MNE Rt Client");
141 
142  m_pRTMSA_Neuromag->data()->initFromFiffInfo(m_pFiffInfo);
143  m_pRTMSA_Neuromag->data()->setMultiArraySize(1);
144 
145  m_pRTMSA_Neuromag->data()->setVisibility(true);
146 
147  m_pRTMSA_Neuromag->data()->setXMLLayoutFile("./mne_x_plugins/resources/Neuromag/VectorViewLayout.xml");
148 
149  m_outputConnectors.append(m_pRTMSA_Neuromag);
150  }
151 
152 }
153 
154 
155 //*************************************************************************************************************
156 
157 void Neuromag::changeConnector(qint32 p_iNewConnectorId)
158 {
159  if(p_iNewConnectorId != m_iActiveConnectorId)
160  {
161  // read meas info
162  (*m_pRtCmdClient)["selcon"].pValues()[0].setValue(p_iNewConnectorId);
163  (*m_pRtCmdClient)["selcon"].send();
164 
165  m_iActiveConnectorId = p_iNewConnectorId;
166 
167  // clear all and request everything new
168  clear();
169 
170  //
171  // request available commands
172  //
173  m_pRtCmdClient->requestCommands();
174 
175  //
176  // Read Info
177  //
178  if(!m_pFiffInfo)
179  requestInfo();
180 
181  //
182  // Read Buffer Size
183  //
184  m_iBufferSize = m_pRtCmdClient->requestBufsize();
185 
186  emit cmdConnectionChanged(m_bCmdClientIsConnected);
187  }
188 }
189 
190 
191 //*************************************************************************************************************
192 
194 {
195  m_pFiffInfo.reset();
196  m_iBufferSize = -1;
197 }
198 
199 
200 //*************************************************************************************************************
201 
203 {
204  if(m_pRtCmdClient.isNull())
205  m_pRtCmdClient = QSharedPointer<RtCmdClient>(new RtCmdClient);
206  else if(m_bCmdClientIsConnected)
207  this->disconnectCmdClient();
208 
209  m_pRtCmdClient->connectToHost(m_sNeuromagIP);
210  m_pRtCmdClient->waitForConnected(1000);
211 
212  if(m_pRtCmdClient->state() == QTcpSocket::ConnectedState)
213  {
214  rtServerMutex.lock();
215 
216  if(!m_bCmdClientIsConnected)
217  {
218  //
219  // request available commands
220  //
221  m_pRtCmdClient->requestCommands();
222 
223  //
224  // set cmd client is connected
225  //
226  m_bCmdClientIsConnected = true;
227 
228  //
229  // Read Info
230  //
231  if(!m_pFiffInfo)
232  requestInfo();
233 
234  //
235  // Read Connectors
236  //
237  if(m_qMapConnectors.size() == 0)
238  m_iActiveConnectorId = m_pRtCmdClient->requestConnectors(m_qMapConnectors);
239 
240  QMap<qint32, QString>::const_iterator it;
241  for(it = m_qMapConnectors.begin(); it != m_qMapConnectors.end(); ++it)
242  if(it.value().compare("Neuromag Connector") == 0 && m_iActiveConnectorId != it.key())
243  changeConnector(it.key());
244 
245  //
246  // Read Buffer Size
247  //
248  m_iBufferSize = m_pRtCmdClient->requestBufsize();
249 
250  emit cmdConnectionChanged(m_bCmdClientIsConnected);
251  }
252  rtServerMutex.unlock();
253  }
254 }
255 
256 
257 //*************************************************************************************************************
258 
260 {
261  if(m_bCmdClientIsConnected)
262  {
263  m_pRtCmdClient->disconnectFromHost();
264  m_pRtCmdClient->waitForDisconnected();
265  rtServerMutex.lock();
266  m_bCmdClientIsConnected = false;
267  rtServerMutex.unlock();
268  emit cmdConnectionChanged(m_bCmdClientIsConnected);
269  }
270 }
271 
272 
273 //*************************************************************************************************************
274 
276 {
277  while(!(m_pNeuromagProducer->m_iDataClientId > -1 && m_bCmdClientIsConnected))
278  qWarning() << "NeuromagProducer is not running! Retry...";
279 
280  if(m_pNeuromagProducer->m_iDataClientId > -1 && m_bCmdClientIsConnected)
281  {
282  // read meas info
283  (*m_pRtCmdClient)["measinfo"].pValues()[0].setValue(m_pNeuromagProducer->m_iDataClientId);
284  (*m_pRtCmdClient)["measinfo"].send();
285 
286  m_pNeuromagProducer->producerMutex.lock();
287  m_pNeuromagProducer->m_bFlagInfoRequest = true;
288  m_pNeuromagProducer->producerMutex.unlock();
289  }
290  else
291  qWarning() << "NeuromagProducer is not connected!";
292 }
293 
294 
295 //*************************************************************************************************************
296 
298 {
299  //Check if the thread is already or still running. This can happen if the start button is pressed immediately after the stop button was pressed. In this case the stopping process is not finished yet but the start process is initiated.
300  if(this->isRunning())
301  QThread::wait();
302 
303  if(m_bCmdClientIsConnected && m_pFiffInfo)
304  {
305  // Initialize real time measurements
306  init();
307 
308  //Set buffer size
309  (*m_pRtCmdClient)["bufsize"].pValues()[0].setValue(m_iBufferSize);
310  (*m_pRtCmdClient)["bufsize"].send();
311 
312  // Buffer
313  m_pRawMatrixBuffer_In = QSharedPointer<RawMatrixBuffer>(new RawMatrixBuffer(8,m_pFiffInfo->nchan,m_iBufferSize));
314 
315  m_bIsRunning = true;
316 
317  // Start threads
318  QThread::start();
319 
320  m_pNeuromagProducer->start();
321 
322  while(!m_pNeuromagProducer->m_bFlagMeasuring)
323  msleep(1);
324 
325  // Start Measurement at rt_Server
326  // start measurement
327  (*m_pRtCmdClient)["start"].pValues()[0].setValue(m_pNeuromagProducer->m_iDataClientId);
328  (*m_pRtCmdClient)["start"].send();
329 
330  return true;
331  }
332  else
333  return false;
334 }
335 
336 
337 //*************************************************************************************************************
338 
340 {
341  if(m_pNeuromagProducer->isRunning())
342  m_pNeuromagProducer->stop();
343 
344  //Wait until this thread (TMSI) is stopped
345  m_bIsRunning = false;
346 
347  if(this->isRunning())
348  {
349  //In case the semaphore blocks the thread -> Release the QSemaphore and let it exit from the pop function (acquire statement)
350  m_pRawMatrixBuffer_In->releaseFromPop();
351 
352  m_pRawMatrixBuffer_In->clear();
353 
354  m_pRTMSA_Neuromag->data()->clear();
355  }
356 
357  return true;
358 }
359 
360 
361 //*************************************************************************************************************
362 
364 {
365  return _ISensor;
366 }
367 
368 
369 //*************************************************************************************************************
370 
371 QString Neuromag::getName() const
372 {
373  return "Neuromag";
374 }
375 
376 
377 //*************************************************************************************************************
378 
380 {
381  NeuromagSetupWidget* widget = new NeuromagSetupWidget(this);//widget is later distroyed by CentralWidget - so it has to be created everytime new
382 
383  return widget;
384 }
385 
386 
387 //*************************************************************************************************************
388 
390 {
391 
392  MatrixXf matValue;
393  while(m_bIsRunning)
394  {
395  //pop matrix
396  matValue = m_pRawMatrixBuffer_In->pop();
397 
398  //emit values
399  m_pRTMSA_Neuromag->data()->setValue(matValue.cast<double>());
400  }
401 }
OutputConnectorList m_outputConnectors
Definition: IPlugin.h:222
Real-time command client.
Definition: rtcmdclient.h:86
virtual QSharedPointer< IPlugin > clone() const
Definition: neuromag.cpp:101
The NeuromagSetupWidget class provides the ECG configuration window.
virtual QString getName() const
Definition: neuromag.cpp:371
void cmdConnectionChanged(bool p_bStatus)
Contains the declaration of the NeuromagProducer class.
static QSharedPointer< PluginOutputData< T > > create(IPlugin *parent, const QString &name, const QString &descr)
The NeuromagProducer class provides a Rt Client data producer for a given sampling rate...
virtual IPlugin::PluginType getType() const
Definition: neuromag.cpp:363
void changeConnector(qint32 p_iNewConnectorId)
Definition: neuromag.cpp:157
Contains the declaration of the NeuromagSetupWidget class.
IOUtils class declaration.
virtual QWidget * setupWidget()
Definition: neuromag.cpp:379