Hello everyone,
I am currently working on implementing a Discrete Integrator using a C-Script block to compare it with the standard PLECS library block.
For the comparison, I set the integration method of the standard Discrete Integrator to ‘Forward Euler’ and applied both integrators to a PLL (Phase Locked Loop) system.
- Figure 1 shows the simulation results using the standard Discrete Integrator block.
- Figure 2 shows the results using my C-Script based integrator.
As you can see, there is a difference between the two results. In the first figure, i_d and v_d remain at 0, while i_q and v_q stably maintain a constant negative value. However, when the C-Script integrator is applied, the signals oscillate significantly, and eventually, the polarity reverses.
Below is the code I wrote for the Forward Euler method. Logic-wise, it seems correct to me, so I am struggling to understand what causes this significant divergence compared to the built-in block.
< Code declarations >
#define u Input(0)
#define reset Input(1)
#define y Output(0)
#define x DiscState(0)
#define u_prev DiscState(1)
float Ts;
float init_val;
< Start function code >
Ts = 0.0001;
init_val = 0.0;
x = 0;
u_prev = 0;
< Output function code >
y = x;
< Update function code >
if (reset != 0.0) {
x = init_Val;
}
else {
x = x + Ts*u_prev;
}
u_prev = u
Could anyone shed some light on what I might be missing here?
I have attached two simulation models for youre reference.
- simulation model using the standard discrete integrator
260116_frame_convert_test_with_default_integ.plecs (62.9 KB)
2. simulation model using the C-Script based integrator
260116_frame_convert_test_with_c-script_integ.plecs (69.6 KB)
I would really appreciate it if you could take a look at the models and share your feedback.
Thank you in advance for your help!
Could anyone advise on what might be causing this discrepancy?




