Problem with compiler C to use int16_t type for translation in Q15 format

12 views (last 30 days)
I have a problem with compiler C. I have this error message
Error using legacycode.LCT.legacyCodeImpl
The data type "int16_t" is neither a built-in data type, nor defined by
a Simulink.AliasType, Simulink.NumericType, Simulink.Bus object, or an
enumerated data type:
--> int16_t y1 = ControlloDig(int16_t u1)
Error in legacy_code (line 101)
[varargout{1:nargout}] = legacycode.LCT.legacyCodeImpl(action,
Error in Discretizza_controllo_generazione_sfun (line 77)
legacy_code('sfcn_cmex_generate', def);
I did a function that works in Q15 format but, when I use the script to compile it, Matlab shows an error on the data type "int16_t". If you need the other files i can upload them.

Answers (2)

Sebastian Kammer
Sebastian Kammer on 26 May 2017
Hi there,
I am afraid it will not be possible to provide a very helpful answer based on the limited information you have provided. The error message itself is not quite useful. Ideally you should provide a detailed explanation of:
  • What you are trying to do
  • How you are trying to do it
  • At what stage of the process you encounter the error
Just by looking at the error message, I am guessing that you are probably trying to use the Legacy Code Tool to build a C MEX S-function from some hand-written C code.
The main issue looks like the "int16_t" data type is not defined. One potential reason for this could be that you are missing a header file in your C code that contains the data type definition. Try adding the following "include" directive to your C code.
#include <tmwtypes.h>
I hope this helps.

Bisca on 27 May 2017
Hi Sebastian,
Thanks for the help, you guessed the problem. Sorry if I gave few details about the problem. I'm going to write an s-function for a dc-motor control. In particular, the s-function could be work in Q15 format and when i do this Matlab shows me that error. I try to do your suggest but the problem persist. I notice that the error disappear when I use only int16. For this reason, I think that the error is on the int16_t because Matlab doesn't reconize the datatype.
Now I write the code
clear all
close all
%%Gain PI
Ki = 4;
Kp = 1;
% ---------------------------------
% method = 'zoh';
contol's transfer function
PI=tf([Kp Ki],[1 0])
PI_discr=c2d(PI, Ts, method)
[numD, denD, Ts]=tfdata(PI_discr);
%%generazione codice versione con double
fID=fopen ('ControlloDig.h', 'wt');
fprintf(fID, '#include <stdint.h> \n\n');
fprintf(fID, '#include <tmwtypes.h> \n\n');
fprintf(fID, '#include <stdlib.h> \n\n');
fprintf(fID, '#ifndef _CONTROLLO_DIG_H_ \n');
fprintf(fID, '#define _CONTROLLO_DIG_H_ \n\n');
fprintf(fID, '#define Q15 \n\n');
fprintf(fID, '#define Q15(X) ((X < 0.0) ? (int16_t)(32768*(X) - 0.5) : (int16_t)(32767*(X) + 0.5)) \n\n');
fprintf(fID, '#define iQ15(X) ((X < 0.0) ? (double)(X+1)/32768.0 : (double)(X-1)/32767.0) \n\n');
fprintf(fID, '#define K (1 << (Q - 1)) \n\n');
fprintf(fID, 'static int16_t A[2]; \n');
fprintf(fID, 'static int16_t B; \n');
fprintf(fID, 'static int16_t ek=0; \n');
fprintf(fID, 'static int16_t uk=0; \n\n');
fprintf(fID, 'static int16_t ek_1=0; \n');
fprintf(fID, 'static int16_t uk_1=0; \n\n');
fprintf(fID, 'static int16_t y1=0; \n\n');
fprintf(fID, 'int16_t ControlloDig(int16_t ek); \n\n');
fprintf(fID, '#endif \n');
fclose (fID);
% per una guida più dettagliata, guarda link seguente:
def = legacy_code('initialize');
def.SourceFiles = {'ControlloDig.c'};
% def.HeaderFiles = {'Copy_of_ControlloDig.h'};
def.HeaderFiles = {'ControlloDig.h'};
def.SFunctionName = 'ControlloDigFun16';
def.OutputFcnSpec = 'int16_t y1 = ControlloDig(int16_t u1)';
def.SampleTime='parameterized'; % permette il settaggio dei parametri
legacy_code('sfcn_cmex_generate', def);
legacy_code('compile', def);
legacy_code('slblock_generate', def);

Sign in to comment.


Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!