(Matlab) S-function - delayed output
5 ビュー (過去 30 日間)
回答済み: Stijn Helsen 2021 年 8 月 19 日
I wonder if it's normal that (in my opinion) using a "pure discrete S-function" (Matlab - Level 2) with variable sample times, the outputs are calculated before the update, so that the outputs are only visible the next sample.
An example is the following:
I wanted to make my own pwm output function, with the PWM signal as an output, and period and "high time" as inputs. I have no problems making it. But it doesn't do exactly what I want to do. I'm sampling only at the toggling points (which I find logical). And trying to get what I want to do I see 2 options:
- In the Update-function, the output is toggled in a discrete state, and this state is used in the Output function.
- I move the toggling to the Output function (also meaning that I don't need a state, since the output-value can be used to toggle.
Both don't do what I want:
- In the first case, the output is only changed in the next sample.This works, except that there is a delay in using the inputs (although DirectFeedTrhough is true), and is seems unlogical that the state is different from the output.
- In the second case, it works in soms sense better, except that the output changes effectively the next sample, and when this next sample is taken depends on the rest of the model. As an example: if "Outputs" is called at t=0, and the OutputPort value is changed (I know that this could be solved by adding extra sample points, but this doesn't "feel right".) One of the consequences is that the effective frequency and duty cycle (as used by the rest of the system) depends on the sampling of that "rest of the system".
My question is:
Is it possible to have a change in a discrete state value direct influence to the output?
(I can send code, but the code that I currently have is more than what I explain here.)
回答 (3 件)
To generate a PWM signal you need to sample much faster than only the switching frequency. You need the higher sample time to be able to toggle the rising and falling edge of the PWM pulses at the duty ratio you want.
Imaging you have a switching frequency of 4kHz. When you have a sample time of 400kHz, the solver can only apply the duty cycle you want with an accuracy of 1%. Since the sample time is 100 times smaller than the switching frequency, you have 100 possibilities within one period to switch.
Typically you will want even higher sample times because you need more accuracy than 1% in a PWM period.
Find more on General Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!