Guy on Simulink

Simulink & Model-Based Design

Using Simulink Functions to Simulate Hardware Services

This week, I want to introduce another feature that becomes useful when combined with theInitialize FunctionReset FunctionandTerminate Functionblocks.

In R2014b,Simulink Functionsgot introduced. Inthis post,we highlighted how Simulink Functions can be used to create anExport-Function model. In this case, the Simulink Functions are placed inside a model. In simulation, you can reference the model and call the functions using the Function Caller block.

Simulink functions used as function library

你可以创erate code for the function library model and call those functions in any way you want from hand-written code.

The opposite

One thing we did not mention at that time, is that it is possible to use Simulink Functions and the Function Caller block in completely opposite way, where the referenced model has a Function Caller block, calling Simulink Functions present in the top model.

Simulation harness

Why would someone want to do that? In short, the answer is to simulate custom code that is not directly available for simulation. Let's see how this works.

Using the Function Caller block to call external code

If we generate code for the child codeGenModel.slx in the above picture model, the code will look like:

Generated Code

By default, this code would not build, since the compiler would not know where to find the functiontimesTwo. However, if you configure your model properly, the functiontimesTwocan come from anywhere you want. For example, it could be in a dynamic library against which you will link to on your embedded target. For this example, imagine that there is atimesTwoservice available in thistimesTwo.cfile:

Custom Code

In the model configuration, I specify that this file should be included in the build process:

Custom Code Configuration

This allows me to generate an executable that calls my customtimesTwo.cimplementation.

Conclusion

To summarize, the idea is to create a Simulink Function that emulates the behavior of external software. You can build a simulation harness model that references your code generation model and it will see the Simulink Function. When time comes to generate code for the child model, it will have no idea the Simulink Function exists, but instead will link against any external code you specify.

It is important to note that this technique is just one way to include custom code in the code generated from Simulink.

If the custom code was available on the host, I would recommend going with anS-Functionto wrap and reuse the same custom code both in simulation and code generation.

However if the code is unavailable, for example because it is available as an OS service on the target embedded processor, this approach can be interesting.

What's next?

Now that we have introduced the concepts ofExport Function models,State Reader and Writer blocks,and this way of using Simulink Functions, I believe we have all the pieces to make a realistic example illustrating how we see theInitialize FunctionReset FunctionandTerminate Functionblocks being used... next week.

|

コメント

コメントを残すには、ここをクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。