Simplified multiplication in phase duty computation.
authorMartin Prudek <prudemar@fel.cvut.cz>
Sun, 10 May 2015 18:50:31 +0000 (20:50 +0200)
committerMartin Prudek <prudemar@fel.cvut.cz>
Sun, 10 May 2015 18:50:31 +0000 (20:50 +0200)
pmsm-control/test_sw/main_pmsm.c

index 26e8308..8706dc9 100644 (file)
 #define INIT_VALUE     1       /*init value for semaphor*/
 
 
-#define PXMC_SIN_FIX_TAB_BITS 9
-#define PXMC_SIN_FIX_IDX_SLR  23
-#define PXMC_SIN_FIX_XD_MASK  0x007fffff
-#define PXMC_SIN_FIX_XD_SLR   8
-#define PXMC_SIN_FIX_A_MASK   0xffffc000
-#define PXMC_SIN_FIX_B_SLL    19
-#define PXMC_SIN_FIX_B_SAR    16
-#define PXMC_SIN_FIX_B_XD_SAR 6
-#define PXMC_SIN_FIX_ZIC_MASK 0x00002000
-#define PXMC_SIN_FIX_ZIC_BIT  13
-
-#define PXMC_SIN_FIX_PI2      0x40000000
-#define PXMC_SIN_FIX_2PI3     0x55555555
-
 #define NSEC_PER_SEC    (1000000000) /* The number of nsecs per sec. */
 
 
@@ -256,33 +242,50 @@ int sin_commutator(int duty){
        #define DEGREE_300      3579139413
        uint32_t j,pos;
        int32_t sin;
-       pos=rps.index_dist*4294967;
+       pos=rps.index_dist;
+       int32_t pwm;
+       /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
+       pos+=38;
+       /*use it as cyclic 32-bit logic*/
+       pos*=4294967;
        if (duty>=0){   /*clockwise rotation*/
                /* 1st phase */
                sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
-                rps.pwm1=mult_cap(sin, duty);
+                pwm=sin*duty/1024;
+                if (pwm<0) pwm=0;
+               rps.pwm1=(uint16_t)pwm;
 
                 /* 2nd phase */
                 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
-                rps.pwm2=mult_cap(sin, duty);
+                pwm=sin*duty/1024;
+                if (pwm<0) pwm=0;
+                rps.pwm2=(uint16_t)pwm;
 
                 /* 3rd phase */
                 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
-                rps.pwm3=mult_cap(sin, duty);
+                pwm=sin*duty/1024;
+                if (pwm<0) pwm=0;
+                rps.pwm3=(uint16_t)pwm;
         }else{
                duty=-duty;
 
                /* 1st phase */
                sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
-                rps.pwm1=mult_cap(sin, duty);
+               pwm=sin*duty/1024;
+               if (pwm<0) pwm=0;
+               rps.pwm1=(uint16_t)pwm;
 
                 /* 2nd phase */
                 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
-                rps.pwm2=mult_cap(sin, duty);
+                pwm=sin*duty/1024;
+                if (pwm<0) pwm=0;
+                rps.pwm2=(uint16_t)pwm;
 
                 /* 3rd phase */
                 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
-                rps.pwm3=mult_cap(sin, duty);
+                pwm=sin*duty/1024;
+                if (pwm<0) pwm=0;
+                rps.pwm3=(uint16_t)pwm;
         }
         return 0;
 }