This commit is contained in:
William Clark 2023-12-13 08:58:39 +00:00
parent eda649c65f
commit f4893c71e4

View File

@ -2,14 +2,14 @@ package th7
import (
"log"
thermo_data "th7/data/thermocouple"
tdata "th7/data/thermocouple"
"th7/filter"
"th7/thermocouple"
"time"
)
const (
DurWaitBetweenChannel = 3 * time.Millisecond
DurWaitBetweenChannel = 1 * time.Millisecond
DurWaitBetweenSample = 1 * time.Millisecond
DurWaitRestart = 1000 * time.Millisecond
)
@ -19,31 +19,29 @@ const (
// by a web-request or similar.
func startCacheService(t *TH7Adapter) {
// []core.Channel
data := t.data
// 2-dimensional f64 array of possibly diff sizes, to store samples
samples := make([][]float64, len(t.cfg.Channels))
// init
// malloc each array
for i := range samples {
samples[i] = make([]float64, t.cfg.Channels[i].Filter.SampleSize)
}
duroffset := time.Duration(0) * time.Millisecond
for t.run {
timer_start := time.Now()
for c := range t.cfg.Channels {
for channel := range t.cfg.Channels {
channel_id := t.cfg.Channels[c].Id
channel_gain := t.cfg.Channels[c].Gain
channel_id := t.cfg.Channels[channel].Id
channel_gain := t.cfg.Channels[channel].Gain
for i := 0; i < t.cfg.Channels[channel].Filter.SampleSize; i++ {
for i := 0; i < t.cfg.Channels[c].Filter.SampleSize; i++ {
// update vref table
t.pcbPort.UpdateTable()
measurement := t.pcbPort.ReadChannel(channel_id, channel_gain)
samples[channel][i] = measurement
samples[c][i] = measurement
time.Sleep(DurWaitBetweenSample)
}
time.Sleep(DurWaitBetweenChannel)
@ -63,12 +61,12 @@ func startCacheService(t *TH7Adapter) {
for k := range t.cfg.Channels {
thermo_type := t.cfg.Channels[k].Thermo
ttype := t.cfg.Channels[k].Thermo
data[k].Id = t.cfg.Channels[k].Id
// if we can apply CJC with this configuration ...
if thermocouple.WithinTemperatureRange(thermo_type, pcb_temperature) {
cjc_uv, err := thermocouple.UV(thermo_type, pcb_temperature)
if thermocouple.WithinTemperatureRange(ttype, pcb_temperature) {
cjc_uv, err := thermocouple.UV(ttype, pcb_temperature)
if err != nil {
log.Println("Error doing CJC", err)
cjc_uv = 0.0
@ -79,8 +77,8 @@ func startCacheService(t *TH7Adapter) {
// if the thermocouple unit is "None", it is for UV only.
// so do not convert it to a value using t/c polys
if t.cfg.Channels[k].Thermo != thermo_data.None {
new_value_c, err := thermocouple.C(thermo_type, data[k].Value)
if t.cfg.Channels[k].Thermo != tdata.None {
new_value_c, err := thermocouple.C(ttype, data[k].Value)
if err != nil {
log.Println("Error converting UV to C", t.cfg.Channels[k].Id, data[k].Value)
new_value_c = data[k].Value
@ -95,31 +93,18 @@ func startCacheService(t *TH7Adapter) {
}
// maybe update the pcb table here ?
// update ratio table
table := t.pcbPort.GetTable()
t.mu.Lock() // LOCK
t.data = data
// update the ratio table of the TH7 adapter
table := t.pcbPort.GetTable()
t.table.Pivdd = table.Pivdd
t.table.Vadj = table.Vadj
t.table.Vref = table.Vref
t.mu.Unlock() // UNLOCK
elapsed := time.Since(timer_start)
total_sample_size := 0
for k := range t.cfg.Channels {
total_sample_size += t.cfg.Channels[k].Filter.SampleSize
}
if elapsed >= (DurWaitRestart + duroffset) {
duroffset += 100 * time.Millisecond
} else {
time.Sleep(DurWaitRestart + duroffset - elapsed)
}
time.Sleep(DurWaitRestart)
}
}