{"id":6075,"date":"2015-07-10T09:00:48","date_gmt":"2015-07-10T13:00:48","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6075"},"modified":"2015-07-11T01:00:12","modified_gmt":"2015-07-11T05:00:12","slug":"flush-your-toilet-with-simulink","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/07\/10\/flush-your-toilet-with-simulink\/","title":{"rendered":"Flush Your Toilet with Simulink!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/51956-auto-flush\">Auto Flush<\/a> by the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/6650597\">Techsource Technical Team<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n\r\n   <p>My pick this week is something that many of us could find useful and have likely thought about at some point during the standard\r\n      workday: a simple design for the controller behind an automatically flushing toilet.\r\n   <\/p>\r\n   <p>Techsource's Technical Team has put together a collection of simple Simulink models to showcase getting started with Simulink\r\n      and an <a href=\"https:\/\/www.arduino.cc\/\">Arduino Board<\/a>, a low cost embedded target.\r\n   <\/p>\r\n   <p>This particular one caught my attention for two reasons.  First, the main model is called <i>'UltraSonic_Pee.slx'<\/i>.  If you want to grab someone's attention, well, a model name like this is a good way to do it. Second, this simple model\r\n      is extensible so that I can include my own sensor input and simulate different flushing scenarios.  Let's see how this is\r\n      done.\r\n   <\/p>\r\n   <p>Here's the original model:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainautoflush\/original.png\"> <\/p>\r\n   <p>For my Arduino board, I only have the LEDs and a simple DC motor. However, I have a Lego NXT that has an ultrasonic distance\r\n      sensor and a DC motor that I can use.  I don't know if the DC motor is actually powerful enough to pull down the handle on\r\n      the urinal, but I'm going to try.\r\n   <\/p>\r\n   <p>The first thing I am going to do is replace the two Arduino Blocks with a Lego Motor block from the Lego Support Package.\r\n       Support packages are additional functionality you can freely download to allow MATLAB and Simulink to take advantage of\r\n      your hardware.\r\n   <\/p>\r\n   <p>One of the beautiful things about Simulink is the ability to have <a href=\"\">Variant Subsystems<\/a>.  These allow you to either have different fidelity models or to substitute in different components.  My second step is going\r\n      to be to make the Signal Builder block into a variant subsystem so I can have my Lego distance sensor as an input instead.\r\n       This is really important for the modeling aspect.  I'm fortunate enough right now to have access to the hardware.  But if\r\n      I did not, I could still develop and test my algorithms by simulating the hardware with other signals that I could build or\r\n      historical data from other tests.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainautoflush\/model2.png\"> <\/p>\r\n   <p>Next I'm going to simulate this in External Mode.  This means that the model will be running in Simulink but be grabbing data\r\n      from the sensors and controlling the motor.  By running in External Mode we can use the full power of the MATLAB and Simulink\r\n      platforms to analyze the results of the model and see it running in semi-real-time.  By doing this, I discovered that I needed\r\n      a couple of gains on the signals to control how close one needs to get to trip the sensor.\r\n   <\/p>\r\n   <p>Once the model has been tested, we can then embed the controller onto the Lego.  One button click in Simulink generates the\r\n      equivalent C-code, compiles it, and moves it onto the Lego.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainautoflush\/deploy.png\"> <\/p>\r\n   <p>Now we're ready to put this to the test!<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainautoflush\/lego1.png\"> <\/p>\r\n   <p>The looks we got from fellow restroom patrons and the cleaning staff were pretty priceless.<\/p>\r\n   <p>Next I got my friend Adam, who is relocating from Massachusetts to California shortly, to test it out.  The control algorithm\r\n      that Techsource used is to have a preflush and a postflush - not the most environmentally friendly of algorithms but we waste\r\n      no water in simulation.  Since this is no longer simulation, I figured Adam should enjoy this luxury while still being on\r\n      the east coast.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainautoflush\/lego2.png\"> <\/p>\r\n   <p>Unfortunately, the Lego motor was not strong enough to pull the handle down and instead lifted the whole robot up.  But hey,\r\n      not too shabby for an hour's worth of work.\r\n   <\/p>\r\n   <h3>Comments<a name=\"2\"><\/a><\/h3>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6075#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/51956-auto-flush#comments\">comment<\/a> for the Techsource Technical Team.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_8760dc50734d445080caf4c7533e8598() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='8760dc50734d445080caf4c7533e8598 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 8760dc50734d445080caf4c7533e8598';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Sean de Wolski';\r\n        copyright = 'Copyright 2015 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_8760dc50734d445080caf4c7533e8598()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015a<br><\/p>\r\n<\/div>\r\n<!--\r\n8760dc50734d445080caf4c7533e8598 ##### SOURCE BEGIN #####\r\n%% Auto Flush\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/51956-auto-flush Auto Flush> by\r\n% the <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/6650597\r\n% Techsource Technical Team>.\r\n% \r\n\r\n%% \r\n%\r\n% My pick this week is something that many of us could find useful and have\r\n% likely thought about at some point during the standard workday: a simple\r\n% design for the controller behind an automatically flushing toilet.\r\n%\r\n% Techsource's Technical Team has put together a collection of simple\r\n% Simulink models to showcase getting started with Simulink and an\r\n% <https:\/\/www.arduino.cc\/ Arduino Board>, a low cost embedded target.\r\n%\r\n% This particular one caught my attention for two reasons.  First, the main\r\n% model is called _'UltraSonic_Pee.slx'_.  If you want to grab someone's\r\n% attention, well, a model name like this is a good way to do it.\r\n% Second, this simple model is extensible so that I can include my own\r\n% sensor input and simulate different flushing scenarios.  Let's see how\r\n% this is done.\r\n%\r\n% Here's the original model:\r\n%\r\n% <<original.png>>\r\n%\r\n% For my Arduino board, I only have the LEDs and a simple DC motor.\r\n% However, I have a Lego NXT that has an ultrasonic distance sensor and a\r\n% DC motor that I can use.  I don't know if the DC motor is actually\r\n% powerful enough to pull down the handle on the urinal, but I'm going to\r\n% try.\r\n%\r\n% The first thing I am going to do is replace the two Arduino Blocks with a\r\n% Lego Motor block from the Lego Support Package.  Support packages are\r\n% additional functionality you can freely download to allow MATLAB and\r\n% Simulink can take advantage of your hardware.\r\n%\r\n% One of the beautiful things about Simulink is the ability to have\r\n% <\r\n% Variant Subsystems>.  These allow you to either have different fidelity\r\n% models or to substitute in different components.  My second step is going\r\n% to be to make the Signal Builder block into a variant subsystem so I can\r\n% have my Lego distance sensor as an input instead.  This is really\r\n% important for the modeling aspect.  I'm fortunate enough right now to\r\n% have access to the hardware.  But if I did not, I could still develop and\r\n% test my algorithms by simulating the hardware with other signals that I\r\n% could build or historical data from other tests.\r\n%\r\n% <<model2.png>>\r\n%\r\n% Next I'm going to simulate this in External Mode.  This means that the\r\n% model will be running in Simulink but be grabbing data from the sensors\r\n% and controlling the motor.  By running in External Mode we can use the\r\n% full power of the MATLAB and Simulink platforms to analyze the results of\r\n% the model and see it running in semi-real-time.  By doing this, I\r\n% discovered that I needed a couple of gains on the signals to control how\r\n% close one needs to get to trip the sensor.\r\n%\r\n% Once the model has been tested, we can then embed the controller onto the\r\n% Lego.  One button click in Simulink generates the equivalent C-code,\r\n% compiles it, and moves it onto the Lego.\r\n%\r\n% <<deploy.png>>\r\n%\r\n% Now we're ready to put this to the test!\r\n%\r\n% <<lego1.png>> \r\n%\r\n% The looks we got from fellow restroom patrons and the cleaning staff\r\n% were pretty priceless.\r\n%\r\n% Next I got my friend Adam, who is relocating from Massachusetts to\r\n% California shortly, to test it out.  The control algorithm that\r\n% Techsource used is to have a preflush and a postflush - not the most\r\n% environmentally friendly of algorithms but we waste no water in\r\n% simulation.  Since this is no longer simulation, I figured Adam should\r\n% enjoy this luxury while still being on the east coast.\r\n%\r\n% <<lego2.png>>\r\n%\r\n% Unfortunately, the Lego motor was not strong enough to pull the handle\r\n% down and instead lifted the whole robot up.  But hey, not too shabby for\r\n% an hour's worth of work.  \r\n\r\n%% Comments\r\n% \r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6075#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/51956-auto-flush#comments\r\n% comment> for the Techsource Technical Team.\r\n%\r\n \r\n\r\n##### SOURCE END ##### 8760dc50734d445080caf4c7533e8598\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainautoflush\/original.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is Auto Flush by the Techsource Technical Team.\r\n      \r\n   \r\n\r\n   My pick this week is something that many of us could find useful and have likely thought... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/07\/10\/flush-your-toilet-with-simulink\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,24],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6075"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6075"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6075\/revisions"}],"predecessor-version":[{"id":6080,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6075\/revisions\/6080"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}