lockinamp.cpp
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 //---------------------------------------------------------------------------
15 #include "ui_lockinampform.h"
16 #include "lockinamp.h"
17 #include "interface.h"
18 #include "analyzer.h"
19 #include "xnodeconnector.h"
20 #include <QStatusBar>
21 
22 XLIA::XLIA(const char *name, bool runtime,
23  Transaction &tr_meas, const shared_ptr<XMeasure> &meas) :
24  XPrimaryDriverWithThread(name, runtime, ref(tr_meas), meas),
25  m_valueX(create<XScalarEntry>("ValueX", false,
26  dynamic_pointer_cast<XDriver>(shared_from_this()))),
27  m_valueY(create<XScalarEntry>("ValueY", false,
28  dynamic_pointer_cast<XDriver>(shared_from_this()))),
29  m_output(create<XDoubleNode>("Output", false)),
30  m_frequency(create<XDoubleNode>("Frequency", false)),
31  m_sensitivity(create<XComboNode>("Sensitivity", false, true)),
32  m_timeConst(create<XComboNode>("TimeConst", false, true)),
33  m_autoScaleX(create<XBoolNode>("AutoScaleX", false)),
34  m_autoScaleY(create<XBoolNode>("AutoScaleY", false)),
35  m_fetchFreq(create<XDoubleNode>("FetchFreq", false)),
36  m_form(new FrmLIA(g_pFrmMain)) {
37  iterate_commit([=](Transaction &tr){
38  tr[ *fetchFreq()] = 1;
39  });
40 
41  meas->scalarEntries()->insert(tr_meas, m_valueX);
42  meas->scalarEntries()->insert(tr_meas, m_valueY);
43 
44  m_form->statusBar()->hide();
45  m_form->setWindowTitle(i18n("Lock-in-Amp - ") + getLabel() );
46 
47  m_output->setUIEnabled(false);
48  m_frequency->setUIEnabled(false);
49  m_sensitivity->setUIEnabled(false);
50  m_timeConst->setUIEnabled(false);
51  m_autoScaleX->setUIEnabled(false);
52  m_autoScaleY->setUIEnabled(false);
53  m_fetchFreq->setUIEnabled(false);
54 
55  m_conSens = xqcon_create<XQComboBoxConnector>(m_sensitivity, m_form->m_cmbSens, Snapshot( *m_sensitivity));
56  m_conTimeConst = xqcon_create<XQComboBoxConnector>(m_timeConst, m_form->m_cmbTimeConst, Snapshot( *m_timeConst));
57  m_conFreq = xqcon_create<XQLineEditConnector>(m_frequency, m_form->m_edFreq);
58  m_conOutput = xqcon_create<XQLineEditConnector>(m_output, m_form->m_edOutput);
59  m_conAutoScaleX = xqcon_create<XQToggleButtonConnector>(m_autoScaleX, m_form->m_ckbAutoScaleX);
60  m_conAutoScaleY = xqcon_create<XQToggleButtonConnector>(m_autoScaleY, m_form->m_ckbAutoScaleY);
61  m_conFetchFreq = xqcon_create<XQLineEditConnector>(m_fetchFreq, m_form->m_edFetchFreq);
62 }
63 
64 void
66 //! impliment form->show() here
67  m_form->showNormal();
68  m_form->raise();
69 }
70 
71 void
73  double x, y;
74  x = reader.pop<double>();
75  y = reader.pop<double>();
76  m_valueX->value(tr, x);
77  m_valueY->value(tr, y);
78 }
79 void
80 XLIA::visualize(const Snapshot &shot) {
81 }
82 
83 void
84 XLIA::onOutputChanged(const Snapshot &shot, XValueNodeBase *) {
85  try {
86  changeOutput(shot[ *output()]);
87  }
88  catch (XKameError& e) {
89  e.print(getLabel() + " " + i18n("Error while changing output, "));
90  return;
91  }
92 }
93 void
94 XLIA::onFreqChanged(const Snapshot &shot, XValueNodeBase *) {
95  try {
96  changeFreq(shot[ *frequency()]);
97  }
98  catch (XKameError& e) {
99  e.print(getLabel() + " " + i18n("Error while changing frequency, "));
100  return;
101  }
102 }
103 void
104 XLIA::onSensitivityChanged(const Snapshot &shot, XValueNodeBase *) {
105  try {
106  changeSensitivity(shot[ *sensitivity()]);
107  }
108  catch (XKameError& e) {
109  e.print(getLabel() + " " + i18n("Error while changing sensitivity, "));
110  return;
111  }
112 }
113 void
114 XLIA::onTimeConstChanged(const Snapshot &shot, XValueNodeBase *) {
115  try {
116  changeTimeConst(shot[ *timeConst()]);
117  }
118  catch (XKameError& e) {
119  e.print(getLabel() + " " + i18n("Error while changing time const., "));
120  return;
121  }
122 }
123 
124 void *
125 XLIA::execute(const atomic<bool> &terminated) {
126  m_output->setUIEnabled(true);
127  m_frequency->setUIEnabled(true);
128  m_sensitivity->setUIEnabled(true);
129  m_timeConst->setUIEnabled(true);
130  m_autoScaleX->setUIEnabled(true);
131  m_autoScaleY->setUIEnabled(true);
132  m_fetchFreq->setUIEnabled(true);
133 
134  iterate_commit([=](Transaction &tr){
135  m_lsnOutput = tr[ *output()].onValueChanged().connectWeakly(
136  shared_from_this(), &XLIA::onOutputChanged);
137  m_lsnFreq = tr[ *frequency()].onValueChanged().connectWeakly(
138  shared_from_this(), &XLIA::onFreqChanged);
139  m_lsnSens = tr[ *sensitivity()].onValueChanged().connectWeakly(
140  shared_from_this(), &XLIA::onSensitivityChanged);
141  m_lsnTimeConst = tr[ *timeConst()].onValueChanged().connectWeakly(
142  shared_from_this(), &XLIA::onTimeConstChanged);
143  });
144 
145  while( !terminated) {
146  double fetch_freq = ***fetchFreq();
147  double wait = 0;
148  if(fetch_freq > 0) {
149  sscanf(( **timeConst())->to_str().c_str(), "%lf", &wait);
150  wait *= 1000.0 / fetch_freq;
151  }
152  if(wait > 0) msecsleep(lrint(wait));
153 
154  double x, y;
155  XTime time_awared = XTime::now();
156  // try/catch exception of communication errors
157  try {
158  get(&x, &y);
159  }
160  catch (XKameError &e) {
161  e.print(getLabel() + " " + i18n("Read Error, "));
162  continue;
163  }
164  auto writer = std::make_shared<RawData>();
165  writer->push(x);
166  writer->push(y);
167  finishWritingRaw(writer, time_awared, XTime::now());
168  }
169  m_output->setUIEnabled(false);
170  m_frequency->setUIEnabled(false);
171  m_sensitivity->setUIEnabled(false);
172  m_timeConst->setUIEnabled(false);
173  m_autoScaleX->setUIEnabled(false);
174  m_autoScaleY->setUIEnabled(false);
175  m_fetchFreq->setUIEnabled(false);
176 
177  m_lsnOutput.reset();
178  m_lsnFreq.reset();
179  m_lsnSens.reset();
180  m_lsnTimeConst.reset();
181  return NULL;
182 }

Generated for KAME4 by  doxygen 1.8.3