vector output in matlab function block

3 views (last 30 days)
Sarah Kern
Sarah Kern on 20 Jan 2020
Commented: Sindar on 20 Jan 2020
Hello,
thanks for reading.
I have the following code in my matlab function block.
function x = fcn(u)
%definition of vehicle forces and torque
u(1) = Fx_v;
u(2) = Fy_v;
u(3) = Mz_v;
%definition of current torque limits
i = 1:4;
u(4) = P_max_1;
u(5) = P_max_2;
u(6) = P_max_3;
u(7) = P_max_4;
u(8) = M_reg_1;
u(9) = M_reg_2;
u(10) = M_reg_3;
u(11) = M_reg_4;
%active stimulation Input
u(12) = k;
u(13) = delta_T_aA;
u(14) = SW_1; %Schwimmwinkel beta
u(15) = SW_2;
u(16) = SW_3;
u(17) = SW_4;
u(18) = w_1;
u(19) = w_2;
u(20) = w_3;
u(21) = w_4;
u(22) = d_delay_1;
u(23) = d_delay_2;
u(24) = d_delay_3;
u(25) = d_delay_4;
u(26) = r_dyn_1;
u(27) = r_dyn_2;
u(28) = r_dyn_3;
u(29) = r_dyn_4;
u(30) = SOC_1;
u(31) = SOC_2;
u(32) = SOC_3;
u(33) = SOC_4;
u(34) = f_lag;
u(35) = n;
u(36) = j;
coder.extrinsic('ParameterSarah');
ParameterSarah;
%case decision
if f_lag > 0 % decision braking or driving mode
fun =@(x) (A/SOC_1)*(x(1))^2+(A/SOC_2)*(x(2))^2+(A/SOC_3)*(x(3))^2+(A/SOC_4)*(x(4))^2+d_delay_1-(SW_1+x(5))+d_delay_2-(SW_2+x(6))+d_delay_3-(SW_3+x(7))+d_delay_4-(SW_4+x(8));
else
fun =@(x) ((B*M_reg_1)/SOC_1)*(1/(x(1))^2)+((B*M_reg_2)/SOC_2)*(1/(x(2))^2)+((B*M_reg_3)/SOC_3)*(1/(x(3))^2)+((B*M_reg_4)/SOC_4)*(1/(x(4))^2)+d_delay_1-(SW_1+x(5))+d_delay_2-(SW_2+x(6))+d_delay_3-(SW_3+x(7))+d_delay_4-(SW_4+x(8));
end
%objektive function
x0 = [150,150,150,150,3,3,3,3];
A = [];
b = [];
Aeq = [0 0 0 0 1 0 0 0; 0 0 0 0 0 1 0 0; 0 0 0 0 0 0 1 0; 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0; cos(d_1) cos(d_2) cos(d_3) cos(d_4) sin(d_1)*c_w sin(d_2) sin(d_3)*c_w sin(d_4)*c_w; sin(d_1) sin(d_2) sin(d_3) sin(d_4) cos(d_1)*c_w cos(d_2)*c_w cos(d_3)*c_w cos(d_4)*c_w; (cos(d_1)*w_vl-l_v*sin(d_1)) (-cos(d_2)*w_vr+sin(d_2)*l_v) (cos(d_3)*w_hl+l_h*sin(d_3)) (-cos(d_4)*w_hr+sin(d_4)*l_h) (sin(d_1)*c_w*w_vl+cos(d_1)*c_w*l_v) (sin(d_2)*c_w*w_vr+cos(d_2)*c_w*l_v) (-sin(d_3)*c_w*w_hl-cos(d_3)*c_w*l_h) (cos(d_4)*c_w*l_h+sin(d_4)*c_w*w_hl)];
SOC_dif1=SOC_1-(SOC_1+SOC_2+SOC_3+SOC_4);
SOC_dif2=SOC_2-(SOC_1+SOC_2+SOC_3+SOC_4);
SOC_dif3=SOC_3-(SOC_1+SOC_2+SOC_3+SOC_4);
SOC_dif4=SOC_4-(SOC_1+SOC_2+SOC_3+SOC_4);
if f_lag==1
if (SOC_dif1 < 0.15) % state of charge of one actor is really low, actor is only used for steering %during driving mode
%SOC_krit = 0.15
Aeq(6,1) = 1;
end
if (SOC_dif2 < 0.15) % state of charge of one actor is really low, actor is only used for steering %during driving mode
Aeq(6,2) = 1;
end
if (SOC_dif3 < 0.15) % state of charge of one actor is really low, actor is only used for steering %during driving mode
Aeq(6,3) = 1;
end
if (SOC_dif4 < 0.15) % state of charge of one actor is really low, actor is only used for steering %during driving mode
Aeq(6,4) = 1;
end
end
% active stimulation for beta
%if k = 1 there is a stimulation
% k, j and n are Inputs, j and n are the actors stimulated
if k == 1
(Aeq(5,j) == 1) && (Aeq(5,n) == -1) && (beq(5) == delta_TaA); % target moduls % delta torque
end
beq = [d_1-SW_1, d_2-SW_2, d_3-SW_3, d_4-SW_4, 0, 0, Fx_v, Fy_v, Mz_v];
%ceq =[Fy_v-sin(d_1)*x(1)-cos(d_1)*x(5)*c_w+sin(d_2)*x(2)-cos(d_2)*x(6)*c_w+sin(d_3)*x(3)-cos(d_3)*x(7)*c_w+sin(d_4)*x(4)-cos(d_4)*x(8)*c_w; Mz_v-(-w_vl)*(cos(d_1)*x(1)-sin(d_1)*x(5)*c_w)+w_vr*(cos(d_2)*x(2)-sin(d_2)*x(6)*c_w)-w_hl*(cos(d_3)*x(3)-sin(d_3)*x(7)*c_w)+w_hr*(cos(d_4)*x(4)-sin(d_4)*x(8)*c_w)+l_v*(sin(d_1)*x(1)-cos(d_1)*x(5)*c_w+sin(d_2)*x(2)-cos(d_2)*x(6)*c_w)-l_h*(sin(d_3)*x(3)-cos(d_3)*x(7)*c_w+sin(d_4)*x(4)*c_w-cos(d_4)*x(8)*c_w); Fx_v-(x(1)/cos(d_1))-c_w*sin(d_1)*x(5)+(x(2)/cos(d_2))-c_w*sin(d_2)*x(6)+(x(3)/cos(d_3))-c_w*sin(d_3)*x(7)+(x(4)/cos(d_4))-c_w*sin(d_4)*x(8)];
%case decision braking or driving backwards (decision input: flag=0 oder 1)
%braking mode
if flag > 0 %if it is possible the regenerative braking torque is the limit
if (Fx_v-sum(Fx(i),(1:4)) == 0) && (Fy_v-sum(Fy(i),(1:4)) == 0) && (Mz_v-sum(Mz(i),(1:4)) == 0) %if it is not possible to follow the target trajectory the maximum of the manual braking is used
% max. regenerative braking torque
lb = [-M_reg_1/r_dyn_1, -M_reg_2/r_dyn_2, -M_reg_3/r_dyn_3, -M_reg_4/r_dyn_4, d_min_vl-SW_1, d_min_vr-SW_2, d_min_hl-SW_3, d_min_hr-SW_4]; %d and betato the left side are positive, to the right side are negative %regenerative braking torque must be positive
% mechanical braking
else
lb = [FxB_max, FxB_max, FxB_max, FxB_max, d_min_vl-SW_1, d_min_vr-SW_2, d_min_hl-SW_3, d_min_hr-SW_4]; %d and betato the left side are positive, to the right side are negative %FxB_max is negative
end % close conditions for braking loop, max braking torque is positive
%driving mode
else
lb = [P_min_1, P_min_2, P_min_3, P_min_4, d_min_vl-SW_1, d_min_vr-SW_2, d_min_hl-SW_3, d_min_hr-SW_4];
end
ub = [P_max_1/(r_dyn_1*w_1), P_max_2/(r_dyn_2*w_2), P_max_3/(r_dyn_3*w_3), P_max_4/(r_dyn_4*w_4), d_max_vl-SW_1, d_max_vr-SW_2, d_max_hl-SW_3, d_max_hr-SW_4];
nlcon = @nonlnconstraints;
options = optimoptions(@fmincon, 'MaxFunctionEvaluations',10.000000e+03);
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nlcon,options);
end
The optimization before was working, but now I dont get any outputs.
Do I have to add anything for the outputs?
I want to get all 8 variables of the output vector x
Thank you very much for your held.
  1 Comment
Sindar
Sindar on 20 Jan 2020
It looks like you are reading the input backwards, and would probably get errors because of it. If you want to pass parameters in 'u', and split them into 'Fx_v','Fy_v', etc., you need to do it this way:
function x = fcn(u)
%definition of vehicle forces and torque
Fx_v = u(1);
Fy_v = u(2);
Mz_v = u(3);
% ...

Sign in to comment.

Answers (0)

Categories

Find more on Brakes and Detents in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!