summaryrefslogtreecommitdiffstats
path: root/pressure_control/remote/pctl-remote
diff options
context:
space:
mode:
Diffstat (limited to 'pressure_control/remote/pctl-remote')
-rwxr-xr-xpressure_control/remote/pctl-remote171
1 files changed, 127 insertions, 44 deletions
diff --git a/pressure_control/remote/pctl-remote b/pressure_control/remote/pctl-remote
index 9dba6f2..e3d8dcf 100755
--- a/pressure_control/remote/pctl-remote
+++ b/pressure_control/remote/pctl-remote
@@ -49,10 +49,14 @@ MSG_SET_HYSTERESIS = 12
MSG_GET_CONFIG_FLAGS = 13
MSG_CONFIG_FLAGS = 14
MSG_SET_CONFIG_FLAGS = 15
+MSG_SET_VALVE = 16
# Message error codes
-MSG_ERR_NONE = 0
-MSG_ERR_CHKSUM = 1
+MSG_ERR_NONE = 0 # No error
+MSG_ERR_CHKSUM = 1 # Checksum error
+MSG_ERR_NOCMD = 2 # Unknown command
MSG_ERR_NOREPLY = -1 # internal. Not sent over wire.
+# Message flags
+MSG_FLAG_REQ_ERRCODE = 0
# Config flags
CFG_FLAG_AUTOADJUST_ENABLE = 0
@@ -92,7 +96,9 @@ def parseArgs():
class RemoteProtocol(QObject):
def __init__(self, ttyfile):
QObject.__init__(self)
+ global remote
+ remote = self
self.serial = Serial(ttyfile, CONFIG_BAUDRATE,
CONFIG_BYTESIZE, CONFIG_PARITY,
CONFIG_STOPBITS)
@@ -101,7 +107,7 @@ class RemoteProtocol(QObject):
self.connect(self.pollTimer, SIGNAL("timeout()"), self.poll)
self.pollTimer.start(50)
- reply = self.sendMessageSyncReply(MSG_PING, "", MSG_PONG)
+ reply = self.sendMessageSyncReply(MSG_PING, 0, "", MSG_PONG)
if not reply:
print "Communication with device failed. No reply to PING request."
sys.exit(1)
@@ -127,22 +133,22 @@ class RemoteProtocol(QObject):
if not self.checksumMessage(msg):
return
id = ord(msg[0])
+ mainwnd.statusBar().showMessage(self.tr("Received message %u" % id))
if (id == MSG_LOGMESSAGE):
str = self.getPayload(msg).rstrip('\0')
mainwnd.centralWidget().log.addText(str)
- if (id == MSG_PING):
- sendMessage(MSG_PING, [])
if (id == MSG_CURRENT_PRESSURE):
mainwnd.centralWidget().parseCurrentPressureMsg(msg)
+ #TODO
def getPayload(self, msg):
return msg[4:-2]
- def sendMessage(self, id, payload):
+ def sendMessage(self, id, flags, payload):
"""Send a message"""
assert(len(payload) <= MSG_PAYLOAD_SIZE)
# Create the header
- msg = "%c\0\0\0" % id
+ msg = "%c%c\0\0" % (id, flags)
# Add the payload
msg += payload
# Pad the payload up to the constant size
@@ -158,19 +164,19 @@ class RemoteProtocol(QObject):
# Send the message
assert(len(msg) == MSG_SIZE)
self.serial.write(msg)
+ mainwnd.statusBar().showMessage(self.tr("Sent message %u" % id))
- def sendMessageSyncReply(self, id, payload, replyId):
+ def sendMessageSyncReply(self, id, flags, payload, replyId):
"""Send a message and synchronously wait for the reply."""
self.pollTimer.stop()
- self.sendMessage(id, payload)
+ self.sendMessage(id, flags, payload)
timeout = QDateTime.currentDateTime().addSecs(2)
while True:
if QDateTime.currentDateTime() >= timeout:
msg = None
break
if self.serial.inWaiting() < MSG_SIZE:
- app.processEvents()
- QThread.msleep(50)
+ QThread.msleep(1)
continue
msg = self.serial.read(MSG_SIZE)
if not self.checksumMessage(msg):
@@ -183,13 +189,40 @@ class RemoteProtocol(QObject):
self.pollTimer.start()
return msg
- def sendMessageSyncError(self, id, payload):
+ def sendMessageSyncError(self, id, flags, payload):
"""Sends a message and synchronously waits for the MSG_ERROR reply."""
- reply = self.sendMessageSyncReply(id, payload, MSG_ERROR)
+ flags |= (1 << MSG_FLAG_REQ_ERRCODE)
+ reply = self.sendMessageSyncReply(id, flags, payload, MSG_ERROR)
if not reply:
return MSG_ERR_NOREPLY
return ord(self.getPayload(reply)[0])
+ def configFlagsFetch(self):
+ reply = self.sendMessageSyncReply(MSG_GET_CONFIG_FLAGS, 0, "",
+ MSG_CONFIG_FLAGS)
+ if not reply:
+ return None
+ reply = remote.getPayload(reply)
+ flags = ord(reply[0]) | (ord(reply[1]) << 8) | \
+ (ord(reply[2]) << 16) | (ord(reply[3]) << 24)
+ return flags
+
+ def configFlagsSet(self, flags):
+ data = "%c%c%c%c" % ((flags & 0xFF), ((flags >> 8) & 0xFF),
+ ((flags >> 16) & 0xFF), ((flags >> 24) & 0xFF))
+ err = self.sendMessageSyncError(MSG_SET_CONFIG_FLAGS, 0, data)
+ return err
+
+ def setValve(self, valveNr, state):
+ data = "%c%c" % (valveNr, (state != 0))
+ i = 5 # Retry a few times
+ while i != 0:
+ err = self.sendMessageSyncError(MSG_SET_VALVE, 0, data)
+ if err == MSG_ERR_NONE:
+ break
+ i -= 1
+ return err
+
def __crc16_update_buffer(self, crc, buf):
for c in buf:
crc ^= ord(c)
@@ -202,7 +235,7 @@ class RemoteProtocol(QObject):
class StatusBar(QStatusBar):
def showMessage(self, msg):
- QStatusBar.showMessage(self, msg, 10000)
+ QStatusBar.showMessage(self, msg, 3000)
class LogBrowser(QTextEdit):
def __init__(self, parent=None):
@@ -239,12 +272,22 @@ class MainWidget(QWidget):
h.addStretch()
layout.addLayout(h)
- self.autoCheckbox = QCheckBox(self.tr("Automatically adjust pressure"), self)
- layout.addWidget(self.autoCheckbox)
+ h = QHBoxLayout()
+ label = QLabel(self.tr("Hysteresis:"), self)
+ h.addWidget(label)
+ self.hystSpin = QDoubleSpinBox(self)
+ self.hystSpin.setMinimum(0.1)
+ self.hystSpin.setMaximum(8)
+ self.hystSpin.setSingleStep(0.1)
+ self.hystSpin.setSuffix(self.tr(" Bar"))
+ self.connect(self.hystSpin, SIGNAL("valueChanged(double)"),
+ self.desiredHysteresisChanged)
+ h.addWidget(self.hystSpin)
+ h.addStretch()
+ layout.addLayout(h)
h = QHBoxLayout()
label = QLabel(self.tr("Desired pressure:"), self)
- h.addStretch()
h.addWidget(label)
self.pressureSpin = QDoubleSpinBox(self)
self.pressureSpin.setMinimum(1)
@@ -254,30 +297,37 @@ class MainWidget(QWidget):
self.connect(self.pressureSpin, SIGNAL("valueChanged(double)"),
self.desiredPressureChanged)
h.addWidget(self.pressureSpin)
+ self.autoCheckbox = QCheckBox(self.tr("Automatically adjust pressure"), self)
+ self.connect(self.autoCheckbox, SIGNAL("stateChanged(int)"),
+ self.autoadjustChanged)
+ h.addWidget(self.autoCheckbox)
+ h.addStretch()
layout.addLayout(h)
h = QHBoxLayout()
- label = QLabel(self.tr("Hysteresis:"), self)
- h.addStretch()
- h.addWidget(label)
- self.hystSpin = QDoubleSpinBox(self)
- self.hystSpin.setMinimum(0.1)
- self.hystSpin.setMaximum(8)
- self.hystSpin.setSingleStep(0.1)
- self.hystSpin.setSuffix(self.tr(" Bar"))
- self.connect(self.hystSpin, SIGNAL("valueChanged(double)"),
- self.desiredHysteresisChanged)
- h.addWidget(self.hystSpin)
+ self.inButton = QPushButton(self.tr("IN-Valve"), self)
+ self.connect(self.inButton, SIGNAL("pressed()"),
+ self.inValvePressed)
+ self.connect(self.inButton, SIGNAL("released()"),
+ self.inValveReleased)
+ h.addWidget(self.inButton)
+ self.outButton = QPushButton(self.tr("OUT-Valve"), self)
+ h.addWidget(self.outButton)
+ self.connect(self.outButton, SIGNAL("pressed()"),
+ self.outValvePressed)
+ self.connect(self.outButton, SIGNAL("released()"),
+ self.outValveReleased)
layout.addLayout(h)
self.log = LogBrowser(self)
layout.addWidget(self.log)
+ self.autoadjustChanged(Qt.Unchecked)
self.setLayout(layout)
def initializeState(self):
# Get the current pressure
- reply = remote.sendMessageSyncReply(MSG_GET_CURRENT_PRESSURE, "",
+ reply = remote.sendMessageSyncReply(MSG_GET_CURRENT_PRESSURE, 0, "",
MSG_CURRENT_PRESSURE)
if not reply:
print "Failed to fetch current pressure. No reply."
@@ -285,7 +335,7 @@ class MainWidget(QWidget):
self.parseCurrentPressureMsg(reply)
# Get the desired pressure
- reply = remote.sendMessageSyncReply(MSG_GET_DESIRED_PRESSURE, "",
+ reply = remote.sendMessageSyncReply(MSG_GET_DESIRED_PRESSURE, 0, "",
MSG_DESIRED_PRESSURE)
if not reply:
print "Failed to fetch desired pressure. No reply."
@@ -295,7 +345,7 @@ class MainWidget(QWidget):
self.pressureSpin.setValue(float(mbar) / 1000)
# Get the hysteresis
- reply = remote.sendMessageSyncReply(MSG_GET_HYSTERESIS, "",
+ reply = remote.sendMessageSyncReply(MSG_GET_HYSTERESIS, 0, "",
MSG_HYSTERESIS)
if not reply:
print "Failed to fetch hysteresis. No reply."
@@ -305,17 +355,12 @@ class MainWidget(QWidget):
self.hystSpin.setValue(float(mbar) / 1000)
# Get the config flags
- reply = remote.sendMessageSyncReply(MSG_GET_CONFIG_FLAGS, "",
- MSG_CONFIG_FLAGS)
- if not reply:
+ flags = remote.configFlagsFetch()
+ if flags == None:
print "Failed to fetch config flags. No reply."
sys.exit(1)
- reply = remote.getPayload(reply)
- flags = ord(reply[0]) | (ord(reply[1]) << 8) | \
- (ord(reply[2]) << 16) | (ord(reply[3]) << 24)
if flags & (1 << CFG_FLAG_AUTOADJUST_ENABLE):
self.autoCheckbox.setCheckState(Qt.Checked)
- #TODO
self.initialized = True
@@ -324,24 +369,62 @@ class MainWidget(QWidget):
mbar = ord(msg[0]) | (ord(msg[1]) << 8)
self.curPressure.display(float(mbar) / 1000)
- def desiredPressureChanged(self):
+ def desiredPressureChanged(self, value):
if not self.initialized:
return
- mbar = int(self.pressureSpin.value() * 1000)
+ mbar = int(value * 1000)
data = "%c%c" % ((mbar & 0xFF), ((mbar >> 8) & 0xFF))
- err = remote.sendMessageSyncError(MSG_SET_DESIRED_PRESSURE, data)
+ err = remote.sendMessageSyncError(MSG_SET_DESIRED_PRESSURE, 0, data)
if err != MSG_ERR_NONE:
self.log.addText(self.tr("Failed to change pressure. Error=%u\n" % err))
- def desiredHysteresisChanged(self):
+ def desiredHysteresisChanged(self, value):
if not self.initialized:
return
- mbar = int(self.hystSpin.value() * 1000)
+ mbar = int(value * 1000)
data = "%c%c" % ((mbar & 0xFF), ((mbar >> 8) & 0xFF))
- err = remote.sendMessageSyncError(MSG_SET_HYSTERESIS, data)
+ err = remote.sendMessageSyncError(MSG_SET_HYSTERESIS, 0, data)
if err != MSG_ERR_NONE:
self.log.addText(self.tr("Failed to change hysteresis. Error=%u\n" % err))
+ def autoadjustChanged(self, state):
+ self.pressureSpin.setEnabled(state == Qt.Checked)
+ self.inButton.setEnabled(state == Qt.Unchecked)
+ self.outButton.setEnabled(state == Qt.Unchecked)
+ if not self.initialized:
+ return
+ flags = remote.configFlagsFetch()
+ if flags == None:
+ self.log.addText(self.tr("Failed to fetch config flags\n"))
+ return
+ if state == Qt.Checked:
+ flags |= (1 << CFG_FLAG_AUTOADJUST_ENABLE)
+ else:
+ flags &= ~(1 << CFG_FLAG_AUTOADJUST_ENABLE)
+ err = remote.configFlagsSet(flags)
+ if err != MSG_ERR_NONE:
+ self.log.addText(self.tr("Failed to set config flags\n"))
+
+ def inValvePressed(self):
+ err = remote.setValve(0, 1)
+ if err != MSG_ERR_NONE:
+ self.log.addText(self.tr("Failed to switch valve 0 ON\n"))
+
+ def inValveReleased(self):
+ err = remote.setValve(0, 0)
+ if err != MSG_ERR_NONE:
+ self.log.addText(self.tr("Failed to switch valve 0 OFF\n"))
+
+ def outValvePressed(self):
+ err = remote.setValve(1, 1)
+ if err != MSG_ERR_NONE:
+ self.log.addText(self.tr("Failed to switch valve 1 ON\n"))
+
+ def outValveReleased(self):
+ err = remote.setValve(1, 0)
+ if err != MSG_ERR_NONE:
+ self.log.addText(self.tr("Failed to switch valve 1 OFF\n"))
+
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
bues.ch cgit interface