// Automatically generated C++ file on Wed Mar 20 12:51:06 2024 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD trial_x1.cpp kernel32.lib #include #include #include #include #include union uData { bool b; char c; unsigned char uc; short s; unsigned short us; int i; unsigned int ui; float f; double d; long long int i64; unsigned long long int ui64; char *str; unsigned char *bytes; }; static double timecheck = 0.0; // int DllMain() must exist and return 1 for a process to load the .DLL // See https://docs.microsoft.com/en-us/windows/win32/dlls/dllmain for more information. int __stdcall DllMain(void *module, unsigned int reason, void *reserved) { return 1; } void display(const char *fmt, ...) { // for diagnostic print statements msleep(30); fflush(stdout); va_list args = { 0 }; va_start(args, fmt); vprintf(fmt, args); va_end(args); fflush(stdout); msleep(30); } // #undef pin names lest they collide with names in any header file(s) you might include. #undef In #undef Out extern "C" __declspec(dllexport) void trial_x1(void **opaque, double t, union uData *data) { double In = data[0].d; // input double &Out1 = data[1].d; // output double &Out2 = data[2].d; // output double wg_1, wg_3, pi, Va, Vb, Vc, f1, f3, Val_g,Vbet_g,Vdg,Vqg, phi; // Implement module evaluation code here: 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 = s1; Out2 = s2; } }