flowcontroller.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_flowcontrollerform.h"
16 #include "flowcontroller.h"
17 #include "interface.h"
18 #include "analyzer.h"
19 #include "xnodeconnector.h"
20 #include <QStatusBar>
21 
22 XFlowControllerDriver::XFlowControllerDriver(const char *name, bool runtime,
23  Transaction &tr_meas, const shared_ptr<XMeasure> &meas) :
24  XPrimaryDriverWithThread(name, runtime, ref(tr_meas), meas),
25  m_flow(create<XScalarEntry>("Flow", false,
26  dynamic_pointer_cast<XDriver>(shared_from_this()), "%.4f")),
27  m_target(create<XDoubleNode>("Target", true)),
28  m_valve(create<XDoubleNode>("Valve", true, "%.4f")),
29  m_rampTime(create<XDoubleNode>("RampTime", true)),
30  m_openValve(create<XTouchableNode>("OpenValve", true)),
31  m_closeValve(create<XTouchableNode>("CloseValve", true)),
32  m_warning(create<XBoolNode>("Warning", true)),
33  m_alarm(create<XBoolNode>("Alarm", true)),
34  m_control(create<XBoolNode>("Control", true)),
35  m_form(new FrmFlowController(g_pFrmMain)) {
36 
37  iterate_commit([=](Transaction &tr){
38  tr[ *control()] = false;
39  });
40 
41  meas->scalarEntries()->insert(tr_meas, m_flow);
42 
43  m_form->statusBar()->hide();
44  m_form->setWindowTitle(i18n("Flow Controller - ") + getLabel() );
45 
46  m_target->setUIEnabled(false);
47  m_rampTime->setUIEnabled(false);
48  m_openValve->setUIEnabled(false);
49  m_closeValve->setUIEnabled(false);
50  m_control->setUIEnabled(false);
51 
52  m_conFlow = xqcon_create<XQLCDNumberConnector>(m_flow->value(), m_form->m_lcdFlow);
53  m_conValve = xqcon_create<XQLCDNumberConnector>(m_valve, m_form->m_lcdValve);
54  m_form->m_dblTarget->setRange(0.0, 1000.0);
55  m_form->m_dblTarget->setSingleStep(0.1);
56  m_conTarget = xqcon_create<XQDoubleSpinBoxConnector>(m_target, m_form->m_dblTarget, m_form->m_slTarget);
57  m_conRampTime = xqcon_create<XQLineEditConnector>(m_rampTime, m_form->m_edRampTime);
58  m_conControl = xqcon_create<XQToggleButtonConnector>(m_control, m_form->m_ckbControl);
59  m_conAlarm = xqcon_create<XQLedConnector>(m_alarm, m_form->m_ledAlarm);
60  m_conWarning = xqcon_create<XQLedConnector>(m_warning, m_form->m_ledWarning);
61  m_conOpenValve = xqcon_create<XQButtonConnector>(m_openValve, m_form->m_btnOpenValve);
62  m_conCloseValve = xqcon_create<XQButtonConnector>(m_closeValve, m_form->m_btnCloseValve);
63 }
64 
65 void
67 //! impliment form->show() here
68  m_form->showNormal();
69  m_form->raise();
70 }
71 
72 void
74  double flow, valve;
75  flow = reader.pop<double>();
76  valve = reader.pop<double>();
77  bool alarm = reader.pop<uint16_t>();
78  bool warning = reader.pop<uint16_t>();
79  m_flow->value(tr, flow);
80  tr[ *m_valve] = valve;
81  tr[ *m_alarm] = alarm;
82  tr[ *m_warning] = warning;
83 }
84 void
86  if(m_form->m_lblUnit->text().isEmpty()) {
87  m_form->m_dblTarget->setRange(0.0, shot[ *this].m_fullScale);
88  m_form->m_lblUnit->setText(shot[ *this].m_unit);
89  }
90 }
91 
92 void
93 XFlowControllerDriver::onTargetChanged(const Snapshot &shot, XValueNodeBase *) {
94  try {
95  changeSetPoint(shot[ *target()]);
96  }
97  catch (XKameError& e) {
98  e.print(getLabel() + " " + i18n("Error while changing target, "));
99  return;
100  }
101 }
102 void
103 XFlowControllerDriver::onRampTimeChanged(const Snapshot &shot, XValueNodeBase *) {
104  try {
105  setRampTime(shot[ *rampTime()]);
106  }
107  catch (XKameError& e) {
108  e.print(getLabel() + " " + i18n("Error while changing conditions, "));
109  return;
110  }
111 }
112 void
113 XFlowControllerDriver::onOpenValveTouched(const Snapshot &shot, XTouchableNode *) {
114  try {
115  setValveState(true);
116  }
117  catch (XKameError& e) {
118  e.print(getLabel() + " " + i18n("Error while opening valve, "));
119  return;
120  }
121 }
122 void
123 XFlowControllerDriver::onCloseValveTouched(const Snapshot &shot, XTouchableNode *) {
124  try {
125  setValveState(false);
126  }
127  catch (XKameError& e) {
128  e.print(getLabel() + " " + i18n("Error while closeing valve, "));
129  return;
130  }
131 }
132 void
133 XFlowControllerDriver::onControlChanged(const Snapshot &shot, XValueNodeBase *) {
134  try {
135  changeControl(shot[ *control()]);
136  }
137  catch (XKameError& e) {
138  e.print(getLabel() + " " + i18n("Error, "));
139  return;
140  }
141 }
142 void *
143 XFlowControllerDriver::execute(const atomic<bool> &terminated) {
144  double fs;
145  bool unit_in_slm;
146  // try/catch exception of communication errors
147  try {
148  fs = getFullScale();
149  unit_in_slm = isUnitInSLM();
150  if(isController()) {
151  m_target->setUIEnabled(true);
152  m_rampTime->setUIEnabled(true);
153  m_openValve->setUIEnabled(true);
154  m_closeValve->setUIEnabled(true);
155  m_control->setUIEnabled(true);
156  }
157  else
158  m_valve->setUIEnabled(false);
159  }
160  catch (XKameError &e) {
161  e.print(getLabel() + " " + i18n("Read Error, "));
162  }
163 
164  iterate_commit([=](Transaction &tr){
165  tr[ *this].m_fullScale = fs;
166  tr[ *this].m_unit = unit_in_slm ? "SLM" : "SCCM";
167  m_lsnTarget = tr[ *target()].onValueChanged().connectWeakly(shared_from_this(), &XFlowControllerDriver::onTargetChanged);
168  m_lsnRampTime = tr[ *rampTime()].onValueChanged().connectWeakly(shared_from_this(), &XFlowControllerDriver::onRampTimeChanged);
169  m_lsnControl = tr[ *control()].onValueChanged().connectWeakly(shared_from_this(), &XFlowControllerDriver::onControlChanged);
170  m_lsnCloseValve = tr[ *closeValve()].onTouch().connectWeakly(shared_from_this(), &XFlowControllerDriver::onCloseValveTouched);
171  m_lsnOpenValve = tr[ *openValve()].onTouch().connectWeakly(shared_from_this(), &XFlowControllerDriver::onOpenValveTouched);
172  });
173 
174  while( !terminated) {
175  msecsleep(100);
176  XTime time_awared = XTime::now();
177  double flow = 0, valve = 0;
178  bool warning = false, alarm = false;
179  // try/catch exception of communication errors
180  try {
181  getStatus(flow, valve, alarm, warning);
182  }
183  catch (XKameError &e) {
184  e.print(getLabel() + " " + i18n("Read Error, "));
185  continue;
186  }
187  auto writer = std::make_shared<RawData>();
188  writer->push(flow);
189  writer->push(valve);
190  writer->push((uint16_t)alarm);
191  writer->push((uint16_t)warning);
192  finishWritingRaw(writer, time_awared, XTime::now());
193  }
194 
195  m_valve->setUIEnabled(false);
196  m_rampTime->setUIEnabled(false);
197  m_openValve->setUIEnabled(false);
198  m_closeValve->setUIEnabled(false);
199  m_control->setUIEnabled(false);
200 
201  m_lsnTarget.reset();
202  m_lsnRampTime.reset();
203  m_lsnControl.reset();
204  m_lsnCloseValve.reset();
205  m_lsnOpenValve.reset();
206  return NULL;
207 }

Generated for KAME4 by  doxygen 1.8.3