{"id":3712,"date":"2014-07-22T09:00:48","date_gmt":"2014-07-22T14:00:48","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=3712"},"modified":"2014-07-22T09:02:52","modified_gmt":"2014-07-22T14:02:52","slug":"stateflow-active-state-output","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2014\/07\/22\/stateflow-active-state-output\/","title":{"rendered":"Stateflow Active State Output"},"content":{"rendered":"<p>This week, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/400549\">Corey Lagunowich<\/a> is here to talk about a very useful Stateflow feature, the Active State Output.<\/p>\r\n\r\n<p><strong>Active State Output<\/strong><\/p>\r\n\r\n<!--introduction-->\r\n<p>Did you know that with Stateflow, it is possible to monitor which state is currently active, and use that information in the rest of the Simulink model? This feature\u00a0is named <a href=\"\">Active State Output<\/a>. The screenshot below shows how to do this for one of the charts in\u00a0the example <a href=\"\">Temporal Logic Using the AT Function<\/a>.<\/p>\r\n<!--\/introduction-->\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/chartConfig.png\" alt=\"Enabling Active State Output\" \/><\/p>\r\n\r\n<p>The checkbox creates a new output port on your chart block, whose value (in simulation) is of an enumerated type with values that match the names of the child states.<\/p>\r\n\r\n<p>When doing this for a chart with parallel decomposition, Active State Output needs to be enabled for each parallel state individually, and each parallel state has a separate corresponding output port. <em>(The <a href=\"\">sf_car demonstration<\/a> uses this to output the current gear selected by the shift logic to its transmission model)<\/em>. <\/p>\r\n\r\n<p>We can also take advantage of this feature in order to accomplish something special in the code generated from Stateflow charts.<\/p>\r\n\r\n<style>\r\nspan.keyword { color:#0000FF }\r\nspan.comment { color:#228B22 }\r\nspan.string { color:#A020F0 }\r\nspan.untermstring { color:#B20000 }\r\nspan.syscmd { color:#B28C00 }\r\n<\/style>\r\n<p><strong>Stateflow States in Generated Code\u00a0 (Default Behavior)<\/strong><\/p>\r\n\r\n<p>When using <a href=\"https:\/\/www.mathworks.com\/products\/embedded-coder\">Embedded Coder<\/a> to generate code from Stateflow charts, by default the states of the chart are created as macros using the <tt>#define<\/tt> directive. For example, here is a snippet from the generated code from the chart shown above.<\/p>\r\n\r\n<p><code><span class=\"comment\">\/* Named constants for Chart: '&lt;Root&gt;\/Temporal Logic' *\/<\/span>\r\n<span class=\"syscmd\">#define<\/span> sf_tlat_IN_NO_ACTIVE_CHILD     ((uint8_T)0U)\r\n<span class=\"syscmd\">#define<\/span> sf_tlat_IN_State               ((uint8_T)1U)\r\n\r\n<span class=\"comment\">\/* Named constants for Chart: '&lt;Root&gt;\/Without Temporal Logic' *\/<\/span>\r\n<span class=\"syscmd\">#define<\/span> sf_tlat_IN_A                   ((uint8_T)1U)\r\n<span class=\"syscmd\">#define<\/span> sf_tlat_IN_B                   ((uint8_T)2U)\r\n<span class=\"syscmd\">#define<\/span> sf_tlat_IN_C                   ((uint8_T)3U)<\/code><\/p>\r\n\r\n<p>The macros are grouped together by chart under an identifying comment line, but there is no intrinsic distinction as to which chart they belong to.<\/p>\r\n\r\n<p><strong>Stateflow States as Enums<\/strong><\/p>\r\n\r\n<p>So what if you would like to have a more organized grouping? Specifically, what if you would like to have your states be members of an enumeration?<\/p>\r\n\r\n<p>As you might have guessed, we need to enable Active State Output.\u00a0But we need to do two more things in order to get this enumeration in a nice, clean, standalone form. First, open the signal properties for the signal coming from the Active State Output port and give the signal a name. Second, set the signal\u2019s storage class (under the Code Generation tab) to ExportedGlobal.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/chartOutputProps.png\" alt=\"Configuring Generated Code for Active State Output\" \/><\/p>\r\n\r\n\r\n\r\n<p>Now, in the *_types.h generated code file, there will be an enumeration definition, like so:<\/p>\r\n\r\n<p><code><span class=\"keyword\">typedef enum<\/span> {\r\n  listofStates_None = 0,               <span class=\"comment\">\/* Default value *\/<\/span>\r\n  listofStates_A,\r\n  listofStates_B,\r\n  listofStates_C\r\n} listofStates;<\/code><\/p>\r\n\r\n<p>And in the generated c code for the model, macros for the chart \u00a0are no longer defined; rather, a global (with the name of the signal) is declared of type listOfStates:<\/p>\r\n\r\n<p><code><span class=\"comment\">\/* Exported block signals *\/ <\/span>\r\nlistofStates currentState;             <span class=\"comment\">\/* '&lt;Root&gt;\/Without Temporal Logic' *\/<\/span>\r\n<\/code><\/p>\r\n\r\n<p>Further down in that c code file, you will find a switch-case statement that implements the logic of the Without Temporal Logic chart, like so:<\/p>\r\n\r\n<p><code><span class=\"keyword\">switch<\/span> (currentState) {\r\n       <span class=\"keyword\">case<\/span> listofStates_A:\r\n        <span class=\"comment\">...<\/span>\r\n        <span class=\"keyword\">break<\/span>;\r\n       <span class=\"keyword\">case<\/span> listofStates_B:\r\n        <span class=\"comment\">...<\/span>\r\n        <span class=\"keyword\">break<\/span>;\r\n       <span class=\"keyword\">default<\/span>:\r\n        <span class=\"comment\">...<\/span>\r\n        <span class=\"keyword\">break<\/span>;<\/code><\/p>\r\n\r\n<p><strong>One Last Note<\/strong><\/p>\r\n\r\n<p>When you enable Active State Output, you can also check the \u201cDefine enumerated type manually\u201d box and click on the helpful hyperlink to automatically create a template m-file that defines the enumerated type.<\/p>\r\n\r\n<p>At the top of this file you will find an enumeration declaration with a list of all the state names. Tempting as it may be, you cannot change these names here unless you also change them in the chart itself. However, you can use this file to change some other behaviors using the methods that follow (but that\u2019s a blog post for another day).<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Are you already using the active state output? Give this a try and let us know what you think by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=3712&#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\/2014Q3\/chartOutputProps.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction-->\r\n<p>Did you know that with Stateflow, it is possible to monitor which state is currently active, and use that information in the rest of the Simulink model? This feature\u00a0is named <a href=\"\">Active State Output<\/a>. The screenshot below shows how to do this for one of the charts in\u00a0the example <a href=\"\">Temporal Logic Using the AT Function<\/a>.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2014\/07\/22\/stateflow-active-state-output\/\">read more >><\/a><\/p>","protected":false},"author":88,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,65,56],"tags":[57,387,386,455],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3712"}],"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\/88"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=3712"}],"version-history":[{"count":60,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3712\/revisions"}],"predecessor-version":[{"id":3970,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3712\/revisions\/3970"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=3712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=3712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=3712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}