{"id":4096,"date":"2014-09-10T05:42:40","date_gmt":"2014-09-10T10:42:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=4096"},"modified":"2014-09-10T05:42:40","modified_gmt":"2014-09-10T10:42:40","slug":"my-introduction-to-embedded-coder","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2014\/09\/10\/my-introduction-to-embedded-coder\/","title":{"rendered":"My Introduction to Embedded Coder"},"content":{"rendered":"<p>This week, I want to share the example which helped me, a long time ago, to get started with <a href=\"https:\/\/www.mathworks.com\/products\/embedded-coder\/\">Embedded Coder<\/a>. For readers who never used it, I hope this example will be as useful for you as it has been for me.<\/p>\r\n\r\n<p><strong>The Problem<\/strong><\/p>\r\n\r\n<p>If you ask me what is the difference between <a href=\"https:\/\/www.mathworks.com\/products\/simulink-coder\/\">Simulink Coder<\/a> and <a href=\"https:\/\/www.mathworks.com\/products\/embedded-coder\/\">Embedded Coder<\/a>, I would tell you that Simulink Coder allows you to generate code from a Simulink model, and Embedded Coder allows you to configure how the code looks like.\r\n\r\n<p>To illustrate that, we will start with a simple hand-written program, and see how to configure a model so that the code generated from it integrates without modification.<\/p>\r\n\r\n<p>In this simple <tt>main<\/tt>, at every second, the program reads data from a text file and stores it in a global variable <tt>u<\/tt>. We want our auto-generated code to access this variable, and use it to compute value of another global variable <tt>y<\/tt> to be used later in the code.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/theMain.png\" alt=\"pseudo code of the main application\" \/><\/p>\r\n\r\n<p><strong>Configuring the Model<\/strong><\/p>\r\n\r\n<p>For this example, let's use the following simple model:.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/algorithmToBeCoded.png\" alt=\"the Model\" \/><\/p>\r\n\r\n<p>By default, if we generate code for this model we get something that looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/defaultCode.png\" alt=\"Default code generated form the model\" \/><\/p>\r\n\r\n<p>It is obvious that modifications are necessary to map the values of <tt>u<\/tt> and <tt>y<\/tt> in the hand-written code to the input and output of the generated code.<\/p>\r\n\r\n<p>To configure the look of the code, the first step is to name the input signal <tt>u<\/tt> and the output <tt>y<\/tt>. Then we need to tell Embedded Coder that <tt>u<\/tt> and <tt>y<\/tt> are <a href=\"http:\/\/en.wikipedia.org\/wiki\/External_variable\">external variables<\/a>, already defined outside of the generated code, in the hand-written code.<\/p>\r\n\r\n<p>For that, right-click on the signal line and select Properties:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/openingSignalProperties.png\" alt=\"Opening Signal Properties\" \/><\/p>\r\n\r\n<p>Go to the Code Generation tab, and set the storage class to <tt>ImportedExtern<\/tt> (Note that the storage class and a lot more could also have been specified using a <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/simulink.signal.html (link no longer works)\">data object<\/a>).<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/signalProperties.png\" alt=\"Signal Properties\" \/><\/p>\r\n\r\n<p><strong>Generated Code<\/strong><\/p>\r\n\r\n<p>Generate code for the model, and you should get something which looks like this:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/stepCode.png\" alt=\"Generated Code for the Step Function\" \/><\/p>\r\n\r\n<p>Where <tt>u<\/tt> and <tt>y<\/tt> are declared as <tt>extern<\/tt>:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/externCode.png\" alt=\"Extern Declaration\" \/><\/p>\r\n\r\n<p>With this modification, the generated code integrates in the hand-written application, I can build the <tt>main<\/tt> program without errors.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>There are many of ways to customize the code generated by Embedded Coder. This example is probably the simplest one possible, but I hope this gives you a good idea of how to get started. Let us know how you got started with the coders by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=4096&#comment\">comment here<\/a>.<\/p>\r\n\r\n\r\n\r\n\r\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/externCode.png\" onError=\"this.style.display ='none';\" \/><\/div><p>This week, I want to share the example which helped me, a long time ago, to get started with Embedded Coder. For readers who never used it, I hope this example will be as useful for you as it has... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2014\/09\/10\/my-introduction-to-embedded-coder\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4096"}],"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=4096"}],"version-history":[{"count":31,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4096\/revisions"}],"predecessor-version":[{"id":4169,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4096\/revisions\/4169"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=4096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=4096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=4096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}