New PWM code by Yeshwanth

Dependencies:   mbed

Revision:
0:4e8fb2adea64
Child:
1:c5f613785c7b
diff -r 000000000000 -r 4e8fb2adea64 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Mar 19 07:24:49 2016 +0000
@@ -0,0 +1,173 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include<math.h>
+#include "pin_config.h"
+#include "mbed.h"
+void FCTN_ACS_GENPWM_MAIN(float Moment[3])
+{
+    printf("\n\rEntered executable PWMGEN function\n"); // entering the PWMGEN executable function
+    
+    float l_duty_cycle_x=0;    //Duty cycle of Moment in x direction
+    float l_current_x=0;       //Current sent in x TR's
+    float l_duty_cycle_y=0;    //Duty cycle of Moment in y direction
+    float l_current_y=0;       //Current sent in y TR's
+    float l_duty_cycle_z=0;    //Duty cycle of Moment in z direction
+    float l_current_z=0;       //Current sent in z TR's
+ 
+    
+    for(int i = 0 ; i<3;i++)
+    {
+     //   printf(" %f \t ",Moment[i]);  // taking the moment values from control algorithm as inputs
+    }
+    
+    //-----------------------------  x-direction TR  --------------------------------------------//
+    
+    
+    float l_moment_x = Moment[0];         //Moment in x direction
+    
+    phase_TR_x = 1;  // setting the default current direction
+    if (l_moment_x <0)
+    {
+        phase_TR_x = 0;    // if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high 
+        l_moment_x = abs(l_moment_x);
+    }
+    
+    l_current_x = l_moment_x * TR_CONSTANT ;        //Moment and Current always have the linear relationship
+    pc_acs.printf("current in trx is %f \r \n",l_current_x);
+    if( l_current_x>0 && l_current_x < 0.0016 ) //Current and Duty cycle have the linear relationship between 1% and 100%
+    {
+        l_duty_cycle_x =  3*10000000*pow(l_current_x,3)- 90216*pow(l_current_x,2) + 697.78*l_current_x - 0.0048; // calculating upto 0.1% dutycycle by polynomial interpolation 
+        pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x);
+        PWM1.period(TIME_PERIOD);
+        PWM1 = l_duty_cycle_x/100 ;
+    }
+    else if (l_current_x >= 0.0016 && l_current_x < 0.0171)
+    {
+        l_duty_cycle_x = - 76880*pow(l_current_x,3) + 1280.8*pow(l_current_x,2) + 583.78*l_current_x + 0.0281; // calculating upto 10% dutycycle by polynomial interpolation
+        pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x);
+        PWM1.period(TIME_PERIOD);
+        PWM1 = l_duty_cycle_x/100 ;            
+    }
+    else if(l_current_x >= 0.0171 && l_current_x < 0.1678)
+    {
+        l_duty_cycle_x =  275.92*pow(l_current_x,2) + 546.13*l_current_x + 0.5316; // calculating upto 100% dutycycle by polynomial interpolation
+        pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x);
+        PWM1.period(TIME_PERIOD);
+        PWM1 = l_duty_cycle_x/100 ;            
+    }
+    else if(l_current_x==0)
+    {
+        printf("\n \r l_current_x====0");
+        l_duty_cycle_x = 0;      // default value of duty cycle
+        pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x);
+        PWM1.period(TIME_PERIOD);
+        PWM1 = l_duty_cycle_x/100 ;            
+    }
+    else                                           //not necessary
+    {
+        g_err_flag_TR_x = 1;
+    } 
+         
+    //------------------------------------- y-direction TR--------------------------------------//
+    
+     
+    float l_moment_y = Moment[1];         //Moment in y direction
+    
+    phase_TR_y = 1;  // setting the default current direction
+    if (l_moment_y <0)
+    {
+        phase_TR_y = 0;   //if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high  
+        l_moment_y = abs(l_moment_y);
+    }
+    
+    
+    l_current_y = l_moment_y * TR_CONSTANT ;        //Moment and Current always have the linear relationship
+     pc_acs.printf("current in try is %f \r \n",l_current_y);
+        if( l_current_y>0 && l_current_y < 0.0016 ) //Current and Duty cycle have the linear relationship between 1% and 100%
+    {
+        l_duty_cycle_y =  3*10000000*pow(l_current_y,3)- 90216*pow(l_current_y,2) + 697.78*l_current_y - 0.0048; // calculating upto 0.1% dutycycle by polynomial interpolation 
+        pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y);
+        PWM2.period(TIME_PERIOD);
+        PWM2 = l_duty_cycle_y/100 ;
+    }
+    else if (l_current_y >= 0.0016 && l_current_y < 0.0171)
+    {
+        l_duty_cycle_y = - 76880*pow(l_current_y,3) + 1280.8*pow(l_current_y,2) + 583.78*l_current_y + 0.0281; // calculating upto 10% dutycycle by polynomial interpolation
+        pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y);
+        PWM2.period(TIME_PERIOD);
+        PWM2 = l_duty_cycle_y/100 ;            
+    }
+    else if(l_current_y >= 0.0171 && l_current_y < 0.1678)
+    {
+        l_duty_cycle_y =  275.92*pow(l_current_y,2) + 546.13*l_current_y + 0.5316; // calculating upto 100% dutycycle by polynomial interpolation
+        pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y);
+        PWM2.period(TIME_PERIOD);
+        PWM2 = l_duty_cycle_y/100 ;            
+    }        
+    else if(l_current_y==0)
+    {
+        printf("\n \r l_current_y====0");
+        l_duty_cycle_y = 0; // default value of duty cycle
+        pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y);
+        PWM2.period(TIME_PERIOD);
+        PWM2 = l_duty_cycle_y/100 ;            
+    }
+    else                               // not necessary
+    {
+      g_err_flag_TR_y = 1;
+    } 
+             
+    //----------------------------------------------- z-direction TR -------------------------//  
+    
+      
+    float l_moment_z = Moment[2];         //Moment in z direction
+    
+    phase_TR_z = 1;   // setting the default current direction
+    if (l_moment_z <0)
+    {
+        phase_TR_z = 0; //if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high 
+        l_moment_z = abs(l_moment_z);
+    }
+    
+    
+    l_current_z = l_moment_z * TR_CONSTANT ;        //Moment and Current always have the linear relationship
+     pc_acs.printf("current in trz is %f \r \n",l_current_z);
+        if( l_current_z>0 && l_current_z < 0.0016 ) //Current and Duty cycle have the linear relationship between 1% and 100%
+    {
+        l_duty_cycle_z =  3*10000000*pow(l_current_z,3)- 90216*pow(l_current_z,2) + 697.78*l_current_z - 0.0048; // calculating upto 0.1% dutycycle by polynomial interpolation 
+        pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z);
+        PWM3.period(TIME_PERIOD);
+        PWM3 = l_duty_cycle_z/100 ;
+    }
+    else if (l_current_z >= 0.0016 && l_current_z < 0.0171)
+    {
+        l_duty_cycle_z = - 76880*pow(l_current_z,3) + 1280.8*pow(l_current_z,2) + 583.78*l_current_z + 0.0281; // calculating upto 10% dutycycle by polynomial interpolation
+        pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z);
+        PWM3.period(TIME_PERIOD);
+        PWM3 = l_duty_cycle_z/100 ;            
+    }
+    else if(l_current_z >= 0.0171 && l_current_z < 0.1678)
+    {
+        l_duty_cycle_z =  275.92*pow(l_current_z,2) + 546.13*l_current_z + 0.5316; // calculating upto 100% dutycycle by polynomial interpolation
+        pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z);
+        PWM3.period(TIME_PERIOD);
+        PWM3 = l_duty_cycle_z/100 ;            
+    }
+    else if(l_current_z==0)
+    {
+        printf("\n \r l_current_z====0");
+        l_duty_cycle_z = 0; // default value of duty cycle
+        pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z);
+        PWM3.period(TIME_PERIOD);
+        PWM3 = l_duty_cycle_z/100 ;            
+    }
+    else                               // not necessary
+    {
+        g_err_flag_TR_z = 1;
+    }   
+    
+    //-----------------------------------------exiting the function-----------------------------------//
+    
+    printf("\n\rExited executable PWMGEN function\n\r"); // stating the successful exit of TR function
+ 
+}
\ No newline at end of file