Include your own header file during code generation

Hello everyone,

My goal is to simulate a control loop in PLECS and generate the code for the controller using PLECS. During code generation, certain variables are assigned the data type int8_t. When building the code, an error occurs stating that this data type is undefined. Now, I want to add a custom-written header file (Typeint.h) to the code generation process so that I don’t have to manually replace all occurrences of int8_t. For that I need the custom-written header file (Typeint.h) in the generated header file from Plecs.

I am attaching the PLECS model and the header file (as jpg)
test_Lp.plecs (8.4 KB)

You can just use #include <stdint.h> to use the int8_t datatype. Note your attached model didn’t have any C-Scripts.

If you need more customization see the C-Scripts + C-Script Examples + Load External Files section of the PLECS Help.

Note you can click on the “Open example model” link at the bottom of the page.

thanks but the goal is that int8_t don’t get generated is that possoble?

Ah you aren’t using the C-Script block and manually coding but rather generating it from PLECS? I don’t see any int8_t data types in the generated code, but there are other types. One can configure datatypes in PLECS using the Data type block or other configurations in the block (e.g. Inputs, Gain, and Constant blocks all have a data type setting).

I’ll also note that the transfer function blocks you’re using all default to the code generation floating point format. If you need further control you might want to implement your biquads using delay blocks in one of the standard forms or using the C-Script block.

Okay. Thanks a lot. I made a big mistake and uploaded the false model. Here is the correct model where int8_t data types are generated.
The code is only generated for the Controler (Gc).

(attachments)

test.plecs (16 KB)

Similar to the transfer function block, the int8_t is an internal data type used by the discrete integrator block for tracking the first execution of the integrator block.

There’s a lot of context missing here which would be helpful to add, but reading between the lines you’re working with a compiler that doesn’t have an int8_t datatype defined in <stdint.h>, which is the case for the TI CGT compiler. So, you’re using another tool to compile the code, which requires an external software architecture, so one could simply add the typedef at a higher level.

There is a way to add it into the PLECS model using a special Target IO block, as is shown in the attached model, but this isn’t really a supported workflow and there’s limited documentation to take this further.

Test_bl.plecs (16.3 KB)

Yes, that’s correct. I’m using Analog Devices with the Compiler "C/C++ for SHARC (21xx).

I can’t find the TargetIO block in Plecs (I’m using Plecs 4.9.4) but I can copy your block into my model.

Can you give me some more information about the workflow with the TargetIO block? When I build the code from plecs the data type int8_t still is generated.

The int8_t type will always be generated by the discrete integrator block. You cannot change that behavior.

You have two options: 1) include your Typeint.h header file in your external project and define the meaning of undefined 8 bit types there or 2) Use the Target IO block to include the header file yourself.

Choosing option 1 seems like the obvious choice, as you add the include file to your larger C project which is specific to that compiler. You don’t have to try to reverse engineer anything with the target IO blocks. Is there a reason why it is not attractive?

The Target IO block isn’t really intended as a user block. It is a block that has an offline simulation model or can interact with the Code generation process through lua scripts.

Hi, thanks for the information.
I see with the TargetIO block the header file “Typeint.h” is included in the generated c-file. That’s the solution I’m looking for.
Is it possible that the header “Typeint.h” is included in the generated header file form plecs instead of the generated c-file?

There is not a straightforward way without establishing your own custom target. I will note there is a more flexible syntax for adding includes, but this still places the include in the *.c file.

local Include = StringList:new()
Include:append("TypeInt.h")
return {Include = Include}