// Automatically generated C++ file on Thu Nov 5 22:20:49 2026 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD -o pid_pwm_controller.cpp kernel32.lib #include #include extern "C" __declspec(dllexport) void (*Display)(const char *format, ...) = 0; // works like printf() extern "C" __declspec(dllexport) void (*EXIT)(const char *format, ...) = 0; // print message like printf() but exit(0) afterward extern "C" __declspec(dllexport) const double *DegreesC = 0; // pointer to current circuit temperature extern "C" __declspec(dllexport) const int *StepNumber = 0; // pointer to current step number extern "C" __declspec(dllexport) const int *NumberSteps = 0; // pointer to estimated number of steps extern "C" __declspec(dllexport) const char* const *InstanceName = 0; // pointer to address of instance name extern "C" __declspec(dllexport) const char *QUX = 0; // path to QUX.exe extern "C" __declspec(dllexport) const bool *ForKeeps = 0; // pointer to whether being evaluated non-hypothetically extern "C" __declspec(dllexport) const bool *HoldICs = 0; // pointer to whether instance initial conditions are being held extern "C" __declspec(dllexport) const void *GUI_HWND = 0; // pointer to Window handle of QUX.exe extern "C" __declspec(dllexport) const double *CKTtime = 0; extern "C" __declspec(dllexport) const double *CKTdelta = 0; extern "C" __declspec(dllexport) const int *IntegrationOrder = 0; extern "C" __declspec(dllexport) const char *InstallDirectory = 0; extern "C" __declspec(dllexport) double (*EngAtof)(const char **string) = 0; extern "C" __declspec(dllexport) const char *(*BinaryFormat)(unsigned int data) = 0; // BinaryFormat(0x1C) returns "0b00011100" extern "C" __declspec(dllexport) const char *(*EngFormat )(double x, const char *units, int numDgts) = 0; // EngFormat(1e-6, "s", 6) returns "1µs" extern "C" __declspec(dllexport) int (*DFFT)(struct sComplex *u, bool inv, unsigned int N, double scale) = 0; // Discrete Fast Fourier Transform extern "C" __declspec(dllexport) void (*bzero)(void *ptr, unsigned int count) = 0; 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 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; } // #undef pin names lest they collide with names in any header file(s) you might include. #undef setpt #undef ctrl #undef fb #undef PWM struct sPID_PWM_CONTROLLER { // declare the structure here double maxstep; double nextSampleTime; // scheduled time of next PID sampling tick bool SampleToggle; // true only on the step a PID update fired (one-shot flag) bool lastPWM; // previous PWM output double error_n1; // error[n-1] double errorI_n1; // errorI[n-1] double errorD_n1; // errorD[n-1] }; extern "C" __declspec(dllexport) void pid_pwm_controller(struct sPID_PWM_CONTROLLER **opaque, double t, union uData *data) { double setpt = data[0].d; // input double fb = data[1].d; // input double fsw = data[2].d; // input parameter double Kp = data[3].d; // input parameter double Ki = data[4].d; // input parameter double Kd = data[5].d; // input parameter double Kv = data[6].d; // input parameter bool Itype = data[7].b; // input parameter double &ctrl = data[8].d; // output bool &PWM = data[9].b; // output if(!*opaque) { *opaque = (struct sPID_PWM_CONTROLLER *) malloc(sizeof(struct sPID_PWM_CONTROLLER)); bzero(*opaque, sizeof(struct sPID_PWM_CONTROLLER)); } struct sPID_PWM_CONTROLLER *inst = *opaque; // Implement module evaluation code here: inst->maxstep = 1/fsw/50; // CLK is clock frequency; outputs Sawtooth (0-1V) double const sawtooth = fmod(t,1/fsw)*fsw; // Natural-sampling PWM comparator: high while ctrl exceeds the carrier ramp if(ctrl > sawtooth){ PWM = true; }else{ PWM = false; } // SampleToggle signals (to caller) that a PID update happened this step inst->SampleToggle = false; // PID block: runs once per switching period (regular sampling) if(t >= inst->nextSampleTime){ // Schedule next PID sampling instant (zero-order hold of ctrl between samples) inst->nextSampleTime = inst->nextSampleTime+1/fsw; // Nominal sample period double Tsampling = 1/fsw; // Tracking error (Kv = setpoint scaling / feedback gain factor) double error = Kv * (setpt - fb); // Proportional term double errorP = error; // Integral term: trapezoidal (more accurate) or rectangular (backward Euler) double errorI; if (Itype) // trapezoidal rule errorI = (error + inst->error_n1) * Tsampling / 2 + inst->errorI_n1; else // rectangular method errorI = error * Tsampling + inst->errorI_n1; // Derivative term (backward difference) double errorD = (error - inst->error_n1) / Tsampling; // PID output -> duty command used by comparator above ctrl = Kp * errorP + Ki * errorI + Kd * errorD; // Store [n-1] states for next PID iteration inst->error_n1 = error; // error[n-1] = error[n] inst->errorI_n1 = errorI; // errorI[n-1] = errorI[n] inst->errorD_n1 = errorD; // errorD[n-1] = errorD[n] inst->SampleToggle = true; // flag: PID ran this step for Trunc() } inst->lastPWM = PWM; // remember PWM output for Trunc() } extern "C" __declspec(dllexport) double MaxExtStepSize(struct sPID_PWM_CONTROLLER *inst, double t) { return inst->maxstep; // implement a good choice of max timestep size that depends on struct sPID_PWM_CONTROLLER } extern "C" __declspec(dllexport) void Trunc(struct sPID_PWM_CONTROLLER *inst, double t, union uData *data, double *timestep) { // limit the timestep to a tolerance if the circuit causes a change in struct sPID_PWM_CONTROLLER const double ttol = 1e-9; // 1ns default tolerance if(*timestep > ttol) { struct sPID_PWM_CONTROLLER tmp = *inst; pid_pwm_controller(&(&tmp), t, data); if(tmp.SampleToggle != inst->SampleToggle | tmp.lastPWM != inst->lastPWM) *timestep = ttol; } } extern "C" __declspec(dllexport) void Destroy(struct sPID_PWM_CONTROLLER *inst) { free(inst); }