Modeling Continuous and Discrete Systems in One Simulink Model | Getting Started with Simulink, Part 8
From the series: Getting Started with Simulink
Switch between continuous and discrete domains. This example shows how to update a Simulink® PID controller block in order to move between the two domains.
Published: 23 Oct 2017
OK. So we've got the models in here. And one thing that I've noticed, we've just been working in the continuous domain so far. I think it would be helpful to show folks that you could easily switch over to the discrete domain as well.
Right.
Because if you're eventually going to be going to hardware, the controller code is-- when you generate code from the controller, you want that to be discrete because that's the way most hardware behaves. So can we model that, as well, pretty easily in here?
Yeah. It's pretty straightforward. So before modeling, let's turn the sample time colors on so we know what domain we are in. Right now, this entire model is in continuous domain as you can see here with the color coding. And if I want a discrete controller, I can simply go to this PID block and convert this to a discrete time controller. And that gives me additional options here if I can choose some other settings, especially the sample time.
Yeah. So let's say 0.1.
Yeah, sure. 0.1 sample time for the discrete controller. And then because we changed that sample time, we might want the input, also, to follow the same sample time. So let's do that. And let's simulate the model. You will notice that the--
Different colors, yeah.
--different colors. And we don't see the output signal in this view at all, which means that, most probably, when we made this change, there was no convergence or there was no steady state achieved at all.
Well, I think, actually, because if we look at the scope itself--
Yeah.
--yeah, I think that dashboard is actually not showing the results anymore because when you did the conversion--
Right.
--of the plant to a reference model, it kind of severed the connection with that dashboard scope.
Right.
All right? So it's not showing up anymore. But in the scope itself, that connection's still there.
Yeah. And you can see these values are not right at all.
Yeah.
And the signal is not achieving its steady state at all.
So we need to retune it.
Exactly. So let's go. I was going to do that exactly. So go back here. Do the same thing. Hit the Tune button. So what happened in this case, I know what happened. Because we changed the sample times, sometimes you would have to change the sample time of the port as well.
Ah, OK.
Yeah. Because most times, it's inheriting. And ideally, it should be inheriting from the source block. Let's see if those settings were correct here. But it should work now. Let's go back to the PID controller and see if we are able to tune it.
OK.
So that worked.
OK. So for a reference model, best practice is to specify the exact sample times of your inports and outports, right?
Right.
Yeah, OK.
So we have the controller up here again. So I think this response looks pretty good.
That looks good. Yeah.
So I just go back and update the block. And the block would get updated with new parameters.
OK.
And if we go back, save the model. So it's asking if I want to save the plant model as well. So I can save that, and it would save all my models. Run the simulation. And just double click.
Yeah. Let's set up that connection again.
Yeah. And set up this plant connection. Yeah. So that--
You have to uncheck the other one. That's not valid anymore.
Yeah, because it becomes a part of that other model. So that's why you have to do that. And now we can see the discrete response.
Great.
Yeah. So basically, your model is now ready. Your controller is ready to be deployed to a hardware. You have Simulink where you have continuous states and you have discrete states, can essentially model everything in the same model.