Monday, July 15, 2024
HomeMatlabSpecifying Variable Values Utilizing Mathematical Expressions: Generated Code » Man on Simulink

Specifying Variable Values Utilizing Mathematical Expressions: Generated Code » Man on Simulink

To assist answering these questions, I’m glad to welcome again visitor blogger David Balbuena.

Default Conduct

My first thought after I learn Man’s latest submit on slexpr was “What about code technology?!”
At the moment I’ll present how you should utilize Embedded Coder™ to protect parameter expressions within the generated code.

Let’s take the a part of Man’s mannequin that makes use of the variable space (I will clarify why I’m leaving out the hypotenuse later):


The place space is created utilizing this code:

a = Simulink.Parameter(3);

b = Simulink.Parameter(4);

space = Simulink.Parameter(slexpr(“(a*b)*0.5”));

In case you generate code utilizing the default settings of Simulink.Parameter objects, the evaluated worth of space will get inlined:

Specifying Storage Lessons to Protect Expressions

It’s attainable for Embedded Coder to protect the mathematical expression within the generated code by making two adjustments:

  1. For a and b change the storage class to Outline
  2. For space, set the storage class to ExportedGlobal
I like to make use of the Code Mappings Editor (ctrl+shift+C) for that, since I can mass-edit code technology settings with ctrl+click on:

Then within the code, space will present up as a tunable parameter:


And the definition for space accommodates the mathematical expression:

As a result of I used the Outline storage class for a and b, these present up as macros within the mannequin header file:


These are usually not the one storage lessons you should utilize. The final rule is:

  • For parameters used within the expression (a and b), the storage class has to generate a macro, similar to Outline and ImportedDefine
  • For the derived parameter (space), storage class have to be non-Auto (for instance ExportedGlobal or ExportToFile)

In my instance, I omitted the hypotenuse (sqrt(a^2+b^2)) parameter as a result of solely sure mathematical expressions are supported, and the calculation for the hypotenuse has two unsupported components: the sqrt() perform and the ^ operator

You may learn extra of the small print and limitations within the documentation.

Now it is your flip

We’re conscious that this characteristic comes with many limitations and helps solely a restricted set of operations. We’re contemplating including help for extra operations and eradicating a few of these limitations. In case you are interested in this characteristic however are impacted by a limitation, tell us within the feedback under to assist us prioritize.


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments