-
Notifications
You must be signed in to change notification settings - Fork 0
/
generalizedoptimizeBernstein.m
executable file
·160 lines (130 loc) · 5.52 KB
/
generalizedoptimizeBernstein.m
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
classdef generalizedoptimizeBernstein
properties
%starting and final way points of trajectory
x0
y0
xf
yf
%lane constraints min and max
xmin
ymin
xmax
ymax
%velocity constraints entire duration of the trajectory
xdotmin
ydotmin
xdotmax
ydotmax
%accleration constraints entire duration
xddotmin
yddotmin
xddotmax
yddotmax
bPoly %object to bernstein
xdoteq %xdot at t0
ydoteq %ydot at t0
% do we need them at all, isn't it restrictive?
xc %xc cordinates apart from the x0 and xf in form of an array [tc1, xc1; tc2 xc2..]
yc %yc cordinates apart from the y0 and yf [tc1, yc1; tc2 yc2; .. ]
tc
end
methods
% exactly as in formulation
function sum1 = cost(obj, x)
t0 = obj.bPoly.t0;
tf = obj.bPoly.tf;
sum1=0;
xmul = x(1:obj.bPoly.degree+1);
ymul = x(obj.bPoly.degree+2:end);
for t = t0:obj.bPoly.dt:tf
[ poly_d ] = obj.bPoly.get_coefficients(t, 1); %xdot
[ poly_dd ] = obj.bPoly.get_coefficients(t, 2); %xddot
[ poly_ddd ] = obj.bPoly.get_coefficients(t, 3); % xdddot
xdot = dot( xmul, poly_d );
ydot = dot( ymul, poly_d );
xddot = dot( xmul, poly_dd );
yddot = dot( ymul, poly_dd );
xdddot = dot( xmul, poly_ddd );
ydddot = dot( ymul, poly_ddd );
sum1 = sum1+(xdddot+ydddot) + 1*(((yddot*xdot-xddot*ydot)^2)/((xdot^2+ydot^2)^3));
end
end
function [A, b, Aeq, beq] = get_constraints(obj)
t0 = obj.bPoly.t0;
tf = obj.bPoly.tf;
t1 = obj.bPoly.tc;
Aeq = [];
beq = [];
A = [];
b = [];
[ poly ] = obj.bPoly.get_coefficients(t0, 0);
Aeq = [Aeq; [poly', zeros(1,obj.bPoly.degree+1)]];
beq = [beq; obj.x0];
Aeq = [Aeq; [zeros(1,obj.bPoly.degree+1), poly']];
beq = [beq; obj.y0];
[ poly ] = obj.bPoly.get_coefficients(tf, 0);
Aeq = [Aeq; [poly', zeros(1,obj.bPoly.degree+1)]];
beq = [beq; obj.xf];
Aeq = [Aeq; [zeros(1,obj.bPoly.degree+1), poly']];
beq = [beq; obj.yf];
if ~isempty(obj.xmin) && ~isempty(obj.xmax) && ~isempty(obj.ymin) && ~isempty(obj.ymax)
counter = 1;
for t = t1
[ poly ] = obj.bPoly.get_coefficients(t, 0);
A = [A; [poly', zeros(1,obj.bPoly.degree+1)]];
b = [b; obj.xmax(counter)];
A = [A; [-1*poly', zeros(1,obj.bPoly.degree+1)]];
b = [b; -obj.xmin(counter)];
A = [A; [zeros(1,obj.bPoly.degree+1),poly']];
b = [b; obj.ymax(counter)];
A = [A; [zeros(1,obj.bPoly.degree+1),-1*poly']];
b = [b; -obj.ymin(counter)];
counter = counter + 1;
end
end
if ~isempty(obj.tc)
for i=1:size(obj.tc,2)
[ poly ] = obj.bPoly.get_coefficients(obj.tc(1,i), 0);
Aeq = [Aeq; [poly', zeros(1,obj.bPoly.degree+1)]];
beq = [beq; obj.xc(1,i)];
Aeq = [Aeq; [zeros(1,obj.bPoly.degree+1),poly']];
beq = [beq; obj.yc(1,i)];
end
end
if ~isempty(obj.xdoteq) && ~isempty(obj.ydoteq)
[ poly ] = obj.bPoly.get_coefficients(t0, 1);
Aeq = [Aeq; [poly', zeros(1,obj.bPoly.degree+1)]];
beq = [beq; obj.xdoteq(1)];
Aeq = [Aeq; [zeros(1,obj.bPoly.degree+1), poly']];
beq = [beq; obj.ydoteq(1)];
[ poly ] = obj.bPoly.get_coefficients(tf, 1);
Aeq = [Aeq; [poly', zeros(1,obj.bPoly.degree+1)]];
beq = [beq; obj.xdoteq(2)];
Aeq = [Aeq; [zeros(1,obj.bPoly.degree+1), poly']];
beq = [beq; obj.ydoteq(2)];
end
% velocity constraints xdotmin, ydotmin
if ~isempty(obj.xdotmin) && ~isempty(obj.ydotmin)
for t = t1
A = [A; [-1*poly', zeros(1,obj.bPoly.degree+1)]];
b = [b; -obj.xdotmin];
A = [A; [zeros(1,obj.bPoly.degree+1),-1*poly']];
b = [b; -obj.ydotmin];
end
end
if ~isempty(obj.xdotmax) && ~isempty(obj.ydotmax)
for t = t1
[ poly ] = obj.bPoly.get_coefficients(t, 1);
A = [A; [poly', zeros(1,obj.bPoly.degree+1)]];
b = [b; obj.xdotmax];
A = [A; [zeros(1,obj.bPoly.degree+1),poly']];
b = [b; obj.ydotmax];
end
end
end
function [c,ceq] = get_nlconstraints(obj, x)
c = [];
ceq = [];
end
end
end