succd: eke out a few extra bits of precision by oversampling ADC

This commit is contained in:
Serge Bazanski 2024-09-12 03:18:31 +02:00
parent 05d102ab9b
commit 9e02f07b90
2 changed files with 16 additions and 5 deletions

View file

@ -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

View file

@ -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.