PWN gen code

Dependencies:   mbed

Fork of trqrod_pwm by GOPA KUMAR K C

Revision:
1:c872ad833d2a
Parent:
0:85b285794bd5
diff -r 85b285794bd5 -r c872ad833d2a main.cpp
--- a/main.cpp	Fri Feb 13 15:48:34 2015 +0000
+++ b/main.cpp	Fri Feb 13 16:43:42 2015 +0000
@@ -1,15 +1,20 @@
-void FUNC_ACS_GENPWM(float M[3])
- {
-     PwmOut PWM1(A0);        //Functions used to generate PWM signal 
-       PwmOut PWM2(A1); 
-        PwmOut PWM3(A2);                   //PWM output comes from pins p6
-Serial pc1(USBTX, USBRX); 
-     printf("\nEnterd PWMGEN function\n");
+#include "mbed.h"
+#include "math.h"
+#include "stdlib.h"
+PwmOut PWM1(D2);        //Functions used to generate PWM signal 
+PwmOut PWM2(D3); 
+PwmOut PWM3(D4);                   //PWM output comes from pins p6
+float M[3];
+
+
+void main()
+{
      float DCx = 0;         //Duty cycle of Moment in x, y, z directions
      float ix = 0;          //Current sent in x, y, z TR's
      float timep = 0.02 ;  
      float Mx=M[0];            //Time period is set to 0.02s  
                              //Moment in x, y, z directions
+     printf("\n the moment is %f",M[0]);
       
      
         ix = Mx * 0.3 ;      //Moment and Current always have the linear relationship
@@ -42,7 +47,7 @@
          }
          else if(ix==0)
          {
-             DCx = 0;
+             DCx = 50;
             PWM1.period(timep);
             PWM1 = DCx/100 ;            
          }
@@ -50,3 +55,97 @@
          {
             // printf("!!!!!!!!!!Error!!!!!!!!!");
          } 
+         
+    printf("\n moment :%f\n",DCx);
+    float DCy = 0;         //Duty cycle of Moment in x, y, z directions
+     float iy = 0;          //Current sent in x, y, z TR's
+       
+    float My=M[1];            //Time period is set to 0.2s  
+                             //Moment in x, y, z directions
+      
+     
+        iy = My * 0.3 ;      //Moment and Current always have the linear relationship
+     
+        if( iy>0&& iy < 0.006 )                     //Current and Duty cycle have the linear relationship between 1% and 100%
+         {
+             DCy =  6*1000000*pow(iy,4) - 377291*pow(iy,3) + 4689.6*pow(iy,2) + 149.19*iy - 0.0008;
+             PWM2.period(timep);
+             PWM2 = DCy/100 ;
+         }
+        else if( iy >= 0.006&& iy < 0.0116)
+         { 
+            DCy = 1*100000000*pow(iy,4) - 5*1000000*pow(iy,3) + 62603*pow(iy,2) - 199.29*iy + 0.7648;
+            PWM2.period(timep);
+            PWM2 = DCy/100 ;             
+         }
+        else if (iy >= 0.0116&& iy < 0.0624)
+         {
+              
+            DCy = 212444*pow(iy,4) - 33244*pow(iy,3) + 1778.4*pow(iy,2) + 120.91*iy + 0.3878;
+            PWM2.period(timep);
+            PWM2 = DCy/100 ;            
+         }
+        else if(iy >= 0.0624&& iy < 0.555)
+         {
+            printf("\nACS entered if\n");
+            DCy =  331.15*pow(iy,4) - 368.09*pow(iy,3) + 140.43*pow(iy,2) + 158.59*iy + 0.0338;
+            PWM2.period(timep);
+            PWM2 = DCy/100 ;            
+         }
+         else if(iy==0)
+         {
+             DCy = 0;
+            PWM2.period(timep);
+            PWM2 = DCy/100 ;            
+         }
+         else
+         {
+            // printf("!!!!!!!!!!Error!!!!!!!!!");
+         } 
+    float DCz = 0;         //Duty cycle of Moment in x, y, z directions
+     float iz = 0;          //Current sent in x, y, z TR's
+       
+     float Mz=M[2];            //Time period is set to 0.2s  
+                             //Moment in x, y, z directions
+      
+     
+        iz = Mz * 0.3 ;      //Moment and Current always have the linear relationship
+     
+        if( iz>0&& iz < 0.006 )                     //Current and Duty cycle have the linear relationship between 1% and 100%
+         {
+             DCz =  6*1000000*pow(iz,4) - 377291*pow(iz,3) + 4689.6*pow(iz,2) + 149.19*iz - 0.0008;
+             PWM3.period(timep);
+             PWM3 = DCz/100 ;
+         }
+        else if( iz >= 0.006&& iz < 0.0116)
+         { 
+            DCz = 1*100000000*pow(iz,4) - 5*1000000*pow(iz,3) + 62603*pow(iz,2) - 199.29*iz + 0.7648;
+            PWM3.period(timep);
+            PWM3 = DCz/100 ;             
+         }
+        else if (iz >= 0.0116&& iz < 0.0624)
+         {
+              
+            DCz = 212444*pow(iz,4) - 33244*pow(iz,3) + 1778.4*pow(iz,2) + 120.91*iz + 0.3878;
+            PWM3.period(timep);
+            PWM3 = DCz/100 ;            
+         }
+        else if(iz >= 0.0624&& iz < 0.555)
+         {
+            printf("\nACS entered if\n");
+            DCz =  331.15*pow(iz,4) - 368.09*pow(iz,3) + 140.43*pow(iz,2) + 158.59*iz + 0.0338;
+            PWM3.period(timep);
+            PWM3 = DCz/100 ;            
+         }
+         else if(iz==0)
+         {
+             DCz = 0;
+            PWM3.period(timep);
+            PWM3 = DCz/100 ;            
+         }
+         else
+         {
+            // printf("!!!!!!!!!!Error!!!!!!!!!");
+         }    
+    
+}
\ No newline at end of file