Discontinuous PWM ( 60-degree Clamp PWM Technique)

Hello everyone,
I want to make DPWM modulation like in the video. but the results are still not as good as in the video. is there still an error in my code?
Thanks


image
this is my code
// Automatically generated C++ file on Thu Jul 18 15:55:31 2024
//
// To build with Digital Mars C++ Compiler:
//
// dmc -mn -WD dpwm1.cpp kernel32.lib

#include <stdio.h>
#include <malloc.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>

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 DllMain entry point (Process.h) - Win32 apps | Microsoft Learn 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);
}

void bzero(void *ptr, unsigned int count)
{
unsigned char *first = (unsigned char *) ptr;
unsigned char *last = first + count;
while(first < last)
*first++ = ‘\0’;
}

// #undef pin names lest they collide with names in any header file(s) you might include.
#undef VrefA
#undef VrefB
#undef VrefC
#undef Vr
#undef Vs
#undef Vt
#undef mcm

struct sDPWM1
{
// declare the structure here
};

extern “C” __declspec(dllexport) void dpwm1(struct sDPWM1 **opaque, double t, union uData *data)
{
double VrefA = data[0].d; // input
double VrefB = data[1].d; // input
double VrefC = data[2].d; // input
double &Vr = data[3].d; // output
double &Vs = data[4].d; // output
double &Vt = data[5].d; // output
double &mcm = data[6].d; // output

if(!*opaque)
{
*opaque = (struct sDPWM1 *) malloc(sizeof(struct sDPWM1));
bzero(*opaque, sizeof(struct sDPWM1));
}
struct sDPWM1 *inst = *opaque;

// Implement module evaluation code here:
// Calculate alpha and beta
double alpha = (2 * sqrt(6) * VrefA - sqrt(6) * VrefB - sqrt(6) * VrefC) / 6;
double beta = (sqrt(2) * VrefB - sqrt(2) * VrefC) / 2;

// Calculate theta
double theta = atan2(beta, alpha);

// Calculate mcm based on theta
if (M_PI / 3 <= theta && theta <= 2 * M_PI / 3) {
mcm = 1 - VrefA;
} else if (2 * M_PI / 3 <= theta && theta <= M_PI) {
mcm = -1 - VrefC;
} else if (M_PI <= theta && theta <= 4 * M_PI / 3) {
mcm = 1 - VrefB;
} else if (4 * M_PI / 3 <= theta && theta <= 5 * M_PI / 3) {
mcm = -1 - VrefA;
} else if (5 * M_PI / 3 <= theta && theta <= 2 * M_PI) {
mcm = 1 - VrefC;
} else {
mcm = 0; // default value if theta is outside expected range
}

// Calculate final values
Vr = VrefA + mcm;
Vs = VrefB + mcm;
Vt = VrefC + mcm;
}

extern “C” __declspec(dllexport) double MaxExtStepSize(struct sDPWM1 *inst)
{
return 1e308; // implement a good choice of max timestep size that depends on struct sDPWM1
}

extern “C” __declspec(dllexport) void Trunc(struct sDPWM1 *inst, double t, union uData *data, double *timestep)
{ // limit the timestep to a tolerance if the circuit causes a change in struct sDPWM1
const double ttol = 1e-9;
if(*timestep > ttol)
{
double &Vr = data[3].d; // output
double &Vs = data[4].d; // output
double &Vt = data[5].d; // output
double &mcm = data[6].d; // output

  // Save output vector
  const double _Vr    = Vr   ;
  const double _Vs    = Vs   ;
  const double _Vt    = Vt   ;
  const double _mcm   = mcm  ;

  struct sDPWM1 tmp = *inst;
  dpwm1(&(&tmp), t, data);

// if(tmp != *inst) // implement a meaningful way to detect if the state has changed
// *timestep = ttol;

  // Restore output vector
  Vr    = _Vr   ;
  Vs    = _Vs   ;
  Vt    = _Vt   ;
  mcm   = _mcm  ;

}
}

extern “C” __declspec(dllexport) void Destroy(struct sDPWM1 *inst)
{
free(inst);
}