# Runnning a loop in C++ block

Hello!

I am beginner in everything here so let’s see how it goes.
Trying to use the C++ block to generate space vector PWM.

and the code looks like as below:

``````if ( (t-timecheck) >= 20.e-6)
{
timecheck = t;    // CHECK THE CURRENT TIME AND STORE IT

pi = 3.141592653589793;

wg_1 = 2 * pi * 50;

Va = In * sin(wg_1*t);

Vb = In * sin(wg_1*t - ((2*pi) / 3));

Vc = In * sin(wg_1*t + ((2*pi) / 3));

double factor = 1.0 / 3.0;

Val_g = (factor) * ((2*Va) - Vb - Vc);

Vbet_g = (factor) * ((sqrt(3)*Vb) - (sqrt(3)*Vc));

phi= pi/2;

Vdg =  Val_g * cos((wg_1*t) - phi) - Vbet_g * sin((wg_1*t) - phi);

Vqg = - Val_g * sin((wg_1*t) - phi) - Vbet_g * cos((wg_1*t) - phi);

double Vdc = 20;

double Ts = t * 0.1;

// Compute Vref
double Vref = sqrt(pow(Vdg, 2) + pow(Vqg, 2));

// Compute phi_pwm
double phi_pwm = atan2(Vqg, Vdg); // atan2(y, x) returns the arctangent of y/x

// Adjust theta to be in the range [-pi, pi)
if (phi_pwm < 0) {
phi_pwm += 2 * M_PI;
}

// Determine the sector based on theta
int sector;
if (phi_pwm >= 0 && phi_pwm < M_PI / 3) {
sector = 1;
} else if (phi_pwm >= M_PI / 3 && phi_pwm < 2 * M_PI / 3) {
sector = 2;
} else if (phi_pwm >= 2 * M_PI / 3 && phi_pwm <  M_PI) {
sector = 3;
} else if (phi_pwm >= M_PI && phi_pwm < 4 * M_PI / 3) {
sector = 4;
} else if (phi_pwm >= 4 * M_PI / 3 && phi_pwm < 5 * M_PI / 3) {
sector = 5;
} else {
sector = 6;
}

double K = sqrt(3)* Ts * Vref / Vdc;

double T1 = K *(sin(sector * (pi / 3) - phi_pwm));

double T2 = K * (sin(phi_pwm - (sector - 1) * (pi / 3)));

double To = (Ts-T1-T2) / 2;

double s1, s2, s3;

s1 = (sector == 1) * (T1 + T2 + To) + (sector == 2) * (T1 + T2 + To) + (sector == 3) * (T1 + To) +
(sector == 4) * (To) + (sector == 5) * (To) + (sector == 6) * (T2 + To);

s2 = (sector == 1) * (To) + (sector == 2) * (T2 + To) + (sector == 3) * (T1 + T2 + To) +
(sector == 4) * (T1 + T2 + To) + (sector == 5) * (T1 + To) + (sector == 6) * (To);

s3 = (sector == 1) * (T1 + To) + (sector == 2) * (To) + (sector == 3) * (To) +
(sector == 4) * (T2 + To) + (sector == 5) * (T1 + T2 + To) + (sector == 6) * (T1 + T2 + To);

double Vsa = (2*s1-s2-s3) * (Vdc / 3);

double Vsb = (2*s2-s1-s3) * (Vdc / 3);

double Vsc = (2*s3-s1-s2) * (Vdc / 3);

Out1 = Vsa;

Out2 = Vsb;

}
}
``````

output:

so basically not able to close the loop and and the value is keep increasing. Ofcourse there is a basic mistake, just do not know where, is there any help available here?

Anup

Hi, Anup.

If you upload the schematic and code (as *.qsch and *.cpp), I’ll take a look at it.

–robert

Hi!

Thank you for quick response, please find the attached.

The problem is that, I am not able to sync the simulation time “t” with the loop in the C++ block and reset it when required.

trial.qsch (1.4 KB)
trial_x1.cpp (3.5 KB)

Best regards,
Anup

Anyway, I’m taking a look now.

Hi!

Yes, changed what I was plotting.

Figured some basic mistakes, like using “t” to calculate the amplitude of the signal, of course it will increase over time, changed “Ts” to a fixed value.

trial.qsch (1.4 KB)
trial_x1.cpp (3.5 KB)

Best regards,
Anup

Sorry for the delayed response, Anup.

Well, you found what I was going to look for (Ts). If you want to use the actual time step increment rather than a hardcoded value, you could change this bit:

``````  //   if ((t - timecheck) >= 20.e-6) {
double Ts = t - timecheck;
if (Ts >= 20.e-6) {
``````

Is it now working as you expected?

–robert

@anup - For the sake of clarity, I took the liberty to format the code in your post as a code would look like.

1 Like

yes, it’s working as expected. thanks for your time and support, means a lot.

thanks, much appreciated. would it be possible to send it back so that I can use it in a better context.

Thanks again, have a great day,

@anup - The code is unchanged; it was just reformatted to look like code.