Compare commits

...

4 commits

8 changed files with 447 additions and 168 deletions

View file

@ -9,6 +9,20 @@ Features:
2. Allows enabling/disabling the diffusion/roughing pumps (builtin controller always keeps them enabled).
3. Allows for simulating vent/pumpdown button presses.
Pins
---
| P9 pin number | Relay number | Relay board / channel number | GPIO number | Function |
|---------------|----------------|------------------------------|-------------|-------------------|
| 27 | -KFA1 | 1 / 4 | 115 | Roughing Pump |
| 23 | -KFA2 | 1 / 3 | 49 | Diffusion Pump |
| n/c | -KFA3 (broken) | 1 / 2 | n/c | |
| 12 | -KFA4 | 1 / 1 | 60 | Vent Button |
| 15 | -KFA5 | 2 / 4 | 48 | Pump Down Button |
| 11 | -KFA6 | 2 / 3 | 30 | Rough Vacuum Out |
| 42 | -KFA7 | 2 / 2 | 7 | High Vacuum Out |
| n/c | -KFA8 | 2 / 1 | n/c | |
Accessing at the lab
---

View file

@ -19,8 +19,15 @@ var (
//go:embed index.html
templateIndexText string
templateIndex = template.Must(template.New("index").Parse(templateIndexText))
//go:embed succd.png
favicon []byte
)
func (d *daemon) httpFavicon(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.Write(favicon)
}
func formatVolts(v float32) string {
return fmt.Sprintf("%.4f V", v)
}
@ -99,18 +106,23 @@ func (d *daemon) httpStream(w http.ResponseWriter, r *http.Request) {
volts, mbar := d.pirani()
rp := d.rpGet()
dp := d.dpGet()
rough, high := d.vacuumStatusGet()
v := struct {
Volts string
Mbar string
MbarFloat float32
RPOn bool
DPOn bool
Volts string
Mbar string
MbarFloat float32
RPOn bool
DPOn bool
RoughReached bool
HighReached bool
}{
Volts: formatVolts(volts),
Mbar: string(formatMbar(mbar)),
MbarFloat: mbar,
RPOn: rp,
DPOn: dp,
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)

View file

@ -2,6 +2,7 @@
<meta charset="utf-8">
<title>succd</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="shortcut icon" type="image/png" href="/favicon.png">
<style>
body {
font-size: 14px;
@ -34,32 +35,53 @@ button {
padding-left: 1.5em;
padding-right: 1.5em;
}
td > span {
padding: 0.2em;
}
.logo {
float: left;
margin-right: 2em;
}
.logo > img {
height: 10em;
}
</style>
<div class="logo"><img src="/favicon.png" /></div>
<h1>succd</h1>
<h2>nothing more permanent than a temporary solution</h2>
<p style="margin-top: 2em;">
<p style="margin-top: 2em; clear: both;">
<table>
<tr>
<th>Voltage</th>
<td id="volts">{{.volts}}</td>
<td id="volts" colspan="4">{{.volts}}</td>
</tr>
<tr>
<th>Pressure</th>
<td id="mbar">{{.mbar}}</td>
<td id="mbar" colspan="4">{{.mbar}}</td>
</tr>
<tr>
<th>Thresholds</th>
<td>Rough:</td>
<td id="trough">...</td>
<td>High:</td>
<td id="thigh">...</td>
</tr>
<tr>
<th>Roughing Pump</th>
<td id="rp">{{ if .rp }}ON{{ else }}OFF{{ end }}</td>
</tr>
<tr>
<th>Diffusion Pump</th>
<td id="dp">{{ if .dp }}ON{{ else }}OFF{{ end }}</td>
<th>Pumps</th>
<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>Evac Control</th>
<td>
<td colspan="4">
<button id="pd">Pump Down</button>
<button id="vent">Vent</button>
<button id="rpon">RP On</button>
@ -70,7 +92,7 @@ button {
</tr>
<tr>
<th>Status</th>
<td id="status">OK</td>
<td id="status" colspan="4">OK</td>
</tr>
</table>
</p>
@ -221,6 +243,8 @@ window.addEventListener("load", (_) => {
let volts = document.querySelector("#volts");
let mbar = document.querySelector("#mbar");
let ping = document.querySelector("#ping");
let trough = document.querySelector("#trough");
let thigh = document.querySelector("#thigh");
// Buttons
let pd = document.querySelector("#pd");
@ -268,6 +292,22 @@ window.addEventListener("load", (_) => {
} else {
dp.style = "background-color: #f06060";
}
let t = [];
if (data.RoughReached) {
trough.innerHTML = "OK";
trough.style = "background-color: #60f060";
} else {
trough.innerHTML = "NOK";
trough.style = "background-color: #f06060";
}
if (data.HighReached) {
thigh.innerHTML = "OK";
thigh.style = "background-color: #60f060";
} else {
thigh.innerHTML = "NOK";
thigh.style = "background-color: #f06060";
}
historicalPush(data.MbarFloat);
ping.innerHTML = Date.now();
});

View file

@ -2,166 +2,26 @@ package main
import (
"context"
"errors"
"flag"
"fmt"
"math"
"net/http"
"os"
"os/signal"
"sync"
"time"
"k8s.io/klog"
)
// 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
gpioDiffusionPump gpio
gpioRoughingPump gpio
gpioBtnPumpDown gpio
gpioBtnVent gpio
// mu guards state variables below.
mu sync.RWMutex
// adcPiraniVolts is a moving window of read ADC values, used to calculate a
// moving average.
adcPiraniVolts []float32
rpOn bool
dpOn bool
// ventScheduled and pumpdownScheduled are timers which expire when the
// vent/pumpdown relays should be deactivated. This allows these outputs to
// be controlled momentarily.
ventScheduled time.Time
pumpdownScheduled time.Time
}
// process runs the pain acquisition and control loop of succd.
func (d *daemon) process(ctx context.Context) {
ticker := time.NewTicker(time.Millisecond * 100)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := d.processOnce(ctx); err != nil {
if errors.Is(err, ctx.Err()) {
return
} else {
klog.Errorf("Processing error: %v", err)
time.Sleep(time.Second * 10)
}
}
case <-ctx.Done():
return
}
}
}
// processOnce runs the main loop step of succd.
func (d *daemon) processOnce(_ context.Context) error {
v, err := d.adcPirani.Read()
if err != nil {
return fmt.Errorf("when reading ADC: %w", err)
}
d.mu.Lock()
defer d.mu.Unlock()
d.adcPiraniVolts = append(d.adcPiraniVolts, v)
trim := len(d.adcPiraniVolts) - 100
if trim > 0 {
d.adcPiraniVolts = d.adcPiraniVolts[trim:]
}
if err := d.gpioRoughingPump.set(d.rpOn); err != nil {
return fmt.Errorf("when configuring RP: %w", err)
}
if err := d.gpioDiffusionPump.set(!d.dpOn); err != nil {
return fmt.Errorf("when configuring RP: %w", err)
}
if err := d.gpioBtnPumpDown.set(!d.pumpdownScheduled.After(time.Now())); err != nil {
return fmt.Errorf("when configuring pumpdown: %w", err)
}
if err := d.gpioBtnVent.set(!d.ventScheduled.After(time.Now())); err != nil {
return fmt.Errorf("when configuring vent: %w", err)
}
return nil
}
// pirani returns the Pirani gauge voltage and pressure.
func (d *daemon) pirani() (volts float32, mbar float32) {
d.mu.RLock()
volts = 0.0
for _, v := range d.adcPiraniVolts {
volts += v
}
if len(d.adcPiraniVolts) != 0 {
volts /= float32(len(d.adcPiraniVolts))
}
d.mu.RUnlock()
// 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
}
// pumpDownPressed toggles the pump down relay for 500ms.
func (d *daemon) pumpDownPress() {
d.mu.Lock()
defer d.mu.Unlock()
if d.pumpdownScheduled.Before(time.Now()) {
d.pumpdownScheduled = time.Now().Add(500 * time.Millisecond)
}
}
// ventPress toggles the vent relay for 500ms.
func (d *daemon) ventPress() {
d.mu.Lock()
defer d.mu.Unlock()
if d.ventScheduled.Before(time.Now()) {
d.ventScheduled = time.Now().Add(500 * time.Millisecond)
}
}
var (
flagFake bool
flagListenHTTP string
flagFake bool
flagListenHTTP string
flagPressureThresholdRough = ScientificNotationValue(1e-1)
flagPressureThresholdHigh = ScientificNotationValue(1e-4)
)
func main() {
flag.BoolVar(&flagFake, "fake", false, "Enable fake mode which allows to run succd for tests outside the succbone")
flag.StringVar(&flagListenHTTP, "listen_http", ":8080", "Address at which to listen for HTTP requests")
flag.TextVar(&flagPressureThresholdRough, "pressure_threshold_rough", &flagPressureThresholdRough, "Threshold for opening up diffusion pump (mbar)")
flag.TextVar(&flagPressureThresholdHigh, "pressure_threshold_high", &flagPressureThresholdHigh, "Threshold for enabling high voltage circuits (mbar)")
flag.Parse()
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
@ -169,6 +29,8 @@ func main() {
d := daemon{
rpOn: true,
}
d.aboveRough.threshold = float64(flagPressureThresholdRough)
d.aboveHigh.threshold = float64(flagPressureThresholdHigh)
if flagFake {
klog.Infof("Starting with fake peripherals")
d.adcPirani = &fakeADC{}
@ -176,6 +38,8 @@ func main() {
d.gpioDiffusionPump = &fakeGPIO{desc: "~dp"}
d.gpioBtnPumpDown = &fakeGPIO{desc: "~pd"}
d.gpioBtnVent = &fakeGPIO{desc: "~vent"}
d.gpioBelowRough = &fakeGPIO{desc: "~rough"}
d.gpioBelowHigh = &fakeGPIO{desc: "~high"}
} else {
adc, err := newBBADC(0)
if err != nil {
@ -191,8 +55,9 @@ func main() {
{&d.gpioDiffusionPump, 49},
{&d.gpioBtnPumpDown, 48},
{&d.gpioBtnVent, 60},
{&d.gpioBelowRough, 30},
{&d.gpioBelowHigh, 7},
} {
// Relay, active low.
*c.out, err = newBBGPIO(c.num, true)
if err != nil {
klog.Exitf("Failed to setup GPIO: %v", err)
@ -201,6 +66,7 @@ func main() {
}
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)

225
succbone/succd/process.go Normal file
View file

@ -0,0 +1,225 @@
package main
import (
"context"
"errors"
"fmt"
"math"
"sync"
"time"
"k8s.io/klog"
)
// 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
gpioDiffusionPump gpio
gpioRoughingPump gpio
gpioBtnPumpDown gpio
gpioBtnVent gpio
gpioBelowRough gpio
gpioBelowHigh gpio
// mu guards state variables below.
mu sync.RWMutex
// 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.
type momentaryOutput struct {
// output of the block.
output bool
// scheduledOff is when the block should be outputting false again.
scheduledOff time.Time
}
func (m *momentaryOutput) process() {
m.output = m.scheduledOff.After(time.Now())
}
func (m *momentaryOutput) trigger() {
m.scheduledOff = time.Now().Add(time.Millisecond * 500)
}
// thresholdOutput outputs true if a given value is above a setpoint/threshold.
// It contains debounce logic for processing noisy analog signals.
type thresholdOutput struct {
// output of the block.
output bool
// debounce is when the debouncer should be inactive again.
debounce time.Time
// threshold is the setpoint of the block.
threshold float64
}
func (t *thresholdOutput) process(value float64) {
if time.Now().Before(t.debounce) {
return
}
new := value > t.threshold
if new != t.output {
t.output = new
t.debounce = time.Now().Add(time.Second * 5)
}
}
// process runs the pain acquisition and control loop of succd.
func (d *daemon) process(ctx context.Context) {
ticker := time.NewTicker(time.Millisecond * 100)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := d.processOnce(ctx); err != nil {
if errors.Is(err, ctx.Err()) {
return
} else {
klog.Errorf("Processing error: %v", err)
time.Sleep(time.Second * 10)
}
}
case <-ctx.Done():
return
}
}
}
// processOnce runs the main loop step of succd.
func (d *daemon) processOnce(_ context.Context) error {
v, err := d.adcPirani.Read()
if err != nil {
return fmt.Errorf("when reading ADC: %w", err)
}
d.mu.Lock()
defer d.mu.Unlock()
// Process pirani ringbuffer.
d.adcPiraniVolts = append(d.adcPiraniVolts, v)
trim := len(d.adcPiraniVolts) - 100
if trim > 0 {
d.adcPiraniVolts = d.adcPiraniVolts[trim:]
}
d.pumpdown.process()
d.vent.process()
_, mbar := d.piraniUnlocked()
d.aboveRough.process(float64(mbar))
d.aboveHigh.process(float64(mbar))
// 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)
}
}
return nil
}
// 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
}
// 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

@ -0,0 +1,33 @@
package main
import (
"fmt"
"strconv"
)
type ScientificNotationValue float64
func (s *ScientificNotationValue) UnmarshalText(text []byte) error {
f, err := strconv.ParseFloat(string(text), 64)
if err != nil {
return err
}
*s = ScientificNotationValue(f)
return nil
}
func (s *ScientificNotationValue) MarshalText() ([]byte, error) {
v := float64(*s)
exp := 0
for v < 1 {
v *= 10
exp -= 1
}
for v >= 10 {
v /= 10
exp += 1
}
res := fmt.Sprintf("%.3f", v)
res += fmt.Sprintf("e%d", exp)
return []byte(res), nil
}

BIN
succbone/succd/succd.png (Stored with Git LFS) Normal file

Binary file not shown.

86
succbone/succd/succd.svg Normal file
View file

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="256"
height="256"
viewBox="0 0 67.733332 67.733333"
version="1.1"
id="svg1"
xml:space="preserve"
inkscape:version="1.3.2 (091e20ef0f, 2023-11-25)"
sodipodi:docname="succd.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"><sodipodi:namedview
id="namedview1"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:showpageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#d1d1d1"
inkscape:document-units="mm"
inkscape:zoom="1.8100325"
inkscape:cx="144.47254"
inkscape:cy="86.738774"
inkscape:window-width="2558"
inkscape:window-height="1418"
inkscape:window-x="0"
inkscape:window-y="20"
inkscape:window-maximized="1"
inkscape:current-layer="layer1" /><defs
id="defs1" /><g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"><path
id="path1-6-26-9"
style="fill:#333333;stroke-width:0.665001;stroke-linecap:round;stroke-linejoin:round"
d="m 54.934253,11.041885 -11.725404,3.026755 7.374684,30.241308 11.774672,-3.192173 c 0.340838,0.02984 0.657689,-0.04009 0.94584,-0.208758 2.388773,-1.406768 2.341457,-9.169829 -0.105684,-17.339529 C 60.94409,16.04748 57.398733,10.697677 54.959229,11.136965 Z"
sodipodi:nodetypes="cccccccc" /><path
id="path1-6-26"
style="fill:#4d4d4d;stroke-width:0.689213;stroke-linecap:round;stroke-linejoin:round"
d="M 36.834788 34.267762 L 28.039647 31.24561 L 26.275151 71.107079 L 34.9414 74.95385 A 6.0778723 20.553801 0 0 0 36.145859 75.365089 A 6.0778723 20.553801 0 0 0 42.223611 54.811692 A 6.0778723 20.553801 0 0 0 36.830602 34.397432 L 36.834788 34.267762 z "
transform="matrix(0.78359623,-0.62127043,0.29230718,0.95632448,0,0)" /><path
id="path1-6-2"
style="fill:#ff6600;stroke-width:0.685186;stroke-linecap:round;stroke-linejoin:round"
d="M 31.632903 29.815088 L 0.2045976 17.686637 A 6.7583387 22.62063 0.41226998 0 0 -0.44146265 17.437591 L -0.78055632 17.306653 L -0.78204159 17.371872 A 6.7583387 22.62063 0.41226998 0 0 -0.91086368 17.346932 A 6.7583387 22.62063 0.41226998 0 0 -7.798345 40.031956 A 6.7583387 22.62063 0.41226998 0 0 -1.6381568 62.531876 L -1.6401479 62.634194 L 29.922324 73.760027 L 29.922895 73.751714 A 22.133209 6.6891561 90 0 0 30.697087 73.904866 A 22.133209 6.6891561 90 0 0 37.38594 51.771489 A 22.133209 6.6891561 90 0 0 31.630325 29.876359 L 31.632903 29.815088 z "
transform="matrix(0.79570833,-0.60567999,0.30336715,0.95287374,0,0)" /><ellipse
style="fill:#b3b3b3;stroke-width:0.694613;stroke-linecap:round;stroke-linejoin:round"
id="path1-6"
cx="0.47263575"
cy="40.426369"
rx="5.0466633"
ry="17.51091"
transform="matrix(0.7683548,-0.64002415,0.27934773,0.96019,0,0)" /><ellipse
style="fill:#1a1a1a;stroke-width:0.718381;stroke-linecap:round;stroke-linejoin:round"
id="path1"
cx="0.0053259684"
cy="40.20768"
rx="3.606401"
ry="13.362189"
transform="matrix(0.67378554,-0.73892696,0.28587782,0.95826607,0,0)" /><text
xml:space="preserve"
style="font-size:12.247px;line-height:1.25;font-family:'Comic Sans MS';-inkscape-font-specification:'Comic Sans MS';letter-spacing:0px;fill:#ececec;stroke-width:0.264583"
x="14.440682"
y="29.016745"
id="text2"
transform="matrix(0.77461007,-0.24176924,0.36716645,1.1763731,0,0)"><tspan
sodipodi:role="line"
id="tspan2"
style="fill:#ececec;stroke-width:0.264583"
x="14.440682"
y="29.016745">succ</tspan></text><text
xml:space="preserve"
style="font-size:12.247px;line-height:1.25;font-family:'Comic Sans MS';-inkscape-font-specification:'Comic Sans MS';letter-spacing:0px;fill:#ececec;stroke-width:0.264583"
x="10.27577"
y="36.975842"
id="text2-3"
transform="matrix(0.77461007,-0.24176924,0.36716645,1.1763731,0,0)"><tspan
sodipodi:role="line"
id="tspan2-7"
style="fill:#ececec;stroke-width:0.264583"
x="10.27577"
y="36.975842">bone</tspan></text></g></svg>

After

Width:  |  Height:  |  Size: 4.4 KiB