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; }; int __stdcall DllMain(void *module, unsigned int reason, void *reserved) { return 1; } #undef fb #undef c #undef g #undef ref #undef d extern "C" __declspec(dllexport) void buck_conv_pwm(void **opaque, double t, union uData *data) { double fb = data[0].d; double c = data[1].d; double ref = data[2].d; double &g = data[3].d; double &d = data[4].d; static double last_t = 0.0; double dt = t - last_t; last_t = t; if (dt == t) { dt = 0.0; } double Kp = 0.02; double Ki = 50.0; static double integral_term = 0.0; double error = ref - fb; integral_term = integral_term + (Ki * error * dt); if (integral_term > 0.90) { integral_term = 0.90; } else if (integral_term < 0.0) { integral_term = 0.0; } d = (Kp * error) + integral_term; if (d > 0.95) { d = 0.95; } else if (d < 0.05) { d = 0.05; } if (d > c) { g = 5.0; } else { g = 0.0; } }