PWN gen code

Dependencies:   mbed

Fork of trqrod_pwm by GOPA KUMAR K C

main.cpp

Committer:
gkumar
Date:
2015-02-13
Revision:
1:c872ad833d2a
Parent:
0:85b285794bd5

File content as of revision 1:c872ad833d2a:

#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
     
        if( ix>0&& ix < 0.006 )                     //Current and Duty cycle have the linear relationship between 1% and 100%
         {
             DCx =  6*1000000*pow(ix,4) - 377291*pow(ix,3) + 4689.6*pow(ix,2) + 149.19*ix - 0.0008;
             PWM1.period(timep);
             PWM1 = DCx/100 ;
         }
        else if( ix >= 0.006&& ix < 0.0116)
         { 
            DCx = 1*100000000*pow(ix,4) - 5*1000000*pow(ix,3) + 62603*pow(ix,2) - 199.29*ix + 0.7648;
            PWM1.period(timep);
            PWM1 = DCx/100 ;             
         }
        else if (ix >= 0.0116&& ix < 0.0624)
         {
              
            DCx = 212444*pow(ix,4) - 33244*pow(ix,3) + 1778.4*pow(ix,2) + 120.91*ix + 0.3878;
            PWM1.period(timep);
            PWM1 = DCx/100 ;            
         }
        else if(ix >= 0.0624&& ix < 0.555)
         {
            printf("\nACS entered if\n");
            DCx =  331.15*pow(ix,4) - 368.09*pow(ix,3) + 140.43*pow(ix,2) + 158.59*ix + 0.0338;
            PWM1.period(timep);
            PWM1 = DCx/100 ;            
         }
         else if(ix==0)
         {
             DCx = 50;
            PWM1.period(timep);
            PWM1 = DCx/100 ;            
         }
         else
         {
            // 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!!!!!!!!!");
         }    
    
}