{"id":6286,"date":"2017-01-20T17:19:19","date_gmt":"2017-01-20T22:19:19","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=6286"},"modified":"2017-01-20T17:19:19","modified_gmt":"2017-01-20T22:19:19","slug":"state-reader-and-state-writer-blocks","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2017\/01\/20\/state-reader-and-state-writer-blocks\/","title":{"rendered":"State Reader and State Writer blocks"},"content":{"rendered":"<p>As mentioned in my previous post, I have been trying for some time to write 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> introduced in R2016b. However, every time I end up realizing that I need to introduce another feature first.<\/p>\n<p>This week: The <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, also introduced in R2016b.<\/p>\n<p><strong>Not your everyday standard block...<\/strong><\/p>\n<p>Let's begin with a simple model where I have a Discrete State-Space block. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/disc_ss.png\" alt=\"Discrete State-Space\" \/><\/p>\n<p\/>\n<p>I can add to the model a State Reader block, open its dialog, and select the Discrete State-Space block to read its states. Notice the diamond shaped \"x\" on top of the Discrete State-Space block. It is there to indicate that its states are being read somewhere else. Also, notice the block name next to the State Reader, indicating where it is reading from.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/selectBlock.png\" alt=\"Selecting block to be read by the State REader block\" \/><\/p>\n<p\/>\n<p>You probably also want to notice that the State Reader block is highlighted in red. This is thanks to a new feature in R2016b: <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/using-the-model-editor.html#bvea91r-1\">Edit-Time Checking<\/a>. If you hover the mouse on top of the block, you will get more details:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/statereaderWarning.png\" alt=\"Edit Time Check\" \/><\/p>\n<p\/>\n<p>Hmm... What does that mean? What's wrong with putting those two blocks in the same subsystem?<\/p>\n<p>As I said, the State Reader and State Writer blocks are not like every other block! Let's try to update the diagram. This time we receive this error:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/stateReaderError.png\" alt=\"State Reader Error\" \/><\/p>\n<p\/>\n<p><strong>The Explanation<\/strong><\/p>\n<p>For those of you reading this blog for a long time, I hope you remember a post I wrote many years ago titled <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2011\/03\/08\/how-do-i-change-a-block-parameter-based-on-the-output-of-another-block\">How Do I Change a Block Parameter Based on the Output of Another Block?<\/a><\/p>\n<p>In this post, I explained that if a block could change the parameter of another block, this would lead to unpredictable sorted order. During one time step, no one could predict if the parameter would be modified before or after the block executes.<\/p>\n<p>A similar reasoning applies to the State Reader and Writer blocks. Since there is no signal connecting the Reader\/Writer to the state owner, there is no explicit data dependency to enforce the sorted order. This is why those blocks can only be used in contexts where the execution order is explicitly specified by the user.<\/p>\n<p>In other words, this \"constraint\" is a good thing, designed to make your life easier on the long run.<\/p>\n<p>Let's look at those constructs where the State Reader and State Writer blocks can be used.<\/p>\n<p><strong>Mutually Exclusive Subsystems<\/strong><\/p>\n<p>In a manner similar to how the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/examples\/building-a-clutch-lock-up-model.html\">Clutch Lock-Up Example<\/a> uses the State port of the Integrator block, it is possible to use the State Reader and State Writer block to pass states between conditionally executed subsystems.<\/p>\n<p>A simple way to illustrate that is to implement the equation of two falling masses rigidly linked together; that suddenly separate. In the first part of the simulation, the motion is computed using two Discrete-Time Integrator blocks in series.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/combinedMass.png\" alt=\"Combined Masses\" \/><\/p>\n<p\/>\n<p>In the second part of the simulation, we use two series of Discrete-Time Integrator blocks, initialized using the State Reader block, configured to read the state of the Integrator blocks from the first part of the simulation<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/splitMasses.png\" alt=\"Split Masses\" \/><\/p>\n<p\/>\n<p>At the top level, the simulation looks like:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/seperationStateReader.png\" alt=\"Top Model\" \/><\/p>\n<p\/>\n<p><strong>Export Function Models<\/strong><\/p>\n<p>Using an Export Function Model like the one described in <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2016\/12\/05\/export-function-models\/\">my previous post<\/a>, you can explicitly control which block executes first.<\/p>\n<p>Begin by creating a model with two Function-Call Subsystems, one for the Discrete State-Space, and one for the State Reader:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/exportFcnModel.png\" alt=\"Export Function Model\" \/><\/p>\n<p\/>\n<p>You can then use this model as a referenced model, where the parent explicitly schedules the order into which the Discrete State-Space and the State Reader blocks are executed<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/exportFcnModelTop.png\" alt=\"Export Function Top Model\" \/><\/p>\n<p\/>\n<p><strong>Branched Function Call<\/strong><\/p>\n<p>This syntax is similar to the previous example, except that everything is in the same model.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/fcnCallStateReader.png\" alt=\"Function Call Scheduler\" \/><\/p>\n<p\/>\n<p><strong>What's next?<\/strong><\/p>\n<p>It is important to realize that the examples above using the branched function call and the Export Function Model are too simple to really illustrate the real power of the State Reader and State Writer blocks. They are the simplest models I can think of to illustrate the usage of those blocks.<\/p>\n<p>In a soon to come post, I will show how to combine these features with the <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> in a more realistic use-case.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q4\/fcnCallStateReader.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>As mentioned in my previous post, I have been trying for some time to write about the new Initialize Function, Reset Function and Terminate Function introduced in R2016b. However, every time I end up... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2017\/01\/20\/state-reader-and-state-writer-blocks\/\">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":[16],"tags":[488,489],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6286"}],"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=6286"}],"version-history":[{"count":19,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6286\/revisions"}],"predecessor-version":[{"id":6316,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6286\/revisions\/6316"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=6286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=6286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=6286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}