Can't find where caused this "Unable to compile data properties" error in Simulink

52 views (last 30 days)
Hello, I was trying to run this code script in Matlab Function in Simulink, however I encountered the Model error and it showed that error in the figure and didn't tell me where it caused. After I preliminary debugging , I think the problems will be in the parts of "path", but the code script runs well in the Matlab Workspace, so I don't know where caused the error. May you help me to check please. Thank you a lot!
Below is my code script in Matlab Function1
function path_adjusted = fcn(waypoints, velocity, window_length, P_N, P_E)
persistent current_index last_index path;
if isempty(current_index)
current_index = 1;
end
if isempty(last_index)
last_index = 0;
end
if isempty(path)
path = zeros(2*window_length, 2);
end
distance_threshold = 30;
poly_degree = 3;
regulator_weight = 0.5;
path_raw = zeros(length(waypoints), 2);
path_adjusted = zeros(2*window_length, 2);
if last_index > 0
last_window_point = waypoints(last_index, 1:2);
distance_to_last_point = norm([P_N, P_E] - last_window_point);
if distance_to_last_point >= distance_threshold
path_adjusted = path;
return;
end
end
start_index = max(current_index, last_index + 1);
end_index = min(last_index + window_length, length(waypoints));
% Check if all waypoints are already processed
if start_index > length(waypoints)
path_adjusted = path;
return;
end
waypoints_window = waypoints(start_index:end_index, :);
velocity_window = velocity(start_index:end_index, :);
time_window = linspace(0, 1, size(waypoints_window, 1));
P_ = zeros((poly_degree+1), (poly_degree+1));
qx = zeros((poly_degree+1), 1);
qy = zeros((poly_degree+1), 1);
for j = 1:size(waypoints_window, 1)
t = time_window(j);
Ti0 = [1; t; t^2; t^3];
Tie0 = Ti0 * Ti0';
Ti1 = [0; 1; 2*t; 3*t^2];
Tie1 = Ti1 * Ti1';
P_ = P_ + Tie0 + Tie1;
qx = qx + -2*waypoints_window(j, 1)*Ti0 + -2*velocity_window(j, 1)*Ti1;
qy = qy + -2*waypoints_window(j, 2)*Ti0 + -2*velocity_window(j, 2)*Ti1;
end
t_last = time_window(end);
t_init = time_window(1);
% Combining the Regular Term
Tir_last = [0 0 0 0; 0 0 0 0; 0 0 4*t_last 6*t_last^2; 0 0 6*t_last^2 12*t_last^3];
Tir_init = [0 0 0 0; 0 0 0 0; 0 0 4*t_init 6*t_init^2; 0 0 6*t_init^2 12*t_init^3];
Tir = Tir_last - Tir_init;
P_ = P_ + window_length*regulator_weight*Tir;
P = 2*P_;
P = 0.5*(P+P');
initial_guess_x = [waypoints(1, 1); 0; 0; 0];
initial_guess_y = [waypoints(1, 2); 0; 0; 0];
% Solve QP
options = optimoptions('quadprog', ...
'Algorithm', 'active-set', ... % Use 'active-set' explicitly
'Display', 'off'); % Suppress output for real-time compatibility
coeff_x = quadprog(P, qx, [], [], [], [], [], [], initial_guess_x, options);
coeff_y = quadprog(P, qy, [], [], [], [], [], [], initial_guess_y, options);
target_pos_poly_x = coeff_x(1) + coeff_x(2)*time_window + coeff_x(3)*time_window.^2 + coeff_x(4)*time_window.^3;
target_pos_poly_y = coeff_y(1) + coeff_y(2)*time_window + coeff_y(3)*time_window.^2 + coeff_y(4)*time_window.^3;
path_raw = [target_pos_poly_x(:), target_pos_poly_y(:)];
if last_index > 0
path = circshift(path,-window_length);
path(window_length+1:end, :) = path_raw;
else
path(window_length+1:end, :) = path_raw;
end
path_adjusted = path;
last_index = end_index;
if end_index < length(waypoints)
current_index = current_index + 1;
end
end

Accepted Answer

Githin George
Githin George on 6 Dec 2024 at 7:49
Hello Cheng,
I suspect that the issue is arising because the size of the output signal of for your “MATLAB Function Block” cannot be determined at compilation time. Notice that “window_length” is an input to your function which means that it is a Simulink signal which may change during the simulation thereby affecting the size of your output signal “path_adjusted”.
See the line:
path_raw = zeros(length(waypoints), 2);
I tried to reproduce the issue with the following setup:
Model Init Code (I‘ve assumed some random data here):
% Define waypoints as an Nx2 matrix, where N is the number of waypoints
waypoints = [
0, 0;
10, 5;
20, 15;
30, 25;
40, 35
];
% Define velocities as an Nx2 matrix
velocity = [
1, 0.5;
1, 0.5;
1, 0.5;
1, 0.5;
1, 0.5
];
% Define other parameters
window_length = 2; % The number of waypoints to process in each window
P_N = 0; % Current North position
P_E = 0; % Current East position
Model Setup: See ModelSetup.png
Notice that after making some changes to the output variable configuration I was able to make the function work.
Default Output Signal Configuration: See DefaultConf.png
Since, “window_length” is set to 2 and my model has a “Constant” block I know that the Signal Size is [4 2] for the output “path_adjusted”. But the compilation fails with errors if I do not set the “Variable Size” property to “true”.
Modified Output Signal Configuration: See ModifiedConf.png
Note that after turning the “Variable Size” checkbox to “true” there are no compilation errors, and I get the output as shown in image 1. To learn more about Variable Size Signals, refer: https://www.mathworks.com/help/simulink/ug/variable-size-signal-basics.html
Based on this information, you may to want to consider modifying your algorithm, or go about the modelling in a different way assuming that “window_length” may change like a parameter during the simulation.
  1 Comment
Cheng Huan
Cheng Huan on 6 Dec 2024 at 8:18
Greeting Githin,
Thank you for your answering. After I followed your steps and modified my algorithm, it successfully compilted!!! I really appreciate you! I can move forward my research now. Hope all your researches go successful.
Best wish

Sign in to comment.

More Answers (0)

Categories

Find more on General Applications in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!