MATLAB Answers

(Matlab) S-function - delayed output

조회 수: 3(최근 30일)
Stijn Helsen
Stijn Helsen 2021년 8월 18일
답변: 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:
  1. In the Update-function, the output is toggled in a discrete state, and this state is used in the Output function.
  2. 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:
  1. 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.
  2. 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개)

Jonas
Jonas 2021년 8월 18일
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.
  댓글 수: 1
Jonas
Jonas 2021년 8월 18일
You mention you use variable sample time, my text above is directed to discrete sample time.
Are you sure the sample time is correctly varied to match the duty ratio?

댓글을 달려면 로그인하십시오.


Stijn Helsen
Stijn Helsen 2021년 8월 18일
Yes, I'm quite sure. The Upate and Output functions of the s-functions are called twice per period (once to go high, once to go low).
(After finishing some "more direct related 'work-work'", I'll try to get a model, code and results.)

Stijn Helsen
Stijn Helsen 2021년 8월 19일
I've made some demo code.
There are different "options" in the s-function.
In the Outputs function, I have the the "bEarlyOut" option, where I don't output the state, but "the next (or toggled) state" (which works in this case since every call results in toggling the output).
In the Update function (line 132) I can force the output port already in Update or not (just by commenting it out). In the figures below the result is shown (all bEaryOut false - so the "normal way").
As can be seen on bottom graphs of the figures, the s-function is only updated in the "toggle-cases".
In figure 3, showing the result with "output port forcing", the result is OK in this case (because of the nice fixed sampling), except one step delayed. Without the "output port forcing" (bottom figure) the output is wrong, resulting in a wrong duty cycle.
This comes because the Output function is called first in each timestep. I can work with it (using that "bEarlyOut" option), but I don't find it logical that the output can't be changed when doing the update.
Below you can also see a "log" of the s-function calls (only Update & Output):
0.00000 Output (0 0 0)
0.00000 Update (1 0 1)
0.00040 Output (1 0 1)
0.00040 Update (0 1 0)
0.00100 Output (0 1 0)
0.00100 Update (1 0 1)
0.00140 Output (1 0 1)
0.00140 Update (0 1 0)
0.00200 Output (0 1 0)
0.00200 Update (1 0 1)
0.00240 Output (1 0 1)
0.00240 Update (0 1 0)
0.00300 Output (0 1 0)
0.00300 Update (1 0 1)
0.00340 Output (1 0 1)
0.00340 Update (0 1 0)
0.00400 Output (0 1 0)
0.00400 Update (1 0 1)
0.00440 Output (1 0 1)
0.00440 Update (0 1 0)
0.00500 Output (0 1 0)
0.00500 Update (1 0 1)
0.00540 Output (1 0 1)
0.00540 Update (0 1 0)
0.00600 Output (0 1 0)
0.00600 Update (1 0 1)

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by