"[verboseMessage] time = 0] {} Kalman Filter Simulation: Calling with flag Initialization(4)"
"[verboseMessage] time = 0] {} Kalman Filter Simulation: Error: block.rpar does not have enough elements for Initialization."
"[verboseMessage] time = 0] {} System_SIM: Calling with flag Initialization(4)"
at line 225 of function System_SIM ( C:\Users\Vimal\Desktop\spacecraft-gnc-tbx\spacecraft-gnc-tbx-8935351021ede09bc5089260c3361f6251d26eb6\macros\System_SIM.sci line 229 )
at line 1 of function scicosim
at line 1 of executed string
at line 252 of function xcos_simulate ( C:\Program Files\scilab-2024.0.0\modules\xcos\macros\xcos_simulate.sci line 268 )
"Function not defined for given argument type(s),"
" check arguments or define function %l_e for overloading."
How can I put check and confirm that how much does block.rpar elements require?
Why on other hand it says xx function is overloading?
// System_SIM.sci
// Simulation file for the System block.
// Implements state-space system dynamics.
function block = System_SIM(block, flag)
// Internal function for verbose messaging
function verboseMessage(debugMessage)
disp("[verboseMessage] time = " + string(scicos_time()) + "] {" + block.label + "} System_SIM: " + debugMessage);
endfunction
select flag
case -5 // Error
verboseMessage("Calling with flag Error(-5)")
case 0 // State Derivative Computation
verboseMessage("Calling with flag StateDerivativeComputation(0)")
// **1. Check and Initialize block.x**
if size(block.x, "*") >= 4 then
x_sys = block.x(1:4);
disp("x_sys:");
disp(x_sys);
else
verboseMessage("Error: block.x does not have enough elements. Initializing to default [0; 0; 0; 0].");
x_sys = [0; 0; 0; 0];
// Extend block.x if necessary
if size(block.x, "*") < 4 then
additional_zeros = 4 - size(block.x, "*");
block.x = [block.x; zeros(additional_zeros, 1)];
disp("block.x extended with additional zeros:");
disp(block.x);
end
end
// **2. Check and Initialize block.inptr**
if size(block.inptr, "*") >= 1 then
u_sys_ptr = block.inptr(1);
if size(u_sys_ptr, "*") >= 1 then
u_sys = u_sys_ptr(1);
disp("u_sys:");
disp(u_sys);
else
verboseMessage("Error: u_sys_ptr does not have enough elements. Setting u_sys to 0.");
u_sys = 0;
end
else
verboseMessage("Error: block.inptr does not have at least one element. Setting u_sys to 0.");
u_sys = 0;
end
// **3. Check and Initialize block.rpar**
expected_rpar_length = 25; // 16 (A) + 4 (B) + 4 (C) + 1 (D)
current_rpar_length = size(block.rpar, "*");
if current_rpar_length >= expected_rpar_length then
// Extract A matrix (4x4)
A = reshape(block.rpar(1:16), 4, 4);
// Extract B matrix (4x1)
B = reshape(block.rpar(17:20), 4, 1);
disp("A_matrix:");
disp(A);
disp("B_matrix:");
disp(B);
else
verboseMessage("Error: block.rpar does not have enough elements for A and B matrices. Initializing A as Identity and B as Zero.");
// Define A as Identity matrix manually
A = [
1, 0, 0, 0;
0, 1, 0, 0;
0, 0, 1, 0;
0, 0, 0, 1
];
// Define B as Zero vector manually
B = [
0;
0;
0;
0
];
disp("A_matrix (Default):");
disp(A);
disp("B_matrix (Default):");
disp(B);
// Append default A and B to block.rpar if possible
missing_elements = expected_rpar_length - current_rpar_length;
if missing_elements >= 20 then
// Append A (16 elements) and B (4 elements)
default_A = [
1;
0;
0;
0;
0;
1;
0;
0;
0;
0;
1;
0;
0;
0;
0;
1
];
default_B = [
0;
0;
0;
0
];
block.rpar = [block.rpar; default_A; default_B];
disp("block.rpar extended with default A and B matrices.");
else
// Not enough space to append; assign defaults without extending
verboseMessage("Warning: Not enough elements to append A and B. Using defaults without modifying block.rpar.");
end
end
// **4. Compute State Derivative**
x_dot = A * x_sys + B * u_sys;
disp("x_dot:");
disp(x_dot);
// **5. Assign to block.xd**
block.xd = x_dot;
case 1 // Output Computation
verboseMessage("Calling with flag OutputComputation(1)")
// **1. Check and Initialize block.x**
if size(block.x, "*") >= 4 then
x_sys = block.x(1:4);
disp("x_sys:");
disp(x_sys);
else
verboseMessage("Error: block.x does not have enough elements. Initializing to default [0; 0; 0; 0].");
x_sys = [0; 0; 0; 0];
// Extend block.x if necessary
if size(block.x, "*") < 4 then
additional_zeros = 4 - size(block.x, "*");
block.x = [block.x; zeros(additional_zeros, 1)];
disp("block.x extended with additional zeros:");
disp(block.x);
end
end
// **2. Check and Initialize block.inptr**
if size(block.inptr, "*") >= 1 then
u_sys_ptr = block.inptr(1);
if size(u_sys_ptr, "*") >= 1 then
u_sys = u_sys_ptr(1);
disp("u_sys:");
disp(u_sys);
else
verboseMessage("Error: u_sys_ptr does not have enough elements. Setting u_sys to 0.");
u_sys = 0;
end
else
verboseMessage("Error: block.inptr does not have at least one element. Setting u_sys to 0.");
u_sys = 0;
end
// **3. Check and Initialize block.rpar**
expected_rpar_length = 25; // 16 (A) + 4 (B) + 4 (C) + 1 (D)
current_rpar_length = size(block.rpar, "*");
if current_rpar_length >= expected_rpar_length then
// Extract C matrix (1x4)
C = reshape(block.rpar(21:24), 1, 4);
// Extract D scalar (1x1)
D = block.rpar(25);
disp("C_matrix:");
disp(C);
disp("D_matrix:");
disp(D);
else
verboseMessage("Error: block.rpar does not have enough elements for C and D matrices. Initializing C as [1 0 0 0] and D as 0.");
// Define C manually
C = [1 0 0 0];
// Define D manually
D = 0;
disp("C_matrix (Default):");
disp(C);
disp("D_matrix (Default):");
disp(D);
// Append default C and D to block.rpar if possible
missing_elements = expected_rpar_length - current_rpar_length;
if missing_elements >= 5 then
// Append C (4 elements) and D (1 element)
default_C = [
1;
0;
0;
0
];
default_D = [
0
];
block.rpar = [block.rpar; default_C; default_D];
disp("block.rpar extended with default C and D matrices.");
else
// Not enough space to append; assign defaults without extending
verboseMessage("Warning: Not enough elements to append C and D. Using defaults without modifying block.rpar.");
end
end
// **4. Compute Output**
y_sys = C * x_sys + D * u_sys;
disp("y_sys:");
disp(y_sys);
// **5. Assign Outputs**
if size(block.outptr, "*") >= 2 then
block.outptr(1) = y_sys; // Output 1: y_sys
block.outptr(2) = y_sys * 2; // Output 2: y_sys with gain (for Kalman Filter)
disp("Output 1 (y_sys):");
disp(block.outptr(1));
disp("Output 2 (y_sys * 2):");
disp(block.outptr(2));
else
verboseMessage("Error: block.outptr does not have enough elements.");
end
case 4 // Initialization
verboseMessage("Calling with flag Initialization(4)")
// **1. Initialize State**
if size(block.state, "*") >= 4 then
x0 = block.state(1:4);
disp("Initialization state (x0):");
disp(x0);
block.x = x0;
else
verboseMessage("Error: block.state does not have enough elements. Assigning default [0; 0.1; 0; 0.1].");
x0 = [0; 0.1; 0; 0.1];
block.x = x0;
end
// **2. Check and Initialize block.rpar**
expected_rpar_length = 25; // 16 (A) + 4 (B) + 4 (C) + 1 (D)
current_rpar_length = size(block.rpar, "*");
if current_rpar_length < expected_rpar_length then
verboseMessage("Error: block.rpar does not have enough elements for Initialization. Initializing block.rpar with default A, B, C, D matrices.");
// Define default A matrix manually
A_default = [
1;
0;
0;
0;
0;
1;
0;
0;
0;
0;
1;
0;
0;
0;
0;
1
];
// Define default B matrix manually
B_default = [
0;
0;
0;
0
];
// Define default C matrix manually
C_default = [
1;
0;
0;
0
];
// Define default D scalar manually
D_default = [
0
];
// Assign default rpar
block.rpar = [A_default; B_default; C_default; D_default];
disp("block.rpar has been initialized with default values:");
disp(block.rpar);
else
verboseMessage("block.rpar already has enough elements for Initialization.");
end
case 2 // Discrete State Update
verboseMessage("Calling with flag DiscreteStateUpdate(2)")
// Implement discrete state update logic here
case 3 // Output Event Timing
verboseMessage("Calling with flag OutputEventTiming(3)")
// Implement output event timing logic here
case 5 // Ending
verboseMessage("Calling with flag Ending(5)")
// Implement termination logic here
case 6 // Reinitialization
verboseMessage("Calling with flag Reinitialization(6)")
// Implement reinitialization logic here
case 9 // Zero-Crossing
verboseMessage("Calling with flag Zero-Crossing(9)")
// Implement zero-crossing logic here
else // Unknown flag
verboseMessage("Calling with unknown flag(" + string(flag) + ")")
end
endfunction