0

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 * .

Arun Kumar
  • 103
  • 13

1 Answers1

0

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!

Community
  • 1
  • 1
Mikhail_Sam
  • 10,602
  • 11
  • 66
  • 102
  • can you tell me how to use this for s_function?? – Arun Kumar Jun 17 '15 at 08:15
  • @ArunKumar I added some information in my answer for you, look at this. In my example I create bus signal in PreLoad and then use it in any s-function – Mikhail_Sam Jun 17 '15 at 12:46
  • The problem i am facing is not with creating bus for vectors...i just need to add dimensions(2D) to a element and it becomes vector. But am facing problem when i am writing s-functions for this vector data. When I try to retrieve an individual element of this vector and copy it to the output it doesn't display the value properly in scope.I thought my method is wrong but it seems there is something wrong with memory allocation. Anyway I have to figure it out myself instead of posting. but I will accept your answer anyway – Arun Kumar Jun 17 '15 at 13:50
  • I think you are right - simulink has some specific memory allocation. I avoid all this problems by describing data types carefully: look at my example - I don't write `a = BusSignal` at the beginning of my function! Every time I set al dimensions for every variable before I use it - all this zeros. Try this! – Mikhail_Sam Jun 17 '15 at 13:55