+3 votes
245 views
by Dimitrije Jelić (140 points)
Hi!

I want to import C code in Typhoon HIL Schematic Editor C function code block, that I previously exported from Typhoon HIL. However, I am not sure how to properly call the functions from the C code I imported in the C function block, can you help me?

1 Answer

+3 votes
by Dimitrije Jelić (140 points)
selected by Dimitrije Jelić
 
Best answer

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!

by Center of Renewable Energy Lab (10 points)
I don't know why you are adopting this approach. Usually people go for C-code generation for SIL testing; like in MATLAB, there should be an atomic block for SIL testing once the C-code of a block gets generated.
However, happy to see a community forum.
by Ognjen G. (289 points)
@Dimitrije Perhaps it would make sense to explain why would someone be motivated to do this "loop" within the Typhoon HIL toolchain? Usually users export C code for RCP purposes and by then it is used on another development platform (i.e. CCS by Texas Instruments) or it is already loaded directly to the target if it is a target-specific code.
by Dimitrije Jelić (140 points)
@Center for Renewable Energy Lab I agree that this approach might seem cumbersome. However, we created a generic solution that can import ANSI C code previously exported from Typhoon or any other solution such as Matlab. We understand that some use cases are more frequent than others, therefore we will try to make these more straightforward. Thank you for your feedback!
...