Stateflow Tutorials, Part 1 States and Transitions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to getting started with stateflow this is part one of a three-part webinar series my name is Michael Curran I'm the product manager for stay flow and so here's the agenda first I'm going to introduce state flow then I'm going to introduce the case study that we'll be using throughout this webinar series and what we're going to do is build a state flowchart from scratch and the idea is that after viewing this webinar series that you will be familiar with the most important stay focused so let's actually see what we're going to be working towards and what's the final version of the state flowchart so you get a sneak peek okay so here's the model let's open the state flowchart and we see that there's a number of parallel States in here and a number of functions as well so the functions that were using there's a graphical function so this is used for defining functions using a flow chart there's a truth table function so we could define our functions using tables and there's an embedded MATLAB function so I could define my functions using then but the MATLAB language and C there's some more details here okay let's look at one of our states so you can see that there's some hierarchy in here we have about warnings as a super-state and that contains running and stop and then running also has its own sub states okay in Warren there's some state actions so on entering the stop state we're going to call a function called stop simulation and this is another type of function that's in here this is the simulating function so I could put Simulink inside of state flow okay so you can see there's a number of concepts that we're going to be ring throughout this webinar series so this is part one of the webinar series so what we're going to do is really cover the fundamental concepts and just introduce States and transitions and also flowcharts okay so before we start building our state flowchart let's talk about why stay fo why would you want to use stay flow well sniffle is useful for a number of different applications so for example reactive control systems if you're designing anything that has logic inside of it for example you know a condition needs to be true in order to take a certain action otherwise we're going to take another action okay so that type of logic and conditional statements truth tables is a good example we saw that in the state flow chart if you have if you're developing finite state machines okay if you're developing any type of scheduling where you need to schedule a number of different processes that have to occur one right after the other or a number of tasks that need to occur okay and if you're using Simulink you could use you get a new state with the schedule you know when to activate different simulating subsystems maybe you're doing fault management or fault detection where you know you need to respond or the system needs to respond to fault that might occur okay so you know if primary engine fails maybe need to have a backup engine in place in case that happens and also it's safe flows useful for event-driven systems you know some type of event occurs and we need to react to that event alright now how to stay full work with Simulink so for those viewers Simulink users you might be asking you know how could I fit stay flow within my Simulink model so first off need to let's look look at it this way we're Simulink is used to respond to continuous changes in dynamic systems and state flow is used to respond to instantaneous changes and dynamic systems and real-world systems have to respond to both types of changes so some examples are shown here for example a car you know had suspension dynamics which you can model with Simulink because it has continuous changes but then the transmission has instantaneous changes right you switch from low gear to high gear or any gears in between okay those are instantaneous changes and you can model those different gears using stay flow okay or maybe the example here in the middle where you have a rocket and the propulsion system has continuous system but it has discrete liftoff stages or on the right here with the robot where the kinematics and how the robot moves that could be modeled with Simulink but the different operational modes that would be modeled with stateflow okay all right so now let's talk about the case study that we'll be looking at so what we're going to do is look at a device that generates heat when it's switched on okay so the operator presses the button the turn turn it on and then some heat is generated and so what we're going to do is find a way to dissipate that heat so we're going to have a fan system and we're going to develop the logic for controlling that fan system okay and also for controlling the operation of the device alright so we'll do a number of things for example turning the fan on or off determining what should be the appropriate speed of the fan what what happens if a primary fan fails do we if we have a backup fan how could we activate that and also having some type of automatic mechanism for turning the device off if the temperature of the device gets too high okay so I've just circled a number of different items in here which would lead you to believe that stay flow would be an appropriate choice or designing this type of system okay so let's open the model okay so this is the model that we're going to be working with so before building up our state flow chart let's look at the model first and this way you understand how it works and some of the different pieces that are already that have already been developed so one piece is the model of the fan okay so we have a primary fan and a backup fan let's look at the primary fan and we built this using physical modeling blocks for example a DC motor number of sensors voltage source okay so this represents the fan that will be used to cool our device okay we use the same type of system for our backup fan alright that's connected to a temperature model block so if I open that what we've done is we derive the temperature of the device using some principles of thermodynamics where the input values are the ambient temperature and how much the device is being used whether or not the device is on and so on okay so that gives us the temperature of the device and that's sent to a scope block so we can monitor the temperature throughout time and then it's sent to a gauge block so you can see the temperature at any given time and then on the Left we have a number of switches the top switch is to turn the device on or off and then we have switches to turn the primary and the back up fans on or off right and last year we do have a GUI that recreated using guide which stands for GUI development environment that comes with MATLAB so this is used to control the behavior of the switches so if I click on or off you can see that this switches the device on or off so there's MATLAB code that we created in order to to do this and I could also control the ambient temperature and once the device is on I could also control how much the device is being used what's its usage level you can see that impact of this game block that's over here okay last thing in this GUI is sim speed so I could control how fast the model runs I'm just going to leave it at its highest value so we can run it as fast as we can ok so let's start building the state flow chart so what I'm going to do is go into the library browser and go to state flow and drag a chart into our model alright and so here's state flow and first thing we need to do is represent the main states of our system so what are the top-level States and so this is usually how I build the state flowcharts as I go from the top level and work down alright so I know that the top level states of this device it will either be on or off all right so I represent those two modes or States with a state so there's there's the state object so we have device off in the base on all right so what I've done is created two exclusive States what that means is that the Vice can only be in one of these two states at any given time and so it can't be both off or on at the same time all right now I need to define how does the system transition from one state to another and so how do we go from off to on and from on to off so I need so I represent that using transitions so if I drag over here and my arrow turns into a crosshair that means that I could define a transition let's click here go from off the on and from on to off all right so now I've created two transitions and if I were to run this model now what would happen is that each time step the system would the state machine would go from off to on and then the next time step it would just go from on to off so just keep oscillating between one and the other that's not what we want to happen we want the device to turn on when the operator switches the device off so to do that I'm going to guard these transitions using conditions okay so I've now created two conditions so condition the syntax you represent a condition using square brackets okay so these square brackets you know it's a condition and the condition says switch on so within the square brackets that expression whatever is in there if it evaluates to 1a then that transition is going to be taken if it evaluates to zero then the transition will not be taken okay and and here we have a knot symbol next to switch on so that means the switch is off then the device is going to be turned off all right so I actually need to define what happens when the device is turned off or on okay so for that I'll define a state entry action okay so on entry into the device off state the variable device on will be set equal to zero and on entry into the device on state device on variables will be set equal to one okay and last thing I need to do is define a starting point for the stateful chart if I were to run this model say flow would not know where to begin does it begin in the off state or does it begin in the on state so we wanted to begin in the device off state so create a default transition okay so that gives you your starting point this is also helpful if you're looking at somebody else's stay folk art I recommend always starting by looking at where the default transition is so that gives you your starting point and then you can analyze the logic from there okay so at this point we're ready to run our model test it out let's go to start sim and we're not quite ready because we haven't defined our variables device on and switch on hey there's also knowing interface would Simulink at this point I were to go to the simulation model you can see our charts just hanging out there and it's not connected to anything okay so let's go to our symbol wizard and what this allows me to do is it analyzes the your state chart and it looks for any variables that you used that you have not yet defined okay so it tells you what those variables are and it allows you to define those variables within the wizard okay so we have device on that's an output variable and switch on is an input variable and so I've checked them both so that means I want to create them both and I just create press the create button now if I look at the Simulink model you can see that there's an input and an output port on my stay flip chart I can drag it in here and now it's connected with my Simulink model now if I try running it now the model is running okay and what happens when you run a model that contains a state flow chart if you look at the diagram you can see that there's highlighting so right now device off is highlighted that means it's currently the active State and if I were to go to the GUI and switch the device on then the device turns on and if I go back it goes from off so on and so on now this is a very simple state chart but imagine that you had something that was more complex this animation is very helpful in helping you see exactly how your the logic and the state machine is behaving okay so you could watch the animation take place it's very helpful also for debugging purposes you know you might you might look and watch the simulation run and see that a state that you thought would be activated was not activated and the animation helps you determine why that's the case okay we could also see that the Simulink model is is working where you could see the gauge showing that the temperature is 152 degrees when the temperature gets gets pretty hot okay next what we want to do is turn the device off if the temperature gets too high right right now the device only turns off if the operator switches it off and so we want to put in an automatic mechanism so that it's turned off when the temperatures to I so to do that we're going to create some complex complex transitions so the Vice will turn on when the operator turns it on and the device is cool and it will be switched off if the operator switches it off or the device is not cool okay so I've now created a complex transition using the ampersand symbols and the vertical lines here so we define another variable here t device in this case so we have to define that now before we use the pattern wizard that was convenient because you could define multiple variables at any one time we could do that again but I'm going to show you another way you could define your variables if you go to add and data and say input from Simulink type in t device it's okay and go to back to our window and now you can see t device is an input variable to the state flow chart and let's feed that back in and there it is okay so things are starting to get a little bit cramped here so I want to make things a little more compact so I'm going to create a super-state for device off and device on let's drag a state in here we'll call it the vise logic I'm going to do is expand it so that it contains device off and device on and move things down a little bit okay so now I've created some hierarchy where device logic is a super-state containing device off and device on and then I'll right-click say make contents sub charted this is like in Simulink when you create subsystems you could create sub charts in stay flow and now I've hit into details and if I double click I could see underneath okay so that's useful for when you devout when you're developing you know pretty complex state machines you can create this type of hierarchy and hide the details of your state charts and sub charts so you can create things that are that are pretty complex okay so let's create this function is cool right we need to define a function for that so now we're going to do this using a graphical function so we're going to define our function using a flowchart okay so that's this icon here drag that in and status is the output and it's called is cool input will be T all right and I'll do a similar thing that we did for device lots you can make that sub charted C double-click all right so what dysfunction essentially going to be is just an if-else statement I'm going to say say if the temperature is less than or equal to a certain value set status equal to one meaning that it's cool else status it's going to be equal to zero meaning it's not cool so let's define this flow chart using the patterns wizard so the patterns wizard can be used to define a number of flow chart constructs okay so we have else we have some loops the switch statement in this case is just a simple if-else statement okay so there's an optional description here I'm going to skip that there's an if conditions I'm going to say when temperature is less than or equal to 160 degrees when that happens well 6 that it's equal to 1 else status equals 0 okay and there's a flow chart okay so let's review the flow chart that gets generated from the pattern wizard and the best way to review it is to start with the default transition that's our starting point okay so we start with the default transition get to a junction what's a junction junction is essentially a decision point okay so when you hit Wednesday will hit the junction it needs to take one of the outgoing paths from that junction and so what staple would do is it will look at the different outgoing paths one at a time and it and it decides its order based on this numbering system that's created here so first it'll check this transition because that's labeled with the number one okay so that's the highest-ranked priority first it's going to check and see if temperature is less than or equal to 160 degrees if that's true then it will proceed along this path and if it proceeds along this path then status is going to be set you to one and you see this is another concept introduced this is a condition to action okay and that's represented by these curly braces so when you see curly braces you know that that means it's an action okay whereas with square brackets that's a condition okay if temperature is not less than or equal to 160 degrees then it's going to take another path and the next path is here and that's just a simple else it's not going to check any other conditions it'll just set status equal to zero okay so that wraps up part one of our webinar series now let's talk about some of the concepts that we've covered here in this first part so first we saw the exclusive states with device off and device on and the state char can only be in one state or another then we saw how do you connect different states together and that you use transitions to do that let me saw how you could use conditions to guard the transitions so when a condition is true that's when the transition is taken from one state to another with state entry actions so there's an action that takes place upon entering a state we saw the default transition that's your starting point in your state flowchart we saw the interface with Simulink and how you connect your state flowchart up with your Simulink model we saw the symbol wizard which is a convenient way to define a number of variables in the state flow dictionary data dictionary at any one time we saw complex transitions where we the ampersand signs and the vertical line to define Ann's and ORS we saw hierarchical States where you can contain certain states within super States we saw sub charts which is similar to the concepts subsystems with in Simulink for hiding the details of your of your different states we saw one of the types of functions a graphical function which is used to find functions using a flow chart and last we saw transition priorities where we have the junction and different paths that it could possibly take and it decides which condition is going to check by looking at those transition priorities which are represented by the numbers on the transitions so if you're interested in learning more about state flow and who is using it and maybe dive into some some of the details you can go to the state flow product page and we have a number of user stories there we have a number of demos there as well so if you want to see some of the different applications for which you could use State flow you can see that all on that on the website we do have a training course for stay flow so this is only an overview we're kind of doing a broad brush over the concepts but if you really want to get detailed comprehensive understanding of state flow we do have a training course available it's one full day and the details are listed here on this link and this is just part one of our webinar series so if you want to see parts two and three you go to the product page and click on demos and webinars and you'll see a listed there or you can just follow the link that I've shown here and that will also take you to the list of recorded state flow webinars and if you're interested in an on-site demonstration of state flow please contact your MathWorks account representative so with that we're going to go offline for a couple minutes and gather any questions that you might have
Info
Channel: MATLAB
Views: 50,362
Rating: 4.9161677 out of 5
Keywords: MATLAB, Simulink, MathWorks
Id: thBxzulFuyg
Channel Id: undefined
Length: 27min 49sec (1669 seconds)
Published: Mon May 01 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.