{"id":11,"date":"2008-04-18T15:30:44","date_gmt":"2008-04-18T20:30:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2008\/04\/18\/bus-objects-and-interface-specifications\/"},"modified":"2016-12-20T17:01:35","modified_gmt":"2016-12-20T22:01:35","slug":"bus-objects-and-interface-specifications","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2008\/04\/18\/bus-objects-and-interface-specifications\/","title":{"rendered":"Bus Objects and Interface Specifications"},"content":{"rendered":"A few recent comments on this blog have asked about bus\r\nobjects. Blog reader <a href=\"https:\/\/blogs.mathworks.com\/seth\/2008\/03\/27\/what-is-a-composite-signal\/#comment-115\">KMR\r\nremarked<\/a> that \"bus objects are one of the most important parts of the\r\nwhole bus concept: allowing you to lock down an interface.\" This week I introduce bus objects\r\nand how they can help avoid modeling errors.\r\n\r\n<strong>Bus objects provide a specification<\/strong>\r\n\r\nIn an <a href=\"https:\/\/blogs.mathworks.com\/seth\/2008\/04\/11\/how-do-you-think-about-virtual-buses\/\">earlier\r\npost<\/a>, we saw that virtual bus signals do not have to include any\r\ninformation about the size and datatype of their signals. This information\r\npropagates from the other blocks (like sources and Inports) in the diagram.\r\nThe <a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/simplebusdemo.mdl\">simplebusdemo.mdl<\/a>\r\ndoesn\u2019t need a specification for the bus signal because it is virtual. Our\r\nmental model of the bus signal is a bundle of rainbow colored wires that link\r\nthe sources to their destinations. We often refer to a bus as a tie-wrap of\r\nsignals.\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/simplebusdemo_diag.png\" alt=\"simplebusdemo_diag.png\" \/>\r\n\r\nIf you did want to lock down the description of this bus,\r\nyou must use a bus object. If the bus is the rainbow colored bundle of wires,\r\nin my mental model the <em>bus object<\/em> is the definition for the <em>cable\r\nconnector<\/em> at the end of that bundle. It defines all the pins and their\r\nexact configuration and asserts that only those types of signals may be\r\nconnected.\r\n\r\nCheck \u201cSpecify properties via bus object\u201d and include the\r\nbus object name on the Bus Creator blocks to add the specification to your\r\nmodel. I\u2019ll get to creating bus objects in the next section.\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/buscreator_BusObj.png\" alt=\"Bus Creator dialog with a bus object\" \/>\r\n\r\nBus Creators use bus objects for error checking. If the\r\ninput signals do not have the same types and dimensions as the elements in the\r\nbus object Simulink will error. There is also a connectivity diagnostic to\r\ncheck for element name mismatch in the bus object. Turn the diagnostic up to\r\nwarning or error to ensure your signals are consistent with the block\r\nspecification.\r\n\r\n<strong>Making a bus object<\/strong>\r\n\r\nThe easiest way to make a bus object is directly from your\r\ndiagram. <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/simulink.bus.createobject.html\">Simulink.Bus.createObject<\/a>\r\nis a function that generates a bus object for the block you specify based on\r\nyour diagram. Specify the bus creator or port that has the highest level in\r\nthe hierarchy of the bus. Simulink.Bus.createObject recursively creates bus objects for buses\r\nthat feed into the given block. For our example, the main_bus is specified by the\r\nsimplebusdemo\/Bus Creator (not Bus Creator1 or Bus Creator2).\r\n\r\n<code>&gt;&gt; Simulink.Bus.createObject(<span style=\"color: #a020f0;\">'simplebusdemo'<\/span>, <span style=\"color: #a020f0;\">'simplebusdemo\/Bus Creator'<\/span>);\r\n&gt;&gt; whos\r\nName Size Bytes Class Attributes\r\n\r\nans 1x1 272 struct\r\nbus1 1x1 Simulink.Bus\r\nbus2 1x1 Simulink.Bus\r\nmain_bus 1x1 Simulink.Bus\r\n<\/code>\r\n\r\nTo look at bus objects, use the buseditor. (click to enlarge)\r\n\r\n<a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/buseditor_full.png\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/buseditor.png\" alt=\"Bus editor for the main_bus\" \/><\/a>\r\n\r\nAs of R2008a Simulink has a new bus editor, so unless you\r\nare using this release, your version will look different. The left pane shows\r\nall bus objects in the workspace. The selected node in the tree displays its\r\nchildren in the center pane, and on the right, you have the details for each\r\nselected element. The information about dimensions, datatype and signal name\r\nare all a part of the bus object. For hierarchical bus signals, the element\r\ndatatype is the name of another bus. Looking at the main_bus, it has two\r\nsignals, bus1 of type bus1, and bus2 of type bus2. The name and type do not\r\nhave to match, but they do in this case.\r\n\r\nIf you are happy with your bus objects, I recommend saving\r\nthem to a MAT-file so you do not have to regenerate them every time. Some\r\npeople prefer to generate an M-file and call that as part of their initialization\r\nroutines, instead of loading the MAT-file. I have automatically generated this\r\nM-file during the call to Simulink.Bus.createObject. Given an output file\r\nname, Simulink.bus.createObject also outputs the code you need to make that bus\r\nobject.\r\n\r\n<code>&gt;&gt; Simulink.Bus.createObject(<span style=\"color: #a020f0;\">'simplebusdemo'<\/span>,...\r\n<span style=\"color: #a020f0;\">'simplebusdemo\/Bus Creator'<\/span>,<span style=\"color: #a020f0;\">'simplebusdemo_busScript'<\/span>);<\/code>\r\n\r\n<strong>Bus specification in action<\/strong>\r\n\r\nIf I change my constant input signal to be type int8 instead\r\nof double Simulink will throw an error during update diagram (Ctrl-D).\r\n<blockquote><code>The input bus to block\r\n'simplebusdemo_bo_error\/Bus Creator1' does not match the bus specified by the\r\nbus object 'bus1' on the block dialog. The following errors were detected :\r\n<\/code>\r\n\r\nBus element 'Constant' of bus\r\nobject 'bus1' is specified to be of datatype 'double', but the incoming signal\r\nhas a datatype of 'int8'.<\/blockquote>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/simplebusdemo_bo_error_diag.png\" alt=\"Bus creator with error\" \/>\r\n\r\nAs you can see, the bus object has locked down the\r\nspecification for the signals fed into the bus creator. When something does\r\nnot match Simulink reports an error.\r\n\r\n<strong>Bus objects can be specified on ports<\/strong>\r\n\r\nIn addition to the bus creators, Inports and Outports can\r\nalso be fully specified with a bus object. When a bus object is used on a port\r\nyou are asserting that only that special type of connector can be used with\r\nthat port. This port could be a root level Inport or Outport, or a port on a\r\nsubsystem. If the subsystem is in a library, you have defined its interface,\r\nand all instances of the library block must match the specification.\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/outportWithBusObjDLG.png\" alt=\"Outport dialog with a bus object\" \/>\r\n\r\nGetting back to the comment that \u201c[bus objects] are one of\r\nthe most important parts of the whole bus concept: allowing you to lock down an\r\ninterface.\u201d The concept of locking down an interface is important to\r\ndeveloping reusable components as well as working with others on large modeling\r\nprojects. This is a very powerful concept I have seen used in conjunction with\r\nan interface control document (ICD) to define the interface to a system. The\r\ndocument specifies the characteristics of the bus signals, and the bus object\r\nenforces that those characteristics are true.\r\n\r\nI have also seen a Simulink model specify the interface, with a script used\r\nto generate the ICD from the model. This makes the Simulink model the single\r\nsource of truth. Your model will always be in sync with the specification if\r\nthis is your workflow.\r\n\r\n<strong>Now it\u2019s your turn<\/strong>\r\n\r\nWhat do you think about bus objects? Do you use them? I\r\nstill have not had anyone volunteer to show off their use of bus signals with a\r\nscreen capture from their model. <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=11&amp;#comment\">Leave a comment<\/a>\r\nand then <a href=\"mailto:Seth.Popinchalk@mathworks.com?subject=Your%20post%20on%20Bus%20Objects\">send\r\nme an e-mail<\/a> with the image. I will post it for you and we can all marvel\r\nat your work.","protected":false},"excerpt":{"rendered":"<p>A few recent comments on this blog have asked about bus\r\nobjects. Blog reader KMR\r\nremarked that \"bus objects are one of the most important parts of the\r\nwhole bus concept: allowing you to lock down... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2008\/04\/18\/bus-objects-and-interface-specifications\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[19,12,444,441],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/11"}],"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=11"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":6121,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/11\/revisions\/6121"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}