Unable to calculate/update Theta value for d-q transformation (Park Transformation)

I am trying to perform a d-q transformation on a signal after performing a clark transformation, but I am unable to calculate/update the theta value required for the transformation. I suspect there is a problem with the line "Theta[n+1]=Theta[n]+(2PI)(fo*Ts);" in my code. I am using an Arduino IDE and STM32f103 for the PLL and PI controller as part of a project on a three-phase voltage source inverter with current controlling technique.

#include <math.h> 
double Ia[]={
  1,0.99,0.99,0.99,0.99,0.98,0.98,0.97,0.96,0.96
  ,0.951056516295154,0.940880768954226,0.929776485888251,0.917754625683981,0.904827052466020,0.891006524188368,0.876306680043864,0.860742027003944,0.844327925502015,0.827080574274562,0.809016994374948,0.790155012375690,0.770513242775789,0.750111069630460,0.728968627421412,0.707106781186548,0.684547105928689,0.661311865323652,0.637423989748690,0.612907053652977,0.587785252292473,0.562083377852131,0.535826794978997,0.509041415750371,0.481753674101715,0.453990499739547,0.425779291565073,0.397147890634781,0.368124552684678,0.338737920245291,0.309016994374947,0.278991106039229,0.248689887164855,0.218143241396542,0.187381314585725,0.156434465040231,0.125333233564304,0.0941083133185143,0.0627905195293133,0.0314107590781284,-1.60812264967664e-16,-0.0314107590781283,-0.0627905195293134,-0.0941083133185144,-0.125333233564304,-0.156434465040231,-0.187381314585725,-0.218143241396543,-0.248689887164855,-0.278991106039229,-0.309016994374947,-0.338737920245291,-0.368124552684678,-0.397147890634780,-0.425779291565073,-0.453990499739547,-0.481753674101715,-0.509041415750371,-0.535826794978997,-0.562083377852131,-0.587785252292473,-0.612907053652977,-0.637423989748690,-0.661311865323652,-0.684547105928689,-0.707106781186548,-0.728968627421411,-0.750111069630460,-0.770513242775789,-0.790155012375690,-0.809016994374947,-0.827080574274562,-0.844327925502015,-0.860742027003944,-0.876306680043864,-0.891006524188368,-0.904827052466020,-0.917754625683981,-0.929776485888251,-0.940880768954226,-0.951056516295154,-0.960293685676943,-0.968583161128631,-0.975916761938747,-0.982287250728689,-0.987688340595138,-0.992114701314478,-0.995561964603080,-0.998026728428272,-0.999506560365732,-1,-0.999506560365732,-0.998026728428272,-0.995561964603080,-0.992114701314478,-0.987688340595138,-0.982287250728689,-0.975916761938748,-0.968583161128631,-0.960293685676943,-0.951056516295154,-0.940880768954226,-0.929776485888252,-0.917754625683981,-0.904827052466020,-0.891006524188368,-0.876306680043864,-0.860742027003944,-0.844327925502015,-0.827080574274562,-0.809016994374948,-0.790155012375691,-0.770513242775789,-0.750111069630460,-0.728968627421411,-0.707106781186548,-0.684547105928689,-0.661311865323652,-0.637423989748690,-0.612907053652977,-0.587785252292473,-0.562083377852131,-0.535826794978997,-0.509041415750372,-0.481753674101715,-0.453990499739547,-0.425779291565072,-0.397147890634781,-0.368124552684679,-0.338737920245291,-0.309016994374948,-0.278991106039229,-0.248689887164854,-0.218143241396543,-0.187381314585725,-0.156434465040231,-0.125333233564305,-0.0941083133185140,-0.0627905195293132,-0.0314107590781283,-1.83697019872103e-16,0.0314107590781288,0.0627905195293128,0.0941083133185145,0.125333233564304,0.156434465040231,0.187381314585724,0.218143241396542,0.248689887164855,0.278991106039229,0.309016994374947,0.338737920245291,0.368124552684677,0.397147890634782,0.425779291565073,0.453990499739547,0.481753674101715,0.509041415750371,0.535826794978996,0.562083377852130,0.587785252292473,0.612907053652976,0.637423989748690,0.661311865323651,0.684547105928688,0.707106781186548,0.728968627421411,0.750111069630460,0.770513242775789,0.790155012375690,0.809016994374948,0.827080574274562,0.844327925502015,0.860742027003944,0.876306680043864,0.891006524188368,0.904827052466020,0.917754625683981,0.929776485888252,0.940880768954226,0.951056516295154,0.960293685676943,0.968583161128631,0.975916761938747,0.982287250728689,0.987688340595138,0.992114701314478,0.995561964603080,0.998026728428272,0.999506560365732,1

};
 double Ib[]={
  
  -0.50,-0.47,-0.44,-0.41,-0.38,-0.35,-0.32,-0.29,-0.26,-0.23,-0.207911690817759,-0.177084740319583,-0.146083028562411,-0.114937150492866,-0.0836778433323153,-0.0523359562429436,-0.0209424198833567,0.0104717841162461,0.0418756537291998,0.0732381971276319,0.104528463267654,0.135715572434305,0.166768746716103,0.197657340379126,0.228350870110656,0.258819045102521,0.289031796944472,0.318959309298070,0.348572047321815,0.377840786818467,0.406736643075800,0.435231099372328,0.463296035119862,0.490903753615141,0.518027009373131,0.544639035015027,0.570713567684432,0.596224874965616,0.621147780278310,0.645457687723951,0.669130606358858,0.692143173870407,0.714472679632804,0.736097087119735,0.756995055651757,0.777145961456971,0.796529918024196,0.815127795728554,0.832921240710100,0.849892692986864,0.866025403784439,0.881303452064992,0.895711760239413,0.909236109047069,0.921863151588501,0.933580426497202,0.944376370237481,0.954240328516277,0.963162566797658,0.971134279909636,0.978147600733806,0.984195607969242,0.989272332962988,0.993372765600396,0.996492859249504,0.998629534754574,0.999780683474846,0.999945169365512,0.999122830098858,0.997314477224458,0.994521895368273,0.990747840471444,0.985996037070505,0.980271174621722,0.973578902873160,0.965925826289068,0.957319497532067,0.947768410009586,0.937281989491891,0.925870584809995,0.913545457642601,0.900318771402194,0.886203579231215,0.871213811120189,0.855364260160506,0.838670567945424,0.821149209133704,0.802817475191114,0.783693457325840,0.763796028634642,0.743144825477394,0.721760228098362,0.699663340513365,0.676875969682660,0.653420603990105,0.629320391049837,0.604599114862375,0.579281172342679,0.553391549243344,0.526955795496677,0.500000000000000,0.472550764869054,0.444635179184927,0.416280792260401,0.387515586452103,0.358367949545300,0.328866646738583,0.299040792256087,0.268919820615265,0.238533457578581,0.207911690817760,0.177084740319583,0.146083028562411,0.114937150492867,0.0836778433323152,0.0523359562429437,0.0209424198833575,-0.0104717841162460,-0.0418756537291996,-0.0732381971276320,-0.104528463267654,-0.135715572434304,-0.166768746716102,-0.197657340379126,-0.228350870110656,-0.258819045102521,-0.289031796944472,-0.318959309298070,-0.348572047321816,-0.377840786818467,-0.406736643075801,-0.435231099372327,-0.463296035119862,-0.490903753615140,-0.518027009373130,-0.544639035015027,-0.570713567684432,-0.596224874965616,-0.621147780278310,-0.645457687723951,-0.669130606358858,-0.692143173870407,-0.714472679632804,-0.736097087119734,-0.756995055651757,-0.777145961456971,-0.796529918024196,-0.815127795728555,-0.832921240710100,-0.849892692986864,-0.866025403784439,-0.881303452064993,-0.895711760239413,-0.909236109047069,-0.921863151588501,-0.933580426497202,-0.944376370237481,-0.954240328516277,-0.963162566797658,-0.971134279909636,-0.978147600733806,-0.984195607969242,-0.989272332962988,-0.993372765600397,-0.996492859249504,-0.998629534754574,-0.999780683474846,-0.999945169365512,-0.999122830098858,-0.997314477224458,-0.994521895368273,-0.990747840471444,-0.985996037070505,-0.980271174621722,-0.973578902873160,-0.965925826289068,-0.957319497532067,-0.947768410009586,-0.937281989491891,-0.925870584809995,-0.913545457642601,-0.900318771402193,-0.886203579231215,-0.871213811120189,-0.855364260160507,-0.838670567945424,-0.821149209133704,-0.802817475191114,-0.783693457325840,-0.763796028634642,-0.743144825477394,-0.721760228098362,-0.699663340513365,-0.676875969682661,-0.653420603990106,-0.629320391049838,-0.604599114862375,-0.579281172342680,-0.553391549243343,-0.526955795496677,-0.500000000000000
  };
  double Ic[]={
    -0.50,-0.52,-0.55,-0.57,-0.60,-0.62,-0.65,-0.67,-0.69,-0.72,-0.743144825477395,-0.763796028634642,-0.783693457325840,-0.802817475191115,-0.821149209133704,-0.838670567945424,-0.855364260160507,-0.871213811120190,-0.886203579231215,-0.900318771402194,-0.913545457642601,-0.925870584809995,-0.937281989491892,-0.947768410009586,-0.957319497532068,-0.965925826289068,-0.973578902873160,-0.980271174621722,-0.985996037070505,-0.990747840471444,-0.994521895368273,-0.997314477224458,-0.999122830098858,-0.999945169365512,-0.999780683474846,-0.998629534754574,-0.996492859249504,-0.993372765600396,-0.989272332962988,-0.984195607969242,-0.978147600733806,-0.971134279909636,-0.963162566797658,-0.954240328516277,-0.944376370237481,-0.933580426497202,-0.921863151588500,-0.909236109047068,-0.895711760239413,-0.881303452064992,-0.866025403784439,-0.849892692986864,-0.832921240710099,-0.815127795728554,-0.796529918024196,-0.777145961456971,-0.756995055651756,-0.736097087119734,-0.714472679632803,-0.692143173870406,-0.669130606358858,-0.645457687723950,-0.621147780278310,-0.596224874965616,-0.570713567684431,-0.544639035015026,-0.518027009373130,-0.490903753615140,-0.463296035119861,-0.435231099372327,-0.406736643075800,-0.377840786818467,-0.348572047321815,-0.318959309298070,-0.289031796944471,-0.258819045102520,-0.228350870110656,-0.197657340379126,-0.166768746716102,-0.135715572434304,-0.104528463267653,-0.0732381971276315,-0.0418756537291992,-0.0104717841162455,0.0209424198833579,0.0523359562429442,0.0836778433323161,0.114937150492867,0.146083028562412,0.177084740319584,0.207911690817760,0.238533457578581,0.268919820615266,0.299040792256087,0.328866646738584,0.358367949545301,0.387515586452104,0.416280792260402,0.444635179184928,0.472550764869054,0.500000000000001,0.526955795496677,0.553391549243344,0.579281172342679,0.604599114862375,0.629320391049838,0.653420603990106,0.676875969682661,0.699663340513366,0.721760228098363,0.743144825477394,0.763796028634643,0.783693457325840,0.802817475191114,0.821149209133704,0.838670567945424,0.855364260160507,0.871213811120190,0.886203579231215,0.900318771402194,0.913545457642601,0.925870584809995,0.937281989491892,0.947768410009586,0.957319497532068,0.965925826289068,0.973578902873160,0.980271174621722,0.985996037070505,0.990747840471444,0.994521895368274,0.997314477224458,0.999122830098858,0.999945169365512,0.999780683474846,0.998629534754574,0.996492859249504,0.993372765600396,0.989272332962988,0.984195607969242,0.978147600733806,0.971134279909636,0.963162566797658,0.954240328516277,0.944376370237481,0.933580426497202,0.921863151588501,0.909236109047068,0.895711760239413,0.881303452064992,0.866025403784439,0.849892692986863,0.832921240710100,0.815127795728554,0.796529918024196,0.777145961456971,0.756995055651756,0.736097087119734,0.714472679632803,0.692143173870407,0.669130606358858,0.645457687723951,0.621147780278310,0.596224874965615,0.570713567684431,0.544639035015027,0.518027009373130,0.490903753615141,0.463296035119862,0.435231099372328,0.406736643075800,0.377840786818467,0.348572047321814,0.318959309298070,0.289031796944472,0.258819045102520,0.228350870110656,0.197657340379125,0.166768746716102,0.135715572434304,0.104528463267652,0.0732381971276307,0.0418756537291998,0.0104717841162452,-0.0209424198833574,-0.0523359562429441,-0.0836778433323164,-0.114937150492867,-0.146083028562412,-0.177084740319584,-0.207911690817760,-0.238533457578581,-0.268919820615266,-0.299040792256087,-0.328866646738584,-0.358367949545301,-0.387515586452103,-0.416280792260401,-0.444635179184928,-0.472550764869054,-0.500000000000000
 };
 
 /*double Ia[]={0.00,0.12,0.134,0.456,0.856};
  double Ib[]={0.00,0.12,0.134,0.456,0.856};
   double Ic[]={0.00,0.12,0.134,0.456,0.856};
 */
 double Ialpha[200]={};
 double Ibeta[200]={};
  double Theta[200]={};
  double u_q[200]={};
  double Uq_plus[200]={};
  double Ud_plus[200]={};
  double a=167.9877775;//filter gain
  double b=165.2122225;//filter gain
  double ylf[]={0.00,0.00};
  double c=200.00;
  double fo=0.00;
  double fn=50;
  double Ts=1/10000;
