From a58f3e8b5f18af69b35691b73ebc4aa8a520ca5c Mon Sep 17 00:00:00 2001 From: William Clark Date: Tue, 21 Nov 2023 14:45:14 +0000 Subject: [PATCH] cleanup --- Makefile | 7 +++ config.toml | 4 +- config/config.go | 38 +++------------- core/th7/service.go | 88 +++++++++++++------------------------- data/config/data.go | 7 ++- data/core/data.go | 2 - data/temperature/data.go | 10 ----- db/dummy.go | 2 +- filter/filter.go | 12 ++++++ main.go | 3 -- pcb/dummy.go | 5 ++- temperature/temperature.go | 68 ----------------------------- web/gin.go | 4 +- 13 files changed, 72 insertions(+), 178 deletions(-) create mode 100644 Makefile delete mode 100644 data/temperature/data.go create mode 100644 filter/filter.go delete mode 100644 temperature/temperature.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..78bd272 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +build: + go build main.go +run: + go run main.go +clean: + rm -rf test.db main + diff --git a/config.toml b/config.toml index f627ebf..c977915 100644 --- a/config.toml +++ b/config.toml @@ -1,14 +1,16 @@ [TH7] port = 9090 -logfreq = 60 +debug = true [DB] type = "sqlite3" path = "test.db" +freq = 60 [Channel_2] type = 'N' unit = 'C' +filter.samples = 20 [Channel_3] type = 'T' diff --git a/config/config.go b/config/config.go index 6d6e985..05c73b8 100644 --- a/config/config.go +++ b/config/config.go @@ -5,7 +5,6 @@ import ( "fmt" "strings" "th7/data/config" - "th7/data/temperature" "th7/data/thermocouple" "github.com/spf13/viper" @@ -28,12 +27,6 @@ var ( "T": thermocouple.T, "UV": thermocouple.None, } - temperatureUnits = map[string]temperature.Unit{ - "C": temperature.C, - "F": temperature.F, - "K": temperature.K, - "UV": temperature.None, - } ) func getThermocoupleType(t string) (thermocouple.Type, error) { @@ -45,15 +38,6 @@ func getThermocoupleType(t string) (thermocouple.Type, error) { } } -func getTemperatureUnit(u string) (temperature.Unit, error) { - u = strings.ToUpper(u) - if val, ok := temperatureUnits[u]; ok { - return val, nil - } else { - return temperature.None, ErrConfigBadTemperatureUnit - } -} - func Load() (config.Config, error) { var cfg config.Config @@ -71,14 +55,14 @@ func Load() (config.Config, error) { v.SetDefault("TH7.port", 8080) v.SetDefault("TH7.cache", true) v.SetDefault("TH7.LED", true) - v.SetDefault("TH7.logfreq", 60) v.SetDefault("TH7.debug", false) v.SetDefault("TH7.nolog", false) + v.SetDefault("DB.freq", 60) cfg.Board.Port = v.GetInt("TH7.port") cfg.Board.Cache = v.GetBool("TH7.cache") cfg.Board.Led = v.GetBool("TH7.LED") - cfg.Board.Logfreq = v.GetInt("TH7.logfreq") + cfg.Board.Logfreq = v.GetInt("DB.freq") cfg.Board.Debug = v.GetBool("TH7.debug") cfg.Board.NoLogging = v.GetBool("TH7.nolog") @@ -91,11 +75,12 @@ func Load() (config.Config, error) { v.SetDefault(head+".gain", 106.8) v.SetDefault(head+".offset", 0.0) v.SetDefault(head+".unit", "UV") + // filter settings + v.SetDefault(head+".filter.samples", 50) } for i := 1; i < 8; i++ { var c config.Channel - var unit temperature.Unit var tc thermocouple.Type var head = fmt.Sprintf("Channel_%d", i) @@ -113,18 +98,9 @@ func Load() (config.Config, error) { return cfg, err } c.Thermo = tc - - if tc == thermocouple.None { - unit = temperature.None - } else { - unit, err = getTemperatureUnit(v.GetString(head + ".unit")) - if err != nil { - fmt.Printf("%s.unit=%s\n", head, v.GetString(head+".unit")) - return cfg, err - } - } - c.Unit = unit - + + c.Filter.SampleSize = v.GetInt(head+".filter.samples") + cfg.Channels = append(cfg.Channels, c) } diff --git a/core/th7/service.go b/core/th7/service.go index 9a2cac3..c1d3065 100644 --- a/core/th7/service.go +++ b/core/th7/service.go @@ -3,8 +3,8 @@ package th7 import ( "fmt" "log" - temp_data "th7/data/temperature" - "th7/temperature" + thermo_data "th7/data/thermocouple" + "th7/filter" "th7/thermocouple" "time" ) @@ -15,22 +15,11 @@ const ( DurWaitRestart = 1000 * time.Millisecond ) -const ( - // TODO: configurable global or per-channel sample - Samples = 50 -) -// TODO: per-channel configurable filter(-stages) -func alphaBetaFilter(arr []float64, init float64) float64 { - - value := init - for i := range arr { - gain := 1.0 / (float64(i + 1)) - value += (gain*arr[i] - value) - } - return value -} +// Continuously sample each configured channel, apply filter, and save result +// in a mutex controlled shared data object, that can be accessed at any time +// by a web-request or similar. func startCacheService(t *TH7Adapter) { data := t.data @@ -39,7 +28,7 @@ func startCacheService(t *TH7Adapter) { // init for i := range samples { - samples[i] = make([]float64, Samples) + samples[i] = make([]float64, t.cfg.Channels[i].Filter.SampleSize) } duroffset := time.Duration(0) * time.Millisecond @@ -47,19 +36,17 @@ func startCacheService(t *TH7Adapter) { for t.run { timer_start := time.Now() + + for channel := range t.cfg.Channels { - for i := 0; i < Samples; i++ { + channel_id := t.cfg.Channels[channel].Id + channel_gain := t.cfg.Channels[channel].Gain - // update Table - t.pcbPort.UpdateTable() - - for k := range t.cfg.Channels { - channel_id := t.cfg.Channels[k].Id - channel_gain := t.cfg.Channels[k].Gain - - val := t.pcbPort.ReadChannel(channel_id, channel_gain) - - samples[k][i] = val + for i:=0; i= (DurWaitRestart + duroffset) { fmt.Println("timer overrun, adding 100ms") diff --git a/data/config/data.go b/data/config/data.go index 6f11ff3..c0081d9 100644 --- a/data/config/data.go +++ b/data/config/data.go @@ -1,16 +1,19 @@ package config import ( - "th7/data/temperature" "th7/data/thermocouple" ) +type Filter struct { + SampleSize int `json:"sample_size"` +} + type Channel struct { Id int `json:"id"` Thermo thermocouple.Type `json:"thermocouple"` Gain float64 `json:"gain"` Offset float64 `json:"offset"` - Unit temperature.Unit `json:"unit"` + Filter Filter `json:"filter"` } type TH7 struct { diff --git a/data/core/data.go b/data/core/data.go index cd75f2a..a749992 100644 --- a/data/core/data.go +++ b/data/core/data.go @@ -1,11 +1,9 @@ package core -import "th7/data/temperature" type Channel struct { Id int `json:"id"` Value float64 `json:"value"` - Unit temperature.Unit `json:"unit"` } type Ratio struct { diff --git a/data/temperature/data.go b/data/temperature/data.go deleted file mode 100644 index 0bd0482..0000000 --- a/data/temperature/data.go +++ /dev/null @@ -1,10 +0,0 @@ -package temperature - -type Unit int - -const ( - None Unit = iota // None is UV - K - C - F -) diff --git a/db/dummy.go b/db/dummy.go index 08eaa5a..7e2567c 100644 --- a/db/dummy.go +++ b/db/dummy.go @@ -19,7 +19,7 @@ func (d *DummyAdapter) Close() { func (d *DummyAdapter) Save(channels []core.Channel) error { fmt.Println("=======================================") for _, val := range channels { - fmt.Printf("Saved: id=%d, unit=%d, value=%g\n", val.Id, val.Unit, val.Value) + fmt.Printf("Saved: id=%d, value=%g\n", val.Id, val.Value) } fmt.Println("=======================================") return nil diff --git a/filter/filter.go b/filter/filter.go new file mode 100644 index 0000000..986d999 --- /dev/null +++ b/filter/filter.go @@ -0,0 +1,12 @@ +package filter + + +func AlphaBetaFilter(arr []float64, init float64) float64 { + + value := init + for i := range arr { + gain := 1.0 / (float64(i + 1)) + value += (gain*arr[i] - value) + } + return value +} \ No newline at end of file diff --git a/main.go b/main.go index 02ff44b..c1b09a3 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" "os" "os/signal" @@ -34,8 +33,6 @@ func main() { log.Fatalf("Error loading config: %v", err) } - fmt.Println(config) - pcbPort, err = pcb.NewAdapter(config) if err != nil { log.Fatalf("Fatal error: %v\n", err) diff --git a/pcb/dummy.go b/pcb/dummy.go index a8a3444..b681f69 100644 --- a/pcb/dummy.go +++ b/pcb/dummy.go @@ -2,7 +2,9 @@ package pcb import ( "fmt" + "math/rand" "th7/data/pcb" + "time" ) type DummyAdapter struct { @@ -10,6 +12,7 @@ type DummyAdapter struct { } func NewDummyAdapter() (*DummyAdapter, error) { + rand.Seed(time.Now().UnixNano()) return &DummyAdapter{}, nil } @@ -29,7 +32,7 @@ func (d *DummyAdapter) GetTable() pcb.Table { } func (d *DummyAdapter) ReadChannel(id int, gain float64) float64 { - return -9000.0 + return -9000.0 * rand.Float64() } func (d *DummyAdapter) ReadPCBTemp() float64 { diff --git a/temperature/temperature.go b/temperature/temperature.go deleted file mode 100644 index c037efb..0000000 --- a/temperature/temperature.go +++ /dev/null @@ -1,68 +0,0 @@ -package temperature - -import ( - "th7/data/temperature" -) - -func c_to_k(val float64) float64 { - return val + 273.15 -} - -func c_to_f(val float64) float64 { - return (val * 9 / 5) + 32.0 -} - -func f_to_c(val float64) float64 { - return (val - 32.0) * 5 / 9 -} - -func f_to_k(val float64) float64 { - return (val-32.0)*5/9 + 273.15 -} - -func k_to_c(val float64) float64 { - return val - 273.15 -} - -func k_to_f(val float64) float64 { - return (val-273.15)*9/5 + 32.0 -} - -func C(unit temperature.Unit, value float64) float64 { - switch unit { - case temperature.C: - return value - case temperature.F: - return c_to_f(value) - case temperature.K: - return c_to_k(value) - } - - return 0 -} - -func F(unit temperature.Unit, value float64) float64 { - switch unit { - case temperature.F: - return value - case temperature.C: - return f_to_c(value) - case temperature.K: - return f_to_k(value) - } - - return 0 -} - -func K(unit temperature.Unit, value float64) float64 { - switch unit { - case temperature.K: - return value - case temperature.F: - return k_to_f(value) - case temperature.C: - return k_to_c(value) - } - - return 0 -} diff --git a/web/gin.go b/web/gin.go index 98c2259..401d182 100644 --- a/web/gin.go +++ b/web/gin.go @@ -95,8 +95,10 @@ func NewGinAdapter(corePort ports.CorePort, port int) *GinAdapter { adapter.corePort = corePort adapter.port = port - adapter.router = gin.Default() + //adapter.router = gin.Default() + adapter.router = gin.New() + // API adapter.router.GET("/ratio", adapter.RatioHandler) adapter.router.GET("/channels", adapter.ChannelsHandler) adapter.router.GET("/channel/:id", adapter.ChannelByIDHandler)