Greetings,
Importing C function code, which was previously exported from Typhoon HIL Schematic editor, requires calling functions defined in the header file exported from Typhoon. Functions for initialization and execution of the code take structures as arguments, therefore it is also necessary to prepare these structures and initialize the structure members.
First, let’s look at the practical example, by exporting the code from the Typhoon HIL Boost converter example model, where the control part consists of the PID regulator, logical and mathematical blocks.
The part of the model we want to export, we will put inside a subsystem and rename all input and output ports. Afterward, C code export will be executed. If you want to learn more about C code export from Typhoon, please take a look at this link.
Now, when the C code is exported, let’s replace this subsystem with a new C code function block. In the General tab, all inputs and outputs should be defined.
In the additional sources tab, all exported files should be included (except backup files).
Now, let’s open the header file (boostcontrol.h) that we exported. There you can find declarations of model data structure that consist of inputs, outputs, sinks, and states. It is necessary to define these structures in the Typhoon Arbitrary variables section, so that Typhoon can access the ones provided in additional sources. Below you can see the structure declaration that we need to copy in the Arbitrary variable section:
When we are copying these declarations to the Arbitrary variable, it is necessary to delete the “extern” keyword.
Now, let’s focus on the init_fnc section. Back in the header file (boostcontrol.h), there is also an initialization function that needs to be run in the init_func of Functions tab:
However, before we call the init function, we have to assign the addresses of variables we defined in Variables arbitrary definitions to ModelData structure member pointers. When we finish, our init_fnc tab should look like this:
Lastly, we can focus on the output_fnc tab. At the beginning of each execution rate, we first have to assign inputs of the C function block to Model Data input structure members (boostcontrol_ExtIn). Afterward, we call the step function while passing the address of the Model Data structure, and in the end we feed our output variables from Model Data output structure members (boostcontrol_ExtOut).
After this step, you can finally compile the model and load the model to HIL device or run it in TyphoonSim.
Additionally, if you want to import dll file instead of a C code, please take a look at this Application note – How to import DLL file for C function, or this section of a C function block documentation – Library import tab.
If you have any additional questions, feel free to post them here.
Hope this helps!