How do i acquire the value of current timestep in s fucntion (level 2 M S)?
6 次查看(过去 30 天)
显示 更早的评论
in the level 2 matab s function, i set the sample time as Inherited sample time:
block.SampleTimes = [-1 0];
then i set the time step in the simulink /configuration parameters /solver, so that each time i want to change the timestep i won't need to modify the s function (the value in the picture just for example, the type could be fixed or auto )
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1285705/image.png)
but in the update section in s function, i need the value of current timestep to calculate the state variables,
Xt+1 = f(dt, Xt, u)
so how can i acquire the exact value of current timestep?
really thanks
0 个评论
回答(1 个)
Manoj Mirge
2023-3-21
Hi Kefan,
Simulink software creates an instance of Simulink.MSFcnRunTimeBlock class for each Level-2 MATLAB S-Function block in a model. This class allows a Level-2 MATLAB S-function or other MATLAB program to obtain information from Simulink software and provide information to Simulink software about a Level-2 MATLAB S-Function block. The instance of the class is passed to MATLAB S Function as argument ‘block’.
We can access some fields of block object at the time of execution in our S-Function. Although you have set SampleTimes as inherited in setup function of your S-Function, you can get Sample Time of your S-Function configured by your model by just querying SampleTimes field of your block object in your update section. Similarly, you can get Current Simulation time of your model by querying CurrentTime field of your block object.
Below is a code snippet that will give you a clear idea:
% You can get sample time as 1 by 2 row vector by querying SampleTimes field of block object.
% Similarly, you can get Current Simulation time of your model by querying CurrentTime field of your block object.
% Your update section
function update(block)
Sample_time=block.SampleTimes;
Curr_simulation_time=block.CurrentTime;
%Your state update logic
end
Hope this helps.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!