8000 Simulink: add optional input port `p_global`, test blazing splines in Simulink by FreyJo · Pull Request #1275 · acados/acados · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Simulink: add optional input port p_global, test blazing splines in Simulink #1275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/full_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ jobs:
if: always()
with:
release: R2021a
products: Simulink Simulink_Test
cache: true

# just needed for blasfeo_target.h
Expand Down
86 changes: 85 additions & 1 deletion examples/acados_matlab_octave/p_global_example/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
function main()

import casadi.*

% Standard OCP
state_trajectories_no_lut_ref = run_example_ocp(false, false);
state_trajectories_no_lut = run_example_ocp(false, true);
Expand Down Expand Up @@ -66,6 +65,91 @@ function main()
if ~all(abs(state_trajectories_with_lut_ref - state_trajectories_with_lut) < 1e-10)
error("State trajectories with lut=true do not match.");
end
%% Simulink test
if ~is_octave()
run_example_ocp_simulink_p_global();
end

end



function run_example_ocp_simulink_p_global()

import casadi.*
lut = true;
use_p_global = true;
fprintf('\n\nRunning example with lut=%d, use_p_global=%d\n', lut, use_p_global);

% Create p_global parameters
[p_global, m, l, C, p_global_values] = create_p_global(lut);

% OCP formulation
ocp = create_ocp_formulation_without_opts(p_global, m, l, C, lut, use_p_global, p_global_values);
ocp = set_solver_options(ocp);

% Simulink options
simulink_opts = get_acados_simulink_opts();
simulink_opts.inputs.p_global = 1;
possible_inputs = fieldnames(simulink_opts.inputs);
for i = 1:length(possible_inputs)
simulink_opts.inputs.(possible_inputs{i}) = 0;
end
simulink_opts.inputs.lbx_0 = 1;
simulink_opts.inputs.ubx_0 = 1;
simulink_opts.inputs.p_global = 1;

simulink_opts.outputs.xtraj = 1;
simulink_opts.outputs.utraj = 1;
simulink_opts.outputs.u0 = 0;
simulink_opts.outputs.x1 = 0;

ocp.simulink_opts = simulink_opts;

% OCP solver
ocp_solver = AcadosOcpSolver(ocp);

%% Matlab test solve
% test with ones such that update is necessary
p_global_values_test = ones(size(p_global_values));
if use_p_global
ocp_solver.set_p_global_and_precompute_dependencies(p_global_values_test);
end

ocp_solver.solve();
xtraj = ocp_solver.get('x');
xtraj = xtraj(:)';
utraj = ocp_solver.get('u');
utraj = utraj(:)';


%% build s funtion
cd c_generated_code;
make_sfun;
cd ..;

%% run simulink block
out_sim = sim('p_global_simulink_test_block', 'SaveOutput', 'on');
fprintf('\nSuccessfully ran simulink block');

%% Evaluation
fprintf('\nTest results on SIMULINK simulation.\n')

disp('checking KKT residual')
% kkt_signal = out_sim.logsout.getElement('KKT_residual');
xtraj_signal = out_sim.logsout.getElement('xtraj');
xtraj_val = xtraj_signal.Values.Data(1, :);
utraj_signal = out_sim.logsout.getElement('utraj');
utraj_val = utraj_signal.Values.Data(1, :);
if norm(xtraj_val - xtraj) > 1e-8
disp('error: xtraj values in SIMULINK and MATLAB should match.')
quit(1);
end
if norm(utraj_val - utraj) > 1e-8
disp('error: utraj values in SIMULINK and MATLAB should match.')
quit(1);
end
disp('Simulink p_global test: got matching trajectories in Matlab and Simulink!')
end


Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@


%% Simulink opts
simulink_opts = get_acados_simulink_opts;
simulink_opts = get_acados_simulink_opts();

