tempcontrol.h
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 #ifndef tempcontrolH
15 #define tempcontrolH
16 //---------------------------------------------------------------------------
17 #include "thermometer.h"
18 #include "dcsource.h"
19 #include "flowcontroller.h"
20 #include "primarydriverwiththread.h"
21 #include "xnodeconnector.h"
22 
23 class XScalarEntry;
24 class Ui_FrmTempControl;
26 
28 public:
29  XTempControl(const char *name, bool runtime, Transaction &tr_meas, const shared_ptr<XMeasure> &meas);
30  //! usually nothing to do
31  virtual ~XTempControl() {}
32  //! show all forms belonging to driver
33  virtual void showForms();
34 
35  class XChannel : public XNode {
36  public:
37  XChannel(const char *name, bool runtime,
38  Transaction &tr_list, const shared_ptr<XThermometerList> &list);
39  const shared_ptr<XItemNode<XThermometerList, XThermometer> > &thermometer() const {return m_thermometer;}
40  const shared_ptr<XThermometerList> &thermometers() const {return m_thermometers;}
41  const shared_ptr<XComboNode> &excitation() const {return m_excitation;}
42  private:
43  const shared_ptr<XItemNode<XThermometerList, XThermometer> > m_thermometer;
44  const shared_ptr<XComboNode> m_excitation;
45  const shared_ptr<XThermometerList> m_thermometers;
46  };
47 
49 
50  const shared_ptr<XChannelList> &channels() const {return m_channels;}
51  //! LOOPs
52  unsigned int numOfLoops() const {return m_loops.size();}
53  const shared_ptr<XItemNode<XChannelList, XChannel> > &currentChannel(unsigned int lp) const {return loop(lp)->m_currentChannel;}
54  const shared_ptr<XDoubleNode> &targetTemp(unsigned int lp) const {return loop(lp)->m_targetTemp;}
55  const shared_ptr<XDoubleNode> &manualPower(unsigned int lp) const {return loop(lp)->m_manualPower;}
56  const shared_ptr<XDoubleNode> &prop(unsigned int lp) const {return loop(lp)->m_prop;}
57  const shared_ptr<XDoubleNode> &interval(unsigned int lp) const {return loop(lp)->m_int;}
58  const shared_ptr<XDoubleNode> &deriv(unsigned int lp) const {return loop(lp)->m_deriv;}
59  const shared_ptr<XComboNode> &heaterMode(unsigned int lp) const {return loop(lp)->m_heaterMode;}
60  const shared_ptr<XComboNode> &powerRange(unsigned int lp) const {return loop(lp)->m_powerRange;}
61  const shared_ptr<XDoubleNode> &heaterPower(unsigned int lp) const {return loop(lp)->m_heaterPower;}
62  const shared_ptr<XDoubleNode> &sourceTemp(unsigned int lp) const {return loop(lp)->m_sourceTemp;}
63  const shared_ptr<XDoubleNode> &powerMax(unsigned int lp) const {return loop(lp)->m_powerMax;}
64  const shared_ptr<XDoubleNode> &powerMin(unsigned int lp) const {return loop(lp)->m_powerMin;}
65  //! holds an averaged error between target temp and actual one
66  const shared_ptr<XDoubleNode> &stabilized(unsigned int lp) const {return loop(lp)->m_stabilized;}
67  //! PID control of an external device.
68  const shared_ptr<XItemNode<XDriverList, XDCSource, XFlowControllerDriver> > &extDevice(unsigned int lp) const {return loop(lp)->m_extDevice;}
69  const shared_ptr<XComboNode> &extDCSourceChannel(unsigned int lp) const {return loop(lp)->m_extDCSourceChannel;}
70  const shared_ptr<XBoolNode> &extIsPositive(unsigned int lp) const {return loop(lp)->m_extIsPositive;}
71 
72 protected:
73  //! This function will be called when raw data are written.
74  //! Implement this function to convert the raw data to the record (Payload).
75  //! \sa analyze()
76  virtual void analyzeRaw(RawDataReader &reader, Transaction &tr) throw (XRecordError&);
77  //! This function is called after committing XPrimaryDriver::analyzeRaw() or XSecondaryDriver::analyze().
78  //! This might be called even if the record is invalid (time() == false).
79  virtual void visualize(const Snapshot &shot);
80 
81  //! Prepares channel names in your constructor.
82  //! \param multiread if true, simultaneous reading of multi channels.
83  //! \param channel_names array of pointers to channel name. ends with null pointer.
84  void createChannels(Transaction &tr, const shared_ptr<XMeasure> &meas,
85  bool multiread, std::initializer_list<XString> channel_names,
86  std::initializer_list<XString> excitations,
87  std::initializer_list<XString> loop_names);
88 
89  //! reads sensor value from the instrument
90  virtual double getRaw(shared_ptr<XChannel> &channel) = 0;
91  //! reads a value in Kelvin from the instrument
92  virtual double getTemp(shared_ptr<XChannel> &channel) = 0;
93  //! obtains current heater power
94  //! \sa m_heaterPowerUnit()
95  virtual double getHeater(unsigned int loop) = 0;
96  //! ex. "W", "dB", or so
97  virtual const char *m_heaterPowerUnit(unsigned int loop) = 0;
98 
99  bool hasExtDevice(const Snapshot &shot, unsigned int lp) const {return loop(lp)->hasExtDevice(shot);}
100 
101  virtual void onPChanged(unsigned int loop, double p) = 0;
102  virtual void onIChanged(unsigned int loop, double i) = 0;
103  virtual void onDChanged(unsigned int loop, double d) = 0;
104  virtual void onTargetTempChanged(unsigned int loop, double temp) = 0;
105  virtual void onManualPowerChanged(unsigned int loop, double pow) = 0;
106  virtual void onHeaterModeChanged(unsigned int loop, int mode) = 0;
107  virtual void onPowerRangeChanged(unsigned int loop, int range) = 0;
108  virtual void onPowerMaxChanged(unsigned int loop, double v) = 0;
109  virtual void onPowerMinChanged(unsigned int loop, double v) = 0;
110  virtual void onCurrentChannelChanged(unsigned int loop, const shared_ptr<XChannel> &ch) = 0;
111 
112  virtual void onExcitationChanged(const shared_ptr<XChannel> &ch, int exc) = 0;
113 private:
114  shared_ptr<XChannelList> m_channels;
115  //! LOOPs
116  class Loop : public XNode {
117  public:
118  Loop(const char *name, bool runtime, shared_ptr<XTempControl>, Transaction &tr,
119  unsigned int idx, Transaction &tr_meas, const shared_ptr<XMeasure> &meas);
120 
121  weak_ptr<XTempControl> m_tempctrl;
122  const unsigned int m_idx;
123  shared_ptr<XItemNode<XChannelList, XChannel> > m_currentChannel;
124  const shared_ptr<XDoubleNode> m_targetTemp;
125  const shared_ptr<XDoubleNode> m_manualPower;
126  const shared_ptr<XDoubleNode> m_prop, m_int, m_deriv;
127  const shared_ptr<XComboNode> m_heaterMode;
128  const shared_ptr<XComboNode> m_powerRange;
129  const shared_ptr<XDoubleNode> m_powerMax, m_powerMin;
130  const shared_ptr<XDoubleNode> m_heaterPower, m_sourceTemp;
131  //! holds an averaged error between target temp and actual one
132  const shared_ptr<XDoubleNode> m_stabilized;
133 
134  const shared_ptr<XItemNode<XDriverList, XDCSource, XFlowControllerDriver> > m_extDevice;
135  const shared_ptr<XComboNode> m_extDCSourceChannel;
136  const shared_ptr<XBoolNode> m_extIsPositive;
137 
138  void start();
139  void stop();
140  void update(double temp);
141  double pid(const Snapshot &shot, XTime time, double temp);
142 
143  bool hasExtDevice(const Snapshot &shot) const {
144  return shared_ptr<XDCSource>(shot[ *m_extDevice]) ||
145  shared_ptr<XFlowControllerDriver>(shot[ *m_extDevice]);
146  }
147 
148  void onPChanged(const Snapshot &shot, XValueNodeBase *);
149  void onIChanged(const Snapshot &shot, XValueNodeBase *);
150  void onDChanged(const Snapshot &shot, XValueNodeBase *);
151  void onTargetTempChanged(const Snapshot &shot, XValueNodeBase *);
152  void onManualPowerChanged(const Snapshot &shot, XValueNodeBase *);
153  void onHeaterModeChanged(const Snapshot &shot, XValueNodeBase *);
154  void onPowerRangeChanged(const Snapshot &shot, XValueNodeBase *);
155  void onPowerMaxChanged(const Snapshot &shot, XValueNodeBase *);
156  void onPowerMinChanged(const Snapshot &shot, XValueNodeBase *);
157  void onCurrentChannelChanged(const Snapshot &shot, XValueNodeBase *);
158  void onExtDeviceChanged(const Snapshot &shot, XValueNodeBase *);
159 
160  std::deque<xqcon_ptr> m_conUIs;
161 
162  shared_ptr<XListener> m_lsnOnPChanged, m_lsnOnIChanged, m_lsnOnDChanged,
163  m_lsnOnTargetTempChanged, m_lsnOnManualPowerChanged, m_lsnOnHeaterModeChanged,
164  m_lsnOnPowerMaxChanged, m_lsnOnPowerMinChanged,
165  m_lsnOnPowerRangeChanged, m_lsnOnCurrentChannelChanged,
166  m_lsnOnSetupChannelChanged, m_lsnOnExtDeviceChanged;
167 
168  double m_pidAccum;
169  double m_pidLastTemp;
170  XTime m_pidLastTime;
171 
172  double m_tempAvg;
173  double m_tempErrAvg;
174  XTime m_lasttime;
175  };
176  std::deque<shared_ptr<Loop> > m_loops;
177  shared_ptr<Loop> loop(unsigned int lp) {return m_loops.at(lp);}
178  const shared_ptr<Loop> loop(unsigned int lp) const {return m_loops.at(lp);}
179 
180  shared_ptr<XItemNode<XChannelList, XChannel> > m_setupChannel;
181 
182  shared_ptr<XListener> m_lsnOnSetupChannelChanged, m_lsnOnExcitationChanged;
183 
184  void onSetupChannelChanged(const Snapshot &shot, XValueNodeBase *);
185  void onExcitationChangedInternal(const Snapshot &shot, XValueNodeBase *);
186 
187  std::deque<shared_ptr<XScalarEntry> > m_entry_temps;
188  std::deque<shared_ptr<XScalarEntry> > m_entry_raws;
189 
190  const qshared_ptr<FrmTempControl> m_form;
191  bool m_multiread;
192 
193  xqcon_ptr m_conSetupChannel,
194  m_conExcitation, m_conThermometer;
195 
196  void *execute(const atomic<bool> &);
197 
198 };
199 
200 #endif

Generated for KAME4 by  doxygen 1.8.3