genlib: Processing file: System_IF.sci
at line 58 of function genlib
at line 42 of function tbx_build_macros ( C:\Program Files\scilab-2024.0.0\modules\modules_manager\macros\tbx_build_macros.sci line 56 )
at line 6 of function buildmacros ( C:\Users\Vimal\Desktop\spacecraft-gnc-tbx-devel-kalman-filter-block\macros\buildmacros.sce line 9 )
at line 14 of executed file C:\Users\Vimal\Desktop\spacecraft-gnc-tbx-devel-kalman-filter-block\macros\buildmacros.sce
at line 13 of function tbx_builder ( C:\Program Files\scilab-2024.0.0\modules\modules_manager\macros\tbx_builder.sci line 26 )
at line 40 of function tbx_builder_macros ( C:\Program Files\scilab-2024.0.0\modules\modules_manager\macros\tbx_builder_macros.sci line 55 )
at line 35 of function main_builder ( C:\Users\Vimal\Desktop\spacecraft-gnc-tbx-devel-kalman-filter-block\builder.sce line 45 )
at line 54 of executed file C:\Users\Vimal\Desktop\spacecraft-gnc-tbx-devel-kalman-filter-block\builder.sce
exprs = list(
^
Error: syntax error, unexpected end of line
genlib: Error in file C:\Users\Vimal\Desktop\spacecraft-gnc-tbx-devel-kalman-filter-block\macros\System_IF.sci.
Below is the code
function [x, y, typ] = System_IF(job, arg1, arg2)
// Initialize output variables
x = [];
y = [];
typ = [];
// Debugging: Display when entering the function
disp('Entering System_IF function');
disp('Job argument:');
disp(job);
disp('Type of job:'), disp(typeof(job));
// Check and display arg1 and arg2
if argn(2) >= 2 then
disp('arg1 provided:');
disp(arg1);
disp('Type of arg1:'), disp(typeof(arg1));
else
arg1 = [];
disp('arg1 not provided, defaulting to empty.');
end
if argn(2) >= 3 then
disp('arg2 provided:');
disp(arg2);
disp('Type of arg2:'), disp(typeof(arg2));
else
arg2 = [];
disp('arg2 not provided, defaulting to empty.');
end
// Main switch-case block to handle different jobs
select job
case 'define' then
disp('Job: define - Proceeding to define the block.');
// Define default model parameters
A_matrix = eye(4); // 4x4 State transition matrix
B_matrix = [0; 0; 0; 0]; // 4x1 Control input matrix
C_matrix = [1, 0, 0, 0]; // 1x4 Output matrix
D_matrix = 0; // 1x1 Feedthrough matrix
x0 = [0; 0.1; 0; 0.1]; // 4x1 Initial state estimate
P_initial = eye(4); // 4x4 Initial covariance matrix
// Display default parameters for debugging
disp('Default state matrix A:');
disp(A_matrix);
disp('Default input matrix B:');
disp(B_matrix);
disp('Default output matrix C:');
disp(C_matrix);
disp('Default feedthrough matrix D:');
disp(D_matrix);
disp('Default initial state x0:');
disp(x0);
disp('Default initial covariance P_initial:');
disp(P_initial);
// Define the model for the block
model = scicos_model();
model.sim = list('System_SIM', 5); // Link to the simulation function
model.in = 1; // One input: control
model.out = [-1; -1]; // Outputs with variable sizes
model.outsz = [1; 4]; // Sizes of outputs: first is scalar, second is vector of size 4
model.outtyp = ones(2,1); // Both outputs are of type 1 (double precision)
model.state = [x0; P_initial(:)]; // [x0; P_initial], 4 + 16 = 20 elements
model.rpar = [A_matrix(:); B_matrix(:); C_matrix(:); D_matrix(:)]; // 16 + 4 + 4 + 1 = 25 elements
model.blocktype = 'c'; // Continuous block
model.dep_ut = [%t, %f]; // Time-dependent, no direct feedthrough
// Define expressions as a list
exprs = list(
'eye(4)',
'[0; 0; 0; 0]',
'[1, 0, 0, 0]',
'0',
'[0; 0.1; 0; 0.1]'
);
// Initialize exprs_in with exprs
exprs_in = exprs;
// Create the block with a 6x6 size
disp('Defining the System block with standard_define.');
x = standard_define([6 6], model, exprs);
x.graphics.id = 'System';
x.graphics.label = 'System'; // Ensure label is set
x.graphics.style = 'blockWithLabel';
disp('System block creation complete.');
case 'set' then
disp('Job: set - Updating block parameters.');
// Ensure arg1 is provided (the block data)
if typeof(arg1) == 'undefined' then
disp('Error: arg1 (block data) is undefined.');
return;
end
x = arg1; // Retrieve the block data
graphics = x.graphics;
model = x.model;
// Display current expressions for debugging
disp('Current expressions (exprs):');
disp(graphics.exprs);
disp('Type of exprs:'), disp(typeof(graphics.exprs));
// Display number of expressions
num_exprs = size(graphics.exprs, 'r'); // number of rows
disp('Number of expressions: ' + string(num_exprs));
// Initialize default expressions
A_str = 'eye(4)';
B_str = '[0; 0; 0; 0]';
C_str = '[1, 0, 0, 0]';
D_str = '0';
x0_str = '[0; 0.1; 0; 0.1]';
// If exprs has five expressions, extract them
if num_exprs >= 5 then
A_str = graphics.exprs(1);
B_str = graphics.exprs(2);
C_str = graphics.exprs(3);
D_str = graphics.exprs(4);
x0_str = graphics.exprs(5);
else
disp('Error: Not enough expressions. Expected five.');
// Proceed with default expressions
end
// Define dialog labels for the input fields
labels = [
'State matrix A (4x4):';
'Input matrix B (4x1):';
'Output matrix C (1x4):';
'Feedthrough matrix D (1x1):';
'Initial state x0 (4x1):'
];
// Define the expected data types for each input field
types = list('mat', [4,4], 'vec', [4,1], 'mat', [1,4], 'scalar', [1,1], 'vec', [4,1]);
// Prepare the expressions list
exprs_in = list(A_str, B_str, C_str, D_str, x0_str);
// Try to open the dialog and get user input
disp('Opening scicos_getvalue dialog with current parameters...');
try
// Capture all required outputs from scicos_getvalue
[ok, A_str, B_str, C_str, D_str, x0_str, exprs_out] = scicos_getvalue('Set System Parameters', labels, types, exprs_in);
catch
disp('Error: scicos_getvalue failed to open dialog box.');
// Assign default values and proceed
ok = %f;
end
if ok then
disp('Evaluating expressions provided from the dialog...');
try
// Evaluate each expression
A_matrix = evstr(A_str);
B_matrix = evstr(B_str);
C_matrix = evstr(C_str);
D_matrix = evstr(D_str);
x0 = evstr(x0_str);
catch
disp('Error: Failed to evaluate matrix expressions. Using default values instead.');
A_matrix = eye(4);
B_matrix = [0; 0; 0; 0];
C_matrix = [1, 0, 0, 0];
D_matrix = 0;
x0 = [0; 0.1; 0; 0.1];
end
// Update graphics expressions
graphics.exprs = exprs_out;
else
// Dialog was canceled, use existing expressions
disp('Dialog was canceled or scicos_getvalue failed. Using existing expressions.');
// Evaluate existing expressions
try
A_matrix = evstr(A_str);
B_matrix = evstr(B_str);
C_matrix = evstr(C_str);
D_matrix = evstr(D_str);
x0 = evstr(x0_str);
catch
disp('Error: Failed to evaluate existing expressions. Using default values.');
A_matrix = eye(4);
B_matrix = [0; 0; 0; 0];
C_matrix = [1, 0, 0, 0];
D_matrix = 0;
x0 = [0; 0.1; 0; 0.1];
end
end
// Now, ensure that A_matrix, B_matrix, etc., are always defined
// Debugging: Display evaluated matrices
disp('A_matrix (after eval):');
disp(A_matrix);
disp('Type of A_matrix:'), disp(typeof(A_matrix));
disp('Dimensions of A_matrix:'), disp(size(A_matrix));
disp('B_matrix (after eval):');
disp(B_matrix);
disp('Type of B_matrix:'), disp(typeof(B_matrix));
disp('Dimensions of B_matrix:'), disp(size(B_matrix));
disp('C_matrix (after eval):');
disp(C_matrix);
disp('Type of C_matrix:'), disp(typeof(C_matrix));
disp('Dimensions of C_matrix:'), disp(size(C_matrix));
disp('D_matrix (after eval):');
disp(D_matrix);
disp('Type of D_matrix:'), disp(typeof(D_matrix));
disp('Dimensions of D_matrix:'), disp(size(D_matrix));
disp('Initial state x0 (after eval):');
disp(x0);
disp('Type of x0:'), disp(typeof(x0));
disp('Dimensions of x0:'), disp(size(x0));
// Checking matrix dimensions for correctness.
disp('Checking matrix dimensions for correctness.');
if size(A_matrix, 1) <> 4 | size(A_matrix, 2) <> 4 then
disp('Error: State matrix A must be 4x4.');
disp('Default A_matrix set to eye(4).');
A_matrix = eye(4);
end
if size(B_matrix, 1) <> 4 | size(B_matrix, 2) <> 1 then
disp('Error: Input matrix B must be 4x1.');
disp('Default B_matrix set to [0; 0; 0; 0].');
B_matrix = [0; 0; 0; 0];
end
if size(C_matrix, 1) <> 1 | size(C_matrix, 2) <> 4 then
disp('Error: Output matrix C must be 1x4.');
disp('Default C_matrix set to [1, 0, 0, 0].');
C_matrix = [1, 0, 0, 0];
end
if size(D_matrix, 1) <> 1 | size(D_matrix, 2) <> 1 then
disp('Error: Feedthrough matrix D must be 1x1.');
disp('Default D_matrix set to 0.');
D_matrix = 0;
end
if size(x0, 1) <> 4 | size(x0, 2) <> 1 then
disp('Error: Initial state x0 must be 4x1.');
disp('Default x0 set to [0; 0.1; 0; 0.1].');
x0 = [0; 0.1; 0; 0.1];
end
// Flatten matrices and update block parameters
disp('Flattening matrices and updating model.rpar...');
model.rpar = [A_matrix(:); B_matrix(:); C_matrix(:); D_matrix(:)];
disp('model.rpar set to:');
disp(model.rpar);
disp('Size of model.rpar:'), disp(size(model.rpar));
// Check if model.rpar has 25 elements; if not, set default values
if size(model.rpar, 1) <> 25 then
disp('Error: model.rpar does not have 25 elements. Assigning default values.');
model.rpar = [eye(4)(:) ; zeros(4,1)(:) ; [1, 0, 0, 0]'; 0];
end
// Correctly initialize the state vector by concatenating the initial state and covariance matrix
disp('Setting model.state with initial state and flattened covariance matrix...');
P_initial = eye(4); // Default covariance
model.state = [x0; P_initial(:)]; // [x0; P_initial], 4 + 16 = 20 elements
disp('model.state set to:');
disp(model.state);
disp('Size of model.state:'), disp(size(model.state));
// Apply updated graphics back to block
x.graphics = graphics;
// Apply updated model back to block
x.model = model;
disp('Block parameters updated successfully.');
else
// Handle unknown job types
disp('Error: Unknown job "' + string(job) + '".');
return;
end
endfunction