so we've been talking about models and modeling and we're gonna look at some examples of creating and using models in this video specifically we're going to review the block diagrams and then we're going to look at flowcharts as a modeling tool and finite state machines as a modeling tool so recall that a model is an abstract representation of some system and it eliminates unnecessary details and there before it simplifies the representation of the system we only include the relevant attributes and we focus the model for a specific purpose the purposes of the model or of modeling are to aid our understandings so that we can make better decisions make better ability to communicate about the systems that we are designing and provide a means for collaboration additionally they allow us to ask and answer questions about the system they allow us to analyze the system examine what if scenarios and ultimately simulate the system something we'll look at more detail in an upcoming module also it allows us to improve the efficiency of the design process the quality of the design reduce the cost and so on we've seen the use of block diagrams to represent a model of a system and we know that systems are all composed of an interface of behavior and a structure and block diagrams specifically are really good at modeling and describing the structure of a system so here we can view the structure of the attitude control and maneuvering system the Gemini spacecraft remember the structure is the set of components that make up the system and how they're interconnected and we can view the structure of an automobile or the structure of a classification hierarchy or the structure of an organization but remember there are many different types of models textual models graphical models mathematical models even physical models or computer models and each has strengths and weaknesses and we're going to look at a particular set of models that are more specific to my personal domain I'm a computer scientists and software engineer so flowcharts are something that I use occasionally to model certain aspects of a system that I'm working on specifically flowcharts are graphical models they're very abstract but not so abstract as to be difficult to understand as a system of equations might be and they're focused on the behavior of a system so flowcharts are composed of a set of graphical symbols there's circles or ovals which represent the start or end of a process there's rectangles that represent some step in the process there are parallelograms that represent an input or an output and then there are diamonds that represent some point at which a decision is made in terms of the behavior of the system and then we show the flow of instructions with arrows connecting these so let's look at a simple example this is an example of a visit to a doctor's office so the patient arrives at the doctor's office and the first thing we do is see if they're in the system and if they're not in the system then they have to complete the paperwork once they've completed the paperwork and they're in the system then we can see if there's a nurse available and if there's no nurse available they go to the waiting room and they stay in the waiting room until a nurse is available at which point a nurse takes their vital signs and if there's a doctor available then they can go see the doctor if not they have to go back to the waiting room and wait until a doctors available and once they see the doctor if they were prescribed some medication then they have to stop by the pharmacy on their way out otherwise they can just leave and so a flowchart is really good at representing the behavior or process of this system of seeing a doctor let's look at a more specific example from computer science this is a flowchart of Euclid's greatest common denominator algorithm and so we start and we collect two inputs a and B and the great common denominator or divisor the greatest common divisor is the largest number that will divide evenly into a and B so let's say that a is 6 and B is 15 so is a equal to B no then is a greater than B if a is 6 and B is 15 then the answer's no and we would subtract a from B so subtracting 6 from 15 leaves us with 6 and 9 and then this takes us back up here it's a equal to B 6 is not equal to 9 is a greater than B 6 is not greater than 9 so again we subtract 6 from 9 and we get 6 & 3 is 6 equal to 3 no is a greater than B yes in this case 6 is greater than 3 so we would subtract the 3 from the 6 and we get 3 and 3 and we come back up here is a equal to B is 3 equal to 3 yes then we come down here and we output a which is 3 as our greatest common divisor and so this algorithm which can be a little complex to explain can be made more simple with the use of a flow chart as a modeling tool finite state machines are another very powerful modeling tool they are like flowcharts graphical they are very abstract like flowcharts but still approachable and they are really good at modeling the behavior of a system as long as that system can be described in these terms it has to be a discrete dynamic system with finite number of states and transitions between the states caused by events and all of that sounds very complicated but let's just break it down a piece by piece because that's what we do as engineers so if it is a dynamic system then that means something about the system changes and what it is that changes is the state of the system so consider a light and there's a switch on the wall and the light is either on or off so the state of the system changes its dynamic sometimes it's on sometimes it's off it can change from on to off it can change from on and it's discrete the states are discrete so when the light is off its off when the light is on it's on their snow in between there's no kind of on or kind of off and there's a finite number of states there's some number in this case two states for the system and the translation transition between the states transitioning from and off is caused by events and so the flicking of a switch up might cause it to transition from an off state to an on state and the flicking of a switch down might cause it to transition from an on state to an off state so there's the basics of a finite state machine and then we can pick a very simple system to model like a door and we can represent this system as a finite state machine and model it with a state diagram and a state diagram looks like this and it's composed of a few pieces there are the states which are represented by circles there are the transitions from one state to another state represented by directed arrows and then there are events which label those transitions and so to read this this finite state diagram of this system that we call door it says that if the door is open and a closed event occurs the system will transition to the door is closed State and if the door is currently in the closed State and an open event occurs the door will transition to the open State this is just a very simple example of how these models work and what we can do with them and what's interesting to me as a computer scientist about these systems is that we can take this model this graphical model and we can very easily reason about it I can put my finger here and say okay the door is currently open what happens if it closes well it becomes closed and this is true for more complex versions of systems that we can represent in the same way and then we can take these graphical representations and we can convert them to a tabular representation and this is a state transition table and it has one row for each transition and it Maps the State through a transition to a next state and so if we capture each of these transitions we've actually captured all the behavior of the system and then this representation is very easy to implement in any programming language like Python or Java it's just a loop that continually executes looking for events to occur and then a set of decisions that maps a current state to an event and produces the next state if the current state is the doors closed and the event is an open event then we open the door and set the state to door open otherwise if the state is currently door open and the event it's a closed event then we close the door and set the state to door closed and we just repeat this process and so it's it's relatively easy to implement in code a complex behavioral system as long as we can describe it in terms of a finite state machine because we can diagram it in terms of a state diagram and then we can translate that to a state transition table and that's easy to translate into code and this is a big part of the problem solving because translating what we want the computer to do into a programming language the computer can understand is a huge challenge let's look at a slightly more complex example instead of a typical door let's look at a garage door and I have here a functional block diagram that shows the major components of a garage door there's a button usually on the wall by the door in your house and it's connected to a controller and when you push that button the controller tells the motor to turn on and that causes the door to raise and the door continues to raise until it trips a limit switch which tells the controller that the switch has been tripped indicating that the door is all the way up in the controller and tells the motor to stop and if you push the button again the controller starts the motor the motor starts bringing the door down the door continues to come down until the limit switch is tripped and the controller tells the motor to stop so there's the basic functional block diagram and we can describe the behavior of this system in terms of a finite state machine and we can use a state diagram to do it so here are the four states the door could be closed right now the door boot could be opening it could be open or it could be closing and then we have transitions between that these states caused by events if the door is closed and you press the button it's going to be opening and if it's opening and the limit switch gets tripped then it's open and if it's open and you press the button then it's going to be closing and if it's closing and the limit switch is tripped then it's closed and if you've played around with one of these garage door systems you know that the system is a little more complex than what I'm describing here there's also a little eye beam that runs across the bottom inside of the door and if the door is coming down and you break that eye beam maybe the vehicle is driving in or driving out or somebody's walking in or walking out while the door is coming down the door immediately starts going back up so if that I beam is tripped that tells the controller the controller tells the motor to start to reverse and raise that door again until it's all the way raised well we can add this behavior to our finite state machine in our state diagram very easily it's just a transition from if the door is closing and the i-beam is tripped then the door goes into the opening state and stays there until the limit switch is tripped at which point the door stops in the open position waiting for us to press the button again and of course if you're like me you've probably played with this system quite a bit you know that if you press this button while the door is opening it stops where it is or while the door is closing it stops where it is and if you press the button again it reverses direction well we can add that to the model as well we can add a couple of additional states a partially open state and a partially closed state so if the door is closing but not yet closed and we press the button it goes into the partially closed state and just stops there and then if we press the button again it reverses and goes into the opening state same thing if it's in the opening state and we press the button it stops and the partially open stay waiting for us to press the button so that it can go into the closing state and so you can see it's really quite easy to describe and expand on the behavior of a system in terms of a model like this and reason about the behavior of the model and once we've designed a system that has the behavior we want this is relatively easy to convert into a state transition table and a state transition table is relatively easy to convert into source code in a programming language like Python or Java and so this renders the complex problem of writing software to control the system like a garage door or a vending machine or the automatic brake system on a vehicle much simpler much more predictable much more reliable let's look at another example that is something that you're going to encounter this semester in this course programming a robot to solve a maze we have a simple robot here that has a bump sensor on the front of it so if it bumps into a wall the sensor will detect that and a little distant sensor on the right hand side that can tell if there's a wall within a certain distance and what we want to be able to do is design a behavioral system that allows this robot to solve this maze by arriving at the star we can do this with finite state machines if we can describe the system in terms of a finite state machine a system with a finite number of discrete States transitions between those states caused by events so let's see if we can do that well we can say that the states would be the vehicle or robot is moving forward maybe another state is the robot is turning right or maybe another state just robot is turning left and we can't identify the events maybe the robot bumps into a wall maybe it detects a wall or maybe it detects no wall so we can identify these states and we can map these states to behaviors so in the forward State the vehicle or robot is going forward the turn right state it turns right and so on and then we can start to put together a finite state diagram that represents this so here's the state's forward turn left turn right we can map in the transitions if it's going forward it might stop and turn left or if it's going forward it might stop and turn right after it makes a turn it goes back to going forward and then we can decide which events causes it to transition from one state to another if it's going forward and it bumps into a wall it should turn left if it's going forward and it detects no wall directly to its right it should turn right and after any turn it should just start going forward again so let's see if this behavior that we're modeling with this state diagram will allow this robot to solve this maze so we start in the forward State and the robot goes forward and it continues to go forward until some event occurs in this case the bump event occurs and so it transitions into the turn left state and the robot turns left and after it turns the turn is finished and it goes back into the forward State and starts moving forward again until an event occurs in this case another bump event so it goes into the turn left state and turns left and that sends it back to the forward State and in this case it detects no wall on that sensor so it transitions into the turn right state and then goes forward it again detects no wall so it turns right and when the turn is finished it goes forward it bumps into a wall so it turns left and then starts going forward again and continues to go forward and it will continue to follow this and I'm pretty sure that if you continue this on your own you'll realize that the robot does make it to the star and a question for you if you want to think about this and you can pause the video to do so is will it also make it to this square will it make it back out of the maze and you can use this model to ask and answer those questions and that's an example of analysis thank you for watching