How can I imitate a for loop within a C-Script block?

I want to track a ‘triangular’ reference which basically turns on and off two IGBTs in order to increase output current (by closing IGBTs) and decrease output current (by opening IGBT when voltage across capacitor is below 1 V and also the current is higher than reference). However, if output current is greater than reference BUT voltage across capacitor is not below 1V yet, then continue to fire both IGBT’s to maintain the capacitor discharging. The next figure, shows that for some reason it is not following the reference at all.

As attachments you have my circuit and code.

Snubber.plecs (29.8 KB)

track.c (250 Bytes)

track.h (88 Bytes)

Can you explain in more detail what output you are expecting?

In any case, you should reconsider your algorithm. As can be seen, as soon as you turn on the IGBTs the capacitor voltage decreases, then increases again when the IGBTs are turned off. This will result in the IGBTs toggling on and off in every simulation step.