# xcosValidateBlockSet

**URL:** <https://scilab.discourse.group/t/xcosvalidateblockset/492>\
**Category:** Xcos\
**Created:** [May 2, 2024, 3:29pm UTC](https://scilab.discourse.group/t/xcosvalidateblockset/492 "2024-05-02T15:29:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vimsrocz](https://avatars.discourse-cdn.com/v4/letter/v/ed8c4c/32.png) [@Vimsrocz](https://scilab.discourse.group/u/Vimsrocz)\
**Post date:** [May 2, 2024, 3:29pm UTC](https://scilab.discourse.group/t/xcosvalidateblockset/492/1 "2024-05-02T15:29:10Z")

</div>

[xcosValidateBlockSet](https://help.scilab.org/xcosValidateBlockSet.html)

With the help of above function, I have tried to validate the block. But there is an error with

```auto
        case "define" then

```

This is whole code as below

```auto
// Kalman_Filter_IF.sci
// The interface file for the generic Kalman Filter block.
// This block is externally activated to take measurements data.
// Takes the continuous positions velocity or accelerations as input, adds some 
// errors (noise, scale, nonlinearity, misalignment) and gives
// it as the output.

// Abbrev.
// IF: Inertial Frame, BF: Body Frame, 

// Model input: 
// 2x2 Real - State vector with position and velocity BF w.r.t. IF 
// 1 Real - Covariance vector

// Model output:
// 2x2 Real - Single axis gyro measurement output w.r.t. BF
// 1x1 Real - Error flag
// 0: No error
// 1: Positive saturation error
// 2: Negative saturation error

// Model states:
// 1x1 Real - Random walk error

// Model parameters:
// 2x2 Real - State Vector measurement unit vector w.r.t. BF (Unit vector)
// 1x1 Real - Noise mean (bias) of the sensors
// 1x1 Real - Noise stddev of the sensors
// 1x1 Real - Scale error
// 2x1 Real - Misalignment error 
// 1x1 Real - Random walk error
function [x, y, typ] = Kalman_Filter_IF(job, arg1, arg2)
    funcprot(0);
    x = [];
    y = [];
    typ = [];
    getInputFromSim = 1;
    initial_state = [0.1; 0.0]; // 2*1 matrix
    initial_covariance = [1.0, 0.0; 0.0, 1.0]; // 2*2 matrix
    
    select job
        // Initialize with mask parameters if any
        case "plot" then
            standard_draw(arg1);
        case "getinputs" then
            [x, y, typ] = standard_inputs(arg1);
        case "getoutputs" then
            [x, y, typ] = standard_outputs(arg1);
        case "getorigin" then
            [x, y] = standard_origin(arg1);

        case "set" then
            // Assignments here comes after assignments of the execution with the "define" flag 
            x = arg1; // This line is essential, otherwise gives "Invalid index." error.
            graphics = arg1.graphics;
            exprs = graphics.exprs;
            model = arg1.model;
            disp('line27')
            if getInputFromSim == 1 then
                exec("macros/Kalman_Filter_Utils.sci",-1); // Not sure about this line in MATLAB, comment it if not necessary
                title = "Set Parameter";
                labels = ['Initial state [x_dot(velocity), x(position)] which is position in ECEF frame and velocity'; 
                          'Covariance which gives the uncertainty at position and velocity'];
                // Changed 'Stiffness' to 'Covariance'
                // In future we can add {'Accelerations', 'Orientations which is obation altitude control system'}
                types = list('vec', 2, 'mat', [2,2]); // Changed 'vec' to 'mat' for Covariance
                endLoop = %f;
                // Create a dialog for getting user inputs
                while ~endLoop do
                    // Initialize dialog for parameters and initial state
                    [ok, State, Covariance, exprs] = getvalue(title, labels, types, exprs); 
                    errMsg = [];

                    // Check return set conditions of the initial state as per meetings
                    if ok then
                        // depends on what type of data we are having
                        if State<0 then // first condition of position and velocity
                            errMsg = ["The velocity is negative [m/s]"];
                        end 

                        if Covariance<0 then // second condition of velocity
                            errMsg = [errMsg; 'Kalman_Filter_IF: There is an no noise present in covariance as it is negative!'];
                        end

                        if isempty(errMsg) then
                            disp("Setting parameters...");
                            [model.rpar, isError] = EncodeRparIpar_KF(model.rpar, State, Covariance);
                            disp('line57')
                            graphics.exprs = exprs;
                            x.graphics = graphics;
                            x.model = model;
                            endLoop = %t;
                            // Check parameters
                            // You can add additional checks for input parameters here if needed
                disp('line64')
                            // Accept inputs and save them
                        else
                            messagebox(errMsg);
                            error(errMsg, 20000);
                            endLoop = %f;
                        end
                    else
                        warning('Kalman_Filter_IF: Set: Cancel!');
                        endLoop = %t;
                    end
                end
            else
                model.state = [concatAB(initial_state,initial_covariance)];
                model.rpar = initial_covariance; // Use the covariance matrix as a parameter
                graphics.exprs = exprs;
                x.graphics = graphics;
                x.model = model;
                break
                message("Failed to update block io");
            end
            disp('line85')
            // Concatenate the covariance matrix to the state vector
            // model.state = [State; Covariance(:)]; // Use the concatenation operator ":" for the matrix
        // Define block properties
                    disp('line89')
        case "define" then
            disp('line91')
            // Create object
            model = scicos_model();
                            disp('line93')
            // Provide name and type
            model.sim = list('Kalman_Filter_sim', 5); // what is case 4 and 5? 
                        disp('line96')
            // Define inputs and outputs
            // One input with a variable-size "double" element
            model.in = [2;2]; // first dimension of the first input whcih is row 2*1 and 2*2 hence it will be [2;2]
            model.in2 = [1;2]; // the second dimension of I/P port is [1;2] from above 
            model.intyp = [1;1]; // all the vector and matrices are real value
            disp('line101')
            model.out = [2;2]; // first dimension of the first output whcih is row 2*1 and 2*2 hence it will be [2;2]
            model.out2 = [1;2]; // the second dimension of O/P port is [1;2] from above 
            model.outtyp = [1;1]; // all the vector and matrices are real value
            //model.state = [initial_state,matrix(initial_covariance)]; // Use the matrix concatenation operator ":" to convert matrix to column vector model.state=[State;Covariance]
            model.state = [concatAB(initial_state,initial_covariance)];
            model.rpar = [initial_covariance(:)]; // Use the covariance matrix as a parameter
            // Define block properties
            model.blocktype = "c";
            model.dep_ut = [%t,%f]; // what does %t and %f indicates? true and false
                            disp('line109')
            // Set block properties
            exprs = ["[0.1; 0.0]";"1.0, 0.0; 0.0, 1.0"]; // How do we get this value?
            x = standard_define([4 4], model, exprs);
            x.graphics.style = ['blockWithLabel;displayedLabel=Kalman_Filter'];
            // other cases other than set or define sections
            
    end
endfunction

```

After line 89, it showing an error  
\*\* msg =

status =

T \*\*  
Can anyone direct me what is this error means?

---

<div class="post-metadata">

**Author:** ![davidcl](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/davidcl/32/1029_2.png) [@davidcl](https://scilab.discourse.group/u/davidcl)\
**Post date:** [May 6, 2024, 6:54am UTC](https://scilab.discourse.group/t/xcosvalidateblockset/492/2 "2024-05-06T06:54:30Z")

</div>

Hello,

Using your code and calling it with `Kalman_Filter_IF("define")`, I got :

```auto
  "line91"

  "line93"

  "line96"

  "line101"
at line 108 of function Kalman_Filter_IF ( E:\nosave\Documents\Kalman_Filter_IF.sci line 139 )

Undefined variable: concatAB

```

It seems you have to concatenate the `initial_state` to the `initial_covariance` in `model.state`. With some editing, I was able to fix the call with:

```scilab
model.state = [initial_state ; initial_covariance(:)];

```

---

<div class="post-metadata">

**Author:** ![Vimsrocz](https://avatars.discourse-cdn.com/v4/letter/v/ed8c4c/32.png) [@Vimsrocz](https://scilab.discourse.group/u/Vimsrocz)\
**Post date:** [May 6, 2024, 8:26am UTC](https://scilab.discourse.group/t/xcosvalidateblockset/492/3 "2024-05-06T08:26:45Z")

</div>

Can you please provide me some option so that I can share .zip file with you? It is multiple file with inbuilt toolbox built up.

[https://drive.google.com/drive/folders/1F3Ci2zuLhOrqyhbDCR-wULM3sLe\_L\_9n?usp=sharing](https://drive.google.com/drive/folders/1F3Ci2zuLhOrqyhbDCR-wULM3sLe_L_9n?usp=sharing)

1- Execute builder.sce which is outside of the folder  
2-then execute runSim.sce

if there is an issue then run utils file from macros which is Kalman\_Filter\_Utils.sce then follow step 1 and step 2. Thank you sir.

---

<div class="post-metadata">

**Author:** ![Vimsrocz](https://avatars.discourse-cdn.com/v4/letter/v/ed8c4c/32.png) [@Vimsrocz](https://scilab.discourse.group/u/Vimsrocz)\
**Post date:** [May 6, 2024, 8:28am UTC](https://scilab.discourse.group/t/xcosvalidateblockset/492/4 "2024-05-06T08:28:54Z")

</div>

This is the issue if you dont run Utils file first then function is not executed and we are calling the functions. Hence please run/execute the Kalman\_Filter\_Utils.sce file then repeat all steps like

1- Execute builder.sce which is outside of the folder  
2-then execute runSim.sce
