{"id":67,"date":"2009-09-25T16:00:08","date_gmt":"2009-09-25T16:00:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2009\/09\/25\/custom-components-in-physical-models\/"},"modified":"2017-01-02T16:06:44","modified_gmt":"2017-01-02T21:06:44","slug":"custom-components-in-physical-models","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2009\/09\/25\/custom-components-in-physical-models\/","title":{"rendered":"Custom Components in Physical Models"},"content":{"rendered":"<p>This week I want to introduce\r\nguest blogger <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/29641\">Steve Miller<\/a> to talk about the Simscape language.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/SteveMiller.jpg\" alt=\"Steve Miller - Physical Modeling Expert\" \/><\/p>\r\n\r\n<p><strong>Introduction to the Simscape\r\nLanguage<\/strong><\/p>\r\n\r\n<p>I\u2019ve been working with MATLAB and\r\nSimulink since my college days, and one of the things I liked most about these\r\ntools is that I\u2019m free to create just about anything with the commands and\r\nblocks.\u00a0 Whenever my boss asked me, \u201cDo you think you could use MATLAB to...,\u201d\r\nthe enthusiastic \u201cYes!\u201d was halfway out of my mouth before he even reached the\r\nend of the sentence.<\/p>\r\n\r\n<p>When I started working with the <a href=\"https:\/\/www.mathworks.com\/solutions\/physical-modeling.html\">physical\r\nmodeling<\/a> products at The MathWorks,\r\nI was hoping that I\u2019d have the same freedom there.\u00a0 Well, starting with Release\r\n2008b, I do!\u00a0 Using the Simscape language (an\r\nenhancement to Simscape, which was first released in R2007a), I can create my\r\nown physical modeling blocks just like I created cool MATLAB scripts and\r\nSimulink subsystems to solve all those tricky problems my boss threw at me.<\/p>\r\n\r\n<p><strong>What is the Simscape language?<\/strong><\/p>\r\n\r\n<p>The Simscape language is a\r\nMATLAB-based, object-oriented language for modeling physical systems.\u00a0 The big\r\ndifference here is that I can use this textual language to create components\r\nthat have physical connections, such as hydraulic ports on valves and electrical\r\nterminals on resistors, instead of inputs and outputs like I\u2019m used to doing in\r\nSimulink.\u00a0 Why is this important?\u00a0 Well the models built with physical\r\nconnections are much easier to understand \u2013 models of electrical systems look\r\nlike an electrical schematic.\u00a0 And, if I create something useful and I want to\r\nreuse it in another model, it is much easier, for I don\u2019t have to worry about\r\nrouting inputs and outputs -- I just plug my new electrical component into the\r\ncircuit and try it.<\/p>\r\n\r\n<p>Here is an example of a component\r\nI have defined using the Simscape language.\u00a0 It\u2019s a DC motor.\u00a0 You\u2019ll see that\r\nit has four physical ports (not inputs and outputs) \u2013 two electrical and two\r\nmechanical.\u00a0 These physical ports let me plug the component into a physical\r\nnetwork.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/Simscape_Language_Post_Image_1a.png\" alt=\"Simscape - Custom DC Motor library\" \/><\/p>\r\n\r\n<p><strong>How does it work?<\/strong><\/p>\r\n\r\n<p>You can use the Simscape language\r\nto define physical domains, components, and libraries of components.\u00a0 It\u2019s\r\nusually easiest to look at an example, so let\u2019s look at the definition of a DC\r\nmotor\u00a0 created in the Simscape language.<\/p>\r\n\r\nHere we see the first 11 lines of\r\nthe file.\u00a0 This is where we define the component name, add a description, and\r\ndefine the physical ports:<\/p>\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/Simscape_Language_Post_Image_2a.png\" alt=\"Simscape Component Definition of Nodes\" \/><\/p>\r\n\r\n<p>You can see that we have two\r\nelectrical (+ and \u2013 terminals) and two mechanical rotational ports (motor shaft\r\nand motor housing).\u00a0 These are reflected in the component shown above.<\/p>\r\n\r\n<p>In the next 6 lines of the file,\r\nwe define the parameters (including units!).\u00a0 These are the values that I or\r\nanother user may want to change:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/Simscape_Language_Post_Image_3a.png\" alt=\"Simscape parameters section\" \/><\/p>\r\n\r\n<p>You\u2019ll notice that in the final\r\ncomponent, the prompt, the default value, and the units all show up in the\r\ndialog box shown here:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/Simscape_Language_Post_Image_4a.png\" alt=\"Simscape Custom DC Motor Parameters dialog box\" \/>\r\n\r\n<p>Just like most Simscape blocks,\r\nthere a link to the source code!\u00a0 In lines 18-23 of the file, we define the\r\nvariables associated with the physical domains (electrical and mechanical) we need for the equations we will define in the\r\nequations section.\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/Simscape_Language_Post_Image_5a.png\" alt=\"Simscape variables section\" \/>\r\n\r\n<p>In the setup section, we set up\r\nthe connections between the variables to the physical ports.\u00a0 Remember\r\nKirchoff\u2019s laws?\u00a0 Well, we\u2019re essentially applying them to both the electrical\r\nand mechanical domains (yep, they work in other physical domains, too, if you\r\ndefine them properly).<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/Simscape_Language_Post_Image_6a.png\" alt=\"Simscape function setup section\" \/><\/p>\r\n\r\n<p>You\u2019ll also notice that we have used\r\nMATLAB to make sure that the value of the resistance in the dialog box is not\r\nless than zero.\u00a0 I can use MATLAB code in this section to do other things like\r\ncalculate values or initialize variables.\u00a0 All that MATLAB I learned is not\r\ngoing to waste!<\/p>\r\n\r\n<p>In the last lines of the file, I\r\ndefine the equations for the motor.\u00a0 Take a look:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/Simscape_Language_Post_Image_7a.png\" alt=\"Simscape equations section\" \/>\r\n<p style=\"text-autospace: none;\">See\r\nthe \u201c==\u201d symbol there?\u00a0 I\u2019ve used that to define an implicit equation.\u00a0 This\r\nmeans I have set up a relationship between these quantities (including time\r\nderivatives \u2013 see the <strong>.<\/strong><strong><span style=\"font-family: 'Courier New'; color: blue;\">der<\/span><\/strong>?).\r\nI\u2019m not assigning values, nor does this represent a Boolean\r\nexpression.\u00a0 I could have defined them as shown below and the results would be\r\nthe same:<\/p>\r\n<\/p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/Simscape_Language_Post_Image_8a.png\" alt=\"Simscape equations section - alternative\" \/><\/p>\r\n\r\n<p>And there you have it!\u00a0 Once I\r\nhave defined the file, I execute <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/physmod\/simscape\/ref\/ssc_build.html\">ssc_build<\/a>\r\nand it produces a Simscape component I can connect to other Simscape components\r\nin my physical model.<\/p>\r\n\r\n<p><strong>Videos of Simscape language in\r\naction<\/strong><\/p>\r\n\r\n<p>I have made a video demo of modeling a nonlinear spring using the Simscape language.\u00a0 Take a look to see the blocks in use and the build\r\nprocess.<\/p>\r\n\r\n<p>For a more complete experience,\r\nwatch this <a href=\"https:\/\/www.mathworks.com\/videos\/physical-modeling-with-the-simscape-language-81733.html\">\r\nwebinar on the Simscape language<\/a>.<\/p>\r\n\r\n<p><strong>Now it\u2019s your turn<\/strong><\/p>\r\n\r\n<p>Want a closer look at this\r\nexample?\u00a0 <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25014-simscape-language--dc-motor\">Download it from the MATLAB Central File Exchange<\/a>.<\/p>\r\n\r\n<p>Are you using the physical\r\nmodeling tools? Have you tried the Simscape language? Post a\r\n<a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=67&amp;#comment\">\r\ncomment here<\/a> and tell us\r\nabout it.<\/p>","protected":false},"excerpt":{"rendered":"<p>This week I want to introduce\r\nguest blogger Steve Miller to talk about the Simscape language.\r\n\r\n\r\n\r\nIntroduction to the Simscape\r\nLanguage\r\n\r\nI\u2019ve been working with MATLAB and\r\nSimulink since my... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2009\/09\/25\/custom-components-in-physical-models\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[65,39,16],"tags":[105,106],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/67"}],"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=67"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":6192,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/67\/revisions\/6192"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}