[MUSIC] In this video, I will show you how to simulate the vehicle model that is given by a continuous time process. And we're going to do that in MATLAB using the simulation toolbox. The first thing we're going to do is to start Simulink, and by typing in simulink, we do that. And here's a browser. The next thing we're going to do is to create a new folder. By going in here and creating a new folder. We're going to call that ContinuousTimeGroundVehicle. We're going to put all the files here. The first file we're going to need is a file that initializes all the variables and everything that is needed to run the simulation. So we're going to call that initialization. All right. We're going to put that aside, we are going to also create a script plot thing. So we want to call it postprocessing. Okay let's put that one aside as well for now. Now let's model that vehicle. If you remember, that vehicle is from a previous video is given by a continuous time differential equation with three states. Let me pull here the snapshot that I have. Okay, so this is a snapshot of the video that I'm referring to. And the video has this equation x1 not equal to x1 sin of x3. X1 is the direction of motion in this axis here, the horizontal. X2 is the vertical. And the equations are given for those two variables. Derivative given by these nonlinear differential equations right here. Where v1 is the forward velocity input, x3 is the angle respect to the vertical, and that angle has a derivative that is controlled via another input, v2. So we need to do is to code this in this simulation toolbox. So for that we are going to create a new simulating model. That simulating model have, essentially, the dynamics that I just showed you of the continuous time vehicle model. It will have two inputs, one is forward velocity input, the other one is the angle of velocity input. And we're going to plot all those variables as a function of time, or as a function of themselves. We're going to see what is more efficient. So in order to do that we are going to go to the Simulink browser. And here in the library we are going to go to Hybrid Equation Toolbox and Cyber Physical Systems, and we are going to grab a continuous time plan. That continuous time plan is going to be modeling the vehicle for us. In addition to this, we're going to apply an input. This input will be second order vector for which we will need to use a signal router. Which in particular we will use a Max for that. We're also going to need to place signals to that. So let's go to sources, and one of the signals I would like to apply is a constant signal which corresponds to the forward velocity that the vehicle will be executing. And the other one will be for the angle, will be a time variance single. And I'm going to use the signal builder here for that. So this is becoming our interconnection of these generators that are maxed to create the input. Let's say that our constant is equal to 1 there, and this signal generator is given by this particular scenario. We want to inspect this but the signal is equal to 0 for 4 seconds, then becomes 1 for 2 seconds, and then remains at zero for another 4 seconds. This is the default signal. Once we model the vehicle, which we're going to in a minute, we would like to plot these signals. So this is the state which will correspond to a three dimensional vector, and we're going to send it to the workspace. So we're going to pick a sync to workspace. And that sync will essentially generate a three dimensional vector. Now since high read time is generated by the simulation, we are going to just send to the workspace an array corresponding to the signals x1, x2, x3. So there's three variables. And we're going to call that x in the work space. We can copy and paste these two more times so that we get hybrid time also in the workspace. So the first one will be called t and it will also be an array. And we are going to pass that there. And the second one it's going to be j and it's also going to be an array. Now since we are not using the previous value of the state before a jump, what we're going to do is to just [INAUDIBLE] warnings to put a terminator there. And this signal terminator is somewhere here. Should be in syncs, correct, terminator, okay. So there it goes. Okay, now the most important part. Let's save this. The most important part is to find the dynamics of the vehicle which is essentially enter the differential equation. For that, we need to go into the plant model, into that block. And it will create this new internal view. The first one is the flow map, which defines the right hand side of the differential equation. For us, we will essentially need to incorporate this information into that right-hand side. So it will depend on the inputs and the state. So one thing that is convenient to do is to actually code it as the mathematical model looks like. So this is how I typically suggest to do it. We have three states, so x1, x2, and x3 will need to be defined. This information here is just a sample that comes with the With a template, I guess, maybe somewhat useful. So the first entry of x will be x1. That's how we define it, as you see. The second component will be x2, and the third component will be x3. Now, the input will also have to have two components because it should be a second order of the vector. And the first one, I'm going to call it v1, kind of matching what is here. And this will be u1. And the second one will be v2, the handler speed. Okay, with doing that, what I can do now is to define x1dot, which is what you see right here, equal to v1 times the sine of x3. And it's the impossible because v1 and x3 will define right above. And then x2dot. We can define it also as v1 times cosine of x3. And then the derivative of the angle which is x3dot is going to be defined equal to v2. Okay, so I like to write comments typically, so this will be a state vector. And input vector, and then we define the differential equations. And then we collect differential equations into a single vector so that we output essentially the right hand side of the differential equation. And this will be no more than x1dot, x2dot, x3dot, okay? So the point is that this block that we have right here, Will receive u which will be a second order vector because of its construction. You will pass it at every step of integration to this algorithm, and this algorithm will actually compute the solution to differential equation as time evolves. So this is a definition of the flow map. Because of the nature of the system we want to model is purely continuous, then the flow set will always need to return 1. So I'm going to remove this sample code, and I will always have this output of this function always a v1. I force it to 1 and with that I force flows. These are parameters here that are for one to use if needed, we don't need those parameters. I'm going to remove them. Again, since the system is a purely contained system, we will now not report jumps any time. And the sample already does that for us. And the jump map is actually going to be irrelevant, because it's never going to be used, since there are no jumps. And it's probably the idea to just keep it at zeros. So with that, we have defined the plant for the system for the plant or the model of the continuous time vehicle. Now we need to initialize some variables. So one of the variables that we need to initialize is the initial condition. So since this is going to be run before the simulations, before we push simulate in Simulink. What I typically do is to clear everything and clear the workspace. Now I'm going to code initial conditions. And the initial conditions that I would like to pick initially would be 1 for the x component 1 for the y component, and let's say 0 angle for the vehicle. Okay, so those are the three values of the state. Then I will need to define a simulation horizon so that we know how many minutes of flows or how many jumps we should simulate. And let's say that we set it up to ten seconds with at most one jump. One variable that we need to pass to the tool is the flag rule which will define essentially how we're going to deal with the possibility of having both able to jump and both able to flow. And that will be a role that one needs to read about in the manual. But if we set rule equal to 1, we're going to give priority for jumps. Meaning that if the state and the input are both in the flow and jump set, then jumps will be executed. And then the last thing which is part of the solution of a differential equation is the tolerances for the solver, and we're going to set those pretty tight to 10 to the minus 6 for the relative tolerance. Let's say the maximum step possible that the solver can do to 10 to the minus 3, okay? Okay, so [COUGH] with this being said, we can go to the workspace and run initialization. And we can check whether this has run correctly if we check which variables are in the workspace, and we see all the variables we have defined. We can inspect the initial condition, or their rule, and they look like what we actually typed. So what we can do now is to go ahead and simulate this system and then decide what we want to plot. So again, this second order vector will come into the input of the plant and the plant from the initial condition that we're given is going to integrate forward in time. The state of a system, and will describe a path of the vehicle. So we push simulate. There will be a state of compilation and then it will run and it's going to create the vectors x, t and j. So what we can do now is to go into workspace and now check what's there. And as you can see, we have these T and J and we have this x new variables that are being generated by our simulation. Simulating by default outputs the intrinsic time. We are not going to use that. But one thing that we can check is to see the size of time, and then check what the last value of time is. So, you didn't have to simulate it at ten seconds, but that sounds reasonable. And then, maybe we can look at how many jumps have happened, and no jumps have happened, that makes total sense. So one thing that we can do now is to plot these quantities. Let's say this state as a function of time. Notice that this state is three dimensional, and has a number entries corresponding to the simulation of steps. So that 10,007 corresponds to ordinary time. So let's go to the post processing, knowing the data we have on the workspace now. What we can is do is to create a new figure, we can clear it, now we can plot as a function, let's say we do a face plot. So we plot all results for position versus all results for the other position component. We hold that on, and then we plot the initial, Point with a star so that we know where things started. And then we use a label for the horizontal, so it will be x1. And then for ylabel, we will use x2. Okay, so I can type that processing, Okay, there is an expected MATLAB expression. Let me see now, I forgot a comma here, okay. Line five, all right, now let's try it again. Okay, so we can play a little bit with this figure. The first thing I would like to do is to make sure that we see all the paths. So it seems like the initial point is at 1,1 and then there's a curve here. But it's hard to see what's going on here. So let's change the axis from 0 to 7 for x1, and from 0 to 7 for x2. And that gives us that, let's make the axis to be equal in scale, and now we see what's going on. So essentially what had happened is that during the four seconds, the vehicle has traveled at the speed 1, from point 1 to point 5, so that's 4 units. And in zero angle, right, so that's corresponding to the vertical. And then it has turned for an amount of two seconds, that's the signal that we provided. And then has moved for another four seconds at essentially the direction given by whatever ended after two seconds. So that's this measure of a continuous time system, which corresponds to the continuous time vehicle model. One can change things around. I, for instance, can go to the signal builder, and now change this signal. Let's say we can always keep it rotating after some time. In which case, this point that is right there, Will be almost close to that, and we can adjust it precisely, actually. We can click the 6,1. That's good, and this one is almost 1 here. Okay, so if we now were to simulate with this, we can keep the other plot, and we can see the resulting signal. So essentially what happens is, we kept rotating, and this is the amount of time allowed to move with constant angular velocity. [MUSIC]