1
0
Fork 0

add ramping

This commit is contained in:
Peter McGoron 2021-05-29 17:44:14 -04:00
parent e5b64b695a
commit 2a933623f0
2 changed files with 76 additions and 16 deletions

View File

@ -1,12 +1,11 @@
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
gi.require_version("GLib", "2.0")
from gi.repository import Gtk, GLib
import serial
import serial.tools.list_ports
import re
CHANGE_BUTTONS = ["SET_do", "RAMP_do", "sel_ref", "sel_do"]
def error_popup(parent, msg):
box = Gtk.MessageDialog(
parent=parent,
@ -20,6 +19,12 @@ def error_popup(parent, msg):
box.show_all()
class SerComm:
def switch_buttons(self, b):
CHANGE_BUTTONS = ["SET_do", "RAMP_do", "sel_ref", "sel_do"]
for x in CHANGE_BUTTONS:
self.getobj(x).set_sensitive(b)
def error_popup(self, x):
error_popup(self.getobj("win"), x)
@ -29,12 +34,25 @@ class SerComm:
self.getobj("debug_win").scroll_to_mark(
o.get_insert(), 0.1, False, 0, 1)
def get(self):
s = self.ser.read_until()
if s is None:
return None
s = s.decode()
self.buf = self.buf + s.rstrip("\r\n")
if not s.endswith("\n"):
return None
s = self.buf
self.buf = ""
self.append(s + "\n")
return s
def get_or_to(self):
s = self.ser.read_until().decode()
if not s:
s = self.get()
if s is None:
raise Exception(f"DAC timeout: '{s}'")
s = s.rstrip("\r\n")
self.append(f'{s}\n')
return s
def send(self, s):
@ -43,16 +61,12 @@ class SerComm:
self.append(f'<span color="blue">{s}</span>\n')
def make_dac_conn(self, f):
for x in CHANGE_BUTTONS:
self.getobj(x).set_sensitive(False)
self.switch_buttons(False)
try:
f()
except Exception as e:
self.error_popup(str(e))
for x in CHANGE_BUTTONS:
self.getobj(x).set_sensitive(True)
self.switch_buttons(True)
""" make connection (*IDN?) """
def connect(self):
@ -101,6 +115,50 @@ class SerComm:
def on_SET(self, _):
self.make_dac_conn(self.do_SET)
""" RAMP """
def RAMP_callback(self, _):
s = self.get()
if s is None:
return GLib.SOURCE_CONTINUE
self.switch_buttons(True)
if s != "RAMP_FINISHED":
self.error_popup(f'DAC reported an error during ramping: {s}')
return GLib.SOURCE_REMOVE
def do_RAMP(self, _):
self.switch_buttons(False)
if not self.ser.is_open:
return None
ch1 = int(self.getobj("RAMP_ch1").get_active_id()) - 1
ch1_st = float(self.getobj("RAMP_st1").get_text())
ch1_ed = float(self.getobj("RAMP_ed1").get_text())
ch2 = self.getobj("RAMP_ch2").get_active_id()
if ch2 is not None:
ch2 = int(ch2) - 1
ch2_st = float(self.getobj("RAMP_st2").get_text())
ch2_ed = float(self.getobj("RAMP_ed2").get_text())
cmd = f"RAMP2,{ch1},{ch2},{ch1_st},{ch2_st},{ch1_ed},{ch2_ed}"
else:
cmd = f"RAMP1,{ch1},{ch1_st},{ch1_ed}"
steps = int(self.getobj("RAMP_step").get_text())
msec = int(self.getobj("RAMP_t").get_text())
cmd = cmd + f",{steps},{msec}"
self.send(cmd)
s = self.get_or_to()
if s != "RAMPING":
raise Exception(f"DAC reported an error: {s}")
"""TODO:
two timeouts? one for the time, and another every 200msec
"""
GLib.timeout_add(200, self.RAMP_callback, None)
def refresh(self, _):
# Does this cause a memory error? This is a GTK interned string
curid = self.sel_box.get_active_id()
@ -123,6 +181,7 @@ class SerComm:
self.getobj = getobj
self.ser = serial.Serial(timeout=3)
self.ser.baudrate = 115200
self.buf = ""
self.sel_box = getobj("sel_box")
@ -130,6 +189,7 @@ class SerComm:
self.refresh(None)
getobj("sel_do").connect("clicked", self.on_connect)
getobj("SET_do").connect("clicked", self.on_SET)
getobj("RAMP_do").connect("clicked", self.do_RAMP)
if __name__ == "__main__":
builder = Gtk.Builder()

View File

@ -17,7 +17,7 @@
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="title" translatable="yes">Seekat Control (Demo)</property>
<property name="title" translatable="yes">Seekat Control (prototype)</property>
<property name="show-close-button">True</property>
<child>
<!-- n-columns=4 n-rows=2 -->
@ -498,7 +498,7 @@
<object class="GtkEntry" id="RAMP_t">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="width-chars">4</property>
<property name="width-chars">7</property>
<property name="placeholder-text" translatable="yes">time</property>
<property name="input-purpose">number</property>
</object>
@ -512,7 +512,7 @@
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">milliseconds between each</property>
<property name="label" translatable="yes">microsconds between each</property>
</object>
<packing>
<property name="expand">False</property>