diff --git a/succd/http.go b/succd/http.go index 07bdc0d..7f4a7b6 100644 --- a/succd/http.go +++ b/succd/http.go @@ -22,7 +22,7 @@ var ( ) func formatVolts(v float32) string { - return fmt.Sprintf("%.3f V", v) + return fmt.Sprintf("%.4f V", v) } // formatMbar formats a millibar value using scientific notation and returns a diff --git a/succd/main.go b/succd/main.go index c4c6fb9..e5d524a 100644 --- a/succd/main.go +++ b/succd/main.go @@ -23,8 +23,9 @@ type daemon struct { // mu guards state variables below. mu sync.RWMutex - // adcPiraniVolts is the last readout of adcPirani. - adcPiraniVolts float32 + // adcPiraniVolts is a moving window of read ADC values, used to calculate a + // moving average. + adcPiraniVolts []float32 } // process runs the pain acquisition and control loop of succd. @@ -55,7 +56,11 @@ func (d *daemon) processOnce(_ context.Context) error { return fmt.Errorf("when reading ADC: %w", err) } d.mu.Lock() - d.adcPiraniVolts = v + d.adcPiraniVolts = append(d.adcPiraniVolts, v) + trim := len(d.adcPiraniVolts) - 100 + if trim > 0 { + d.adcPiraniVolts = d.adcPiraniVolts[trim:] + } d.mu.Unlock() return nil @@ -64,7 +69,13 @@ func (d *daemon) processOnce(_ context.Context) error { // pirani returns the Pirani gauge voltage and pressure. func (d *daemon) pirani() (volts float32, mbar float32) { d.mu.RLock() - volts = d.adcPiraniVolts + 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.