void setup() {
  // put your setup code here, to run once:
//SystemClock_Config();
  Serial.setRx(PA10);
  Serial.setTx(PA9);
  Serial.begin(2400);
  //Serial.print("start");
}

void loop() {
  
  // put your main code here, to run repeatedly:
for(int n=2;n<200;n++)
{ 
  Serial.print("n= ");

  Serial.print(n);
  Serial.println("Ia =");
  Serial.print(Ia[n]);
  Serial.println("Ib =");
  Serial.print(Ib[n]);
  Serial.println("Ic =");
  Serial.print(Ic[n]);

Ialpha[n] =(2.0/3.0)*(Ia[n]-0.5*(Ib[n]+Ic[n]));
Ibeta[n] =(2.0/3.0)*0.866*(Ib[n]-Ic[n]);

Serial.print("Ialpha =");
Serial.println(Ialpha[n]);
Serial.print("Ibeta =");
Serial.println(Ibeta[n]);

Ud_plus[n]=Ialpha[n]*cos(Theta[n])+Ibeta[n]*sin(Theta[n]);
Uq_plus[n]=-Ialpha[n]*sin(Theta[n])+Ibeta[n]*cos(Theta[n]);
u_q[0]=Uq_plus[n];  
ylf[0]=ylf[1]+a*u_q[0]-b*u_q[1];
ylf[0]=min(ylf[0],c);
u_q[1]=u_q[0];
ylf[1]=ylf[0];
fo=fn+ylf[0];
Serial.print("fo=");
Serial.println(fo);


Serial.print(Theta[2]);
Theta[n+1]=Theta[n]+(2*PI)*(fo*Ts);
if (Theta[n+1]>=((2*PI)-(2*PI)*(fo*Ts)))
{
Theta[n+1]=0;
}
Serial.print("ud =");
Serial.print(Ud_plus[n]);
Serial.print("uq =");
Serial.print(Uq_plus[n]);

}
ylf[0]=0.00;
ylf[1]=0.00;
Theta[0]=0.00;
Theta[1]=0.00;
Theta[2]=0.00;
Serial.println("ends");
/*for(int i=0;i<9;i++)
{
  Serial.print("ud =");
  Serial.print(Ud_plus[i]);
  }
  */
}

