StateFlow Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
stateflow so I'm not going to explain what state machine is went over that with the people that are here in front of me before so I also inform with it so what state fluids it's a built-in function of Simulink just like how stimulating to the part of MATLAB State flow is a part of Simulink it came after Simulink just like how 3,000,000 came after MATLAB matlab's been around since the 90s or something so it is evolved a lot over time but it'd be the reason that they developed it is because state machines are becoming a lot more common and controlled and I have no idea how to code a good state machine in actual code so I'm really bad at state flow exist there are ways that you can do it in and actually I can pull it up first the guys that are here in front of me later but I have a really good example of why you want a state flow and not enabled subsystems which is how you probably would do before I'll attach that to the power point or something anyways so the way that you actually get a state flow and if you're in your Simulink library browser you can come down here in your toolboxes and there's one that's just called state flow and well you can use truth tables and other things in general what we use is just a regular old chart you just click and drag it in if my lab doesn't crash it should just generate a new one I alright so as you can see I have a chart like a subsystem it's something that separates a lot of logic into a completely different out way out so that it's not cluttering other things you can see I don't have any inputs or outputs right now I'll go over how you actually add those and use them so then I go into my chart you can see you can always tell when you're in state flow because it will have this yellow background it might be difficult to see on the monitor hopefully the video captures this properly but it is very yellow compared to this white that you would see in regular MATLAB you can change that if you want so maybe it's not way for you guys but should be by default so you also see over here on the left added a whole bunch of other items that you don't see when you come back up so that's where pretty much all the state flow items are kept you don't have you know usual library browser to pull things and do a state flowchart there's a lot of very basic items but it's all about how you combine them that actually creates how something can work so what actually are they so this first one here if you stay over it you'll see it is a state I click and drag it in and it starts with just this little question mark and the other thing it starts with is this little guy so what bad is you can see it's also on the left here that's a default transition so you always need one of these state flow block if you don't include that state flow has no idea what state it's supposed to start in and so if we look at my my other model here you can see I have one but I immediately put it into a little note here and test some stuff you don't necessarily have to do that you can just take your default transition and put it directly onto a state so that is just a requirement of state flow I'm not exactly sure what it means code wise but if you don't have it stateful will not work especially as you get to having several states gone that could be operating in parallel on a single choice the next thing you need to do is name your state it's good practice to never use spaces in state pearl for the names of the state in our case when we use RTI to build code for our controller a space and a stateful block will crash the entire build and you have to start again and what's really fun is when somebody you name every single state in a state flow with spaces and you have to go in and check every single stateful block in the entire code to make sure there's no spaces left sometimes it doesn't tell you which state has a space in it so basic thing do don't ever put spaces in your state folding so I like to start states with a capital letter even though my MATLAB stuff you know searchable lowercase its MATLAB actually isn't all that particular about starting with an uppercase or starting with the lowercase but it makes it really clear that that's a state and not an input or an output or anything like that so I could have this as like a PU on a PU off something like that and then the sorry so the next thing that you want to do with your state is you might want to do something like a perform a calculation or assign a value to something when you enter the state or during the state or when you ignore the state so there's after the first line if you just hit enter the first line is always just going to be the name nothing else but after that is going to be essentially MATLAB code that runs every time you enter the state it's going to check these things so you can do entry and do out one in the pool one it's really important if you do an assignment like that that you put a semicolon on it because since it executes that like MATLAB code if you don't put that sending Col and it's going to start filling up your work your display box on MATLAB yeah ah so so what the entry does there's so there's three different commands that you can put there your entry there's exit and there's during so in entry statement and you can do as many statements as you want after you put in entry so I could say in out one equals in one I can say another thing of come on I could do out to improve into stuff like that entry says that every time you enter the state you're going to execute these commands and so if I've got two states and every time I enter this one I want to perform some action or set some value I would use the entry command if yeah yep you can have you can have as many entry during an exit statement as you want in a state flow State so you have out one equals in one out two in two and then maybe during you so you would put during yeah I think you can I don't think you have to go entry during exit I think you can do them in whatever order you want but you want to make sure all your entries are together in one grouping and all your aggregates and during they're together so then during you could do some other command but I usually don't use turns there's a couple of things if you maybe want to have a timer output or something you could use a during to basically constantly increase any time you're in this state increase something and then once you leave that state you stop increasing it right so if you wanted something like that you could use the during command on exit maybe I want to trigger something every time I ate so to speak so if I wanted to get a kind of email if I want to every time I exit state one I want to print something to the MATLAB workspace that says state one exhibit is something you could do that or if it you want it to be a trigger for other things to happen either in state flow or in Simulink you could have output there so that when you either the state and go to any other state for any reason and you said that so then the next important so that does that make sense II is just the basics of how you get a state how you can do things during an entry condition during a good condition all right so the big thing about a state machine though is that you have multiple states so I want to add a second state here and just going to do two and you can see a newer versions of Simulink I get this little error here and then err it's technically a warning but that warning is going to tell me that it's an unreachable state so I have no way to get to this regardless of what code it's supposed to be executed so what I need to do if you bring your cursor over to the edge of the state you can see I get the little cross if I click and drag now I've got a condition so if you put an empty condition like that what's going to happen is the default transition is going to put us into state 1 and then the order in which stateful does stuff is when you enter a state it immediately performs the entry command but then the before it executes the during command it immediately starts checking its output condition or exit condition and since I don't have an exit condition here if I had a during condition in state 1 I wouldn't it wouldn't do anything because I do my entry condition or my entry command and then immediately state flow would see that we you know quote unquote meet the conditions to ignore the state but really there are no conditions page of the state so that's a problem so if you double-click on here you have put a square bracket some versions of Simulink add the square bracket for you but and never remember which versions do it and when it does when it doesn't always add the square brackets yourself it's safest way to avoid issues so I want to put conditions so I use a double equal because maybe you guys have done this in MATLAB but the double equal is an equality check it's not an assignment like a single equals is but you can use less than you can use greater than you can use and and or generally if you're doing an and statement you should use a double ampersand and when you do an or it's with slash but you hit shift it's just the vertical bars I have no idea what that character is called here do you reach it up i p-- alright cool you put a double pipe learn something new every day so that's an or statement and I think if you're doing an an if you have multiple ands and ORS you should always put parentheses around the statements that are supposed to be going together if you don't speak low we'll take everything into its own own hands and figure out which one it wants to check first and stuff like that in general for an ends what it'll do is it'll check the first one and if the first one is false it won't even bother checking the other side of the ampersand if you have an or and the first one is true it went bother checking the second side of the pipes because it knows that this statement is true because I already have one truth if you want to do a not so not equal to or not and stuff like that what I what I think is better to do is I would take well let me go ahead and like that we the ampersand yeah you can see it's already showing an error because I didn't say anything if I want to make sure that this is not I would put parentheses around it and put an exclamation mark so that's the in an exclamation mark or is it despoil is it squiggle hard information I actually MATLAB is the squizzle but I think it's the explanation masking stateful so much so what I'm saying is I prefer to do it this way because sometimes stateflow does like it so not greater than or stuff like that or you want to do the opposite of an end or something if you want to make an and I don't think exclamation marks and force and works this wiggle works way altering I'm not a MATLAB scientist it's as long as it is a valid logical statement stateflow will be fine with it I do suggest for ampersands and pipes for and and/or statements always use with double although I think it will throw it it won't throw an error if you just use one but always use a double and always Greek and an or statement with parentheses to keep everything where it should be in your grouping because it'll run two fees before it evaluates either stress that was a bigger change of attack can be already thirty minutes and so you can see Oh object contains of some text maybe you're right maybe you're right maybe you're right ah you're right Ricky so for those of you watching at home in Addis wiggle it sloppy exclamation think so too bad I can't go back and delete that anyways so I'm going to get rid of that though because I don't want it to just I don't want there to not there Oh prep I mean bleep it out yourself come on that is it is out all right so I want to have I'm going to have that condition so the other things that you'll see here is a junction block what the junction does it's not a jido man it's not an actual state so you can't do commands in it but you can use it for is say I want to have one condition here but then I want to check to another condition and if it's true or false I want to do separate things or go to separate states I can check the one condition here and rather than have like if a and B has one transition and then if a and not B the another another transition here to lead off to two states it's not as big of a deal like this but in some of our models we have ten conditions for state transitions on stuff so at that point it's really nice to be able to group the common ones that have to be true for all of those transitions into a single node but then from here you could have one condition that brings you here and maybe another condition that brings you to state three so this condition needs to be true for either of them then once we reach this node we want to check these two and you'll notice here it's giving me a warning with transition shadowing so a transition shadowing is neither of these have a condition and so the problem is is it will never do transition to because transition one is true all the time the way to avoid this is if you're going to have a blank transition state you're not going to set a condition on it at all you want that to be the very last in terms of number and so if I so if this has the conditions I forgot my belief with and so you can see it added the brackets for me a lot of the times when you use timers and stuff and staple it won't add it for you it can be a real pain just make sure you put them in so you can see now if this is going to be a problem because I have a condition here that I want to check but my default transition is shadowing it so I don't want that I want this to be transition number two and so when I do that that should say go so now it's no longer shadowing it because condition two gets checked on transition one here prior to it defaulting into transition 2 so this is acceptable but I would suggest that you never leave something up to a default transition unless it's not a like safety critical thing so default transitions can be good if say you want to check all these things but then at the end it's like well none of those are true so enter that's like limp home mode or something yeah so it is if you get to view switch cases before yeah not all of them they have I love them a lot if you don't already use them in MATLAB they make running drive cycles a lot easier I can show you how to do that but so that's why you always want to leave it for last because you want to check everything else that you might want to see before you do your default transition and so you and we just add something here so I'm going to leave that as a default transition and then here I'm going to do condition two equal equals 1 and 0 0 1 0 and condition I gather to watch my capital condition 1 there we go so the reason I want to double-check the condition 1 is equal to 0 before I do the transition is I don't want to hop over to right in here and then go in to speak to I want to make sure that before I do this state transition that I'm not immediately going to skip state 1 that's something that we have struggled with on a CVT at least is if you transfer into a state but haven't actually checked the Avett conditions for that state you're just going to skip right through it we had a problem before with waking up a controller where we when we do the shutdown condition we didn't make sure that all the exits all of the exit conditions for off position were in the off state and so the last thing that we should have checked is whether or not we have actually shut it down yet and what we ended up having it was this constant loop and it'll throw a warning for that but it will still run because there's nothing invalid about just constantly running through a loop of states that's something that you can actually visualize with state flow which I'll show you in one second so I covered entries one just add an entry to this so we can see oh but here's a problem I didn't add an entry consider a conditional today want to make sure of it condition one Bureau so now if I I'm going to start in state 1 if I set condition 1 to 1 or transition to state 2 and once I turn off condition 1 then it's going to check if condition 2 is equal to 1 if it is it will go to state 3 if Mac is going to go over to state 1 again changing condition once I ended up in Texas and is we have in general to get the rest of the state go organizing everything else Christmas Day first before it leaves it will do entry but it won't do during but if it immediately exists it will also do with exit statement and so if you have an entry and an exit statement and you enter in an immediately exit you'll see for one data point that you were in that you were in state 2 even though you immediately acquitted it and then you'll see the egg of it condition happened so good thing to always check is that you're not just going to fly through a state so an eggs a condition it's good to make sure that it's similar things to the entry condition if you have like a series of states that string along like state 1 state 2 3 4 and then transition and then state for is the only one that can loop back to state 1 then the loops back should check the entry condition for state 1 because otherwise you're going to go from state 2 sorry otherwise you're going to go from 4 to 2 without staying in state 1 this is all pretty general stuff the the stateful assignment that you guys are going to have this like a mini assignment is just taking some some data on speed ranges and nothing else it's just speed ranges and assigning a gear to that so from 0 to 10 miles an hour we want to have first gear you know 10 to 20 seconds here not how real transmission works but it's going to be six states you're going to have to check these entry conditions and I know the messy way to do it in state flow and I know the clean way to do it in state flow so the interesting to see when you guys end up with both of those functionally identical neither of them are going to cause any problems but one of them is a lot nicer to look at so and run more efficiently but we don't we're not at the point where we need to optimizer code yet this is all still stuff our computers can solve in fractions of a second so so just to make this easier to visualize I'm going to do an entry and one equals out to or into it what you see be recording all right so we're 23 minutes until I went ahead and pause while I did this so the last thing I need to do is actually set up the inputs and outputs I've named them out one and in one condition1 and condition2 and stuff but I haven't actually made it if we go back up chart one still doesn't have any i/o so if I go in I go up here to model Explorer and it will default if you're in a state for a little default to that chart you can see I have my three states and it has the actual code that goes in it and stuff like that but you just want to go to the chart and you want to go up to add data which is up here so you're going to add that and it's going to default to local but what we actually want to set it at we want to have in our case we have two inputs which are in one then we into we've got two outputs out one and out two and then two additional inputs which are our conditions so we need an input named in one and now if we come in here you can see it's already added in one for it so I need to do that for four and then two so these ones I'm going to change to an output and do out one and I do make another output due out so this is condition one and this is condition that your problem so now when I go out I've got so you can see here the scope some of the other options are constant that's something that's not going to change at all obviously parameter is something that you can load from the MATLAB workspace if you want to just look directly at the MATLAB workspace for a parameter but local is going to be something that isn't going to look at MATLAB workspace it's not going to be an input it's not going be an output it's something that stays just inside the state form do we lose this room at seven eight Andre all right at 26 all right so with that I can go back here and as I come out I got to expand this so you can actually see it if you got in one in two condition one condition to go for I'm going to get grabbed some constants just to make this easy and so I get a constant equal one constant equal to two and zero zero and then I want to display the output I all right so now we're just going to set this to run indefinitely that'll make it super simple and by super simple I mean slightly complicated but you know so it's running infinite time so if we go into a state flow it's going to highlight state one so that's what we're in and now if I I'm going to pause it really quick I can edit these constants while we're doing it so I'm going to change condition one to one right and now because I have it paused that hasn't actually done the transition yet so I start stepping through you can see it has jumped through condition one in the state two and as I keep stepping it through its stuck at its in state 2 because it's exit condition is for condition 1 2 equals 0 and now you can see first thing in state 2 is that you flip them and you can see here that they are foot in one is going to find out - and oh there's uh hmm I'll stop saying crap on this recording alright so you can see that and then if I set condition back condition 2 1 you can see it didn't actually jump yet because I haven't met to the exact condition for state 2 and that's where nodes really help is condition - has changed but I don't want to transition into state 3 until connives in total condition 1 has changed so I set this to 0 and now you can see there's both 0 so now we're in state 3 and then it will only transition once both of these are 0 so I took this to zero we're back to state 1 so that's introduction to state flow kind of the assignment it's way simpler than it sounds because it's really just one input and then two outputs it's going to be the gearing number and it's going to be the gear ratio which are defined by the gear number so it's not really going to be that complicated in terms of your vio for the state flow but setting up the logic properly to function correctly is where it's going to where the actual work is going to go in alright so that's it for this state troll introduction which was twice as long as I thought it was going to be there you go
Info
Channel: HEVT Controls
Views: 37,532
Rating: 4.7761192 out of 5
Keywords:
Id: E7aSTxhezPo
Channel Id: undefined
Length: 29min 9sec (1749 seconds)
Published: Thu Mar 16 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.