-
Notifications
You must be signed in to change notification settings - Fork 0
/
traectory.go
131 lines (106 loc) · 2.92 KB
/
traectory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"math"
"log"
"github.com/ZzEeKkAa/fuzzy-logic/fuzzy"
"github.com/golang/geo/r2"
"github.com/golang/geo/r3"
)
func BuildTraectory(a, b float64, start r2.Point, startAngel float64, end r2.Point, d float64, am fuzzy.AssociativeMemory, s Surface) []struct {
r2.Point
float64
} {
var ans []struct {
r2.Point
float64
}
ans = append(ans, struct {
r2.Point
float64
}{start, startAngel})
// Vehicle angel in two systems
floatAngel := startAngel
angel := r3.Vector{X: math.Cos(startAngel / 180 * math.Pi), Y: math.Sin(startAngel / 180 * math.Pi), Z: 0}
destination := r3.Vector{X: end.X, Y: end.Y, Z: 0}
max := 10000
for p := start; end.Sub(p).Norm() > d && max > 0; max-- {
log.Println(p)
log.Println("Current Pos:", p)
log.Println("Current Ang:", floatAngel, r3.Vector{X: 1, Y: 0, Z: 0}.Angle(angel).Degrees())
log.Println("Dist left:", end.Sub(p).Norm())
_, tilt := s.GetVehicleAngels(p.X, p.Y, floatAngel, a, b)
deviation := destination.Sub(r3.Vector{X: p.X, Y: p.Y, Z: 0}).Angle(angel).Degrees()
deviation = normalizeAngel(deviation)
log.Println("til:", tilt)
log.Println("dev:", deviation)
turn := am.Defuzzify(deviation, tilt, d)
log.Println("Turn:", turn)
floatAngel += turn
angel = r3.Vector{X: math.Cos(floatAngel / 180 * math.Pi), Y: math.Sin(floatAngel / 180 * math.Pi), Z: 0}
floatAngel = normalizeAngel(floatAngel)
mv := angel.Normalize().Mul(d)
log.Println(angel.Normalize())
p.X += mv.X
p.Y += mv.Y
log.Println()
ans = append(ans, struct {
r2.Point
float64
}{p, floatAngel})
}
if max == 0 {
log.Println("Steps limit reached")
} else {
log.Println("End point reached. Number of steps", 1000-max)
}
return ans
}
func normalizeAngel(a float64) float64 {
//a -= float64(int(a/360)) * 360
//if a > 360 {
// a -= 360
//}
//if a < 0 {
// a += 360
//}
if a > 180 {
a -= 360
}
if a < -180 {
a += 360
}
return a
}
func BuildTrajectoryNew(v Vehicle, end r2.Point, moveStep float64, eps float64, amUp fuzzy.AssociativeMemory, amDown fuzzy.AssociativeMemory, h func(float64, float64) float64) (ans []VehiclePosition) {
d := 0.01
ans = append(ans, VehiclePosition{v.x, v.y, v.alpha})
var defuzzify = amDown.Defuzzify
var alpha = 15.
max := 100000
for ; end.Sub(r2.Point{v.x, v.y}).Norm() > eps && max > 0; max-- {
tang, tilt := v.FindDegrees(h)
deviation := v.FindDeviation(end.X, end.Y)
log.Println(v.x, v.y)
log.Println("til:", tilt)
log.Println("dev:", deviation)
var turn float64
if tang < -alpha {
defuzzify = amDown.Defuzzify
} else if tang > alpha {
defuzzify = amUp.Defuzzify
}
turn = defuzzify(deviation, tilt, d)
log.Println("Turn:", turn)
log.Println("Angel", v.alpha)
v.TurnRight(turn)
v.MoveForward(moveStep)
ans = append(ans, Pos(&v))
log.Println()
}
if max == 0 {
log.Println("Steps limit reached")
} else {
log.Println("End point reached. Number of steps", 10000-max)
}
return
}