{"id":30,"date":"2008-09-18T21:00:45","date_gmt":"2008-09-19T02:00:45","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2008\/09\/18\/follow-the-bouncine-ball\/"},"modified":"2017-01-02T15:50:01","modified_gmt":"2017-01-02T20:50:01","slug":"follow-the-bouncine-ball","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2008\/09\/18\/follow-the-bouncine-ball\/","title":{"rendered":"Follow the Bouncing Ball"},"content":{"rendered":"<p>In my last post about the <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/2008\/09\/07\/continuous-time-integrator\/\">Continuous\r\nTime Integrator<\/a>, I talked about the bouncing ball model <em>sldemo_bounce<\/em>.\r\n<a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/2008\/09\/07\/continuous-time-integrator\/#comment-536\">Amin<\/a>\r\ncommented:<\/p>\r\n\r\n<p><blockquote>Hi Seth!<br>\r\n    Is it necessary to rest the position integral in the bouncing ball demo?<br>\r\n    I get same result without reset the integral and set the internal initial\r\ncondition to 10.<\/blockquote><\/p>\r\n\r\n<p>I forwarded this to my friend Fu who works on developing the\r\nsolvers.\u00a0 Fu said:<\/p>\r\n\r\n<p><blockquote>Hi, Seth:<br>\r\nThe bouncing ball can be modeled in many ways. Actually if you check an old version\r\nof MATLAB, you can find another implementation of this model.<\/blockquote><\/p>\r\n\r\n<p>I looked back at the R13 <em>bounce.mdl<\/em> demo model, and\r\nfound a different implementation.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q4\/bounceR13.png\" alt=\"R13 bouncing ball model\"><\/p>\r\n\r\n<p>There are many design decisions you have to make building a\r\nmodel.\u00a0 I started with the R2008a version and explored what Fu means.<\/p>\r\n\r\n<p><strong>The R2008a demo model: optimized for readability<\/strong><\/p>\r\n\r\n<p>This model ships with Simulink (sldemo_bounce.mdl).\u00a0 <\/p>\r\n\r\n<a target=\"_blank\">\r\n    <img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q4\/sldemo_bounce1_annotated.png\" alt=\"Simulink bouncing ball model\" border=\"0\">\r\n<\/a>\r\n\r\n<p>It has many nice qualities.\u00a0 I can read the diagram and see:<\/p>\r\n\r\n<ol>\r\n    <li>Initial conditions of the velocity and position integrators<\/li>\r\n    <li>There is a lower limit on the position integrator of 0<\/li>\r\n    <li>This lower limit triggers resets in the velocity and position states<\/li>\r\n    <li>The position state resets to zero (ground)<\/li>\r\n    <li>The velocity state resets to -0.8*V (80% elastic rebound)<\/li>\r\n<\/ol>\r\n\r\n<p>As Amin found out, you can modify this model and still get\r\nthe same answer.<\/p>\r\n\r\n<p><strong>A slight change: removing the position reset<\/strong><\/p>\r\n\r\n<p>Just like when optimizing M-code, you might want to remove\r\nunnecessary parts of your design.\u00a0 If we do not need to reset the position\r\nstate, because of the limit, we can remove the connection to the reset port.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q4\/sldemo_bounce2_annotated.png\" alt=\"Removed position reset signal\"><\/p>\r\n\r\n<p>I had to use the output of the position integrator for the\r\nreset signal on the velocity state.\u00a0 This is because state ports can only be\r\nused to break algebraic loops or to &quot;hand-off&quot; states between\r\nsystems.\u00a0 <\/p>\r\n\r\n<p><strong>Using the saturation port to trigger state reset<\/strong><\/p>\r\n\r\n<p>The continuous time integrator has an optional saturation\r\noutput.\u00a0 Instead of deriving a triggering signal from the position by\r\ncalculating when Z&lt;=0, we could use the saturation signal.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q4\/sldemo_bounce3_annotated.png\" alt=\"Using the saturation port\"><\/p>\r\n\r\n<p>This signal (1 above) is zero when the position is within\r\nthe bounds of the limits (0 inf), one when it saturates on the upper limit and\r\nminus one when it saturates on the lower limit.\u00a0 The transition from 0 to -1 is\r\na falling zero crossing and it can replace the calculated reset signal.\u00a0 We\r\nhave to change the velocity integrator to reset on falling zero crossings (2\r\nabove).<\/p>\r\n\r\n<p><strong>Simulink Golf: using the fewest base blocks<\/strong><\/p>\r\n\r\n<p>You may have heard of MATLAB Golf.\u00a0\r\nThis is the game\/pastime where you attempt to write a complete algorithm with\r\nthe fewest number of characters.\u00a0 A similar game can be played using Simulink.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q4\/sldemo_bounce4_annotated.png\" alt=\"Trying to reduce the number of blocks\"><\/p>\r\n\r\n<p>That is my attempt at a model with the fewest base blocks\r\n(not counting virtual blocks like mux and selectors).\u00a0 I have to admit I was\r\njust playing around when I built this.\u00a0 It is much harder to understand how all\r\nthe dynamics fit together, and it would be harder to upgrade and maintain.\u00a0 I combined\r\nall the states into a single integrator (1) and had to add the selector block\r\n(2) to pull out the position state.<\/p>\r\n\r\n<p><strong>Summary<\/strong><\/p>\r\n\r\n<p>Your choice of implementation in Simulink involves many\r\nfactors.\u00a0 In the R2008a demo model, the emphasis is on readability.\u00a0 You can\r\nunderstand most of the model just by looking at the diagram, and it doesn\u2019t\r\nrequire a lot of clicking to find the parameters.\u00a0 I believe this is the most\r\nimportant measure of design in Simulink.<\/p>\r\n\r\n<p>To make a model readable, follow the same general rules as\r\nwhen writing good code.\u00a0 Two of the design rules that come to mind are:<\/p>\r\n\r\n<ol>\r\n    <li>Avoid terse constructs (like Simulink Golf)<\/li>\r\n    <li>Add comments to explain how\u00a0 features of the model work<\/li>\r\n<\/ol>\r\n\r\n<p><strong>Now it\u2019s your turn<\/strong><\/p>\r\n\r\n<p>What do you consider when making design decisions?\u00a0 Leave a\r\n<a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=30&#comment\">comment here<\/a> to share your process with the community.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>In my last post about the Continuous\r\nTime Integrator, I talked about the bouncing ball model sldemo_bounce.\r\nAmin\r\ncommented:\r\n\r\nHi Seth!\r\n    Is it necessary to rest the position integral in the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2008\/09\/18\/follow-the-bouncine-ball\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30],"tags":[49,447,446,441],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/30"}],"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\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=30"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":6183,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/30\/revisions\/6183"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}