Problems transforming S-Function block code to S-Function Builder C-Code

1 view (last 30 days)
Hello,
I need a S-Function and the .tlc file for a simulation in RTW.
I did the M-Code for the S-Function block and it works properly, but i have problems with the transformation to C-Code in the S-Function Builder block. It doesn´t works correctly.
How it works?
The block receive an input (u), it compares with a threshold (umbral), and if it is bigger the new value is the input. If not, the value is the last value stored. The final value (new or stored) is the output (y).
*The first if condition is because i have 10 seconds at the beginning with 0´s.
My S-Function:
global Theta_anterior
global i
global j
global y
global umbral
if flag==2,
if (u(1) == 0 & i == 0)
y = u(1);
elseif (u(1) ~= 0 & i == 0)
y = u(1);
Theta_anterior = u(1);
i=i+1;
elseif (abs(u(1) - Theta_anterior) > umbral)
y = u(1);
Theta_anterior = u(1);
elseif (abs(u(1) - Theta_anterior) <= umbral)
y = Theta_anterior;
end
elseif flag==3
sys=[y] ;
elseif flag==0
Theta_anterior=0 ;
i=0;
j=0;
y=0;
umbral = (0.2*pi)/180;
disp('inicio')
sys=[0 0 1 1 0 0] ;
x0=[];
elseif flag==9
disp('final')
end;
In the Builder i do:
S-Function parameters:
Theta_anterior=0
i=0
j=0
And the C-Code:
double variable=0,Theta_actual=0,umbral=0.035;
double *entrada,*anterior,*var,*cont;
entrada = &Theta_actual;
anterior = &Theta_anterior;
var = &variable;
cont=&i;
*entrada = u[0];
*var = *entrada - *anterior;
if (*entrada == 0 && i == 0){
y[0] = *entrada;
}
else if (*entrada != 0 && i == 0){
y[0] = *entrada;
*anterior = *entrada;
*cont++;
}
else if (fabs(variable) > umbral) {
y[0] = *entrada;
*anterior = *entrada;
}
else if (fabs(variable) <= umbral){
y[0] = *anterior;
}
The compiler don´t show me errors
What is wrong?
Thanks!!

Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!