Compare commits

..

No commits in common. "f66afc0c8fd1989f4a0928740f96a29aa81a48bf" and "85d2afbdd0ae5be00af852ebf120ca01a8179313" have entirely different histories.

6 changed files with 253 additions and 347 deletions

View file

@ -15,10 +15,6 @@ import (
"k8s.io/klog"
)
type httpServer struct {
d daemonController
}
var (
//go:embed index.html
templateIndexText string
@ -27,7 +23,7 @@ var (
favicon []byte
)
func (s *httpServer) viewFavicon(w http.ResponseWriter, r *http.Request) {
func (d *daemon) httpFavicon(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.Write(favicon)
}
@ -54,103 +50,45 @@ func formatMbar(v float32) template.HTML {
return template.HTML(res)
}
// apiData is the data model served to the user via HTTP/WebSockets
type apiData struct {
// Safety interlocks.
Safety struct {
// Failsafe mode enabled - pirani gauge seems disconnected.
Failsafe bool
// HighPressure interlock enabled - pressure too high to run diffusion
// pump.
HighPressure bool
}
// Pirani gauge data.
Pirani struct {
// Volts read from the gauge (0-10).
Volts string
// Mbar read from the gauge, formatted as HTML.
Mbar template.HTML
// MbarFloat read from the gauge.
MbarFloat float32
}
// Pump state.
Pumps struct {
// RPOn means the roughing pump is turned on.
RPOn bool
// DPOn means the diffusion pump is turned on.
DPOn bool
}
// Pressure feedback into evacuation board.
Feedback struct {
// RoughReached is true when the system has reached a rough vacuum
// stage.
RoughReached bool
// HighReached is true when the system has reached a high vacuum stage.
HighReached bool
}
// System junk.
System struct {
// Load of the system.
Load string
// Hostname of the system.
Hostname string
}
}
// apiData returns the user data model for the current state of the system. If
// skipSystem is set, the System subset is ignored (saves system load, and is
// not being served via websockets).
func (s *httpServer) apiData(skipSystem bool) *apiData {
state := s.d.snapshot()
volts, mbar := state.pirani()
rough, high := state.vacuumStatus()
var hostname, load string
var err error
if !skipSystem {
hostname, err = os.Hostname()
if err != nil {
hostname = "unknown"
}
loadB, err := os.ReadFile("/proc/loadavg")
load = "unknown"
if err == nil {
parts := strings.Fields(string(loadB))
load = strings.Join(parts[:3], " ")
}
}
ad := apiData{}
ad.Safety.Failsafe = state.safety.failsafe
ad.Safety.HighPressure = state.safety.highPressure
ad.Pirani.Volts = formatVolts(volts)
ad.Pirani.Mbar = formatMbar(mbar)
ad.Pirani.MbarFloat = mbar
ad.Pumps.RPOn = state.rpOn
ad.Pumps.DPOn = state.dpOn
ad.Feedback.RoughReached = rough
ad.Feedback.HighReached = high
ad.System.Load = load
ad.System.Hostname = hostname
return &ad
}
// httpIndex is the / view.
func (s *httpServer) viewIndex(w http.ResponseWriter, r *http.Request) {
func (d *daemon) httpIndex(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
data := s.apiData(false)
//data.Pirani.Mbar = html.St
templateIndex.Execute(w, data)
volts, mbar := d.pirani()
rp := d.rpGet()
dp := d.dpGet()
failsafe, highpressure := d.safetyStatusGet()
loadB, err := os.ReadFile("/proc/loadavg")
load := "unknown"
if err == nil {
parts := strings.Fields(string(loadB))
load = strings.Join(parts[:3], " ")
}
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
templateIndex.Execute(w, map[string]any{
"failsafe": failsafe,
"highpressure": highpressure,
"volts": formatVolts(volts),
"mbar": formatMbar(mbar),
"rp": rp,
"dp": dp,
"hostname": hostname,
"load": load,
})
}
// httpStream is the websocket clientwards data hose, returning a 10Hz update
// stream of pressure/voltage.
func (s *httpServer) viewStream(w http.ResponseWriter, r *http.Request) {
func (d *daemon) httpStream(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
if err != nil {
return
@ -167,7 +105,33 @@ func (s *httpServer) viewStream(w http.ResponseWriter, r *http.Request) {
c.Close(websocket.StatusNormalClosure, "")
return
case <-t.C:
v := s.apiData(true)
// TODO(q3k): don't poll, get notified when new ADC readout is available.
volts, mbar := d.pirani()
rp := d.rpGet()
dp := d.dpGet()
rough, high := d.vacuumStatusGet()
failsafe, highpressure := d.safetyStatusGet()
v := struct {
Failsafe bool
HighPressure bool
Volts string
Mbar string
MbarFloat float32
RPOn bool
DPOn bool
RoughReached bool
HighReached bool
}{
Failsafe: failsafe,
HighPressure: highpressure,
Volts: formatVolts(volts),
Mbar: string(formatMbar(mbar)),
MbarFloat: mbar,
RPOn: rp,
DPOn: dp,
RoughReached: rough,
HighReached: high,
}
if err := wsjson.Write(ctx, c, v); err != nil {
klog.Errorf("Websocket write failed: %v", err)
return
@ -177,54 +141,39 @@ func (s *httpServer) viewStream(w http.ResponseWriter, r *http.Request) {
}
// httpMetrics serves minimalistic Prometheus-compatible metrics.
func (s *httpServer) viewMetrics(w http.ResponseWriter, r *http.Request) {
func (d *daemon) httpMetrics(w http.ResponseWriter, r *http.Request) {
// TODO(q3k): also serve Go stuff using the actual Prometheus metrics client
// library.
// TODO(q3k): serve the rest of the data model
state := s.d.snapshot()
_, mbar := state.pirani()
_, mbar := d.pirani()
fmt.Fprintf(w, "# HELP sem_pressure_mbar Pressure in the SEM chamber, in millibar\n")
fmt.Fprintf(w, "# TYPE sem_pressure_mbar gauge\n")
fmt.Fprintf(w, "sem_pressure_mbar %f\n", mbar)
}
func (s *httpServer) viewRoughingPumpEnable(w http.ResponseWriter, r *http.Request) {
s.d.rpSet(true)
func (d *daemon) httpRoughingPumpEnable(w http.ResponseWriter, r *http.Request) {
d.rpSet(true)
fmt.Fprintf(w, "succ on\n")
}
func (s *httpServer) viewRoughingPumpDisable(w http.ResponseWriter, r *http.Request) {
s.d.rpSet(false)
func (d *daemon) httpRoughingPumpDisable(w http.ResponseWriter, r *http.Request) {
d.rpSet(false)
fmt.Fprintf(w, "succ off\n")
}
func (s *httpServer) viewDiffusionPumpEnable(w http.ResponseWriter, r *http.Request) {
s.d.dpSet(true)
func (d *daemon) httpDiffusionPumpEnable(w http.ResponseWriter, r *http.Request) {
d.dpSet(true)
fmt.Fprintf(w, "deep succ on\n")
}
func (s *httpServer) viewDiffusionPumpDisable(w http.ResponseWriter, r *http.Request) {
s.d.dpSet(false)
func (d *daemon) httpDiffusionPumpDisable(w http.ResponseWriter, r *http.Request) {
d.dpSet(false)
fmt.Fprintf(w, "deep succ off\n")
}
func (s *httpServer) viewButtonPumpDown(w http.ResponseWriter, r *http.Request) {
s.d.pumpDownPress()
func (d *daemon) httpButtonPumpDown(w http.ResponseWriter, r *http.Request) {
d.pumpDownPress()
}
func (s *httpServer) viewButtonVent(w http.ResponseWriter, r *http.Request) {
s.d.ventPress()
}
func (s *httpServer) setupViews() {
http.HandleFunc("/", s.viewIndex)
http.HandleFunc("/favicon.png", s.viewFavicon)
http.HandleFunc("/stream", s.viewStream)
http.HandleFunc("/metrics", s.viewMetrics)
http.HandleFunc("/button/vent", s.viewButtonVent)
http.HandleFunc("/button/pumpdown", s.viewButtonPumpDown)
http.HandleFunc("/rp/on", s.viewRoughingPumpEnable)
http.HandleFunc("/rp/off", s.viewRoughingPumpDisable)
http.HandleFunc("/dp/on", s.viewDiffusionPumpEnable)
http.HandleFunc("/dp/off", s.viewDiffusionPumpDisable)
func (d *daemon) httpButtonVent(w http.ResponseWriter, r *http.Request) {
d.ventPress()
}

View file

@ -10,10 +10,6 @@ body {
}
table {
font-size: 40px;
margin-top: 1em;
}
table.status td {
width: 2em;
}
th, td {
background-color: #e8e8e8;
@ -62,66 +58,50 @@ td > span {
<p style="margin-top: 2em; clear: both;">
<table>
<tr>
<th rowspan="2">Pirani Gauge</th>
<th>Voltage</th>
<td id="volts">{{ .Pirani.Volts }}</td>
<td id="volts" colspan="4">{{.volts}}</td>
</tr>
<tr>
<th>Pressure</th>
<td id="mbar">{{ .Pirani.Mbar }}</td>
<td id="mbar" colspan="4">{{.mbar}}</td>
</tr>
</table>
<table class="status">
<tr>
<th>Thresholds</th>
<th>Rough</th>
<td id="trough">{{ if .Feedback.RoughReached }}OK{{ else }}NOK{{ end }}</td>
<th>High</th>
<td id="thigh">{{ if .Feedback.HighReached }}OK{{ else }}NOK{{ end }}</td>
<td>Rough:</td>
<td id="trough">...</td>
<td>High:</td>
<td id="thigh">...</td>
</tr>
<tr>
<th>Pumps</th>
<th>RP</th>
<td id="rp">{{ if .Pumps.RPOn }}ON{{ else }}OFF{{ end }}</td>
<th>DP</th>
<td id="dp">{{ if .Pumps.DPOn }}ON{{ else }}OFF{{ end }}</td>
<td>RP:</td>
<td id="rp">{{ if .rp }}ON{{ else }}OFF{{ end }}</td>
<td>DP:</td>
<td id="dp">{{ if .dp }}ON{{ else }}OFF{{ end }}</td>
</tr>
<tr>
<th>Safety</th>
<th style="font-size: 0.7em;">Pirani<br />Failsafe</th>
<td id="failsafe">{{ if .Safety.Failsafe }}ON{{ else }}OFF{{ end }}</td>
<th style="font-size: 0.7em;">DP<br />Lockout</th>
<td id="highpressure">{{ if .Safety.HighPressure }}ON{{ else }}OFF{{ end }}</td>
</tr>
</table>
<table>
<tr>
<th rowspan="3">Control</th>
<th>RP</th>
<td>
<button id="rpon">On</button>
<button id="rpoff">Off</button>
</td>
</tr>
<tr>
<th>DP</th>
<td>
<button id="dpon">On</button>
<button id="dpoff">Off</button>
</td>
</tr>
<tr>
<td colspan="2">
<th>Evac Control</th>
<td colspan="4">
<button id="pd">Pump Down</button>
<button id="vent">Vent</button>
<button id="rpon">RP On</button>
<button id="rpoff">RP Off</button>
<button id="dpon">DP On</button>
<button id="dpoff">DP Off</button>
</td>
</tr>
<tr>
<th>Status</th>
<td id="status" colspan="4">OK</td>
</tr>
<tr>
<th>Failsafe</th>
<td id="failsafe" colspan="4">OK</td>
</tr>
<tr>
<th>Diffusion Pump Lockout</th>
<td id="highpressure" colspan="4">OK</td>
</tr>
</table>
</p>
<p style="margin-top: 2em;">
@ -129,7 +109,7 @@ td > span {
</p>
<p style="font-style: italic; font-size: 12px; margin-top: 5em;">
{{ .System.Hostname }} | load: {{ .System.Load }} | <a href="/debug/pprof">pprof</a> | <a href="/metrics">metrics</a> | ws ping: <span id="ping"></span>
{{.hostname}} | load: {{.load}} | <a href="/debug/pprof">pprof</a> | <a href="/metrics">metrics</a> | ws ping: <span id="ping"></span>
</p>
<script>
@ -308,53 +288,50 @@ window.addEventListener("load", (_) => {
});
socket.addEventListener("message", (event) => {
const data = JSON.parse(event.data);
volts.innerHTML = data.Pirani.Volts;
mbar.innerHTML = data.Pirani.Mbar;
if (data.Safety.Failsafe) {
failsafe.innerHTML = "ON";
volts.innerHTML = data.Volts;
mbar.innerHTML = data.Mbar;
failsafe.innerHTML = data.Failsafe ? "ON" : "OFF";
if (data.Failsafe) {
failsafe.style = "background-color: #f06060";
} else {
failsafe.innerHTML = "OFF";
failsafe.style = "background-color: #60f060";
}
if (data.Safety.HighPressure) {
if (data.HighPressure) {
highpressure.innerHTML = "ON";
highpressure.style = "background-color: #f06060";
} else {
highpressure.innerHTML = "OFF";
highpressure.style = "background-color: #60f060";
}
if (data.Pumps.RPOn) {
rp.innerHTML = "ON";
rp.innerHTML = data.RPOn ? "ON" : "OFF";
if (data.RPOn) {
rp.style = "background-color: #60f060";
} else {
rp.innerHTML = "OFF";
rp.style = "background-color: #f06060";
}
if (data.Pumps.DPOn) {
dp.innerHTML = "ON";
dp.innerHTML = data.DPOn ? "ON" : "OFF";
if (data.DPOn) {
dp.style = "background-color: #60f060";
} else {
dp.innerHTML = "OFF";
dp.style = "background-color: #f06060";
}
let t = [];
if (data.Feedback.RoughReached) {
if (data.RoughReached) {
trough.innerHTML = "OK";
trough.style = "background-color: #60f060";
} else {
trough.innerHTML = "NOK";
trough.style = "background-color: #f06060";
}
if (data.Feedback.HighReached) {
if (data.HighReached) {
thigh.innerHTML = "OK";
thigh.style = "background-color: #60f060";
} else {
thigh.innerHTML = "NOK";
thigh.style = "background-color: #f06060";
}
historicalPush(data.Pirani.MbarFloat);
historicalPush(data.MbarFloat);
ping.innerHTML = Date.now();
});
socket.addEventListener("close", (event) => {

View file

@ -26,9 +26,9 @@ func main() {
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
d := daemon{}
d.daemonState.rpOn = true
d := daemon{
rpOn: true,
}
d.aboveRough.threshold = float64(flagPressureThresholdRough)
d.aboveHigh.threshold = float64(flagPressureThresholdHigh)
if flagFake {
@ -65,10 +65,16 @@ func main() {
}
}
httpServer := httpServer{
d: &d,
}
httpServer.setupViews()
http.HandleFunc("/", d.httpIndex)
http.HandleFunc("/favicon.png", d.httpFavicon)
http.HandleFunc("/stream", d.httpStream)
http.HandleFunc("/metrics", d.httpMetrics)
http.HandleFunc("/button/vent", d.httpButtonVent)
http.HandleFunc("/button/pumpdown", d.httpButtonPumpDown)
http.HandleFunc("/rp/on", d.httpRoughingPumpEnable)
http.HandleFunc("/rp/off", d.httpRoughingPumpDisable)
http.HandleFunc("/dp/on", d.httpDiffusionPumpEnable)
http.HandleFunc("/dp/off", d.httpDiffusionPumpDisable)
klog.Infof("Listening for HTTP at %s", flagListenHTTP)
go func() {

View file

@ -4,18 +4,22 @@ import (
"context"
"errors"
"fmt"
"math"
"sync"
"time"
"k8s.io/klog"
)
// daemon is the main service of the succdaemon.
// daemon is the main state of the succdaemon.
type daemon struct {
// adcPirani is the adc implementation returning the voltage of the Pfeiffer
// Pirani gauge.
adcPirani adc
failsafe bool
highPressure bool
gpioDiffusionPump gpio
gpioRoughingPump gpio
gpioBtnPumpDown gpio
@ -23,9 +27,18 @@ type daemon struct {
gpioBelowRough gpio
gpioBelowHigh gpio
// mu guards the state below.
// mu guards state variables below.
mu sync.RWMutex
daemonState
// adcPiraniVolts is a moving window of read ADC values, used to calculate a
// moving average.
adcPiraniVolts []float32
rpOn bool
dpOn bool
vent momentaryOutput
pumpdown momentaryOutput
aboveRough thresholdOutput
aboveHigh thresholdOutput
}
// momentaryOutput is an output that can be triggered for 500ms.
@ -106,47 +119,44 @@ func (d *daemon) processOnce(_ context.Context) error {
d.pumpdown.process()
d.vent.process()
_, mbar := d.daemonState.pirani()
_, mbar := d.piraniUnlocked()
d.aboveRough.process(float64(mbar))
d.aboveHigh.process(float64(mbar))
// Check if the pirani gauge is disconnected. Note: this will assume the
// pirani gauge is connected for the first couple of processing runs as
// samples are still being captured.
if d.piraniDetection() == piraniDetectionDisconnected {
if d.piraniWireBreakDetection() {
// Unrealistic result, Pirani probe probably disconnected. Failsafe mode.
if !d.safety.failsafe {
d.safety.failsafe = true
if !d.failsafe {
d.failsafe = true
klog.Errorf("Pirani probe seems disconnected; enabling failsafe mode")
}
} else {
if d.safety.failsafe {
if d.failsafe {
if mbar >= 1e2 {
d.safety.failsafe = false
d.failsafe = false
klog.Infof("Values are plausible again; quitting failsafe mode")
}
}
}
if mbar >= 1e-1 {
if !d.safety.highPressure {
d.safety.highPressure = true
if !d.highPressure {
d.highPressure = true
klog.Errorf("Pressure is too high; enabling diffusion pump lockout")
}
} else if mbar < (1e-1)-(1e-2) {
if d.safety.highPressure {
d.safety.highPressure = false
} else {
if d.highPressure {
d.highPressure = false
klog.Infof("Pressure is low enough for diffusion pump operation; quitting diffusion pump lockout")
}
}
if d.safety.failsafe {
if d.failsafe {
d.aboveRough.output = true
d.aboveHigh.output = true
d.dpOn = false
}
if d.safety.highPressure {
if d.highPressure {
d.dpOn = false
}
@ -180,3 +190,100 @@ func (d *daemon) processOnce(_ context.Context) error {
return nil
}
func (d *daemon) piraniWireBreakDetection() bool {
if len(d.adcPiraniVolts) < 3 {
return true
}
volts := float32(0.0)
for _, v := range d.adcPiraniVolts[len(d.adcPiraniVolts)-3:] {
volts += v
}
volts /= 3.0
bar := math.Pow(10.0, float64(volts)-8.5)
mbar := float32(bar * 1000.0)
return mbar < 4e-6
}
// pirani returns the Pirani gauge voltage and pressure.
func (d *daemon) pirani() (volts float32, mbar float32) {
d.mu.RLock()
volts, mbar = d.piraniUnlocked()
d.mu.RUnlock()
return
}
func (d *daemon) piraniUnlocked() (volts float32, mbar float32) {
volts = 0.0
for _, v := range d.adcPiraniVolts {
volts += v
}
if len(d.adcPiraniVolts) != 0 {
volts /= float32(len(d.adcPiraniVolts))
}
// Per Pirani probe docs.
bar := math.Pow(10.0, float64(volts)-8.5)
mbar = float32(bar * 1000.0)
return
}
// rpSet enables/disables the roughing pump.
func (d *daemon) rpSet(state bool) {
d.mu.Lock()
defer d.mu.Unlock()
d.rpOn = state
}
// rpGet returns whether the roughing pump is enabled/disabled.
func (d *daemon) rpGet() bool {
d.mu.RLock()
defer d.mu.RUnlock()
return d.rpOn
}
// dpSet enables/disables the diffusion pump.
func (d *daemon) dpSet(state bool) {
d.mu.Lock()
defer d.mu.Unlock()
d.dpOn = state
}
// dpGet returns whether the diffusion pump is enabled/disabled.
func (d *daemon) dpGet() bool {
d.mu.RLock()
defer d.mu.RUnlock()
return d.dpOn
}
func (d *daemon) vacuumStatusGet() (rough, high bool) {
d.mu.RLock()
defer d.mu.RUnlock()
rough = !d.aboveRough.output
high = !d.aboveHigh.output
return
}
func (d *daemon) safetyStatusGet() (failsafe, highPressure bool) {
d.mu.RLock()
defer d.mu.RUnlock()
failsafe = d.failsafe
highPressure = d.highPressure
return
}
// pumpDownPressed toggles the pump down relay for 500ms.
func (d *daemon) pumpDownPress() {
d.mu.Lock()
defer d.mu.Unlock()
d.pumpdown.trigger()
}
// ventPress toggles the vent relay for 500ms.
func (d *daemon) ventPress() {
d.mu.Lock()
defer d.mu.Unlock()
d.vent.trigger()
}

View file

@ -1,49 +0,0 @@
package main
// daemonController is the control/data interface passed on to external system
// controllers, eg. the web interface.
//
// This is a subset of daemon functions limited to a safe, explicit subset.
type daemonController interface {
// snapshot returns an internally consistent copy of the daemon state.
snapshot() *daemonState
// rpSet enables/disables the roughing pump.
rpSet(bool)
// dpSet enables/disables the diffusion pump.
dpSet(bool)
// pumpDownPress simulates a pumpdown button press.
pumpDownPress()
// ventPRess simulates a vent button press.
ventPress()
}
func (d *daemon) snapshot() *daemonState {
d.mu.RLock()
ds := d.daemonState
d.mu.RUnlock()
return &ds
}
func (d *daemon) rpSet(state bool) {
d.mu.Lock()
defer d.mu.Unlock()
d.rpOn = state
}
func (d *daemon) dpSet(state bool) {
d.mu.Lock()
defer d.mu.Unlock()
d.dpOn = state
}
func (d *daemon) pumpDownPress() {
d.mu.Lock()
defer d.mu.Unlock()
d.pumpdown.trigger()
}
func (d *daemon) ventPress() {
d.mu.Lock()
defer d.mu.Unlock()
d.vent.trigger()
}

View file

@ -1,84 +0,0 @@
package main
import "math"
// daemonState contains all the state of the daemon. A copy of it can be
// requested for consumers, eg. the web view.
type daemonState struct {
safety safetyStatus
// adcPiraniVolts is a moving window of read ADC values, used to calculate a
// moving average.
adcPiraniVolts []float32
rpOn bool
dpOn bool
vent momentaryOutput
pumpdown momentaryOutput
aboveRough thresholdOutput
aboveHigh thresholdOutput
}
type safetyStatus struct {
// failsafe mode is enabled when the pirani gauge appears to be
// disconnected, and is disabled only when an atmosphere is read.
failsafe bool
// highPressure mode is enabled when the pressure reading is above 1e-1
// mbar, locking out the diffusion pump from being enabled.
highPressure bool
}
type piraniDetection uint
const (
// piraniDetectionUnknown means the system isn't yet sure whether the pirani
// gauge is connected.
piraniDetectionUnknown piraniDetection = iota
// piraniDetectionConnected means the system assumes the pirani gauge is
// connected.
piraniDetectionConnected = iota
// piraniDetectionDisconnected means the system assumes the pirani gauge is
// disconnected.
piraniDetectionDisconnected = iota
)
// piraniDetection guesses whether the pirani gauge is connected.
func (d *daemonState) piraniDetection() piraniDetection {
if len(d.adcPiraniVolts) < 3 {
return piraniDetectionUnknown
}
volts := float32(0.0)
for _, v := range d.adcPiraniVolts[len(d.adcPiraniVolts)-3:] {
volts += v
}
volts /= 3.0
bar := math.Pow(10.0, float64(volts)-8.5)
mbar := float32(bar * 1000.0)
if mbar < 4e-6 {
return piraniDetectionDisconnected
}
return piraniDetectionConnected
}
func (d *daemonState) pirani() (volts float32, mbar float32) {
volts = 0.0
for _, v := range d.adcPiraniVolts {
volts += v
}
if len(d.adcPiraniVolts) != 0 {
volts /= float32(len(d.adcPiraniVolts))
}
// Per Pirani probe docs.
bar := math.Pow(10.0, float64(volts)-8.5)
mbar = float32(bar * 1000.0)
return
}
func (d *daemonState) vacuumStatus() (rough, high bool) {
rough = !d.aboveRough.output
high = !d.aboveHigh.output
return
}