% inputs
simulink_opts.inputs.y_ref = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ static void mdlInitializeSizes (SimStruct *S)
{%- if dims.np > 0 and simulink_opts.inputs.parameter_traj -%} {#- parameter_traj #}
{%- set n_inputs = n_inputs + 1 -%}
{%- endif -%}
{%- if dims.np_global > 0 and simulink_opts.inputs.p_global -%} {#- p_global #}
{%- set n_inputs = n_inputs + 1 -%}
{%- endif -%}
{%- if dims.ny_0 > 0 and simulink_opts.inputs.y_ref_0 -%} {#- y_ref_0 -#}
{%- set n_inputs = n_inputs + 1 -%}
{%- endif -%}
Expand Down Expand Up @@ -243,6 +246,12 @@ static void mdlInitializeSizes (SimStruct *S)
ssSetInputPortVectorDimension(S, {{ i_input }}, (N+1) * {{ dims.np }});
{%- endif %}

{%- if dims.np_global > 0 and simulink_opts.inputs.p_global -%} {#- p_global #}
{%- set i_input = i_input + 1 %}
// parameters
ssSetInputPortVectorDimension(S, {{ i_input }}, 1 + {{ dims.np_global }});
{%- endif %}

{%- if dims.ny > 0 and simulink_opts.inputs.y_ref_0 %}
{%- set i_input = i_input + 1 %}
// y_ref_0
Expand Down Expand Up @@ -606,6 +615,9 @@ static void mdlOutputs(SimStruct *S, int_T tid)
{%- if dims.ny_e > 0 and simulink_opts.inputs.cost_W_e %} {#- cost_W_e #}
{%- set buffer_sizes = buffer_sizes | concat(with=(dims.ny_e * dims.ny_e)) %}
{%- endif %}
{%- if dims.np_global > 0 and simulink_opts.inputs.p_global %} {#- p_global #}
{%- set buffer_sizes = buffer_sizes | concat(with=(dims.np_global)) %}
{%- endif %}

// local buffer
{%- set buffer_size = buffer_sizes | sort | last %}
Expand Down Expand Up @@ -634,6 +646,18 @@ static void mdlOutputs(SimStruct *S, int_T tid)
ocp_nlp_constraints_model_set(nlp_config, nlp_dims, nlp_in, 0, "ubx", buffer);
{%- endif %}

{%- if dims.np_global > 0 and simulink_opts.inputs.p_global -%} {#- p_global #}
{%- set i_input = i_input + 1 %}
in_sign = ssGetInputPortRealSignalPtrs(S, {{ i_input }});
buffer[0] = (double)(*in_sign[0]);
if (buffer[0] != 0)
{
for (int i = 0; i < {{ dims.np_global }}; i++)
buffer[i] = (double)(*in_sign[i+1]);
{{ name }}_acados_set_p_global_and_precompute_dependencies(capsule, buffer, {{ dims.np_global }});
}
{%- endif %}

{%- if dims.np > 0 and simulink_opts.inputs.parameter_traj -%} {#- parameter_traj #}
// parameters - stage-variant !!!
{%- set i_input = i_input + 1 %}
Expand Down
9E81
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
{% endif %}
{%- endif %}

COMPFLAGS = [COMPFLAGS ' {{ solver_options.ext_fun_compile_flags }}'];
CFLAGS = [CFLAGS ' {{ solver_options.ext_fun_compile_flags }}'];

try
% mex('-v', '-O', CFLAGS, LDFLAGS, COMPFLAGS, COMPDEFINES, INCS{:}, ...
Expand Down Expand Up @@ -152,6 +154,13 @@
i_in = i_in + 1;
{%- endif %}

{%- if dims.np_global > 0 and simulink_opts.inputs.p_global -%} {#- p_global #}
input_note = strcat(input_note, num2str(i_in), ') global parameters - first value indicates if update should be performed (0 means no update)\n');
input_note = strcat(input_note, '\tafterwards: new numerical values of p_global, size [1 + {{ dims.np_global }}]\n');
sfun_input_names = [sfun_input_names; 'p_global [1 + {{ dims.np_global }}]'];
i_in = i_in + 1;
{%- endif %}

{%- if dims.ny_0 > 0 and simulink_opts.inputs.y_ref_0 %}
input_note = strcat(input_note, num2str(i_in), ') y_ref_0 - size [{{ dims.ny_0 }}]\n ');
sfun_input_names = [sfun_input_names; 'y_ref_0 [{{ dims.ny_0 }}]'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"lbx_0": 1,
"ubx_0": 1,
"parameter_traj": 1,
"p_global": 0,
"y_ref_0": 1,
"y_ref": 1,
"y_ref_e": 1,
Expand Down
Loading
0