[MUSIC] In this video, we are going to simulate a ground vehicle model that is given by this discrete-time model difference equation. We've done that in a previous video, where as you can see right here, we have the state of the vehicle being x1, x2, and x3. x1 and x2 correspond to the position of the vehicle on the plane, and x3 corresponds to the angle. And the input to the vehicle is given by the force v1, for velocity, and the angular speed v2 that allows to change the orientation of the vehicle. So the way we're going to do this, is essentially by using Simulink. And the blocks that already come with Simulink, we are not going to use our toolbox for this. Let's just start a new Simulink model. And in that Simulink model, we would like to actually implement this difference equation that we saw in a previous video, right here. The simplest way to do that is to actually use a, Delay block, so let me find this. It's in Discrete, Single Delay, a Unit Delay block. And to that, feed the expression that corresponds to the function G. To do that, we are going to use the following block. It's a user defined function, it's a MATLAB Function. So the idea is that this block, this MATLAB Function is going to compute this expression. In order to do that we need to provide the inputs, which are v1 and v2, and also the current value of the state, which will be the output to this Unit Delay block here. So for now we're going to use a routing block, so it's essentially this one right here, the Mux. And we will need to have, These will be for the two inputs, we need to add an extra input right here. So one of them will be v1, the top one. The next one will be v2, and this one will be the state of the system at the current timestamp. For the forward velocity, we can apply a constant signal, just keep it constant. You can play with different choices. Let's say that it's just unitary. For v2, which is angular velocity input, we can pick a signal that is built with this block, Which generates a signal of time. This thing over here corresponds to a pulse that is set 0 for four seconds, then it becomes 1 for two seconds, and then goes back to 0 for another four seconds. And that seems to be a reasonable input. Another thing we would like to do, is to output the result of the simulation to the workspace so we can plot it. So we are going to use this block, To Workspace, for that purpose. And so now we only inherit time with the information I will just put here to be an array. And I will call it x, which will be a three-dimensional vector for each simulation time. Now inside this MATLAB Function, here's where we're going to model this expression. So if we double-click on the MATLAB Function, we can actually enter that information and implement that particular expression for G. So the first thing that we will do is to parse the input to this function, and build v1, build v2, and then the state, Which is three-dimensional. So v1 will be the first entry of u, because of the way we put the mux. v2 will be the second, and so on. So here we parse u, and define v1, v2, and x. We can also pass parameters to this function, but to make it simpler we are going to define those here. And these parameters are here d, which we could set to be 0.01. And c, which we could set to be 0.1. The next thing will be the definition of G. So G is a function of x and v. And we can define the individual components of G, which is a vector, as x1plus, x2plus, and x3plus. And the first one will be no more than what we have right here. So it will be x1 + d * sin(x3), all that times the input one. The second one will be very similar, the only difference being cosine being used instead of sine. And the third one will be the current value of the angle plus a constant, times the velocity. Great, and then the output of these functions is labeled here by default as y. We're going to keep that, and it will be the stack of these different choices of the function G. [COUGH] If it is correct, we will have the function G defined. Let's put this into a model, into folder. First a Simulink model, That we're going to call just the same name. All right, there's no worry about that. And [COUGH] what we can do now is simulate this system, and, Before that, we need to actually define an initial condition of the vehicle. In other words, the initial value of x1, x2, and x3. They're typically defined in an initialization file. So let's do a new script, and let's call it initialization, Inside there. I think you guys will like that I already have that function open somewhere else. Okay, anyhow, let's try again. All right, and then let's call this initialization. And I'm going to just define initial condition. So I'm going to call that x0, and I'm going to say that the vehicle starts at x1 equal to 1, x2 equal to 1, and 0 angle. [COUGH] And that's probably good enough for now, that's all we need. Okay, so with this being the system, we can now do initialization here. First we need to get into the folder. We can check which variables are available, the initial conditions should be there. And maybe we should just clear everything out so that we don't run into issues of variables already defined. So let's go again and check. And now that's better, only the initial condition is there. And now what I would like to do is to run this, however it didn't let me save because I had already opened a similar file. But now I think I'm all good to go, so I'm going to save it again Okay, so I'm going to call it, To, Here it is. Those, I had a different copy up. All right, so let me save it again. All right, so now we are good to go. So that's the model we created. And we have the initial conditions set, so we can just simulate. All right, so we have an error, line 6 column 8 of the MATLAB Function. Line 6, column 8. All right, so this is good experience to figure out where the error is. The input is five-dimensional, that seems correct. Correct. So the initial condition block should have, in the initial condition parameter, it has to be told that it's a three-dimensional delay. Otherwise you will not inherit the right dimensions. So with that being said, and changed, we should be good to go. Okay, so we have a sampling time now. I see, the sampling time now is inherit, so the sampling time should be also chosen. So we work through all these errors, we get some confidence on what we want to simulate. And now this is a, let's say 0.1. Now it did do the simulation. So in other words, every 0.1 seconds, what's going to happen is that this signal, and this signal, and the current value of the state which initially would be 1,1,0 is going to be measured. This function G that we coded is going to be evaluated, and the result of that is going to be put here, which will be used to update the output at the next step. And this continues happening until 10 seconds of real time, which will be 100 steps because we put a step size of 0.1 have elapsed. So we can now go and see what we have in the workspace. We have the output x, which is the one we created, and also the time of the simulation. And we can actually go ahead and create a post processing file that will pretty much plot our functions. And, This date, so this will be as simple as the following plot commands. We're going to plot, let's say the first component of x, which is x1, versus the second component of x, which is x2. And since we have a discrete system, let's say that we highlight it with blue and with stars. We'll save that, and then we're going to mark the initial value of our simulation. And let's say we do that with a red star. And then the horizontal will be x1, and the vertical will be x2. Okay, and then we can call this postprocessing. So with this, we can now call that function postprocessing. And as you see here, the vehicle is moving along this direction for this amount of time. What we expected to see, let's see, is if this is the signal that appears into the angular velocity, and the signal is equal to 0, we were expecting to have a straight line. So this is not looking like what we expect. Let's check the model that we coded, and we see we have an error here. This x1 should read x2, which I missed. The input should really be 1. So what we expect is that during the time that the input v2 is equal to 0, that because initial x3 is equal to 0, this sine is equal to 0. So x1 remains constant, and x2 keeps growing. And then eventually when v2 becomes 1, we have a turn of the vehicle. And then once v2 becomes 0 again, it goes in another straight line. So let's run this again now that we fixed this function G. Undo the postprocessing. So that's what we expect, that the vehicle starts at 1,1, it moves forward with constant x1, and then it turns. And after v2 becomes 0 again, it will move straight until the number of steps is finished. We can actually make this plot to be easier to see by changing the axis. So let's say we go from 0 to 1.6, and from 0 to 1.6 on the top, and then we make the axes to be equal. And that's the path of the vehicle resulting from this discrete-time model. [MUSIC]