QXRD  0.11.16
qxrdacquisitionextrainputs.cpp
Go to the documentation of this file.
1 #include "qxrddebug.h"
4 #include "qxrdacquisition.h"
5 #include "qcepmutexlocker.h"
7 #include "qcepimagedata.h"
8 #include "qcepsettingssaver.h"
11 #include <QTimer>
12 
14  QcepObject("extraInputs", NULL),
15  m_Enabled(QcepSettingsSaverPtr(), this, "enabled", 0, "Extra Inputs Enabled?"),
16  m_Skipping(QcepSettingsSaverPtr(), this, "skipping", 0, "Skipping initial readout?"),
17  m_SampleRate(saver, this, "sampleRate", 1000.0, "Sampling Rate for Extra Inputs"),
18  m_AcquireDelay(saver, this, "acquireDelay", 0.107, "Delay between exposure end and Image available in QXRD"),
19  m_ExposureTime(QcepSettingsSaverPtr(), this, "exposureTime", 0.107, "Exposure time (in seconds)"),
20  m_DeviceName(saver, this, "deviceName", "", "NI-DAQ Device Name"),
21  m_Experiment(doc),
22  m_Acquisition(acq),
23  m_Saver(saver),
24  m_Channels(),
26 {
28  printf("QxrdAcquisitionExtraInputs::QxrdAcquisitionExtraInputs(%p)\n", this);
29  }
30 
32 
33  if (acqp) {
34  m_NIDAQPlugin = acqp->nidaqPlugin();
35  }
36 
37  connect(prop_SampleRate(), &QcepDoubleProperty::valueChanged,
39 
40  if (acqp) {
41  connect(acqp->prop_ExposureTime(), &QcepDoubleProperty::valueChanged,
43  }
44 }
45 
47 {
48 }
49 
51 {
52 #ifndef QT_NO_DEBUG
53  printf("Deleting acquisition extra inputs\n");
54 #endif
55 
57  printf("QxrdAcquisitionExtraInputs::~QxrdAcquisitionExtraInputs(%p)\n", this);
58  }
59 }
60 
62 {
64 }
65 
67 {
68  return m_NIDAQPlugin;
69 }
70 
71 void QxrdAcquisitionExtraInputs::readSettings(QSettings *settings, QString section)
72 {
73  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
74 
75  QcepObject::readSettings(settings, section);
76 
77  int n = settings->beginReadArray(section+"/channels");
78 
79  while (m_Channels.count() > n) {
80  removeChannel();
81  }
82 
83  while (m_Channels.count() < n) {
84  appendChannel();
85  }
86 
87  for (int i=0; i<n; i++) {
88  settings->setArrayIndex(i);
89 
91 
92  if (chan) {
93  chan->readSettings(settings, "");
94  }
95  }
96 
97  settings->endArray();
98 
99  initiate();
100 }
101 
102 void QxrdAcquisitionExtraInputs::writeSettings(QSettings *settings, QString section)
103 {
104  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
105 
106  QcepObject::writeSettings(settings, section);
107 
108  settings->beginWriteArray(section+"/channels");
109 
110  int n = m_Channels.count();
111 
112  for (int i=0; i<n; i++) {
113  settings->setArrayIndex(i);
115 
116  if (chan) {
117  chan->writeSettings(settings, "");
118  }
119  }
120 
121  settings->endArray();
122 }
123 
124 void QxrdAcquisitionExtraInputs::printMessage(QString msg, QDateTime ts) const
125 {
127 
128  if (acq) {
129  acq->printMessage(msg, ts);
130  }
131 }
132 
133 void QxrdAcquisitionExtraInputs::criticalMessage(QString msg, QDateTime ts) const
134 {
136 
137  if (acq) {
138  acq->criticalMessage(msg, ts);
139  }
140 }
141 
142 void QxrdAcquisitionExtraInputs::statusMessage(QString msg, QDateTime ts) const
143 {
145 
146  if (acq) {
147  acq->statusMessage(msg, ts);
148  }
149 }
150 
152 {
153  if (!get_Enabled()) {
154  initiate();
155  }
156 }
157 
159 {
161 
162  if (acq && m_NIDAQPlugin) {
163  QStringList uniqueChannels;
164  QVector<double> channelMinimum, channelMaximum;
165 
167  if (chanp && chanp->get_Enabled()) {
168  QString channame = chanp->get_ChannelName();
169  double chanmin = chanp->get_Min();
170  double chanmax = chanp->get_Max();
171 
172  int physChan = uniqueChannels.indexOf(channame);
173 
174  if (physChan < 0) {
175  uniqueChannels.append(channame);
176  physChan = uniqueChannels.count() - 1;
177  channelMinimum.append(chanmin);
178  channelMaximum.append(chanmax);
179  } else {
180  channelMinimum[physChan] = qMin(channelMinimum[physChan], chanmin);
181  channelMaximum[physChan] = qMin(channelMaximum[physChan], chanmax);
182  }
183 
184  chanp->set_PhysicalChannel(physChan);
185  }
186  }
187 
188  if (m_NIDAQPlugin->prepareContinuousInput(get_SampleRate(),
189  get_AcquireDelay(),
190  acq->get_ExposureTime(),
191  uniqueChannels,
192  channelMinimum,
193  channelMaximum) == 0) {
194  set_Skipping(true);
195  set_ExposureTime(acq->get_ExposureTime());
196 
197  QTimer::singleShot(1000*(get_AcquireDelay() + acq->get_ExposureTime() + 1.0),
198  this, SLOT(timerDone()));
199 
200  set_Enabled(true);
201  }
202  }
203 }
204 
206 {
207  if (get_Enabled()) {
208  initiate();
209  }
210 }
211 
213 {
214  set_Skipping(false);
215 }
216 
217 QVector<QxrdAcquisitionExtraInputsChannelPtr> QxrdAcquisitionExtraInputs::channels() const
218 {
219  return m_Channels;
220 }
221 
223 {
224  return m_Channels.value(chan);
225 }
226 
228 {
230  int n = (ch < 0 ? m_Channels.size() : ch+1);
231 
232  m_Channels.insert(n,
234  chan = new QxrdAcquisitionExtraInputsChannel(n, m_Saver, m_Experiment, sharedFromThis())));
235 
237 
239 
240  if (saver) {
241  saver->changed();
242  }
243 
244  emit channelCountChanged();
245 }
246 
248 {
249  m_Channels.remove((ch < 0 ? m_Channels.size()-1 : ch));
250 
252 
253  if (saver) {
254  saver->changed();
255  }
256 
257  emit channelCountChanged();
258 }
259 
261 {
262  if (get_Enabled()) {
263  if (!get_Skipping()) {
265 
266  if (acq && m_NIDAQPlugin) {
267  m_NIDAQPlugin->readContinuousInput();
268  }
269 
271  if (chanp && chanp->get_Enabled()) {
272  chanp -> set_Value(
273  chanp -> evaluateChannel());
274 
275  if (acq && chanp->get_Triggered()) {
276  acq->set_Triggered(true);
277  }
278  }
279  }
280 
281  emit newDataAvailable();
282  }
283  }
284 }
285 
287 {
288  if (get_Enabled() && img) {
289  img->set_ExtraInputs(evaluateChannels().toList());
290  }
291 }
292 
294 {
296 
297  if (acq && m_NIDAQPlugin) {
298  m_NIDAQPlugin->finishContinuousInput();
299  }
300 
301  set_Enabled(false);
302 }
303 
305 {
306  QVector<double> res;
307 
308  for(int i=0; i<m_Channels.count(); i++) {
310 
311  if (chanp && chanp->get_Enabled()) {
312  double val = chanp->evaluateChannel();
313 
315  printMessage(tr("QxrdAcquisitionExtraInputs::evaluateChannels [%1] = %2").arg(i).arg(val));
316  }
317 
318  res.append(val);
319  }
320  }
321 
322  return res;
323 }
324 
326 {
328 
329  if (chanp) {
330  return chanp->evaluateChannel();
331  } else {
332  return 0;
333  }
334 }
335 
337 {
339 
340  if (chanp) {
341  QVector<double> res(chanp->readChannel().count());
342  double offset = get_AcquireDelay();
343  double rate = get_SampleRate();
344 
345  for (int i=0; i<res.count(); i++) {
346  res[i] = i/rate - offset;
347  }
348 
349  return res;
350  } else {
351  return QVector<double>();
352  }
353 }
354 
356 {
358 
359  if (chanp) {
360  return chanp->readChannel();
361  } else {
362  return QVector<double>();
363  }
364 }
365 
367 {
369 
370  if (chanp) {
371  return chanp->averageChannel();
372  } else {
373  return 0;
374  }
375 }
376 
378 {
379  QVector<double> res = readChannel(ch);
380 
381  double n=res.count(), sum=0;
382 
383  for(int i=0; i<n; i++) {
384  sum += res[i];
385  }
386 
387  return sum;
388 }
void readSettings(QSettings *settings, QString section)
QxrdAcquisitionExtraInputsChannelPtr channel(int chan) const
QxrdAcquisitionExtraInputs(QcepSettingsSaverWPtr saver, QxrdExperimentWPtr doc, QxrdAcquisitionWPtr acq)
qint64 qcepDebug(int cond)
Definition: qcepdebug.cpp:26
QVector< QxrdAcquisitionExtraInputsChannelPtr > m_Channels
QSharedPointer< QxrdAcquisition > QxrdAcquisitionPtr
virtual void readSettings(QSettings *set, QString section)
Definition: qcepobject.cpp:119
QxrdNIDAQPluginInterfacePtr nidaqPlugin() const
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime()) const
void setNIDAQPlugin(QxrdNIDAQPluginInterfacePtr nidaqPlugin)
void writeSettings(QSettings *settings, QString section)
QWeakPointer< QxrdExperiment > QxrdExperimentWPtr
void logToImage(QcepInt16ImageDataPtr img)
QVector< double > readChannel(int ch)
QSharedPointer< QxrdAcquisitionExtraInputsChannel > QxrdAcquisitionExtraInputsChannelPtr
void statusMessage(QString msg, QDateTime ts=QDateTime::currentDateTime()) const
QxrdNIDAQPluginInterfacePtr m_NIDAQPlugin
QSharedPointer< QcepSettingsSaver > QcepSettingsSaverPtr
QSharedPointer< QxrdNIDAQPluginInterface > QxrdNIDAQPluginInterfacePtr
virtual void writeSettings(QSettings *set, QString section)
Definition: qcepobject.cpp:114
QWeakPointer< QxrdAcquisition > QxrdAcquisitionWPtr
QWeakPointer< QxrdAcquisitionParameterPack > QxrdAcquisitionParameterPackWPtr
void valueChanged(double val, int index)
void prepareForAcquisition(QxrdAcquisitionParameterPackWPtr parms)
QVector< QxrdAcquisitionExtraInputsChannelPtr > channels() const
QSharedPointer< QcepInt16ImageData > QcepInt16ImageDataPtr
void criticalMessage(QString msg, QDateTime ts=QDateTime::currentDateTime()) const
QWeakPointer< QcepSettingsSaver > QcepSettingsSaverWPtr