{"id":1955,"date":"2013-04-05T09:58:03","date_gmt":"2013-04-05T14:58:03","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=1955"},"modified":"2013-04-05T14:08:07","modified_gmt":"2013-04-05T19:08:07","slug":"to-latch-or-not-to-latch","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2013\/04\/05\/to-latch-or-not-to-latch\/","title":{"rendered":"To Latch or not to Latch?"},"content":{"rendered":"<p>Yesterday I explained to a colleague the effect of the Inport block option <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/simulink\/slref\/inport.html#brqe18y-28\">Latch input for feedback signals of function-call subsystem outputs\r\n<\/a>. I thought it would be interesting to share here.<\/p>\r\n\r\n<p><strong>The Problem<\/strong><\/p>\r\n\r\n<p>In the following <a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/Problem_latch.slx\">model<\/a>, inside the <em>calib<\/em> <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/functioncallsubsystem.html\">function-call subsystem<\/a>, the <em>Count<\/em> signal is connected to a <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/unitdelay.html\">Unit Delay<\/a> block.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/fcnCallLoopModel.png\" alt=\"Model with a Function-Call subsystem involved in a direct feedback loop\" \/><\/p>\r\n\r\n<p>When logging data, we can see that the input and output of the Unit Delay are identical, as if the signal was not delayed!<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/fcnCallOutput.png\" alt=\"Input and output of the Unit Delay are equal\" \/><\/p>\r\n\r\n<p>Even though this might seem strange, this behavior is expected. Let's see why.<\/p> \r\n\r\n<p><strong>The simulation loop<\/strong><\/p>\r\n\r\n<p>To understand what is happening, you first need to be familiar with the order into which the block methods are executed within the simulation loop. To really understand the simulation loop I recommend the following documentation pages:\r\n\r\n<ul>\r\n\t<li>First, you need to know that the equations for blocks are implemented in multiple <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/simulink\/ug\/modeling-dynamic-systems.html#f7-22144\">block methods<\/a>: Outputs, Update, Derivative, etc.<\/li>\r\n\t<li>To get an overview of the order into which those methods are called: <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/simulink\/sfg\/how-s-functions-work.html\">How S-Functions Work<\/a><\/li>\r\n\t<li>To get the complete details of all the block methods available: <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/simulink\/sfg\/how-the-simulink-engine-interacts-with-c-s-functions.html\">Simulink Engine Interaction with C S-Functions<\/a><\/li>\r\n<\/ul>\r\n\r\n<p>In our case, we have a model that is fixed-step and discrete, so the simulation loop is very simple and looks like this:<\/p> \r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/simulationloop.png\" alt=\"Simulation loop\" \/><\/p>\r\n\r\n<p> The important thing to understand is that Simulink executes the <strong>Outputs<\/strong> method of all blocks, and then the <strong>Update<\/strong> method of all blocks.<\/p>\r\n\r\n<p><strong>The execution order<\/strong><\/p>\r\n\r\n<p>The second thing we need to know is the order in which blocks are executed. For that, we display the block <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/simulink\/ug\/controlling-and-displaying-the-sorted-order.html\">sorted order<\/a>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/sortedOrder.png\" alt=\"Sorted Order\" \/><\/p>\r\n\r\n<p>When you display the sorted order, you can see red numbers displayed on the block. The first number is the number of the system, so the function-call subsystem in this model is system number 3. The second number is the order of the block in this system. In this case, the Gain is the first (<font color=\"red\"><tt>3:0<\/tt><\/font>), Unit Delay the second (<font color=\"red\"><tt>3:1<\/tt><\/font>), Counter the third (<font color=\"red\"><tt>3:2<\/tt><\/font>), etc.<\/p>\r\n\r\n<p><strong>The effect of the latch<\/strong><\/p>\r\n\r\n<p>To see the effect of latching, I generated code for this model and added comments to highlight the fact that the feedback loop causes the input signal to change immediately as the output is written:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/fcnCallCode.png\" alt=\"Generated Code without latch\" \/><\/p>\r\n\r\n<p>Such feedback loops break the assumptions on which the Output-Update logic is built... and can make results difficult to interpret. This is why the Latch is available.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/latchInportDialog.png\" alt=\"Inport block dialog\" \/><\/p>\r\n\r\n<p>With the latch, a copy of the input signal is made at the beginning of the step and this copy is used for the computations:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/fcnCallCodeWithLoop.png\" alt=\"Generated Code with latch\" \/><\/p>\r\n\r\n<p><strong>Conclusion<\/strong><\/p>\r\n\r\n<p>In conclusion, we can say that introducing the latch is safer, but costs an additional copy. If you know that your function-call subsystem might be involved in a direct feedback loop, use Inport Latching to avoid surprises.<\/p>\r\n\r\n<p><strong>Now it's you turn<\/strong><\/p>\r\n\r\n<p>Are you latching the inputs of your function-call subsystems? Let us know the reason why by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=1955&#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q1\/fcnCallCodeWithLoop.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Yesterday I explained to a colleague the effect of the Inport block option Latch input for feedback signals of function-call subsystem outputs\r\n. I thought it would be interesting to share... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2013\/04\/05\/to-latch-or-not-to-latch\/\">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,43,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/1955"}],"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=1955"}],"version-history":[{"count":32,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/1955\/revisions"}],"predecessor-version":[{"id":2128,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/1955\/revisions\/2128"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=1955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=1955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=1955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}