2024-09-27 00:11:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2024-09-28 12:26:24 +00:00
|
|
|
"sync/atomic"
|
2024-09-27 00:11:04 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"k8s.io/klog"
|
|
|
|
)
|
|
|
|
|
2024-09-28 06:10:33 +00:00
|
|
|
// daemon is the main service of the succdaemon.
|
2024-09-27 00:11:04 +00:00
|
|
|
type daemon struct {
|
|
|
|
// adcPirani is the adc implementation returning the voltage of the Pfeiffer
|
|
|
|
// Pirani gauge.
|
|
|
|
adcPirani adc
|
|
|
|
|
|
|
|
gpioDiffusionPump gpio
|
|
|
|
gpioRoughingPump gpio
|
|
|
|
gpioBtnPumpDown gpio
|
|
|
|
gpioBtnVent gpio
|
|
|
|
gpioBelowRough gpio
|
|
|
|
gpioBelowHigh gpio
|
|
|
|
|
2024-09-28 12:26:24 +00:00
|
|
|
load atomic.Int64
|
|
|
|
|
2024-09-28 06:10:33 +00:00
|
|
|
// mu guards the state below.
|
2024-09-27 00:11:04 +00:00
|
|
|
mu sync.RWMutex
|
2024-09-28 06:10:33 +00:00
|
|
|
daemonState
|
2024-09-28 05:35:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-28 07:48:50 +00:00
|
|
|
// 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 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
|
|
|
|
}
|
|
|
|
|
|
|
|
piraniVolts100 ringbufferInput
|
2024-09-28 08:17:05 +00:00
|
|
|
piraniMbar100 pfeifferVoltsToMbar
|
2024-09-28 07:48:50 +00:00
|
|
|
piraniVolts3 ringbufferInput
|
2024-09-28 08:17:05 +00:00
|
|
|
piraniMbar3 pfeifferVoltsToMbar
|
2024-09-28 07:48:50 +00:00
|
|
|
|
|
|
|
rpOn bool
|
|
|
|
dpOn bool
|
|
|
|
|
2024-10-06 09:29:04 +00:00
|
|
|
rpOperatingTime time.Duration
|
|
|
|
dpOperatingTime time.Duration
|
|
|
|
|
2024-09-28 07:48:50 +00:00
|
|
|
vent momentaryOutput
|
|
|
|
pumpdown momentaryOutput
|
|
|
|
aboveRough thresholdOutput
|
|
|
|
aboveHigh thresholdOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *daemonState) vacuumStatus() (rough, high bool) {
|
|
|
|
rough = !d.aboveRough.output
|
|
|
|
high = !d.aboveHigh.output
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-27 00:11:04 +00:00
|
|
|
// process runs the pain acquisition and control loop of succd.
|
|
|
|
func (d *daemon) process(ctx context.Context) {
|
2024-09-28 12:26:24 +00:00
|
|
|
var lastRun time.Time
|
|
|
|
hz := 100
|
|
|
|
period := time.Second / time.Duration(hz)
|
|
|
|
// Extra grace period for GC pauses and other non-realtime system jitter.
|
|
|
|
periodGrace := period + time.Millisecond*5
|
|
|
|
|
|
|
|
ticker := time.NewTicker(period)
|
2024-09-27 00:11:04 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2024-09-28 12:26:24 +00:00
|
|
|
now := time.Now()
|
2024-10-06 09:29:04 +00:00
|
|
|
var elapsed time.Duration = 0
|
|
|
|
if !lastRun.IsZero() {
|
|
|
|
elapsed = now.Sub(lastRun)
|
|
|
|
if elapsed > periodGrace {
|
|
|
|
klog.Warningf("Processing loop lag: took %s, want %s", elapsed, period)
|
|
|
|
}
|
2024-09-28 12:26:24 +00:00
|
|
|
}
|
|
|
|
lastRun = now
|
2024-10-06 09:29:04 +00:00
|
|
|
if err := d.processOnce(ctx, elapsed); err != nil {
|
2024-09-27 00:11:04 +00:00
|
|
|
if errors.Is(err, ctx.Err()) {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
klog.Errorf("Processing error: %v", err)
|
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 12:26:24 +00:00
|
|
|
runtime := time.Since(lastRun)
|
|
|
|
load := int64(100 * runtime / period)
|
|
|
|
d.load.Store(load)
|
2024-09-27 00:11:04 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processOnce runs the main loop step of succd.
|
2024-10-06 09:29:04 +00:00
|
|
|
func (d *daemon) processOnce(_ context.Context, elapsed time.Duration) error {
|
2024-09-27 00:11:04 +00:00
|
|
|
v, err := d.adcPirani.Read()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("when reading ADC: %w", err)
|
|
|
|
}
|
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
|
2024-09-28 07:35:41 +00:00
|
|
|
// Process pirani ringbuffers.
|
|
|
|
d.piraniVolts3.process(v)
|
2024-09-28 08:17:05 +00:00
|
|
|
d.piraniMbar3.process(d.piraniVolts3.avg)
|
2024-09-28 07:35:41 +00:00
|
|
|
d.piraniVolts100.process(v)
|
2024-09-28 08:17:05 +00:00
|
|
|
d.piraniMbar100.process(d.piraniVolts100.avg)
|
2024-09-27 00:11:04 +00:00
|
|
|
|
|
|
|
d.pumpdown.process()
|
|
|
|
d.vent.process()
|
|
|
|
|
2024-09-28 08:17:05 +00:00
|
|
|
// Run safety checks based on small ringbuffer.
|
|
|
|
if d.piraniVolts3.saturated() {
|
|
|
|
mbar := d.piraniMbar3.mbar
|
|
|
|
if !d.safety.failsafe && mbar < 4e-6 {
|
|
|
|
// Unrealistic result, Pirani probe probably disconnected. Failsafe mode.
|
2024-09-28 12:27:37 +00:00
|
|
|
d.safety.failsafe = true
|
|
|
|
klog.Errorf("SAFETY: Pirani probe seems disconnected; enabling failsafe mode")
|
2024-09-27 20:29:56 +00:00
|
|
|
}
|
2024-09-28 08:17:05 +00:00
|
|
|
if d.safety.failsafe && mbar > 1e2 {
|
|
|
|
d.safety.failsafe = false
|
2024-09-28 08:22:40 +00:00
|
|
|
klog.Infof("SAFETY: Pirani probe value (%s) is plausible again; quitting failsafe mode", formatMbar(mbar))
|
2024-09-28 08:17:05 +00:00
|
|
|
}
|
2024-09-27 20:29:56 +00:00
|
|
|
|
2024-09-28 08:17:05 +00:00
|
|
|
if !d.safety.highPressure && mbar >= 1e-1 {
|
2024-09-28 05:35:45 +00:00
|
|
|
d.safety.highPressure = true
|
2024-09-28 08:22:40 +00:00
|
|
|
klog.Warningf("SAFETY: Pressure is too high (%s mbar); enabling diffusion pump lockout", formatMbar(mbar))
|
2024-09-27 20:29:56 +00:00
|
|
|
}
|
2024-09-28 08:17:05 +00:00
|
|
|
if d.safety.highPressure && mbar < (1e-1)-(1e-2) {
|
2024-09-28 05:35:45 +00:00
|
|
|
d.safety.highPressure = false
|
2024-09-28 08:22:40 +00:00
|
|
|
klog.Infof("SAFETY: Pressure is low enough (%s mbar) for diffusion pump operation; quitting diffusion pump lockout", formatMbar(mbar))
|
2024-09-27 00:24:19 +00:00
|
|
|
}
|
2024-09-28 08:17:05 +00:00
|
|
|
} else {
|
|
|
|
d.safety.highPressure = true
|
2024-09-27 00:24:19 +00:00
|
|
|
}
|
|
|
|
|
2024-09-28 08:17:05 +00:00
|
|
|
// Control threhold/feedback values based on main pirani ringbuffer, failing
|
|
|
|
// safe if not enough data is present.
|
|
|
|
if d.piraniVolts100.saturated() {
|
|
|
|
mbar := d.piraniMbar100.mbar
|
|
|
|
d.aboveRough.process(float64(mbar))
|
|
|
|
d.aboveHigh.process(float64(mbar))
|
|
|
|
} else {
|
|
|
|
d.aboveRough.output = true
|
|
|
|
d.aboveHigh.output = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply safety overrides.
|
2024-09-28 05:35:45 +00:00
|
|
|
if d.safety.failsafe {
|
2024-09-27 00:24:19 +00:00
|
|
|
d.aboveRough.output = true
|
|
|
|
d.aboveHigh.output = true
|
2024-09-27 21:49:24 +00:00
|
|
|
d.dpOn = false
|
2024-09-27 20:29:56 +00:00
|
|
|
}
|
2024-09-28 05:35:45 +00:00
|
|
|
if d.safety.highPressure {
|
2024-09-27 20:29:56 +00:00
|
|
|
d.dpOn = false
|
2024-09-27 00:24:19 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 00:11:04 +00:00
|
|
|
// Update relay outputs.
|
|
|
|
for _, rel := range []struct {
|
|
|
|
name string
|
|
|
|
gpio gpio
|
|
|
|
// activeHigh means the relay is active high, ie. a true source will
|
|
|
|
// mean that NO/COM get connected, and a false source means that NC/COM
|
|
|
|
// get connected.
|
|
|
|
activeHigh bool
|
|
|
|
source bool
|
|
|
|
}{
|
|
|
|
{"rp", d.gpioRoughingPump, false, d.rpOn},
|
|
|
|
{"dp", d.gpioDiffusionPump, true, d.dpOn},
|
|
|
|
{"pumpdown", d.gpioBtnPumpDown, true, d.pumpdown.output},
|
|
|
|
{"vent", d.gpioBtnVent, true, d.vent.output},
|
|
|
|
{"rough", d.gpioBelowRough, false, d.aboveRough.output},
|
|
|
|
{"high", d.gpioBelowHigh, false, d.aboveHigh.output},
|
|
|
|
} {
|
|
|
|
val := rel.source
|
|
|
|
if rel.activeHigh {
|
|
|
|
// Invert because the relays go through logical inversion (ie. a
|
|
|
|
// GPIO false is a relay trigger).
|
|
|
|
val = !val
|
|
|
|
}
|
|
|
|
if err := rel.gpio.set(val); err != nil {
|
|
|
|
return fmt.Errorf("when outputting %s: %w", rel.name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 09:29:04 +00:00
|
|
|
// Update operating time counters
|
|
|
|
if d.rpOn && elapsed > 0 {
|
|
|
|
d.rpOperatingTime += elapsed
|
|
|
|
}
|
|
|
|
if d.dpOn && elapsed > 0 {
|
|
|
|
d.dpOperatingTime += elapsed
|
|
|
|
}
|
|
|
|
|
2024-09-27 00:11:04 +00:00
|
|
|
return nil
|
|
|
|
}
|