How do I incorporate my own C code into my Stateflow diagram?

14 views (last 30 days)
The manual is clear as to how to include MATLAB functions within my Stateflow diagram, however, I would like to do this for my own C code function.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 19 Jun 2015
The documentation has been updated for Release 2008a (R2008a). For previous product releases, read below:
Incorporating your own C code (user function) into a Stateflow diagram is documented in Chapter 9 page 9-25 of the Stateflow 4.2.1 User's Guide:
 
<http://www.mathworks.com/help/releases/R2011a/toolbox/stateflow/ug/f0-65269.html#f0-56035>
Within the Stateflow diagram, the function can be called as follows:
 
output=function_name(arg1, arg2,...etc)
The function can be any number of subfunctions written in C. Say for example, you wanted to include the following C code in your Stateflow diagram:
 
int test(int in)
{
int out;
out=in*2;
return(out);
}
Save this file as test.c. Within the Stateflow diagram, refer to test.c through the following syntax:
 
a=test(x)
where x is defined as x=1 inside a state.
In order for Stateflow to link in the appropriate C file when compiling, first open the Stateflow chart, then choose Tools --> Open Simulation Target and then click on Target Options. In the dialog, there is a section called Custom Code (included at top of generated source code). Within this field, type the following:
 
#include "test.c"
Now when you build the Stateflow chart, it will include the appropriate files. Instruction sto these steps can be found at the following link:
<http://www.mathworks.com/help/releases/R2011a/toolbox/stateflow/ug/brj7moh.html#brj0wt2>
If you are using Stateflow 2.0, please read the following:
The previous versions of Stateflow (prior to 2.0) used to generate code for the entire model containing multiple charts in a single source file. Hence, the restrictions on what can or cannot be included in the Custom Code property in Stateflow 2.0 did not apply in prior versions. The new code generation scheme in Stateflow 2.0 generates multiple source and header files making possible intelligent incremental code generation that is essential to building huge models with lots of charts. If you have models developed by pre-2.0 versions of Stateflow (i.e., Stateflow versions 1.0, 1.05, 1.06, 1.07) which have custom code containing global variable definitions and function definitions, you will have to convert them to work with Stateflow 2.0.
Suppose you had the following text in the Custom Code property of the target:
 
#define TRUE 1
#define FALSE 0
int x;
double y=1.2;
void myfun(void)
{
/* some stuff */
}
wherein you were defining a global variable named x and a function named myfun to be accessed by the generated code. Since this code is included by multiple C source files generated by Stateflow 2.0, the compilation would fail because the symbols x and myfun will have multiple definitions.
In order to convert this model to be Stateflow 2.0 compliant, you should follow these steps
1. Create a new header file (named myheader.h, for example) and keep all the #define macros and extern declarations for global variables and functions. It is also a good practice to have "include guards" (shown below). For the example shown above, this file would contain
 
#ifndef MYHEADER_H
#define MYHEADER_H
/* The above is an include guard that makes sure that this
header file is included only once. */
#define TRUE 1
#define FALSE 0
extern int x;
extern double y;
extern void myfun(void);
#endif /*MYHEADER_H*/
2. Create a new source file (named mysrc.c, for example) and keep the global variable definitions and functions in it. For the example shown above, this file would contain
 
#include "myheader.h"
int x;
double y=1.2;
void myfun(void)
{
/* some stuff */
}
3. Add the following text to the Simulation target property "Custom Code included at the top of the generated code"
 
#include "myheader.h"
4. If you keep these two files (myheader.h and mysrc.c) in the same directory as the model file, then specify the following in the Simulation target property "Custom Source Files"
 
mysrc.c
 

More Answers (0)

Categories

Find more on Stateflow in Help Center and File Exchange

Tags

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!