th7 pcb and more

This commit is contained in:
William Clark 2022-11-16 19:53:11 +00:00
parent dbe22b25dd
commit 8b116f51a6
10 changed files with 151 additions and 35 deletions

View File

@ -1,9 +1,7 @@
[TH7]
port = 8080
logfreq = 60
#cache = true
#LED = false
#debug = true
debug = true
[DB]
type = "sqlite3"

View File

@ -8,9 +8,6 @@ import (
func NewAdapter(pcb ports.PCBPort, cfg config.Config) (ports.CorePort, error) {
if cfg.Board.Debug {
return NewDummyAdapter(pcb), nil
} else {
return th7.NewTH7Adapter(pcb, cfg), nil
}
return th7.NewTH7Adapter(pcb, cfg), nil
}

View File

@ -2,20 +2,21 @@ package th7
import (
"fmt"
"log"
"time"
)
const (
DurWaitBetweenChannel = 3 * time.Millisecond
DurWaitBetweenSample = 1 * time.Millisecond
DurWaitRestart = 500 * time.Millisecond
DurWaitRestart = 1000 * time.Millisecond
)
const (
Samples = 10
// TODO: configurable global or per-channel sample
Samples = 50
)
// TODO: per-channel configurable filter(-stages)
func alphaBetaFilter(arr []float64, init float64) float64 {
value := init
@ -37,18 +38,22 @@ func startCacheService(t *TH7Adapter) {
samples[i] = make([]float64, Samples)
}
duroffset := time.Duration(0) * time.Millisecond
for t.run {
timer_start := time.Now()
for i := 0; i < Samples; i++ {
// update Table
t.pcbPort.UpdateTable()
for k := range t.cfg.Channels {
val, err := t.pcbPort.ReadChannel(t.cfg.Channels[k].Id)
if err != nil {
log.Printf("Error reading channel %d: %v\n", t.cfg.Channels[k].Id, err)
}
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
time.Sleep(DurWaitBetweenSample)
@ -63,12 +68,21 @@ func startCacheService(t *TH7Adapter) {
data[i].Value = alphaBetaFilter(samples[i], data[i].Value)
}
// read pcb temp and apply CJC
t.mu.Lock()
t.data = data
t.mu.Unlock()
fmt.Println("data updated ...")
elapsed := time.Since(timer_start)
time.Sleep(DurWaitRestart)
fmt.Printf("updated %d channels, %d samples each (total=%d) dur=%s\n", len(samples), Samples, len(samples)*Samples, elapsed)
if elapsed >= (DurWaitRestart + duroffset) {
fmt.Println("timer overrun, adding 100ms")
duroffset += 100 * time.Millisecond
} else {
time.Sleep(DurWaitRestart + duroffset - elapsed)
}
}
}

1
go.mod
View File

@ -33,6 +33,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/spf13/viper v1.14.0
github.com/stianeikeland/go-rpio/v4 v4.6.0
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect

2
go.sum
View File

@ -193,6 +193,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.14.0 h1:Rg7d3Lo706X9tHsJMUjdiwMpHB7W8WnSVOssIY+JElU=
github.com/spf13/viper v1.14.0/go.mod h1:WT//axPky3FdvXHzGw33dNdXXXfFQqmEalje+egj8As=
github.com/stianeikeland/go-rpio/v4 v4.6.0 h1:eAJgtw3jTtvn/CqwbC82ntcS+dtzUTgo5qlZKe677EY=
github.com/stianeikeland/go-rpio/v4 v4.6.0/go.mod h1:A3GvHxC1Om5zaId+HqB3HKqx4K/AqeckxB7qRjxMK7o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

View File

@ -36,7 +36,7 @@ func main() {
fmt.Println(config)
pcbPort, err = pcb.NewAdapter(config.Board.Led)
pcbPort, err = pcb.NewAdapter(config)
if err != nil {
log.Fatalf("Fatal error: %v\n", err)
}

View File

@ -1,10 +1,15 @@
package pcb
import (
"th7/data/config"
"th7/ports"
)
func NewAdapter(LED bool) (ports.PCBPort, error) {
func NewAdapter(cfg config.Config) (ports.PCBPort, error) {
return NewDummyAdapter(LED)
if cfg.Board.Debug {
return NewDummyAdapter()
}
return NewTH7Adapter()
}

View File

@ -6,20 +6,14 @@ import (
)
type DummyAdapter struct {
table pcb.Table
use_led bool
table pcb.Table
}
func NewDummyAdapter(led bool) (*DummyAdapter, error) {
var adapter DummyAdapter
adapter.use_led = led
return &adapter, nil
func NewDummyAdapter() (*DummyAdapter, error) {
return &DummyAdapter{}, nil
}
func (d *DummyAdapter) Deinit() error {
// turn LED off ..
fmt.Println("dummy pcb adapter deinit")
return nil
}
@ -34,10 +28,10 @@ func (d *DummyAdapter) GetTable() pcb.Table {
return d.table
}
func (d *DummyAdapter) ReadChannel(id int) (float64, error) {
return -9000.0, nil
func (d *DummyAdapter) ReadChannel(id int, gain float64) float64 {
return -9000.0
}
func (d *DummyAdapter) ReadPCBTemp() (float64, error) {
return 25.0, nil
func (d *DummyAdapter) ReadPCBTemp() float64 {
return 25.0
}

105
pcb/th7.go Normal file
View File

@ -0,0 +1,105 @@
package pcb
import (
"sync"
"th7/data/pcb"
"github.com/stianeikeland/go-rpio/v4"
)
type TH7Adapter struct {
mu sync.Mutex
table pcb.Table
}
func NewTH7Adapter() (*TH7Adapter, error) {
var adapter TH7Adapter
err := rpio.Open()
if err != nil {
return &adapter, err
}
err = rpio.SpiBegin(rpio.Spi0)
if err != nil {
return &adapter, err
}
return &adapter, nil
}
func (t *TH7Adapter) Deinit() error {
rpio.SpiEnd(rpio.Spi0)
return rpio.Close()
}
func (t *TH7Adapter) UpdateTable() {
var newTable pcb.Table
var cb1, cb2 byte
rpio.SpiChipSelect(0) //CE0
// command bytes
cb1 = 4 + 2 + ((0 & 4) >> 2)
cb2 = ((0 & 3) << 6)
buffer := []byte{cb1, cb2, 0x00}
rpio.SpiExchange(buffer)
newTable.Vref = float64(buffer[1])*256.0 + float64(buffer[2]) //TODO: fix
//newTable.Vref = float64(int(buffer[1])<<8 | int(buffer[2]))
newTable.Vadj = newTable.Vref / 3355.4432
newTable.Pivdd = 5.0 / newTable.Vadj
t.mu.Lock()
defer t.mu.Unlock()
t.table = newTable
}
func (t *TH7Adapter) GetTable() pcb.Table {
t.mu.Lock()
defer t.mu.Unlock()
return t.table
}
func (t *TH7Adapter) ReadChannel(id int, gain float64) float64 {
table := t.GetTable()
channel := byte(id)
var ch, perfect, big_v, uv float64
var cb1, cb2 byte
rpio.SpiChipSelect(0) // CE0
// command bytes
cb1 = 4 + 2 + ((channel & 4) >> 2)
cb2 = ((channel & 3) << 6)
buffer := []byte{cb1, cb2, 0x00}
rpio.SpiExchange(buffer)
ch = float64(buffer[1])*256.0 + float64(buffer[2])
perfect = ((table.Vref - ch) * table.Vadj)
big_v = (perfect / 4096.0) * 5.0
uv = big_v * 100 * gain
return uv
}
func (t *TH7Adapter) ReadPCBTemp() float64 {
rpio.SpiChipSelect(1) // CE1
buffer := []byte{0x00, 0x00, 0x00, 0x00}
rpio.SpiExchange(buffer)
var pcb_temp, number float64
number = float64(buffer[0])*256.0 + float64(buffer[1])
//(number/8)*(1/16) == (number/(8*16)) == number/128 == number >> 7 ?
pcb_temp = (number / 8.0) * 0.0625
return pcb_temp
}

View File

@ -8,6 +8,6 @@ type PCBPort interface {
Deinit() error
UpdateTable()
GetTable() pcb.Table
ReadChannel(int) (float64, error)
ReadPCBTemp() (float64, error)
ReadChannel(int, float64) float64
ReadPCBTemp() float64
}