I have tried to locate the problem in the code, particularly in the line "Theta[n+1]=Theta[n]+(2PI)(fo*Ts);" where I suspect the issue lies. I expected the theta value to be calculated and updated properly, which is necessary for performing the d-q transformation accurately. However, due to the issue with the theta value calculation, I am not able to obtain the desired output after the d-q transformation.

Thanks in advance!

Welcome to the forum.

Can you do a few things:

  • Can the baudrate be 115200 ?
  • Can you print "Ts" in setup(). The compiler defaults to integer, and "1" is a integer and "10000" is an integer and the result of "1/10000" is integer zero. That integer zero is then converted to double and put in the "Ts" variable. It is as zero as zero can be.
  • Please right-click on the code and select "Format Document". The text of your source code should be clean and nice with every space, every comma, every indent, every newline at the right place, especially with such complex math calculations.
  • The "Theta[n + 1]" will use index 200 for the last one, but that does not exist in the array. You have to fix that.

When I print the array size in setup(), then Ia, Ib and Ic are 201. Is that right ?

  Serial.println(sizeof(Ia)/sizeof(double));
  Serial.println(sizeof(Ib)/sizeof(double));
  Serial.println(sizeof(Ic)/sizeof(double));

I worked on second point and that's worked for me.
Thanks for your answer!
Stay Happy, Take care :slight_smile:

No, not yet :grimacing:
The "Format Document" is important, and using a memory location that you don't own is a bug.

I will be happy if you show a good sketch :smiley:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.