qpidctrl  0.7.1
qpiddevice.cpp
Go to the documentation of this file.
1 #include "qpiddevice.h"
2 #include "qpidcontroller.h"
3 #include "qcepmutexlocker.h"
4 #include "qpiddebug.h"
5 #include "qpidcommport.h"
6 #include "qcepproperty.h"
7 #include "qpidlogger.h"
8 
10  QcepSettingsSaverWPtr saver,
11  int deviceType,
12  QString deviceName,
13  int pollIntervalMsec,
14  int timeoutMsec) :
15  QObject(NULL),
16  m_DeviceType(saver, this, "deviceType", deviceType, "Device Type"),
17  m_DeviceTypeName(saver, this, "deviceTypeName", QPidDevice::deviceTypeName(deviceType), "Device Type Name"),
18  m_DeviceLongTypeName(saver, this, "deviceLongTypeName", QPidDevice::deviceLongTypeName(deviceType), "Device Long Type Name"),
19  m_DeviceName(saver, this, "deviceName", deviceName, "Device Name"),
20  m_RunState(QcepSettingsSaverWPtr(), this, "runState", 0, "Run State"),
21  m_PollingInterval(saver, this, "pollingInterval", pollIntervalMsec/1000.0, "Polling Interval (sec)"),
22  m_TimeoutInterval(saver, this, "timeoutInterval", timeoutMsec/1000.0, "Timeout Interval (sec)"),
23  m_Controller(ctrl),
24  m_CommPort(new QPidCommPort(saver, ctrl)),
25  m_CommandMutex(QMutex::Recursive)
26 {
27  connect(prop_RunState(), SIGNAL(valueChanged(int,int)), this, SLOT(changeRunState(int)));
28  connect(prop_PollingInterval(), SIGNAL(valueChanged(double,int)), this, SLOT(changePollingInterval(double)));
29  connect(prop_TimeoutInterval(), SIGNAL(valueChanged(double,int)), this, SLOT(changeTimeoutInterval(double)));
30 
32 
33  if (cntrl) {
34  connect(cntrl->prop_GraphInterval(), SIGNAL(valueChanged(double,int)), this, SLOT(changeGraphingInterval(double)));
35  connect(cntrl->logger()->prop_UpdateInterval(), SIGNAL(valueChanged(double,int)), this, SLOT(changeLoggingInterval(double)));
36  }
37 
38  if (qcepDebug(DEBUG_CONSTRUCTORS)) {
39  printf("QPidDevice::QPidDevice(%p)\n", this);
40  }
41 }
42 
44 {
45  if (qcepDebug(DEBUG_CONSTRUCTORS)) {
46  printf("QPidDevice::~QPidDevice(%p)\n", this);
47  }
48 }
49 
51 {
52  QString res="Unk";
53 
54  switch (typ) {
55  case NoDevice:
56  res = "None";
57  break;
58 
59  case WatlowDevice:
60  res = "Watlow";
61  break;
62 
63  case PTC10Device:
64  res = "PTC10";
65  break;
66 
67  case LakeshoreDevice:
68  res = "LShore";
69  break;
70 
71  case CryostreamDevice:
72  res = "Crystm";
73  break;
74 
75  case EurothermDevice:
76  res = "Euthrm";
77  break;
78  }
79 
80  return res;
81 }
82 
84 {
85  QString res="Unk";
86 
87  switch (typ) {
88  case NoDevice:
89  res = "No Device";
90  break;
91 
92  case WatlowDevice:
93  res = "Watlow 988 Series Controller";
94  break;
95 
96  case PTC10Device:
97  res = "Stanford PTC10 Controller";
98  break;
99 
100  case LakeshoreDevice:
101  res = "Lakeshore Cryogenics LS3xx Controller";
102  break;
103 
104  case CryostreamDevice:
105  res = "Oxford Cryostream Controller";
106  break;
107 
108  case EurothermDevice:
109  res = "Eurotherm 2400 Series Controller";
110  break;
111  }
112 
113  return res;
114 }
115 
117 {
118  return 6;
119 }
120 
122 {
123  return m_CommPort;
124 }
125 
127 {
128  if (qcepDebug(DEBUG_PIDDEVICE)) {
129  printMessage(tr("QPidDevice::changeRunState(%1)").arg(state));
130  }
131 
132  if (state == true) {
133  start();
134  } else {
135  stop();
136  }
137 }
138 
140 {
142 
143  if (ctrl) {
144  connect(ctrl->prop_SetPoint(), SIGNAL(valueChanged(double,int)), this, SLOT(changeSetPoint(double)));
145  connect(ctrl->prop_RampRate(), SIGNAL(valueChanged(double,int)), this, SLOT(changeRampRate(double)));
146  connect(ctrl->prop_Proportional(), SIGNAL(valueChanged(double,int)), this, SLOT(changeProportional(double)));
147  connect(ctrl->prop_Integral(), SIGNAL(valueChanged(double,int)), this, SLOT(changeIntegral(double)));
148  connect(ctrl->prop_Derivative(), SIGNAL(valueChanged(double,int)), this, SLOT(changeDerivative(double)));
149  connect(ctrl->prop_Enabled(), SIGNAL(valueChanged(bool,int)), this, SLOT(enable(bool)));
150  connect(ctrl->prop_LoopEnabled(), SIGNAL(valueChanged(bool,int)), this, SLOT(loopEnable(bool)));
151  }
152 
153  readoutInitial();
154 
155 // QMetaObject::invokeMethod(this, "readoutInitial", Qt::QueuedConnection);
156 
157  connect(&m_PollingTimer, SIGNAL(timeout()), this, SLOT(onPollingTimeout()));
158 
159  if (get_PollingInterval() > 0) {
160  m_PollingTimer.setSingleShot(true);
161  m_PollingTimer.start(get_PollingInterval()*1000.0);
162  }
163 
164  connect(&m_TimeoutTimer, SIGNAL(timeout()), this, SLOT(onTimeoutTimeout()));
165 
166  if (get_TimeoutInterval() > 0) {
167  m_TimeoutTimer.setSingleShot(true);
168  }
169 
170  connect(&m_GraphingTimer, SIGNAL(timeout()), this, SLOT(onGraphingTimeout()));
171 
172  if (ctrl && ctrl->get_GraphInterval() > 0) {
173  m_GraphingTimer.setSingleShot(false);
174  m_GraphingTimer.start(ctrl->get_GraphInterval()*1000.0);
175  }
176 
177  connect(&m_LoggingTimer, SIGNAL(timeout()), this, SLOT(onLoggingTimeout()));
178 
179  QPidLoggerPtr logger(ctrl->logger());
180 
181  if (logger && logger->get_UpdateInterval() > 0) {
182  m_LoggingTimer.setSingleShot(false);
183  m_LoggingTimer.start(logger->get_UpdateInterval()*1000.0);
184  }
185 
186  connect(m_CommPort.data(), SIGNAL(readyRead()), this, SLOT(readyRead()));
187 }
188 
190 {
192 
193  if (ctrl) {
194  disconnect(ctrl->prop_SetPoint(), SIGNAL(valueChanged(double,int)), this, SLOT(changeSetPoint(double)));
195  disconnect(ctrl->prop_RampRate(), SIGNAL(valueChanged(double,int)), this, SLOT(changeRampRate(double)));
196  disconnect(ctrl->prop_Proportional(), SIGNAL(valueChanged(double,int)), this, SLOT(changeProportional(double)));
197  disconnect(ctrl->prop_Integral(), SIGNAL(valueChanged(double,int)), this, SLOT(changeIntegral(double)));
198  disconnect(ctrl->prop_Derivative(), SIGNAL(valueChanged(double,int)), this, SLOT(changeDerivative(double)));
199  disconnect(ctrl->prop_Enabled(), SIGNAL(valueChanged(bool,int)), this, SLOT(enable(bool)));
200  disconnect(ctrl->prop_LoopEnabled(), SIGNAL(valueChanged(bool,int)), this, SLOT(loopEnable(bool)));
201  }
202 
203  disconnect(&m_PollingTimer, SIGNAL(timeout()), this, SLOT(onPollingTimeout()));
204 
205  if (get_PollingInterval() > 0) {
206  m_PollingTimer.stop();
207  m_PollingTimer.setSingleShot(true);
208  }
209 
210  disconnect(&m_TimeoutTimer, SIGNAL(timeout()), this, SLOT(onTimeoutTimeout()));
211 
212  if (get_TimeoutInterval() > 0) {
213  m_TimeoutTimer.stop();
214  }
215 
216  disconnect(&m_GraphingTimer, SIGNAL(timeout()), this, SLOT(onGraphingTimeout()));
217 
218  if (ctrl && ctrl->get_GraphInterval() > 0) {
219  m_GraphingTimer.stop();
220  }
221 
222  disconnect(&m_LoggingTimer, SIGNAL(timeout()), this, SLOT(onLoggingTimeout()));
223 
224  QPidLoggerPtr logger(ctrl->logger());
225 
226  if (logger && logger->get_UpdateInterval() > 0) {
227  m_LoggingTimer.stop();
228  }
229 
230  disconnect(m_CommPort.data(), SIGNAL(readyRead()), this, SLOT(readyRead()));
231 }
232 
234 {
235  if (qcepDebug(DEBUG_PIDDEVICE)) {
236  printMessage(tr("Polling interval changed to %1 seconds").arg(interval));
237  }
238 
239  if (get_RunState()) {
240  if (m_PollingTimer.isActive()) {
241  if (interval > 0) {
242  m_PollingTimer.setInterval(interval*1000.0);
243  } else {
244  m_PollingTimer.stop();
245  }
246  } else if (interval > 0) {
247  m_PollingTimer.start(interval*1000.0);
248  }
249  }
250 }
251 
253 {
254  if (qcepDebug(DEBUG_PIDDEVICE)) {
255  printMessage(tr("Timeout interval changed to %1 seconds").arg(interval));
256  }
257 
258  if (get_RunState()) {
259  m_TimeoutTimer.setInterval(interval*1000.0);
260  }
261 }
262 
264 {
265  if (qcepDebug(DEBUG_PIDDEVICE)) {
266  printMessage(tr("Graphing interval changed to %1 seconds").arg(interval));
267  }
268 
269  if (get_RunState()) {
270  if (m_GraphingTimer.isActive()) {
271  if (interval > 0) {
272  m_GraphingTimer.setInterval(interval*1000.0);
273  } else {
274  m_GraphingTimer.stop();
275  }
276  } else if (interval > 0) {
277  m_GraphingTimer.start(interval*1000.0);
278  }
279  }
280 }
281 
283 {
284  if (qcepDebug(DEBUG_PIDDEVICE)) {
285  printMessage(tr("Logging interval changed to %1 seconds").arg(interval));
286  }
287 
288  if (get_RunState()) {
289  if (m_LoggingTimer.isActive()) {
290  if (interval > 0) {
291  m_LoggingTimer.setInterval(interval*1000.0);
292  } else {
293  m_LoggingTimer.stop();
294  }
295  } else if (interval > 0) {
296  m_LoggingTimer.start(interval*1000.0);
297  }
298  }
299 }
300 
302 {
303  stop();
304  start();
305 }
306 
307 int QPidDevice::slowly(int msec)
308 {
309  if (qcepDebug(DEBUG_SLOWLY)) {
310  return msec*20;
311  } else {
312  return msec;
313  }
314 }
315 
317 {
318  return m_Controller;
319 }
320 
321 void QPidDevice::printMessage(QString msg, QDateTime ts)
322 {
324 
325  if (ctl) {
326  ctl->printMessage(msg, ts);
327  }
328 }
329 
330 void QPidDevice::updateConnected(double newConnected)
331 {
333 
334  if (ctl) {
335  ctl->updateConnected(newConnected);
336  }
337 }
338 
339 void QPidDevice::updateTemperature(double newTemperature)
340 {
342 
343  if (ctl) {
344  ctl->updateTemperature(newTemperature);
345  }
346 }
347 
348 void QPidDevice::updateRampPoint(double newRampPoint)
349 {
351 
352  if (ctl) {
353  ctl->updateRampPoint(newRampPoint);
354  }
355 }
356 
357 void QPidDevice::updateOutputLevel(double newOutputLevel)
358 {
360 
361  if (ctl) {
362  ctl->updateOutputLevel(newOutputLevel);
363  }
364 }
365 
366 void QPidDevice::writeSettings(QSettings *set, QString section)
367 {
368  if (m_CommPort) {
369  m_CommPort -> writeSettings(set, section + "/commPort");
370  }
371 
372  QcepProperty::writeSettings(this, &staticMetaObject, section, set);
373 }
374 
375 void QPidDevice::readSettings(QSettings *set, QString section)
376 {
377  if (m_CommPort) {
378  m_CommPort -> readSettings(set, section + "/commPort");
379  }
380 
381  QcepProperty::readSettings(this, &staticMetaObject, section, set);
382 }
383 
384 double QPidDevice::cK2K(double cK)
385 {
386  return cK/100.0;
387 }
388 
389 double QPidDevice::K2cK(double K)
390 {
391  return K*100.0;
392 }
393 
394 double QPidDevice::K2degC(double K)
395 {
396  return K-273.15;
397 }
398 
399 double QPidDevice::degC2K(double degC)
400 {
401  return degC+273.15;
402 }
403 
405 {
406  if (qcepDebug(DEBUG_PIDDEVICE)) {
407  printMessage("resetTimeout()");
408  }
409 
410  m_TimeoutTimer.start(slowly(get_TimeoutInterval()*1000.0));
411 }
412 
414 {
415  if (qcepDebug(DEBUG_PIDDEVICE)) {
416  printMessage(tr("QPidDevice::onPollingTimeout"));
417  }
418 
419  readoutPolling();
420 
421  m_PollingTimer.start(slowly(get_PollingInterval()*1000.0));
422 }
423 
425 {
426  if (qcepDebug(DEBUG_PIDDEVICE)) {
427  printMessage(tr("QPidDevice::onTimeoutTimeout"));
428  }
429 
430  readoutTimeout();
431 
432  updateConnected(false);
433 
434  resetTimeout();
435 }
436 
438 {
439  QcepMutexLocker lock(__FILE__, __LINE__, &m_CommandMutex);
440 
441  m_CommandQueue.append(cmd);
442 
443  if (m_CommandQueue.count() == 1) {
444  issueCommand();
445  }
446 }
447 
449 {
450  QcepMutexLocker lock(__FILE__, __LINE__, &m_CommandMutex);
451 
452  while (m_CommandQueue.length() > 0) {
454 
455  if (cmd) {
456  cmd->issueCommand();
457 
458  if (cmd->replyExpected()) {
459  resetTimeout();
460  break;
461  } else {
462  QPidDeviceCommandPtr cmd = m_CommandQueue.takeFirst();
463 
464  delete cmd;
465  }
466  } else {
467  break;
468  }
469  }
470 }
471 
473 {
474  QcepMutexLocker lock(__FILE__, __LINE__, &m_CommandMutex);
475 
476  while (m_CommandQueue.length() > 0) {
477  QPidDeviceCommandPtr cmd = m_CommandQueue.takeFirst();
478 
479  delete cmd;
480  }
481 }
482 
483 void QPidDevice::completeCommand(QByteArray response)
484 {
485  QcepMutexLocker lock(__FILE__, __LINE__, &m_CommandMutex);
486 
487  if (m_CommandQueue.length() > 0) {
488  QPidDeviceCommandPtr cmd = m_CommandQueue.first();
489 
490  if (cmd) {
491  cmd->processReply(response);
492 
493  updateConnected(true);
494 
495  cmd = m_CommandQueue.takeFirst();
496  delete cmd;
497 
498  issueCommand();
499  }
500  }
501 }
502 
504 {
505  if (qcepDebug(DEBUG_PIDDEVICE)) {
506  printMessage(tr("QPidDevice::onGraphingTimeout"));
507  }
508 }
509 
511 {
512  if (qcepDebug(DEBUG_PIDDEVICE)) {
513  printMessage(tr("QPidDevice::onLoggingTimeout"));
514  }
515 }
516 
517 void QPidDevice::loopEnable(bool /*on*/)
518 {
519 }
520 
522 {
523 }
524 
525 void QPidDevice::write(QString cmd)
526 {
527  commPort()->write(qPrintable(cmd));
528 }
529 
530 QString QPidDevice::readLine(int sz)
531 {
532  return commPort()->readLine(sz);
533 }
534 
536 {
537  return commPort()->waitForReadyRead(tmout);
538 }
539 
541 {
542  return commPort()->bytesAvailable();
543 }
544 
546 {
547  return commPort()->canReadLine();
548 }
static int deviceTypeCount()
Definition: qpiddevice.cpp:116
virtual bool replyExpected() const =0
virtual void changeLoggingInterval(double interval)
Definition: qpiddevice.cpp:282
QString deviceLongTypeName
Definition: qpiddevice.h:130
virtual void write(QString cmd)
Definition: qpiddevice.cpp:525
virtual void readoutTimeout()=0
QPidControllerWPtr controller() const
Definition: qpiddevice.cpp:316
virtual void restart()
Definition: qpiddevice.cpp:301
QPidDeviceCommandQueue m_CommandQueue
Definition: qpiddevice.h:149
virtual void writeSettings(QSettings *set, QString section)
Definition: qpiddevice.cpp:366
void onPollingTimeout()
Definition: qpiddevice.cpp:413
QWeakPointer< QPidController > QPidControllerWPtr
void flushCommandQueue()
Definition: qpiddevice.cpp:472
QSharedPointer< QPidLogger > QPidLoggerPtr
Definition: qpidlogger-ptr.h:6
virtual ~QPidDevice()
Definition: qpiddevice.cpp:43
QTimer m_GraphingTimer
Definition: qpiddevice.h:153
QSharedPointer< QPidController > QPidControllerPtr
virtual void changeGraphingInterval(double interval)
Definition: qpiddevice.cpp:263
QTimer m_PollingTimer
Definition: qpiddevice.h:150
virtual void readoutPolling()=0
virtual int canReadLine()
Definition: qpiddevice.cpp:545
void updateRampPoint(double newRampPoint)
Definition: qpiddevice.cpp:348
int deviceType
Definition: qpiddevice.h:124
double K2cK(double K)
Definition: qpiddevice.cpp:389
virtual bool processReply(QString reply)=0
virtual int waitForReadyRead(int tmout=0)
Definition: qpiddevice.cpp:535
void issueCommand()
Definition: qpiddevice.cpp:448
virtual void enable(bool on)=0
virtual void changeRampRate(double rate)=0
virtual void changeIntegral(double integ)=0
QPidDevice(QPidControllerWPtr ctrl, QcepSettingsSaverWPtr saver, int deviceType, QString deviceName, int pollIntervalMsec, int timeoutMsec)
Definition: qpiddevice.cpp:9
virtual void onLoggingTimeout()
Definition: qpiddevice.cpp:510
virtual void loopEnable(bool on)
Definition: qpiddevice.cpp:517
void onTimeoutTimeout()
Definition: qpiddevice.cpp:424
virtual void readoutInitial()=0
virtual int bytesAvailable()
Definition: qpiddevice.cpp:540
QPidControllerWPtr m_Controller
Definition: qpiddevice.h:146
QPidCommPortPtr commPort()
Definition: qpiddevice.cpp:121
void resetTimeout()
Definition: qpiddevice.cpp:404
virtual void issueCommand()=0
double K2degC(double K)
Definition: qpiddevice.cpp:394
virtual void readyRead()
Definition: qpiddevice.cpp:521
int slowly(int msec)
Definition: qpiddevice.cpp:307
double degC2K(double degC)
Definition: qpiddevice.cpp:399
virtual void changeDerivative(double deriv)=0
virtual void changeTimeoutInterval(double interval)
Definition: qpiddevice.cpp:252
QString deviceTypeName
Definition: qpiddevice.h:127
virtual void readSettings(QSettings *set, QString section)
Definition: qpiddevice.cpp:375
virtual void start()
Definition: qpiddevice.cpp:139
void updateOutputLevel(double newOutputLevel)
Definition: qpiddevice.cpp:357
virtual void stop()
Definition: qpiddevice.cpp:189
QTimer m_TimeoutTimer
Definition: qpiddevice.h:151
void updateTemperature(double newTemperature)
Definition: qpiddevice.cpp:339
virtual QString readLine(int sz=0)
Definition: qpiddevice.cpp:530
virtual void changeRunState(int state)
Definition: qpiddevice.cpp:126
QTimer m_LoggingTimer
Definition: qpiddevice.h:152
double cK2K(double cK)
Definition: qpiddevice.cpp:384
virtual void changeSetPoint(double setPoint)=0
QMutex m_CommandMutex
Definition: qpiddevice.h:148
void completeCommand(QByteArray response)
Definition: qpiddevice.cpp:483
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
Definition: qpiddevice.cpp:321
void updateConnected(double newConnected)
Definition: qpiddevice.cpp:330
virtual void changeProportional(double prop)=0
QString deviceName
Definition: qpiddevice.h:133
virtual void changePollingInterval(double interval)
Definition: qpiddevice.cpp:233
void pushCommand(QPidDeviceCommandPtr cmd)
Definition: qpiddevice.cpp:437
virtual void onGraphingTimeout()
Definition: qpiddevice.cpp:503
QPidCommPortPtr m_CommPort
Definition: qpiddevice.h:147
QSharedPointer< QPidCommPort > QPidCommPortPtr