Modeling State Machines with Stateflow | What's Your State?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a finite state machine is a model of a system that has a finite number of states and behaviors associated with those states and then we also have transitions um to determine when or the other and in fact what you're seeing here is one of these discovery pages that sort of explains the concept of what finite state machines are and so these are great for modeling complex control logic or decision logic yeah so so i i think i think if you could if you could map that to a real world problem right let's let's think of let's think of i don't know a car driving or something and you've got something like an automatic cruise control right um so it's your state machines or your state logic that's sort of controlling when it shifts between the different modes within the within the automatic cruise control control so an automatic cruise control essentially works in this in the way that um if you're close enough to the car in front of you you'd sort of hold your speed and maintain that particular distance but if that distance increases beyond some threshold you can actually keep you know your car will exceed the speed that you have set for it in in order to to make sure that it meets that distance so essentially uh your system has a finite set of um a finite set of modes of operation and you can use state machines to model that right if if if that's one way of putting it exactly and so now that we have some basic understanding of what those are let's take a look at what are some of the tools that you have available to build them model them and ultimately implement them in the real world so today we want to focus on state flow and again this is the software or the product for modeling and simulating decision logic with state machines and flow charts so it really gives you those you know uh these elements so that you can build those flow charts straight transition tables truth tables pretty easily and the great thing is stateflow works with both matlab and simulink but today we're going to focus on stateflow for simulink users so if you're interested in stateful for matlab users let us know in the comments below so uh so far we've covered state machines stateflow and and i think we can infer that stateflow is great for developing supervisor control logic task scheduling fault management applications and other things like communications protocols user interfaces and much more okay so this right here what i'm showing is the product page of stateful you can come here and and sort of explore more what are some of the things that you can do and uh just to give you an idea of where stateful fits with matlab and simulink for those of you who are not too familiar i want to show you this slide right we have matlab which is the the programming environment for numeric analysis numeric computations and much more simulink we also have a live stream on that is the platform for modeling dynamic systems and this is a graphical or low code environment for modeling and simulating those dynamic systems and then we have stateflow which is an add-on that again works with both matlab and simulink and in fact we have over 100 add-ons that help you do specialized tasks more easily for everything right yeah all right so now i think we answered the first question what is state flow state machines and so on we have a little bit of background in context let's jump into some examples so let's tackle a real world problem and let's think of cell balancing let's imagine we have a battery pack maybe this is the battery pack that you would find on a hybrid electric vehicle and so as you cycle this battery pack that eventually causes the battery cells to go out of balance and what that means is that you end up having cells with different soc levels or state of charge levels and that's undesired because the weakest cell or the one with the lowest state of charge that limits how much of the battery you can use and the strongest cell determines how much uh charge you can put into the battery so that's undesirable and so we want to go from something that looks like this to something that looks more like this where all of our cells are in balance so that's the problem that we're trying to tackle here so let's jump into the product to access state flow i can do that we're going to focus on simulink today so i can access simulink from the start page and to get to state flow i can create a blank new model or i can start with templates for stateful here so this is the nice thing with the start page that i get a few templates for blank charts simple charts so those give you a starting point i already have a model that i put together to tackle this charge balancing problem so let's use that as a starting point so what i have to the left are the inputs of the model here i have the bms state so this is associated with this switch and these are dashboard blocks that allow me to interface with my model or interact with my model so this bms state is just a switch right on off one zero here we have the cell voltages so this is an array of values that are associated with these knobs so that i can change the voltage values and then from there we're just taking the maximum and minimum cell voltages and then on the output side we're looking at the balancing command for each one of the cells okay so our challenge is to build a state machine that looks at all these inputs determines the operating state of the system and then gives us a command or an output based on that right so it's essentially the the brain of your charging system if you could if you could exactly that's one way to think about it so well there's a couple ways that i can get started here i can go to the library browser we have videos doing this before where you can explore all the blocks and libraries that simulink gives you or i could just use quick insert so i'm going to go and type stateful here and it recognizes that i'm trying to find a chart from the stateflow library so i just select that and here is my chart so now i'm going to make connections first and then we'll worry about beautifying the model so i'll make this bms state connection here and you notice that i can just create ports by clicking and dragging and then it's really nice that these ports are automatically named after the signals and i can even edit those port names as i create them so let's continue making these connections here and now i'm going to connect the output here the balancing command on the output side and now the tool strip um gives me some really nice formatting options to match the size and align that area and my state flow chart so that's where i want to have things now we're ready to jump into the safely chart and start start building the the brains of our supervisory logic or our state chart so one thing i want to bring up is you know the tool strip it shows me different capabilities based on uh what is available for state float so if i go into this debug tab i'll see things that are specific for stateful same in the modeling tab so i want to bring up this symbols pane because this is pretty important because this will help me understand what are the inputs outputs data and parameters that i'm working with in my stateflow chart all right now that's the symbols pane here i have my panel again this panel is a collection of dashboard blocks and when you make them a panel they come with you to different levels of the model hierarchy so pretty useful for modeling testing and simulating your model the nice thing is while i don't need it i can just double click it and minimize it so that it's not in the way while i build my model this was a relatively new feature right i think it was added when was it 20 a maybe uh releases ago yeah probably 20 i think so yeah pretty nice and you can have multiple tabs with the panel too so it's not just one tab you can change multiple inputs even look at the outputs in the same panel so it's pretty nice so let's start adding a state so you could add elements here with the tools that you get on the left or you could also use quick insert so here i'll just start typing in the canvas and i want to state so let's select that and here i have a state and here you also notice this dark dot at the top that's the default transition so when the stateful chart wakes up and because it starts executing that's the first point that we're going to go to so i want to name this first state balancing off right or operation is basically off now i'm going to create another state so i can just draw and select state and this one is going to be balancing on so now we have these two states now we need to define transitions to go from one state to the other right so i could just draw lines here by dragging the edge of the state and the transitions to go from one state to the other can be events they can also be conditions or they can be actions that happen as those conditions are evaluated so i'm going to go with the square brackets since those are conditions and the condition here to go from off to on is that the bms state has to be equal to 1. if you remember the bms state is the switch that we're using in our dashboard panel so now to go from on to off you guessed it i want another transition that is probably just the opposite where bms eight is equal to zero so i think a quick thing that we should point out here is uh essentially every transition has three components to it you either have an event a condition and an action right um uh so we haven't really gone into events we'll go into events a little later in in the stream but i think the one thing we need to point out here is every time you put something within a square bracket it's a condition those are typically inequalities or boolean type expressions that you want to have um and the the the term that we use to to record conditions we say conditions god trump or conditions guard transitions if that makes sense so um as soon as soon as your program enters the state it's looking for that condition to be true so that you can take that transition and go to another state that's that's just sort of a high level understanding of what's going on here yep those are excellent points so now we got states we got transitions we need to determine what happens or define what happens when i'm in one of the states right and so the way we do that is with actions and in stateful you can have entry actions during actions and exit actions so you can have them individually or you can even combine them and so the names are self-explanatory entry actions happen when you enter a state during actions happen while you're in a state and exit actions happen when you leave a state in this case i just want to use entry and during actions and the first thing i want to do here is i want to set the balancing command if you remember that is the output that we're working with i want to set that to false when i'm in this state right balancing is off nothing is happening um probably just copy that and then in the balancing state i also want to establish entry and during actions now a nice thing is that i can abbreviate those actions with just the first two letters so there's a little tip so i want my balancing command to be true all right and so i think at this point we should have some we have some basic logic that we should be able to simulate and if we look at the symbols pane we have the the outputs the inputs and these three things that are not being used yet so those are the warnings i'm getting here but i think we're ready to try a first simulation so it's going to compile and then as we start running this you'll notice that this is going to come to the default transition and it's going to go it should go into the balancing off state and one of the my favorite things about stapler is this animation that helps you visually understand what's going on and debug at your uh complex decision logic yeah so it's essentially it'll it'll light up whichever state the code's in at that point right exactly and so another nice thing is that when you hover over states or transitions it gives you some information on the data that you're working with so pretty nice okay so we're in this state if you notice here our balancing command the value is zero right now as we would expect as false if we flip the switch here to go to on we evaluate that condition and that becomes true and now we're in the balancing on state therefore our output is one if i flip the switch back to off yeah we go back to off and so we got some working logic some elementary logic that's that's in place and we're ready to go exactly so i'll get my dashboard out of the way and we can continue adding a little bit of complexity um as well so now let's imagine that we want to compare the individual cell voltages to a target delta voltage and for those cells that are out of balance then we're going to send them an individual balancing command so let's think about how we could do that and so i want to start by calculating a delta cell voltage so this is a new variable here and that's just the difference between the maximum cell voltage and the minimum cell voltage you remember these are inputs that we're working with this is a new variable that we're working with i'm also going to use that in this state the balancing on state so we know what the delta cell voltage is at all times and i also want to use this delta cell voltage in my condition so the nice thing is that i can combine logic uh with these transitions right so i can just say and um we want delta or whenever delta cell voltage greater than let's say two volts this could be a parameter or something i'm just going to go with two volts then we'll transition here exactly all right and the last thing we need to do here is we need to determine the balancing command for the individual cells so what we'll do here is we're going to look at the cell voltages so that's the array of values that we have that's one of the inputs and we're gonna compare that to the minimum cell voltage so to the lowest value and whenever that delta is greater than two so the threshold that we're working with then we'll send a balancing command to those cells right so we just added a little bit more complexity here and so if we look at the symbols pane there's another thing that tells me if i have a typo or not so i know this is an input but right now it's not resolved so i got a typo there thank you symbol spain and then this one is an undefined symbol so all we need to do here is just tell stateful hey this is local data right because this is something that is being calculated in the chart and now all of our symbols are resolved so we have uh you know now we can simulate our logic and see what happens if it breaks or if it works so when we run it fingers crossed it's always fun doing live demos right that's the best part it's working um so we started the balancing off state uh again the output is should be a an array of zeros right because they're all false and we can double check that if i go out here yep they're all zeros here you can see that in the display now if i flip the switch now i have one of the conditions to go into the balancing on state but you see that the delta cell voltage is only 0.3 volts right and so we need it to be more than 3 volts so now i can change the voltages in the cells here you know in reality you might have a physical system that is giving you sensor feedback on these voltages or you could have a plant model that gives you feedback on these voltages here we're just going to do it by hand and and change the voltages for cells one two and three so you'll see that when i start changing these values then the delta cell voltage became like seven volts and therefore we have all the conditions to go into the balancing on state and so if we look at the outputs now if i go out one level you see that we're sending a balancing command to all the cells except the lowest value because we're implementing a passive balancing method here so we're trying to discharge all the cells with the higher state of charge down to match the the lowest state of charge which is cell one in this case so you know this is a functional logic that you could apply in the real world and it just took us i don't know 10 minutes to build this and so the alternative would be to program this with a bunch of statements yeah and so you could end up writing hundreds or thousands of lines of code whereas with stateflow is just you know a chart that helps makes it really easy to create these state machines debug them and analyze them and so now let's see what happens if i turn off the switch right because this condition should actually take over and so if i switch it off then there we go we end up back in the balancing off state and as you would expect our commands are zero again so that is the first example of how to build some cell balancing decision logic with state flow yeah but i think i think i think now is a good time to stop and take some questions while we uh maybe we do some some technical settings in the background so we can show you another example on how to fly quadcopters with using uh um with using state flow so i'm just i'm just going to look in the chat to see if there are any questions uh we have a question on integrating state flow with autosar um this one's interesting so so uh do you have a good answer for that uh integrate uh can you can can we share more details yeah i i think i think so so my understanding of what star is autosar is a programming construct or a programming standard that you have to sort of meet uh align to and um i think for the most part state flow state flow charts sort of make up the largest simulink environment so if you in the in the realm of us of a simulink uh model a state flow chart is just another signaling block if you will so as long as you can sort of get your model to work within um within um you know within those autosar constructs it should just work uh but but there's definitely a lot more information in in the documentation i think that's out of the scope of this of this webinar but you can take a look at the auto start block set in the documentation to to sort of uh to sort of go through that yep that's a good point any other questions coming up um i don't stop is this is this clip gonna get saved uh yes you can you can actually watch the um the the livestream after it's after it's done all right if we go back to the previous example that i was covering can you explain why the cell balancing command is being set to the cell voltages minus the minimum cell voltage divided by two or greater than two i'm sorry so what we're doing there is comparing this the voltage of each individual cell we're comparing that to the lowest voltage in the array of values and if that's greater than two volts and again it could be any number that too that's just a threshold that we're working with so if that delta is greater than that then that cell needs to receive a command that we need to balance it so discharge it to match the lowest um soc that we have in the pack or the lowest voltage that we have in the pack so hopefully that makes sense again this is going to be recorded so feel free to go back re-watch the explanation and look at the code so that you understand what's going on there and then we have another question saying is stateflow obligatory to be a part of the control system model uh i don't think it's obligatory i don't think it's it's it's a necessity it's a nice to have the the way i like to think of it is you have some sort of pid or mpc or whatever low level control that's controlling the actual actuation of motors and things like that on your system um stateflow sort of acts as the high level controller so it's sort of generating those control commands that would go into your pid controllers if you will so i i think i think um i i don't think it's an i don't think anything is a necessity it's just state flow is a nice thing to have to control the high level decision logic of your system right it's a choice that you get to make right if you implement this kind of logic again with uh c code or just plain text code you're gonna end up with hundreds thousands of lines to handle all the complexity you know when you start developing large systems whereas with stateflow it's really intuitive to create this graphically to debug it to analyze it and that's one of the benefits that you get right in addition to the automatic code generation that you can do at the end so you can just transform these state flowcharts into c code with just a few clicks and we also give you a lot of the tools to do all the simulation testing verification and validation uh before you end up in code or even during simulation so those are some of the benefits of state flow yeah uh and then there's there's one more question on how to model an electric micro grid i think that's out of the scope of this particular webinar but i highly recommend you go take a look at the simscape documentation which has got a bunch of information on on modeling electric grids and stuff like that um all right so i i i think i think we're ready to move on to our second uh our second demo for the day um um as soon as soon as you guys can see my screen on on the stream all right so uh for today's example what we're going to take a look at is we're going to look at a simple quadcopter and how we could how we could fly a quadcopter using stateflow to govern the sort of decision logic of it okay so um i'm i'm going to pull up this is this is a simscape what you can see on the screen is a simscape um or a simulated 3d animation visualization of of of this padded mini drone uh quadcopter that we're flying if you look at our simulink model over here it's it's got a bunch you know this is this is the plant model it contains the dynamics of of the quadcopter that's not what we're interested in today we're going to go into the flights flight control system and take a look at our our control system and for the person that asks the question about using state flow in your controllers this is a good example of how you could use state flow to do the high level control so um as you can see this controller block that we've got here on screen um this is this essentially has your position altitude in your controller we're not going to touch that at all that's they're just pids implemented under the hood um we're going to go into this spot planning stage state that we have here or part planning subsystem um now if you if you look at the pathline subsystem we've got a bunch of data coming in from the state estimator again this is a very simplified uh model we're not gonna really touch that a whole lot we're just gonna think of think of it as doing some sort of dead reckoning where we ask the drone to take off sort of move around until it sees a blue color square on on the ground um and we're going to use this um this information coming in from our vision based data so if we go up a couple levels uh there's an image processing system that's doing a simple color thresholding algorithm to identify the blue color right so that system will will will give us a a flag if you will saying okay i've seen something that's blue um what we're going to do is is we're going to go in and use state flow to um to actually tell the drone to move through its different states right so uh as as ed did earlier in simulink online i'm using the simulink desktop version um just because you know there is there are some aspects of simulink that are not supported in the on in online yet especially things like the um like like the visualizations and stuff that so that that's why we're in the um in in simulink desktop if you will so i'm going to go and go ahead and grab a state flow chart real quick um insert now in this state flow chart we essentially our system is going to run through three states right if you want to take off fly around for a little bit and then land we essentially need three states so um let's call the first guy uh take off remember you have to name your states otherwise this it'll let her out so i it's a good practice the first time you you try to stay in is you know just just give it a name um you can right click and drag to make a copy of it uh uh and in state flow smart enough to tell me that it's take on i don't think it stays if you want to call it take on but but this state flow will is sometimes smart enough so if you put in like an off state it'll know that your next day it's going to be an on stage so it'll sort of help you with that um but but let's let's call this state go right because we we know again we we're doing a very simple dead reckoning over here we know that the blue block is to the right of our of our starting position um and then i'm going to copy this and make a a third state and we're going to call this oh go wrong i like that don't you like it when sibling makes suggestions to you uh i love this and you know one of the nice things about stateflow is that it it uses edit time checking so yeah it does a pretty good job letting you know giving you warnings and errors yeah the thing is not gonna compile and run but i love these uh these suggestions that it's giving you that's awesome so let's let's call this the uh the land state right now um again you know we we'd have to go in and set some some actions right so actions and transitions um um so in in the take off in the takeoff state you know all of our actions are going to be during actions so i'm just going to call it bu and then what we want to do is again now this is not this this logic is not controlling the commands that go to the motors it's controlling what goes into your control right so um the way i model setup is i can use my state flow chart to just give it x y z positions and uh my controller is smart enough to sort of handle that and adjust the right motor voltages and stuff like that to make the drone fly there so um in the takeoff state obviously i'm going to say x equals zero um that's that's definitely one one action that we want y equals zero which is the other one so we know that it's going to be at the origin um and then we want to give uh we want to give it some some displacement in the z direction now the convention with simulink is um is negative is upwards especially with the aerospace block set so um we say z equals negative 1.1 and we just wanted to basically take off to a height of 1.1 units above the ground um okay so we've got our takeoff state in place um now let's let's let's drop some transitions and i'm going to show you something really cool with state flow so what we saw in in ed's example was ed was using sort of boolean conditional statements right uh you can also model things like temporal logic with with um with um with state flow so um if you want to essentially have some sort of a time buffer between um between how you move from one state to the other it's actually really simple you can so as you remember um square brackets indicate a a condition so square brackets and then you just say after after um then you open up the brackets and say let's say let's say two seconds but you can also do something like two takes two minutes to whatever um okay so and so so this is this is essentially just just a simple temporal logic it's going to say uh once it enters the takeoff state wait for two seconds and then move to move on to the go right state so what we're doing is we're saying okay drone take off and then hover for a little bit so you can stabilize before you start making any motions to the right or the left um okay so again with the go right we're going to have a during condition again over here and in this condition we wanted to move in the right direction so the positive y so let's just let's just use y equals y plus um and let's get just give it some small nominal values so you know it'll it'll move it'll move smoothly rather than just just jumping through um what it has to do um so so this is our go right state we know that we wanted to sort of keep moving on until our downward facing camera and our vision system sees that blue blue box right um so this is where we're going to use information coming from as an input now um if you so let me go and drop the connection of nope that's not what it is let me drop the connection uh we want to have another another condition here obviously and again these are the three sort of constructs that we spoke about earlier the events conditions and actions actions are always within within curly braces if they're on a transition um but let's let's go into this condition and let's say let's call this is uh blue and this is interesting because because is blue is essentially a flag that's coming in from my my vision systems right so that could just be my transition you know is blue is it positive or is it negative or is it true or is it false rather um so if i if i go up a level um let me grab uh this connection here and you know just drag it into the block so now so this is this is my is blue condition that's coming in um and then i can also go and connect these up so um let's actually let's actually go back in here and and complete this before we connect that up so um in the in the lan state again uh we're just going to set z equals z plus um let's do 0.01 now again remember it's plus because our sign convention is negative in the upward direction um okay let's let's go into the modeling tab real quick and open up the symbols pane so we can resolve our symbols um so obviously it's telling us that it doesn't know what all of these are um [Music] so let's let's call this an input um let's delete this guy okay so it is blue is the input and we know that x y and z are going to be our outputs which which we'll send to um which you're going to send outside of so we're going to send outside the state flow chart so this is resolved to an output uh let's call this an output as well resolve this guy to an output yeah and you know we can do this manually or sometimes when you already have the connections for the input and output it's smart enough to realize what's an input and what's an output so that's another way to go about it now if you look at i'm glad you brought up the point about the temporal logic so the after that you see in the first condition because this is a really powerful concept if if you try to imagine implementing temporal logic with code you know there's a bunch of things you have to keep track of um as you write that textual code whereas here is just one line of code and the nice thing is you can combine operational logic right like uh something that you have on the right logic so operational and temporal logic it is just gives you a lot of flexibility and a lot of power with just and then we've got the numbers are kind of wonky but x y see let me go and probably resize that a little bit yep let's do that i i i'm sure that uh there are a bunch of uh sibling power users out there that hate that my signals are crossing but this is that's the pedals of doing live demos right i promise i'll clean this up if i'm publishing this anyway um let's let's actually pull up the visualization so we can see what's happening in here so so this is our state flow chart i'm just going to go and grab visualizer let me just resize this a little bit so we can see what's happening and then we can also view what our camera is seeing so it'll pop up once we actually run this so i'm going to go ahead and uh i've set the pacing on for this i'm going to go and run this system and you should see how the drone takes off and then sort of hovers so you see it's starting from that zero zero position and uh there we go this is this is what the this is what the downward camera on on the drone is seeing and see it'll hover it sees that green the the blue block and then and then lands and then i just have a flag that's saying hey you know stop the simulation when it lands um so can we run that again from here i want to see sort of the progression of the different states and things like that yeah so so let's i think i think one good way to visualize this is let's let's open up this guy here and and now let's run this so essentially what we know is when when we start running the simulation um it'll it'll get into the takeoff state sort of hover up to that 1.1 unit mark um and that's when you you you should start seeing that that red strip sort of come into view and then now we know that it moves to go right where it'll sort of hover to the right and then switch that's that's essentially uh you know a quick a quick introduction of how you can use state flow as a high level logic now obviously in this case we we're just using dead reckoning right right we're just telling it you know move forward for a certain time or for a particular you know for a particular amount of time um you can just as well grab information from the state estimator and sort of compare values with that so say okay you know if the state estimator has moved so much only then switch states and things of that so um there's a lot that you can do this is just a this is just a quick example of of uh of how you can use stateflow for um for decision logic now um do we want to stop and and take some questions before we move on to an another demo let's do that i actually have a couple of questions um so if you go over to the debug tab uh what are some of the ways that you have to debug state machines right that's you start adding complexity because i just thought of that as i saw the the error i know it's an assertion but you may run into other errors as you develop this logic and so maybe it's worth going through some of these options for sure so so if if you're a matlab users or simulink users you're all familiar with the concept of breakpoints right so breakpoints are one really nice way of of sort of debugging the state i think i think the the first the first debugging method is actually visualizing how the states light up like for me for me if if i understand how my staple chart needs to work as soon as i run it the first time i'll try and introduce different conditions to make sure that it's actually making all those transitions correctly and and visualizely you know or visually uh debugging the charts right visualizing that's that's not a word visually debugging the charts um but you can also you can also do something more more concrete like adding a breakpoint so let's say if you want to add a breakpoint to the go right state you can right click on the state and say set breakpoint and what you see is this little this little red dot that comes here it's if you've used if you've written a lot of matlab code you know that what that breakpoint does so um if we run this guy real quick you'll see what this does it'll essentially stop the code once it gets into it into that state see that takes off and then now it said okay there we go code stop and and then you can use um you know if if you want to if you want to make sure if you are if your code is actually performing correctly you can sort of do steps um where you say say step forward one yep and that gives you a chance to sort of slow down and inspect the behavior of the state machine to make sure it's what you actually would hope would happen just like matlab code and breakpoint so yeah it's really powerful to have this so so this is this is one way of doing it uh yeah do you want to talk about some other ways of debugging state for charts or we covered all of it uh no there's plenty i mean i don't think we're going to go through all of them in the uh yeah debugger but one one of the ones that i want to point out right now is grayed out but it's the activity profiler and so that gives you a report after you run the simulation of where are the states where you're spending spending the most time right and so we probably don't have to run it but that's another really good option that we added recently and it's a really powerful way to debug state charts that's awesome all right and you can always log the outputs and things like that for analysis but we probably want to move on so let's take a few other questions there was a question that might be a very good segue for the next demo and the question was about parallel states so can we talk more about that connell what are parallel states and and you know what kind of logic can you do with stateflow can it only be sequential logic or can you do uh parallel logic as well perfect so in fact it's the perfect segue into what we're going to talk about next so the uh essentially what a parallel state is so the states that we've been looking at so far are sequential right where you know you follow a default transition it enters one state it executes whatever it has to execute within that state but it's also looking for transitions to move out at the same time a parallel state essentially means that there are two states that are awake and listening for information the same at the same time so um let's take a look at this this model this is an automatic uh gear transmission controller um and if so essentially what's gonna happen is you have to decide you have to decide two things with with an automatic gear controller uh basically what speed range you're in and then when you cross that particular threshold do you have to move in the upward direction or the down you know you have to up shift or downshift right those are essentially two things that you're thinking about if when you're trying to model an automatic uh transmission um so let's take a look at the state oops and i have a third state here i don't know why all right cool so so so these are these are what's called parallel states you know you can sort of denote a parallel state by noticing this this dashed border around it um and then you have a number associated to the hierarchy so this is the first parallel state this is the second parallel state right um now no now what's going to happen is as soon as this chart wakes up both these states are going to be active at the same time and you'll see it when i run the model um what we're doing here is we're essentially saying okay so in the first state let's pick which gear we want to be in whether we want to be in first gear second year or third gear right and then the second state is what is what we like to call the selection state where you're either in a steady state mode where you're not shifting any gears or you cross some threshold within that gear range that you want to up shift and go to a higher gear or downshift and go to a lower gear right um and so the the way we're going to compute this threshold is we're using what's called a simulink function now this exists really expand state flow because you can use things like simulink functions uh matlab functions graphical functions truth tables etcetera and all of those are visible on this on on this this left-hand tool strip um but if if you open up the sibling function what you have in here is um essentially just a a couple of lookup tables so based on throttle position and and gear value it'll it'll sort of tell you what your thresholds are you know it's it's just a lookup table this could very well be pid controllers or whatever state you know whatever simulink models that you have i think i think one good way of looking at this is if you've got say a vehicle that has two modes of operation one is position control and one is velocity control you can sort of use state flow to control that and sort of pick which which controller is is being used at any point in time right okay and so i i think another powerful concept is the idea of reuse right so if you have these subsystems or matlab functions you don't have to recreate all of that you just make a function and then you call it as much as you need to and there's a couple of other points i want to highlight here with the simulink function you notice that the syntax that you have in the box is pretty similar to matlab syntax right you have the function outputs the function call and then the inputs to the function and so that brings me to my other point which is the action language for these charts usually you can have matlab as the action language for these charts that makes it really intuitive so it's like you're using matlab code in stateflow for all the syntax or you can have c actual language for the chart and you can actually see that in the little bottom left corner icon there it shows you what the action language is for that chart looks like it's c for this one right yep all right so so let's let's introduce the the concept of events i know we spoke about events a little earlier but if you if you if you're in us if you're in a simulink or if you're in a state flow chart and you see something with this gold orange text color that's what that's what's called an event right so essentially what we're saying is um when so if you if you look at this particular area right where um if you look at the the selection state you know we we'd start we'll start this the chart will start by entering the steady state um when your vehicle speed goes below this this you know this this threshold that we've computed using this this uh uh the simulink function it'll go into what's called the the downshifting stage the the the downshifting state rather now um once once it goes into the downshifting state we're going to use this after and then t weight is just it's just a local parameter you know this could be five it could be whatever just could be a number we're saying after some amount of ticks um if your vehicle speed and notice the square brackets indicating that this is the condition so if the vehicle speed is less than or equal to that same down threshold value um let's trigger this event so the way you trigger an event is you say gear underscore state which is the name of this big state up here and then you call that event dot and then you call the event after that so you say gear underscore state dot down now what's going to happen is as soon as this gets this gets this action gets called uh depending on which state we're in it'll it'll take the transition that that is triggered by that that is guarded by that down event right right so so that's essentially how how events work at a very high level so we can actually go ahead and run this um run this code real quick and we can we can see how this will let me actually open up um the results tab as well and pull that in here uh just give me a second um yep and so as you run this one one of the things that i want to point your attention to again because kind of covered it is when you look at those uh transitions uh down at the bottom right you see you're combining temporal logic with the conditions and then actions right to trigger the events so if you look at the square braces the curly braces you get the power of it all in in just one uh transition line so yep so really nice if if we go ahead and and run this we should you know we should see that these these engine if if you're if you're an automotive person and you know what it means typically a drop in engine rpm is is when you you shift your gear um so let's actually let's actually see what happens and we'll watch it sort of switch through you saw that that that blip and the state change so we're doing a little bit of upshifting i mean sort of in steady state and then now we're going to downshift and you'll see that the rpms will increase um but yeah this is this is this is a sort of notice that when you have parallel states both the states are active you know they're both sort of highlighted and listening for information at the same time i think i think one good use case of parallel states is um say if you have like some vehicle that's that's that's you know doing something and you have um like a a telemetry system where you can update you know missions to that vehicle if you will um your communication layer could be a parallel state that's also listening in in you know at the same time as the other states running and then whenever you upload a new mission from from your ground station it'll you know that that parallel state will change will change you know change some parameters it can actually change the way your system's operating so that's just that's just one example of how you could use parallel states this is another one uh but yeah this is this is at a high level how you could how you can go through state floor um yeah this is this is pretty awesome pretty good overview and again just to recap we saw the three demos so the the charge balancing logic uh then we went into the the drone logic and and another real world example of how you could implement an automatic transition with temporal logic events and so on any other things we want to cover here before we move to the third section on resources and getting started i think let's let's see there any questions based on what we've done so far let's give it like a couple minutes of questions that we can go back to um while they're setting up our screens in the back uh to go through our last our last section um let's let's try and cover up uh let's answer any questions that you'll have so please pop them in the chat so i see a question coming in about how to transfer data between two parallel states in the state flow um so technically both of these states are are viewing the same base workspace under the hood so um so you don't you don't really need to you know that's how data's they can both see the same the same data variables if you want to send like for example in this case we've used uh uh to sort of send this um this this event we're not technically sending an event we're just triggering an event value and then because both of these states are looking at the same base workspace the same workspace under the hood um they they know when these values get updated um so hopefully hopefully that answers your question right and you you showed another example with the event right where when you triggered the event you had the state name and then also the event name to trigger that so but but yes i think the right answer is what you said they're looking at the same local data or data repository so they would know when a parameter has changed and it can work with that value can we create a test case list to see if the control logic fails in any condition or not that's an excellent question and so yes we give you the tools to verify and validate the design right and so testing or simulation is a form of testing you can wrap your logic in a test hardness to sort of work with predetermined inputs analyze the outputs or you can use simulink test and create a test suite where you know you go through different scenarios different inputs and you can even automate the process of uh testing and then you get nice reports that you can share with colleagues and and other groups and you can even do things like coverage but you know that those are more sophisticated topics that we can cover in different livestreams yep um how do we reduce cyclomatic complexity i'm not sure i'm familiar what that means i'm learning new words today uh let's let's let's do what all engineers do and google what that means uh i've heard the term before i think it has i'm not a computer scientist so maybe i'll just yeah me either i'll google it too cyclomatic complexity all right while you google that can you give us one or more examples for parallel and or um well you can i think we shared in the chat the example that conol was working with for the automatic transmission that has parallel states also the first demo that i showed you for the cell balancing logic that's an isolated component of a much larger uh system where we have a closed loop model of a bms a battery battery management system and the plant for the battery itself and so there we have a the supervisory logic for that and it has parallel states and uh fault detection and you can see basically the full power of state flow on that one as well so i recommend you to check out those links and i can talk about examples in just a few minutes here uh okay so so so maybe this is me i'm gonna try and attempt to answer the cyclomatic complexity question and uh the question that talks about parallel and and all states so um uh so when what we saw are two parallel states the parallel states are also called you know they're also like the the or condition states right both are active or or rather and conditioned states where both are active at the same time um you can also use a concept called super states and sub states so essentially if if we look at one of the models that we were looking at you know we had um can you pull up that charge balancing uh model if your screen is uh um is ready to go so let's do it yep we're working through it sweet so uh if everyone can see um can you can see the screen that's that's going on um so essentially what we've got is we've got a balancing off and a balancing on now this whole both of these could be embedded into what's called a super state right and essentially once you you only get into the super state or these these smaller states only become active once you're in the largest super state so one good good sort of convention and a good modeling practice is to try and use uh sub states and superstates as far as possible because this will what will happen is you can sort of collapse this and and reuse different parts of this to hopefully reduce your um the complexity of having you know multiple lines and and and and all of these kind of things but but this is an example of of often you know so like i i know in in this case we've got transition between transitions between these two states but if these two states aren't connected to each other that would be an example of an um of of an or state if you were right it would either come in and activate one of those or the other one based on on which condition it is um yep and just to go over the mechanics here is how you can change that chart decomposition right to be exclusive or parallel which is exactly what you're talking about so just go to the modeling tab and you can play with that for the different states in your chart yeah so i i i think for in terms of cyclomatic complexity you want to use uh these sort of superstate substates kind of like how you use subsystems in in in simulink um but yeah hopefully hopefully this answers your question uh all right as as a non-computer scientist so we've covered a pretty good amount of ground let's go into the last question or the last section that we had for today um and that is how do you get started with stateflow and so i want to show you a couple of ways for that i want to go back to the simulink start page so when you open up simulink you can actually see this examples tab and this is something that we have mentioned before in our previous live streams so you can check out um modeling and automatic transmission so that's how you can access the one that you were working with connell there's a bunch of others for simulink and other products and down here we have some for staples you can see that the featured ones here and if you want to see all the examples available to you you can also click on the view all and that'll take you to that page so you can run those examples modify and tweak them and try to fit them to your custom needs so that's one way the other way i want to point you or that i want to show you is with the on-ramps and these on-ramps are again free interactive tutorials that help you get started and familiar with the tools in just a couple of hours so we have them for simulink state flow simscape control design and they work with simulink online so if we look at the stapler on-ramp sort of gives you a curriculum or a set of activities here and if we open for example this task or this section on running state machines we'll see it'll bring up the interface and it has different panes to basically walk you through the exercises so now if we i maximize that this is pretty nice and so what you see here is to the left i have the tasks and all the instructions that i have to follow here i have you know my working area with the stateflow or simulink canvas and then here i have things like the uh the results and also the symbols pane so and you can submit and progress through and you get immediate feedback on how you're doing with these on-ramps so definitely encourage you to check that out you can also just google it and that'll take you to the state flow or the simulink on-ramp page we have pages for that and you can get started that way and so we've covered examples on ramps and then the last thing i would like to mention is the community we have a pretty strong community and um i think many of the viewers are part of that community so thank you for that so if you go to matlab central here you can look at answers provided by mathworks experts but also the community and then file exchange when you can you can download the work of other people with matlab simulink and all the other products so that's what i wanted to point you to with that i think uh maybe well we're at we run out of time questions but we can take a few questions and a few questions wrap up after that uh for state flow analysis can i use the simulink model i'm not sure what you mean by that question but yes you can essentially all that we've done today is we've been working within simulink models uh but we just they just launched a feature called uh state flow for matlab so you can essentially you know build a state flow chart but then if you want to test it call it from from from matlab and um and sort of you know give it like test cases and things like that so that's def you know you can use it with both with simulink and with matlab um yep and also to complement that answer you can actually log state outputs uh state outputs with uh in your simulink model to basically do some graphical analysis of the behavior of your state charts all right anything else all right so i i think i think i don't see any more questions coming in uh thank you all of you all for taking the time out to to listen to us talk about stateflow um and uh yeah please hit us up in the comments and let us know what what more you'd like us to talk about you know there are i know we've got a lot of requests for programming hardware and we promise you that we're going to do one at some point in the future but uh but but yes if you if you um if you have any topics to talk about please please let us know um in the comments and hit that subscribe button so you know that you can you can find us again when we go live uh and the notification bell so that you know whenever smash that like button all right folks thank you very much for your time and your attention this was great thank you connell thanks ed you
Info
Channel: MATLAB
Views: 6,890
Rating: 5 out of 5
Keywords: work from home live, matlab, simulink, mathworks, matlab tutorial, time series data in matlab, python
Id: rUeMUCrlxP0
Channel Id: undefined
Length: 60min 49sec (3649 seconds)
Published: Thu Mar 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.