Search code examples
vectorsimulinkbus

Sending vector data in the bus


I have a vector data (an array variable for example float32 mydata[5];). for transmitting a single primitve/basic data in a bus its pretty simple.

inside_data=Simulink.BusElement;
inside_data.Name='somename';
inside_data.SampleTime = -1;
inside_data.datatype='single';

this element can be put inside a using

Bus=Simulink.Bus;
Bus.Elements=inside_data;

But this works when the input is a primitive. But what if my data is a vector. like float32 a[5]; then how can i send this data element in a bus.

UPDATE So I tried to use a constant block named a with datatype single in which the input part i changed it as [1 2 3] which is a vector input. another element is b with uint8 datatype.

i used the s-function builder just to check the working of this model. i already set everything (bus_mode on , datatype to be bus type etc). in the output part i used something like:

y0[0]=u0->a[0];
y0[1]=u0->a[1];
y0[2]=u0->a[2];
y1[0]=u0->b;

But it throws error as

c:\program files (x86)\matlab_v7111_r10bsp1\extern\include\matrix.h(313) : error C2061: syntax error : identifier 'mxLogical' 
c:\program files (x86)\matlab_v7111_r10bsp1\extern\include\matrix.h(313) : error C2059: syntax error : ';' 

my final aim is to use it for s_function

so if i declare a variable in s_func as

real32_T *a_output[5]=(real32_T *)ssGetOutputPortRealSignal(S,0); 

and then i have a strcuture(because am transmitting data with a bus so the bus header file has this structure) and how do i declare and assign the input to the output.

a_output[0]=my_struct->a_input[0];
a_output[1]=my_struct->a_input[1];
a_output[2]=my_struct->a_input[2];
a_output[3]=my_struct->a_input[3];
a_output[4]=my_struct->a_input[4];

but the problem is with the declaration. it gives me error cannot convert from real32_T to real32_T * .


Solution

  • The main idea is to create Bus of type you need. I did it this way:

    num = zeros(15,15,15);
    move   = zeros(15,15,15);
    a = struct('number',num,'movement', move);
    
    busInfo = Simulink.Bus.createObject(a);
    

    You can see it's possible to create any data structure, array, vector anything you want and then create Bus Signal of the same type.

    I use this in callbacks/preLoadFcn (Model Explorer) to define this type in workspace, it creates slBus1 variable (its Bus Signal of my type), so I need to define output (or input if necessary) of any block like slBus1 only. And then use Bus Selector to work array data.

    Can it helps to you?

    ADD NEW INFORMATION

    It depends of what you want. For example: I create s-function for feedback system. It use my structure like this:

    function a = fcn(BusSignal)
    %#codegen
    num = zeros(15,15,15);
    move   = zeros(15,15,15);
    %...some math actions with num and move...
    a = struct('number',num,'movement', move);
    %...and some action with a structure... for example:
    if (c>b)
        a.number(1,2,3) = 15;
        a.movement(1,2,3)   = 42;
    else
        a = BusSignal;
    end
    

    Look at this - I use Bus signal at entrance and at exit and use Bus Selector to work it's data with. REMEMBER to define input and output data as Bus Signals!