Hello everyone,
I would like to consult with you about a problem I encountered while working with Simulink.
I am trying to implement AUV trajectory tracking control using acados with the EXTERNAL cost function type. I am passing my trajectory data using the parameter p
as described in the official example. I successfully completed the MATLAB script simulation, and the results are great, thanks to the acados maintainers!
Below is the output from my controller:
Next, following the official example, I used make_sfun_sim and make_sfun to generate the necessary files for the S-function. I then ran the simulation in Simulink, but encountered some issues. Specifically, the controller’s output exhibits oscillations, which seem to be related to time—both the time intervals and their durations. The graphs are shown below:
Below is the Simulink model I have built
"I hope the official team and fellow forum members can provide some guidance. Thank you in advance for your help
Hi 
differences between the behaviour in MATLAB and Simulink are typically due to different initializations. Please check how the iterates are initialized in Matlab and Simulink.
As a next step, you could compile acados with the flag ACADOS_DEBUG_SQP_PRINT_QPS_TO_FILE. With this flag, the QPs as well as the iterates are saved to file and you can compare when and where the differences between Simulink and Matlab occur.
Hope this is helpful!
Katrin
Thank you for your guidance, the issue has been resolved. The error in the Simulink simulation was due to an incorrect extraction of the expected state. However, I still have one more question: When I use EXTERNAL
to define the cost function, does the expected state have to be passed through the parameter p
? In my code, it is as follows:
**model.p = x_ref;**
model.name = 'auv_model';
cost_expr_ext_cost_0 = 0.5 * u' * R * u;
cost_expr_ext = 0.5 * (x - **x_ref**).' * Q * (x - x_ref) + 0.5 * u .' * R * u;
cost_expr_ext_e = 0.5 * (x - x_ref).' * Qe * (x - x_ref);
model.cost_expr_ext_cost_0 = cost_expr_ext_cost_0;
model.cost_expr_ext_cost = cost_expr_ext;
model.cost_expr_ext_cost_e = cost_expr_ext_e;
Then, I pass the parameters p for each stage in the simulation, as shown in the following image:
for i = 1:Tstep
Xref = Refall(:, i : i+N);
for n = 1 : N+1
ocp_solver.set('p', Xref(:,n), n-1);
end
...
end
Additionally, compared to the official Simulink model, my model has an extra input parameter. What is this input parameter?
Also, this parameter has no impact on my simulation because the desired trajectory is the controller input. Originally, I thought it was the interface for the parameter p
in my model.
Yes with EXTERNAL
you need to model the state reference as a parameter (with the cost module NONLINEAR_LS
this would be yref
)
Not sure what you mean with additional input. When you create the S-function, a list of the available ports with a short description is printed. Did you check there?
1 Like
The terminal output is as follows:
- x0, initial state, size [10]
- u, size [5]
- parameters, size [10]
I don’t understand what the third input (parameters) is. Initially, I thought it was the parameter p
used to pass parameters for each stage. However, in my simulation, the parameters (the desired states) are directly passed to the controller via the parameters_traj
interface. So, I don’t understand the purpose of the third input parameter in the auvModel
.
The input port description you printed corresponds to the one of an integrator. For an integrator, there is only one parameter value, no trajectory.
If your parameters just enter the cost, it doesn’t matter which values they have for the integrator.
1 Like
Got it, thank you both for the answers.
1 Like