From 70cde175081f92259d6174bab073d98555de9952 Mon Sep 17 00:00:00 2001 From: William Clark Date: Mon, 5 Dec 2022 12:40:07 +0000 Subject: [PATCH] first draft cjc --- core/th7/service.go | 27 +++++++++++++++++++++++++++ thermocouple/thermocouple.go | 25 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/core/th7/service.go b/core/th7/service.go index 5bda8f6..adf7bf6 100644 --- a/core/th7/service.go +++ b/core/th7/service.go @@ -2,6 +2,8 @@ package th7 import ( "fmt" + "log" + "th7/thermocouple" "time" ) @@ -69,6 +71,31 @@ func startCacheService(t *TH7Adapter) { } // read pcb temp and apply CJC + // the board temperature wont noticeably change for the ~300 ms of sampling + // so only reading pcb temp and applying cjc after this duration + // probably wont affect the readings. + pcb_temperature := t.pcbPort.ReadPCBTemp() + + for k := range t.cfg.Channels { + + tc := t.cfg.Channels[k].Thermo + if thermocouple.WithinTemperatureRange(tc, pcb_temperature) { + // if we can apply CJC with this configuration ... + cjc_uv, err := thermocouple.UV(tc, pcb_temperature) + if err != nil { + log.Println("Error doing CJC", err) + continue + } + + data[k].Value -= cjc_uv + data[k].Unit = t.cfg.Channels[k].Unit + } else { + // CJC can not be applied + // board temp out of range for specified thermocouple type + // perhaps just subtract board temperature? dont know + fmt.Println("No valid CJC", t.cfg.Channels[k]) + } + } t.mu.Lock() t.data = data diff --git a/thermocouple/thermocouple.go b/thermocouple/thermocouple.go index 1e72d70..369b78e 100644 --- a/thermocouple/thermocouple.go +++ b/thermocouple/thermocouple.go @@ -4,6 +4,31 @@ import ( "th7/data/thermocouple" ) +func WithinTemperatureRange(thermo thermocouple.Type, c float64) bool { + + switch thermo { + case thermocouple.B: + return c >= B_MIN_C && c <= B_MAX_C + case thermocouple.E: + return c >= E_MIN_C && c <= E_MAX_C + case thermocouple.J: + return c >= J_MIN_C && c <= J_MAX_C + case thermocouple.K: + return c >= K_MIN_C && c <= K_MAX_C + case thermocouple.N: + return c >= N_MIN_C && c <= N_MAX_C + case thermocouple.R: + return c >= R_MIN_C && c <= R_MAX_C + case thermocouple.S: + return c >= S_MIN_C && c <= S_MAX_C + case thermocouple.T: + return c >= T_MIN_C && c <= T_MAX_C + } + + // None type + return false +} + func C(thermo thermocouple.Type, uv float64) (float64, error) { switch thermo {