Monday, July 22, 2024
HomeMatlabCustomizing the Code Generated from Simulink » Man on Simulink

Customizing the Code Generated from Simulink » Man on Simulink

I need to begin by supplying you with a heads up that visitor blogger Erin McGarrity shall be coming again quickly with extra posts about Simulink Coder and Embedded Coder. Earlier than going deeper into code era matters, I believed it could be a good suggestion to re-visit the fundamentals of customizing the code generated from Simulink fashions utilizing Embedded Coder.
It has been virtually 10 years for the reason that final time I wrote a fundamental introduction to code era on this weblog. As you may think about, issues have modified quite a bit since then, so this week I’m sharing the 2023 model of a fundamental introduction to Embedded Coder.

Default Habits

Let’s take this straightforward instance mannequin and generate code for it utilizing Embedded Coder

By default, the “step” perform for this mannequin seems to be like this:

Whereas this code is legitimate, there are a number of causes for desirous to customise it.

For this instance, I’ll make the generated code look extra like what a human would write, utilizing easy variables like u and y as an alternative of grouping them within the construction modelname_Y.

Customizing Code utilizing Simulink.Parameter and Simulink.Sign Objects

If I do not need the variables being saved in these modelname_Y, modelname_P and modelname_DW buildings, the “basic” strategy is to outline Simulink.Sign and Simulink.Parameter objects for the totally different modeling components and specify a special storage class:

u.CoderInfo.StorageClass = ‘ExportedGlobal’;

y.CoderInfo.StorageClass = ‘ExportedGlobal’;

x.CoderInfo.StorageClass = ‘ExportedGlobal’;

a.CoderInfo.StorageClass = ‘ExportedGlobal’;

b.CoderInfo.StorageClass = ‘ExportedGlobal’;

Within the mannequin, I affiliate these objects with indicators and block properties:

And the generated code now seems to be like this:

Customizing Code utilizing Code Mapping

A number of years in the past, we launched the Code Mapping panel to offer you a central location the place you may configure how the totally different modeling components in a Simulink mannequin map to C code.
To get began, you first launch the Embedded Coder App. This may be executed from the Apps Gallery within the Simulink toolstrip, or utilizing the keyboard shortcut Ctrl+Shift+C. As I described in a earlier publish, my favourite strategy to launch this type of app is hitting Ctrl+. to launch the Actions search and sort what I want:

Within the C Code tab, the Code Interface dropdown incorporates the Code Mapping associated entries:

The very first thing I can attempt is altering the default storage class and see if I can get what I need. I set the Inports, Outports, States and Exterior Parameters default storage class to ExportedGlobal:

and now the code I get is:

The information shouldn’t be saved in buildings anymore and the parameters are displaying up as a and b, however I nonetheless must do a further step for the indicators and states.

Code Mapping works properly teamed up with the Property Inspector. For Inputs, Outputs, Alerts and States, Property Inspector permits me to specify the identifier I need.

As soon as the identifiers have been specified for ports and states, I can generate code and ensure that it behaves as I count on:

Notice that these two methods (parameter/sign objects and Code Mapping) will not be mutually unique. It’s doable to specify code era properties utilizing sign and parameter objects on prime of Code Mapping. That is helpful if you’d like a finer granularity in your specification and/or if you happen to want to maintain these specs outdoors of the mannequin file.

Now it is your flip

I like to recommend visiting the Code Mapping documentation to learn extra on this subject.

After studying this fundamental introduction, which different code era matters would you wish to see in future posts? Tell us within the feedback beneath.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments