This commit is contained in:
Cyrille Nofficial 2022-08-22 23:56:13 +02:00
parent a93159df90
commit 7d27ea866f
3 changed files with 86 additions and 22 deletions

View File

@ -1,9 +1,11 @@
package steering package steering
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/cyrilix/robocar-protobuf/go/events" "github.com/cyrilix/robocar-protobuf/go/events"
"go.uber.org/zap" "go.uber.org/zap"
"os"
) )
type Corrector struct { type Corrector struct {
@ -26,7 +28,7 @@ FixFromObjectPosition modify steering value according object positions
60% |-----|-----|-----|-----|-----|-----| 60% |-----|-----|-----|-----|-----|-----|
: | 0 | 0.25| 0.5 |-0.5 |-0.25| 0 | : | 0 | 0.25| 0.5 |-0.5 |-0.25| 0 |
80% |-----|-----|-----|-----|-----|-----| 80% |-----|-----|-----|-----|-----|-----|
: |-0.25|-0.5 | 1 | -1 |-0.5 | 0.25| : | 0.25| 0.5 | 1 | -1 |-0.5 |-0.25|
100%|-----|-----|-----|-----|-----|-----| 100%|-----|-----|-----|-----|-----|-----|
2. For straight (current steering near of 0), search nearest object and if: 2. For straight (current steering near of 0), search nearest object and if:
@ -59,12 +61,12 @@ func (c *Corrector) FixFromObjectPosition(currentSteering float64, objects []*ev
} }
if nearest.Left < 0 && nearest.Right < 0 { if nearest.Left < 0 && nearest.Right < 0 {
return currentSteering + c.fixValues.ValueOf(currentSteering, nearest.Right) return currentSteering + c.fixValues.ValueOf(currentSteering, float64(nearest.Right))
} }
if nearest.Left > 0 && nearest.Right > 0 { if nearest.Left > 0 && nearest.Right > 0 {
return currentSteering + c.fixValues.ValueOf(currentSteering, nearest.Left) return currentSteering + c.fixValues.ValueOf(currentSteering, float64(nearest.Left))
} }
return currentSteering + c.fixValues.ValueOf(currentSteering, nearest.Left+(nearest.Right-nearest.Left)/2.) return currentSteering + c.fixValues.ValueOf(currentSteering, float64(nearest.Left)+(float64(nearest.Right)-float64(nearest.Left))/2.)
} }
// Search if current steering is near of Right or Left // Search if current steering is near of Right or Left
@ -90,27 +92,45 @@ func (c *Corrector) nearObject(objects []*events.Object) (*events.Object, error)
return result, nil return result, nil
} }
type FixesTable struct { func NewFixesTableFromJson(fileName string) (*FixesTable, error) {
values map[string]map[int]float64 content, err := os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("unable to read content from %s file: %w", fileName, err)
}
var ft FixesTable
err = json.Unmarshal(content, &ft)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal json content from %s file: %w", fileName, err)
}
// TODO: check structure is valid
return &ft, nil
} }
func (f *FixesTable) ValueOf(steering float64, distance float32) float64 { type FixesTable struct {
var key string DistanceSteps []float64 `json:"distance_steps"`
if steering < -0.66 { SteeringSteps []float64 `json:"steering_steps"`
key = "<-0.66" Data [][]float64 `json:"data"`
} else if steering < -0.33 { }
key = "-0.66:-0.33"
} else if steering < 0 { func (f *FixesTable) ValueOf(steering float64, distance float64) float64 {
key = "-0.33:0" // search column index
} else if steering < 0.33 { var idxCol int
key = "0:0.33" // Start loop at 1 because first column should be skipped
} else if steering < 0.66 { for i := 1; i < len(f.SteeringSteps); i++ {
key = "0.33:0.66" if steering < f.SteeringSteps[i] {
} else { idxCol = i - 1
key = ">= 0.66" break
}
} }
keyDistance := int(distance / 20) var idxRow int
return f.values[key][keyDistance] // Start loop at 1 because first column should be skipped
for i := 1; i < len(f.DistanceSteps); i++ {
if distance < f.DistanceSteps[i] {
idxRow = i - 1
break
}
}
return f.Data[idxRow][idxCol]
} }

View File

@ -186,3 +186,35 @@ func TestCorrector_nearObject(t *testing.T) {
}) })
} }
} }
func TestNewFixesTableFromJson(t *testing.T) {
type args struct {
fileName string
}
tests := []struct {
name string
args args
want *FixesTable
wantErr bool
}{
{
name: "default config",
args: args{
fileName: "test_data/config.json",
},
},
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewFixesTableFromJson(tt.args.fileName)
if (err != nil) != tt.wantErr {
t.Errorf("NewFixesTableFromJson() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewFixesTableFromJson() got = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -0,0 +1,12 @@
{
"steering_steps":[-1, -0.66, -0.33, 0, 0.33, 0.66, 1],
"distance_steps": [0, 0.2, 0.4, 0.6, 0.8, 1],
"data": [
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0.25, -0.25, 0, 0],
[0, 0.25, 0.5, -0.5, -0.25, 0],
[0.25, 0.5, 1, -1, -0.5, -0.25]
]
}