Sampling for C-Script block causes non vertikal step edges in switching signals

Hello together,

I’ve implemented a Selective-Harmonic-Elimination PWM-Pulsgeneration within a C-Script Block in PLECS.

A piece of the source code is listed below:

304

The switching times (angles) are precalculated and compared with a running variable which is synchronized with the electrical angle (theta). Is theta greater or equal than the switching angle, the Transistor T3 (in this example) needs to be turned off (0).

With continuous sampling (Sample time: 0), I get this switching signal for T3: 304

The red line is what it should look like… The sampling points are at both edges of the green line and is obviously linearly interpolated between both sampling points.

If I change the sampling from continuous to discrete times, the problem gets smaller (and the compute time gets bigger) but is not really satisfactory. This just feels like a problem with the sampling settings.

If you need more information regarding this, please let me know.

Your help would be greatly appreciated.

Thomas

It’s not easy to comment on your problem with just a few lines of C code and a screenshot that is barely legible.

Anyway, here’s the golden rule when designing a modulator: Don’t use C-Scripts.

Really. I mean it.

Use the blocks provided by PLECS instead, e.g. Sum, Offset, Relational Operator, Logical Operator. The reason is that these blocks provide extra functionality that is not immediately obvious. The Relational Operator, for example, silently adds a zero crossing detection such that the solver is forced to place a simulation step at exactly the time where the condition changes. If you write a C-Script, on the other hand, you have to take care of this functionality yourself.

If you really have to use a C-Script because there is no way that the desired functionality can be modelled with the blocks given in PLECS, make sure you’ve understood how PLECS works (see the respective chapter in the PLECS manual) and how to write a proper C-Script. If you think you did all that, feel free to attach your complete model here or send it to info@plexim.com and we’ll take a look.

Hello Mr. Schwartz,

thanks for replying on my problem.

I kinda understand what you are trying to tell me. But in my case, a C-Script for modelling the SHE-modulator would be kinda advantageous. The goal is to implement a SHE-modulator which can operate with a different number of switching angles (e.g. 3, 5, 7, 9, 11, 13, 15, 17, … 33) as a function of the fundamental frequency.

As I understood right, there are different ideas to get this working with a C-Script to avoid sampling-related issues. Either I can add Zero Crossing Signals to tell the Script when to execute or using macros like “IsMajorStep”. I’ve already tried some ideas, but I don’t really know how to get this working with it.

The reason why we decide to use a C-Script for this is, having one Script for all possible switching angle number cases. I’ve wanted to give the switching angles as a variable (dependent on the fundamental) vectorized input into the Script, which creates the switching signals for all cases the same way.

Without using the Script it’s also kinda elaborate or even in German a “barrel without a bottom”. In my mind there is only one way to implement it using vectors/arrays for the switching angles and compare the vectorized signal (multiplexed signal) with the reference signal. And at the end, demultiplex it and create the switching signal by adding all enclosed parts.

Anyway, I upload my .m-file for calculating the switching angles in the case of 3 switching angles and the simulink/plecs-file.

I hope you can understand what I want to achieve, it’s kinda hard to describe. And I hope we can find a solution for it. I would be also up for talking on the telephone.

PS: The sampling problem is pretty random. For the first few tries it worked like we wanted. But for example, after adding death-time correction, it showed the problem with the script. The uploaded file just shows it in T3 at the first edge. Adding more correction in the angles means more problems with sampling. And please don’t wonder, just phase R is implemented with the Script, Phase S and T is implemented with simple PLECS-blocks (sum, compare, …).

If you really want to use a C-Script you basically have two alternatives

Add a zero-crossing signal for each comparison in your scriptCalculate the time at which the next comparison changes and add a separate task in the C-Script which is executed at exactly the calculated timesWhile option 2 is more difficult to implement it is probably more efficient, since each zero-crossing adds computational overhead.Zero-crossing signals need to be evaluated in every simulation step (major and minor), otherwise they don’t work. There’s no real need to use IsMajorStep in this case.

I found a way to implement it. Thank you.