succd: factor out ringbuffer, do not recalculate average on every request

This commit is contained in:
Serge Bazanski 2024-09-28 09:35:41 +02:00
parent 42c9ae2fa7
commit 4df00f0a63
3 changed files with 47 additions and 32 deletions

View file

@ -66,6 +66,33 @@ func (t *thresholdOutput) process(value float64) {
}
}
// ringbufferInput accumulates analog data up to limit samples, and calculates
// an average.
type ringbufferInput struct {
data []float32
limit uint
avg float32
}
func (r *ringbufferInput) process(input float32) {
// TODO(q3k): use actual ringbuffer
// TODO(q3k): optimize average calculation
// TODO(q3k): precalculate value in mbar
r.data = append(r.data, input)
trim := len(r.data) - int(r.limit)
if trim > 0 {
r.data = r.data[trim:]
}
avg := float32(0.0)
for _, v := range r.data {
avg += v
}
if len(r.data) != 0 {
avg /= float32(len(r.data))
}
r.avg = avg
}
// process runs the pain acquisition and control loop of succd.
func (d *daemon) process(ctx context.Context) {
ticker := time.NewTicker(time.Millisecond * 100)
@ -96,12 +123,9 @@ func (d *daemon) processOnce(_ context.Context) error {
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:]
}
// Process pirani ringbuffers.
d.piraniVolts3.process(v)
d.piraniVolts100.process(v)
d.pumpdown.process()
d.vent.process()