feat: publish pwm feedback
This commit is contained in:
5
pkg/tools/test_data/config.json
Normal file
5
pkg/tools/test_data/config.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"threshold_steps": [ 0.07, 0.08, 0.09, 0.1, 0.125, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 ],
|
||||
"min_valid": 500,
|
||||
"data": [ 8700, 4800, 3500, 2550, 1850, 1387, 992, 840, 750, 700, 655, 620, 590, 570, 553, 549, 548 ]
|
||||
}
|
65
pkg/tools/thresholdconfig.go
Normal file
65
pkg/tools/thresholdconfig.go
Normal file
@ -0,0 +1,65 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultThresholdConfig = ThresholdConfig{
|
||||
ThresholdSteps: []float64{0.07, 0.08, 0.09, 0.1, 0.125, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0},
|
||||
MinValid: 500,
|
||||
Data: []int{8700, 4800, 3500, 2550, 1850, 1387, 992, 840, 750, 700, 655, 620, 590, 570, 553, 549, 548},
|
||||
}
|
||||
)
|
||||
|
||||
func NewThresholdConfig() *ThresholdConfig {
|
||||
return &defaultThresholdConfig
|
||||
}
|
||||
|
||||
func NewThresholdConfigFromJson(fileName string) (*ThresholdConfig, error) {
|
||||
content, err := os.ReadFile(fileName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to read content from %s file: %w", fileName, err)
|
||||
}
|
||||
var ft ThresholdConfig
|
||||
err = json.Unmarshal(content, &ft)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to unmarshal json content from %s file: %w", fileName, err)
|
||||
}
|
||||
return &ft, nil
|
||||
}
|
||||
|
||||
type ThresholdConfig struct {
|
||||
ThresholdSteps []float64 `json:"threshold_steps"`
|
||||
MinValid int `json:"min_valid"`
|
||||
Data []int `json:"data"`
|
||||
}
|
||||
|
||||
func (tc *ThresholdConfig) ValueOf(pwm int) float64 {
|
||||
if pwm < tc.MinValid || pwm > tc.Data[0] {
|
||||
return 0.
|
||||
}
|
||||
if pwm == tc.Data[0] {
|
||||
return tc.ThresholdSteps[0]
|
||||
}
|
||||
|
||||
if pwm < tc.Data[len(tc.Data)-1] && pwm >= tc.MinValid {
|
||||
return 1.
|
||||
}
|
||||
// search column index
|
||||
var idx int
|
||||
// Start loop at 1 because first column should be skipped
|
||||
for i := 1; i < len(tc.ThresholdSteps); i++ {
|
||||
if pwm == tc.Data[i] {
|
||||
return tc.ThresholdSteps[i]
|
||||
}
|
||||
if pwm > tc.Data[i] {
|
||||
idx = i - 1
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return tc.ThresholdSteps[idx] - (tc.ThresholdSteps[idx]-tc.ThresholdSteps[idx+1])/2.
|
||||
}
|
135
pkg/tools/thresholdconfig_test.go
Normal file
135
pkg/tools/thresholdconfig_test.go
Normal file
@ -0,0 +1,135 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewThresholdConfigFromJson(t *testing.T) {
|
||||
type args struct {
|
||||
fileName string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *ThresholdConfig
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "default config",
|
||||
args: args{
|
||||
fileName: "test_data/config.json",
|
||||
},
|
||||
want: &defaultThresholdConfig,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := NewThresholdConfigFromJson(tt.args.fileName)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("NewThresholdConfigFromJson() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(*got, *tt.want) {
|
||||
t.Errorf("NewThresholdConfigFromJson() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
if !reflect.DeepEqual(got.MinValid, tt.want.MinValid) {
|
||||
t.Errorf("NewThresholdConfigFromJson(), bad minValid value: got = %v, want %v", got.MinValid, tt.want.MinValid)
|
||||
}
|
||||
if !reflect.DeepEqual(got.ThresholdSteps, tt.want.ThresholdSteps) {
|
||||
t.Errorf("NewThresholdConfigFromJson(), bad ThresholdSteps: got = %v, want %v", got.ThresholdSteps, tt.want.ThresholdSteps)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestThresholdConfig_ValueOf(t *testing.T) {
|
||||
type fields struct {
|
||||
ThresholdSteps []float64
|
||||
MinValue int
|
||||
Data []int
|
||||
}
|
||||
type args struct {
|
||||
pwm int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want float64
|
||||
}{
|
||||
{
|
||||
name: "big value",
|
||||
fields: fields{
|
||||
ThresholdSteps: defaultThresholdConfig.ThresholdSteps,
|
||||
MinValue: defaultThresholdConfig.MinValid,
|
||||
Data: defaultThresholdConfig.Data,
|
||||
},
|
||||
args: args{
|
||||
pwm: 11000.,
|
||||
},
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "little value",
|
||||
fields: fields{
|
||||
ThresholdSteps: defaultThresholdConfig.ThresholdSteps,
|
||||
MinValue: defaultThresholdConfig.MinValid,
|
||||
Data: defaultThresholdConfig.Data,
|
||||
},
|
||||
args: args{
|
||||
pwm: defaultThresholdConfig.MinValid - 1,
|
||||
},
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "pwm at limit",
|
||||
fields: fields{
|
||||
ThresholdSteps: defaultThresholdConfig.ThresholdSteps,
|
||||
MinValue: defaultThresholdConfig.MinValid,
|
||||
Data: defaultThresholdConfig.Data,
|
||||
},
|
||||
args: args{
|
||||
pwm: defaultThresholdConfig.Data[2],
|
||||
},
|
||||
want: defaultThresholdConfig.ThresholdSteps[2],
|
||||
},
|
||||
{
|
||||
name: "between 2 limits",
|
||||
fields: fields{
|
||||
ThresholdSteps: defaultThresholdConfig.ThresholdSteps,
|
||||
MinValue: defaultThresholdConfig.MinValid,
|
||||
Data: defaultThresholdConfig.Data,
|
||||
},
|
||||
args: args{
|
||||
pwm: 800,
|
||||
},
|
||||
want: 0.275,
|
||||
},
|
||||
{
|
||||
name: "over last value and > minValue",
|
||||
fields: fields{
|
||||
ThresholdSteps: defaultThresholdConfig.ThresholdSteps,
|
||||
MinValue: defaultThresholdConfig.MinValid,
|
||||
Data: defaultThresholdConfig.Data,
|
||||
},
|
||||
args: args{
|
||||
pwm: defaultThresholdConfig.MinValid + 3,
|
||||
},
|
||||
want: 1.,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f := &ThresholdConfig{
|
||||
ThresholdSteps: tt.fields.ThresholdSteps,
|
||||
MinValid: tt.fields.MinValue,
|
||||
Data: tt.fields.Data,
|
||||
}
|
||||
got := f.ValueOf(tt.args.pwm)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("ValueOf() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user