{"id":6040,"date":"2016-12-05T10:15:48","date_gmt":"2016-12-05T15:15:48","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=6040"},"modified":"2019-02-01T09:48:52","modified_gmt":"2019-02-01T14:48:52","slug":"export-function-models","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2016\/12\/05\/export-function-models\/","title":{"rendered":"Export Function Models"},"content":{"rendered":"<p>I started writing this post with the goal of talking about the new <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/initializefunction.html\">Initialize Function<\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/resetfunction.html\">Reset Function<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/terminatefunction.html\">Terminate Function<\/a> blocks, along with the closely related <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/statereader.html\">State Reader<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/statewriter.html\">State Writer<\/a> blocks introduced in R2016b.<\/p>\n<p>However I quickly realized that those new features are very closely related to a type of model architecture I almost never talked about on this blog: <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/export-function-models.html\">Export Function Models<\/a>.<\/p>\n<p><strong>Generating Code<\/strong><\/p>\n<p>Let's use the following model as example. It contains a Bias and Unit Delay block executing at 0.001s and a Math Function block executing at 0.01s.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/flatModel.png\" alt=\"Simple Example model\" \/><\/p>\n<p>As you all probably know, with Embedded Coder it is possible to generate C code from a Simulink model. Using the <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016b\/ecoder\/ug\/select-an-ert-target.html (link no longer works)\">default Embedded Coder System Target File<\/a>, the code you will get will look something like:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/flatStep.png\" alt=\"Code Generated from Simple Example model\" \/><\/p>\n<p>As you can see, the code is made of one Initialize function, and a Step function. The step function is designed to be called at the model base rate, 0.001s in our case. Perfect to execute the code in a single-tasking context.<\/p>\n<p>If you prefer, you can ask Simulink to <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/gui\/treat-each-discrete-rate-as-a-separate-task.html\">Treat each discrete rate as a separate task<\/a>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/multiTaskingconfig.png\" alt=\"Multi-Tasking\" \/><\/p>\n<p>In this case, the generated code will be composed of one function par sample time. It is then possible for you to implement the scheduler and call each rate the way you want.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/flatMultiRate.png\" alt=\"Multi-Tasking Code\" \/><\/p>\n<p><strong>Exporting Functions<\/strong><\/p>\n<p>Having one function per rate is useful, but what if you want more control over the execution of the code? For example, you could want one rate to be divided into multiple tasks, and assign different priority to each of them.<\/p>\n<p>For that, Embedded Coder offers the possibility to <a title=\"https:\/\/www.mathworks.com\/help\/ecoder\/ug\/export-function-call-subsystems.html (link no longer works)\">export functions<\/a>. Exporting functions provides direct control over the generated functions and the ability to simulate their scheduling and prioritization.<\/p>\n<p>To be compatible with the concept of export functions, your model must be <a title=\"https:\/\/www.mathworks.com\/help\/ecoder\/ug\/export-function-call-subsystems.html#bqnrwky-4 (link no longer works)\">built in a specific way<\/a>: The top level of the system for which you want to export functions must contain only Function Call Subsystems, Inports and Outports. That way, one function per subsystem will be generated.<\/p>\n<p>For our example model, we can rearrange it that way:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/exportFunctionSS.png\" alt=\"Export Function Subsystem\" \/><\/p>\n<p>For simulation, the scheduling of the different tasks must be done explicitly, for example using Stateflow as in the above image. When time comes to generate code, you can right-click on the subsystem and export the functions:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/generateSS.png\" alt=\"Export Function Subsystem Model\" \/><\/p>\n<p>The code will look like the following:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/exportFunctionCode.png\" alt=\"Export Function Subsystem Code\" \/><\/p>\n<p>And you will be able to include this code in your hand-written scheduler. As mentioned above, this would allow you to divide one rate into multiple functions, or tasks.<\/p>\n<p><strong><strong>Export Function Model<\/strong><\/strong><\/p>\n<p>For large projects, it is also possible to create <a href=\"https:\/\/www.mathworks.com\/help\/ecoder\/examples\/exporting-functions.html\">Export Function Models<\/a>.<\/p>\n<p>In this case, the top model will be used for simulation only, and you will generate code for the child model. Simulink will automatically recognize that the model is designed to export function, and the code will be similar to the one generated from the subsystem above.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/top_export_model.png\" alt=\"Export Function Model\" \/><\/p>\n<p><strong>What's next?<\/strong><\/p>\n<p>Next week, we will see how the  new <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/initializefunction.html\">Initialize Function<\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/resetfunction.html\">Reset Function<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/terminatefunction.html\">Terminate Function<\/a> blocks can be used inside export function models to simulate the shutdown and restarting of the function or task.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/top_export_model.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>I started writing this post with the goal of talking about the new Initialize Function, Reset Function and Terminate Function blocks, along with the closely related State Reader and State Writer... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2016\/12\/05\/export-function-models\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,24],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6040"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=6040"}],"version-history":[{"count":24,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6040\/revisions"}],"predecessor-version":[{"id":8572,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6040\/revisions\/8572"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=6040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=6040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=6040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}