Main Content

Access Custom C++ Code in Stateflow Charts

This example shows how to integrate custom C++ code with Stateflow® charts in Simulink® models. By sharing data and functions between your custom code and your Stateflow charts, you can augment the capabilities of Stateflow and take advantage of your preexisting code. For more information, see Reuse Custom Code in Stateflow Charts.

In this example, a chart that uses C as the action language calls a custom code function named adderOutput. This function changes the value of the global custom variable adderVar by a specified amount. The chart stores the output of this function as the chart output counter.

When counter is less than or equal to 100, the chart calls the custom function to increase the global variable by the chart input increment. When counter is greater than 100, the chart calls the custom function to decrease the global variable by the chart input increment.

Use Wrappers to Access Custom Code Files

To integrate your custom C++ code, use wrappers. In this example, the model uses the existing source file adder_cpp.cpp and the header file adder_cpp.h. The source file defines a class called adder that has two methods, add_one and add_two.

adder::adder()
{
    int_state = 0;
}
int adder::add_one(int increment)
{
    int_state += increment;
    return int_state;
}
int adder::get_val()
{
    return int_state;
}

To access the adder class without modifying the existing code, the model uses the wrapper file adder_wrapper.c. The wrapper file defines methods that create, delete, and receive output from instances of the adder class.

adder *createAdder()
{
    return new adder;
}
void deleteAdder(adder *obj)
{
    delete obj;
}
double adderOutput(adder *obj, int increment)
{
    obj->add_one(increment);
    return obj->get_val();
}

Choose a C++ Compiler

To view or change the default compiler, in the MATLAB® Command Window, enter:

mex -setup c++

For more information, see Choose a C++ Compiler. For a list of supported compilers, see Supported and Compatible Compilers.

Include Custom Code Files for Simulation

Configure your simulation target and select C++ as the custom code language:

  1. Open the Configuration Parameters dialog box.

  2. In the Simulation Target pane, set the Language parameter to C++.

  3. In the Code Information tab, specify your header and source files, as described in Configure Custom Code.

When you simulate the model, the Scope block shows the value of the chart output counter increase or decrease by the value of the chart input increment.

C++ Code Generation

Select C++ as the code generation language and configure your model to use the same custom code settings specified for the simulation target:

  1. Open the Configuration Parameters dialog box.

  2. In the Code Generation pane, set the Language parameter to C++.

  3. In the Code Generation > Custom Code pane, select Use the same custom code settings as Simulation Target.

  4. Generate C++ code as described in Generate Code Using Simulink Coder (Simulink Coder) or Generate Code Using Embedded Coder (Embedded Coder).

See Also

Topics

External Websites