WIP
This commit is contained in:
		@@ -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
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func NewFixesTableFromJson(fileName string) (*FixesTable, 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 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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type FixesTable struct {
 | 
					type FixesTable struct {
 | 
				
			||||||
	values map[string]map[int]float64
 | 
						DistanceSteps []float64   `json:"distance_steps"`
 | 
				
			||||||
 | 
						SteeringSteps []float64   `json:"steering_steps"`
 | 
				
			||||||
 | 
						Data          [][]float64 `json:"data"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (f *FixesTable) ValueOf(steering float64, distance float32) float64 {
 | 
					func (f *FixesTable) ValueOf(steering float64, distance float64) float64 {
 | 
				
			||||||
	var key string
 | 
						// search column index
 | 
				
			||||||
	if steering < -0.66 {
 | 
						var idxCol int
 | 
				
			||||||
		key = "<-0.66"
 | 
						// Start loop at 1 because first column should be skipped
 | 
				
			||||||
	} else if steering < -0.33 {
 | 
						for i := 1; i < len(f.SteeringSteps); i++ {
 | 
				
			||||||
		key = "-0.66:-0.33"
 | 
							if steering < f.SteeringSteps[i] {
 | 
				
			||||||
	} else if steering < 0 {
 | 
								idxCol = i - 1
 | 
				
			||||||
		key = "-0.33:0"
 | 
								break
 | 
				
			||||||
	} else if steering < 0.33 {
 | 
							}
 | 
				
			||||||
		key = "0:0.33"
 | 
					 | 
				
			||||||
	} else if steering < 0.66 {
 | 
					 | 
				
			||||||
		key = "0.33:0.66"
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		key = ">= 0.66"
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	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]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										12
									
								
								pkg/steering/test_data/config.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								pkg/steering/test_data/config.json
									
									
									
									
									
										Normal 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]
 | 
				
			||||||
 | 
					  ]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user