]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control.git/blobdiff - pmsm-control/test_sw/main_pmsm.c
Added detection os index signal lost.
[fpga/rpi-motor-control.git] / pmsm-control / test_sw / main_pmsm.c
index 7e29253afa3def8717b91e810172d6023a60fb8e..6e539b2d6fbd256065029b1a8d085c2bf97e90ab 100644 (file)
 #include <sched.h>     /*sheduler*/
 #include <unistd.h>    /*usleep*/
 #include <pthread.h>   /*threads*/
+#include <time.h>      /*nanosleep*/
 
 #include "rpin.h"      /*gpclk*/
 #include "rp_spi.h"    /*spi*/
 #include "misc.h"      /*structure for priorities*/
+#include "pxmc_sin_fixed.h"    /*to test sin commutation */
+
 
 
 #define PRUM_PROUD     2061
 #define PRUM_SOUC      6183
+#define MAX_DUTY       128
+#define PID_P          0.1
 
 #define PRIOR_KERN     50
 #define PRIOR_HIGH     49
 #define PRIOR_LOW      20
 
 #define THREAD_SHARED  0
-#define INIT_VALUE     0       /*init value for semaphor*/
+#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. */
+
+
 
-struct sigaction sighnd; /*struktura pro signal handler*/
 struct rpi_in data;
 struct rpi_state{
        uint8_t test;
        uint16_t pwm1, pwm2, pwm3;
+       uint16_t t_pwm1, t_pwm2, t_pwm3;
        char commutate;
-       int simple_hall_duty;
+       int duty;                       /* duty cycle of pwm */
        uint16_t index_dist;            /* distance to index position */
        unsigned char index_ok;
        uint32_t tf_count;              /*number of transfer*/
+       int desired_pos;                /* desired position */
 }rps;
 
 
@@ -65,13 +90,12 @@ inline void clk_disable(){
 /**
  * \brief Signal handler pro Ctrl+C
  */
-void sighnd_fnc(){
+void appl_stop(){
        spi_disable();
        clk_disable();
        /*muzeme zavrit semafor*/
        sem_destroy(&thd_par_sem);
         printf("\nprogram bezpecne ukoncen\n");
-        exit(0);
 }
 
 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
@@ -117,23 +141,25 @@ void printData(){
        }
        puts("");
        printf("\npozice=%d\n",(int32_t)data_p.pozice);
+       printf("chtena pozice=%d\n",s.desired_pos);
        printf("transfer count=%u\n",s.tf_count);
-       printf("raw_pozice=%d\n",(int32_t)data_p.pozice_raw);
+       printf("raw_pozice=%u\n",data_p.pozice_raw);
        printf("raw_pozice last12=%u\n",(data_p.pozice_raw&0x0FFF));
        printf("index position=%u\n",data_p.index_position);
-       printf("distance to index=%u\n",s.index_dist);
        printf("hal1=%d, hal2=%d, hal3=%d\n",data_p.hal1,data_p.hal2,data_p.hal3);
        printf("en1=%d, en2=%d, en3=%d (Last sent)\n",!!(0x40&s.test),!!(0x20&s.test),!!(0x10&s.test));
        printf("shdn1=%d, shdn2=%d, shdn3=%d (L.s.)\n",!!(0x08&s.test),!!(0x04&s.test),!!(0x02&s.test));
        printf("PWM1=%u(L.s.)\n",s.pwm1);
        printf("PWM2=%u(L.s.)\n",s.pwm2);
        printf("PWM3=%u(L.s.)\n",s.pwm3);
+       printf("distance to index=%u\n",s.index_dist);
+       printf("T_PWM1=%u T_PWM2=%u T_PWM3=%u\n",s.t_pwm1,s.t_pwm2, s.t_pwm3);
        printf("Pocet namerenych proudu=%u\n",data_p.adc_m_count);
        printf("(pwm1) (ch1)=%d (avg=%4.0f) (%2.2f%%)\n",data_p.ch1,cur1,diff_p(cur1));
        printf("(pwm2) (ch2)=%d (avg=%4.0f)(%2.2f%%)\n",data_p.ch2,cur2,diff_p(cur2));
        printf("(pwm3) (ch0)=%d (avg=%4.0f)(%2.2f%%)\n",data_p.ch0,cur0,diff_p(cur0));
        printf("soucet prumeru=%5.0f (%2.2f%%)\n",cur0+cur1+cur2,diff_s(cur0+cur1+cur2));
-       printf("duty=%d\n",s.simple_hall_duty);
+       printf("duty=%d\n",s.duty);
        if (s.index_ok) printf("index ok\n");
        if (s.commutate) printf("commutation in progress\n");
 }
@@ -162,36 +188,38 @@ void prepare_tx(uint8_t * tx){
         * bit 89 - shutdown3
         *      .
         *      .
+        *      Unused
+        *      .
         *      .
-        * bits 66 .. 56 - match PWM1
-        * bits 55 .. 45 - match PWM2
-        * bit 11,12 - Unused
-        * bits 42 .. 32  - match PWM3
+        * bits 47 .. 32 - match PWM1
+        * bits 31 .. 16 - match PWM2
+        * bits 15 .. 0  - match PWM3
         */
 
 
        uint16_t tmp;
 
-       /* keep the cap*/
+       /* keep the 11-bit cap*/
+
        if (rps.pwm1>2047) rps.pwm1=2047;
        if (rps.pwm2>2047) rps.pwm2=2047;
        if (rps.pwm3>2047) rps.pwm3=2047;
 
        tx[0]=rps.test; /*bit 94 - enable PWM1*/
 
+       /*now we have to switch the bytes due to endianess */
+       /* ARMv6 & ARMv7 instructions are little endian */
        /*pwm1*/
-       tx[7]=(tx[7] & 0xF8) | (0x07 & ((uint8_t*)&rps.pwm1)[1]); /*MSB*/
-       tx[8]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
+       tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
+       tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
 
        /*pwm2*/
-       tmp=rps.pwm2;
-       tmp<<=5;
-       tx[9]=((uint8_t*)&tmp)[1]; /*MSB*/
-       tx[10]=(tx[10] & 0x1F) | (0xE0 & ((uint8_t*)&tmp)[0]); /*LSB*/
+       tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
+       tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
 
        /*pwm3*/
-       tx[10]=(tx[10] & 0xF8) | (0x07 & ((uint8_t*)&rps.pwm3)[1]); /*MSB*/
-       tx[11]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
+       tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
+       tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
 
 
 }
@@ -199,19 +227,141 @@ void prepare_tx(uint8_t * tx){
  * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
  */
 void * pos_monitor(void* param){
-       set_priority(param);            /*set priority*/
        while(1){
                printData();
                usleep(1000000);        /*1 Hz*/
        }
        return (void*)0;
 }
+
+inline
+int sin_commutator(int duty){
+       #define DEGREE_60        715827883
+       #define DEGREE_120      1431655765
+       #define DEGREE_180      2147483648
+       #define DEGREE_240      2863311531
+       #define DEGREE_300      3579139413
+       uint32_t j,pos;
+       int32_t sin;
+       uint16_t pwm;
+       pos=rps.index_dist*4294967;
+       if (duty>=0){   /*clockwise rotation*/
+               /* 1st phase */
+               sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
+                pwm=0;
+                for(j=0;j!=11;j++){
+                       /* multiplicate as if maximum sinus value was unity */
+                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
+                }
+                rps.pwm1=pwm;
+
+                /* 2nd phase */
+                sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
+                pwm=0;
+                for(j=0;j!=11;j++){
+                       /* multiplicate as if maximum sinus value was unity */
+                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
+                }
+                rps.pwm2=pwm;
+
+                /* 3rd phase */
+                sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
+                pwm=0;
+                for(j=0;j!=11;j++){
+                       /* multiplicate as if maximum sinus value was unity */
+                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
+                }
+                rps.pwm3=pwm;
+        }else{
+               duty=-duty;
+
+               /* 1st phase */
+               sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
+                pwm=0;
+                for(j=0;j!=11;j++){
+                       /* multiplicate as if maximum sinus value was unity */
+                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
+                }
+                rps.pwm1=pwm;
+
+                /* 2nd phase */
+                sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
+                pwm=0;
+                for(j=0;j!=11;j++){
+                       /* multiplicate as if maximum sinus value was unity */
+                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
+                }
+                rps.pwm2=pwm;
+
+                /* 3rd phase */
+                sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
+                pwm=0;
+                for(j=0;j!=11;j++){
+                       /* multiplicate as if maximum sinus value was unity */
+                        pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
+                }
+                rps.pwm3=pwm;
+        }
+        return 0;
+}
+/*
+ * \brief
+ * Test function to be placed in controll loop.
+ * Switches PWM's at point where they produce same force.
+ * This points are found thanks to IRC position,
+ */
+inline
+void simple_ind_dist_commutator(int duty){
+       if (duty>=0){ /* clockwise - so that position increase */
+               /* pwm3 */
+               if ((rps.index_dist>=45 && rps.index_dist<=373) ||
+               (rps.index_dist>=1048 && rps.index_dist<=1377)){
+                       rps.pwm1=0;
+                       rps.pwm2=0;
+                       rps.pwm3=duty;
+                       /* pwm1 */
+               }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
+               (rps.index_dist>=1377 && rps.index_dist<=1711)){
+                       rps.pwm1=duty;
+                       rps.pwm2=0;
+                       rps.pwm3=0;
+                       /* pwm2 */
+               }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
+               (rps.index_dist>=711 && rps.index_dist<=1048) ||
+               (rps.index_dist>=1711 && rps.index_dist<=1999)){
+                       rps.pwm1=0;
+                       rps.pwm2=duty;
+                       rps.pwm3=0;
+               }
+       }else{  /*counter-clockwise - position decrease */
+               /* pwm3 */
+               if ((rps.index_dist>=544 && rps.index_dist<=881) ||
+               (rps.index_dist>=1544 && rps.index_dist<=1878)){
+                       rps.pwm1=0;
+                       rps.pwm2=0;
+                       rps.pwm3=-duty;
+                       /* pwm1 */
+               }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
+               (rps.index_dist>=881 && rps.index_dist<=1210) ||
+               (rps.index_dist>=1878 && rps.index_dist<=1999)){
+                       rps.pwm1=-duty;
+                       rps.pwm2=0;
+                       rps.pwm3=0;
+                       /* pwm2 */
+               }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
+               (rps.index_dist>=1210 && rps.index_dist<=1544)){
+                       rps.pwm1=0;
+                       rps.pwm2=-duty;
+                       rps.pwm3=0;
+               }
+       }
+}
 /*
  * \brief
  * Test function to be placed in controll loop.
  * Switches PWM's at point where they produce same force
  */
-inline void simple_hall_commutator(struct rpi_in data, int duty){
+inline void simple_hall_commutator(int duty){
        if (duty>=0){ /* clockwise - so that position increase */
                /* pwm3 */
                if (data.hal2 && !data.hal3){
@@ -249,27 +399,105 @@ inline void simple_hall_commutator(struct rpi_in data, int duty){
        }
 }
 /**
- * Funkce pravidelne vycita data z motoru
+ * \brief
+ * Computation of distance to index.
+ *
+ * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
+ * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
+ *     to je 3999 bodu
+ *     =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
+ *             signalu indexu
  */
-inline void comIndDist(){
-       rps.index_dist=0x0FFF & (data.pozice_raw - data.index_position);
-       /*
-        * if distance is bigger than 2047, the distance underflown
-        * -> if 12th bit is set, substract 2096
-        */
-       rps.index_dist-=((rps.index_dist & 0x0800)>>11)*2096;
+void comIndDist(){
+       uint16_t pos = 0x0FFF & data.pozice_raw;
+       uint16_t dist;
+       uint16_t index = data.index_position;
+
+       if (index<1999){                /*index e<0,1998> */
+               if (pos<index){                 /*pozice e<0,index-1> */
+                       /*proti smeru h.r. od indexu*/
+                       dist=pos+2000-index;
+               }else if (pos<=index+1999){     /*pozice e<index,index+1999> */
+                       /*po smeru h.r. od indexu*/
+                       dist=pos-index;
+               }else if (pos<index+2096){      /*pozice e<index+2000,index+2095> */
+                       goto index_lost;
+               }else{                          /*pozice e<index+2096,4095> */
+                       /*proti smeru h.r. od indexu - podtecena pozice*/
+                       dist=pos-index-2096;
+               }
+       }else if (index<=2096){         /*index e<1999,2096>*/
+               if (pos<index-1999){            /*pozice e<0,index-2000> */
+                       goto index_lost;
+               }else if (pos<index){           /*pozice e<index-1999,index-1> */
+                       /*proti smeru h.r. od indexu*/
+                       dist=pos+2000-index;
+               }else if (pos<=index+1999){     /*pozice e<index,index+1999> */
+                       /*po smeru h.r. od indexu*/
+                       dist=pos-index;
+               }else {                         /*pozice e<index+2000,4095> */
+                       goto index_lost;
+               }
+       }else{                          /*index e<2097,4095> */
+               if (pos<=index-2097){           /*pozice e<0,index-2097> */
+                       /*po smeru h.r. od indexu - pretecena pozice*/
+                       dist=4096+pos-index;
+               }else if (pos<index-1999){      /*pozice e<index-2096,index-2000> */
+                       goto index_lost;
+               }else if (pos<index){           /*pozice e<index-1999,index-1> */
+                       /*proti smeru h.r. od indexu*/
+                       dist=pos+2000-index;
+               }else{                          /*pozice e<index,4095> */
+                       /*po smeru h.r. od indexu*/
+                       dist=pos-index;
+               }
+       }
+
+       rps.index_dist = dist;
+       return;
+
+       index_lost:
+               rps.index_ok=0;
+               return;
 }
+/*
+ * \brief
+ * Very simple PID regulator.
+ * Now only with P-part so that the error doesnt go to zero.
+ * TODO: add anti-wind up and I and D parts
+ */
+inline void pid(){
+       int duty_tmp;
+       duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
+       if (duty_tmp>MAX_DUTY){
+               rps.duty=MAX_DUTY;
+       }else if (duty_tmp<-MAX_DUTY){
+               rps.duty=-MAX_DUTY;
+       }else{
+               rps.duty = duty_tmp;
+       }
+}
+/*
+ * \brief
+ * Feedback loop.
+ */
 void * read_data(void* param){
        int i;
        struct rpi_in pocatek;
+       struct timespec t;
+       int interval = 1000000; /* 1ms ~ 1kHz*/
        uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
        char first=1;
        uint16_t last_index;                            /*we have index up-to date*/
-       set_priority(param);                            /*set priority*/
        pocatek = spi_read(tx);
+       clock_gettime(CLOCK_MONOTONIC ,&t);
+       /* start after one second */
+       t.tv_sec++;
                while(1){
-                       prepare_tx(tx);                 /*save the data to send*/
+                       /* wait until next shot */
+                       clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
                        sem_wait(&thd_par_sem);         /*---take semaphore---*/
+                       prepare_tx(tx);                 /*save the data to send*/
                        data = spi_read(tx);            /*exchange data*/
                        /*subtract initiate postion */
                        rps.tf_count++;
@@ -283,55 +511,33 @@ void * read_data(void* param){
                                        rps.index_ok=1;
                                }
                        }
+                       pid();
                        if (rps.index_ok && rps.commutate){
-                               simple_hall_commutator(data,rps.simple_hall_duty);
+                               /*simple_ind_dist_commutator(rps.duty);*/
+                               sin_commutator(rps.duty);
                        }else if(!rps.index_ok && rps.commutate){
-                               simple_hall_commutator(data,rps.simple_hall_duty);
+                               simple_hall_commutator(rps.duty);
                        }
                        sem_post(&thd_par_sem);         /*--post semaphore---*/
-                       usleep(1000);                           /*1kHz*/
+
+                       /* calculate next shot */
+                       t.tv_nsec += interval;
+
+                       while (t.tv_nsec >= NSEC_PER_SEC) {
+                               t.tv_nsec -= NSEC_PER_SEC;
+                               t.tv_sec++;
+                       }
+
                }
 }
 
+
 /**
- * \brief Main function.
+ * \brief
+ * Commands detection.
  */
-
-int main(){
+void poll_cmd(){
        unsigned int tmp;
-
-       /*nastaveni priorit vlaken*/
-       struct thread_param tsp;
-       tsp.sch_policy = SCHED_FIFO;
-
-       /*nastaveni signalu pro vypnuti pomoci Ctrl+C*/
-       sighnd.sa_handler=&sighnd_fnc;
-       sigaction(SIGINT, &sighnd, NULL );
-
-       clk_init();             /* inicializace gpio hodin */
-       spi_init();             /* iniicializace spi*/
-
-       /*semafor pro detekci zpracovani parametru vlaken*/
-       sem_init(&thd_par_sem,THREAD_SHARED,INIT_VALUE);
-
-       /*vlakna*/
-       pthread_t tid;                  /*identifikator vlakna*/
-       pthread_attr_t attr;            /*atributy vlakna*/
-       pthread_attr_init(&attr);       /*inicializuj implicitni atributy*/
-
-
-
-       /*ziskavani dat z motoru*//*vysoka priorita*/
-       tsp.sch_prior = PRIOR_HIGH;
-       pthread_create(&tid, &attr, read_data, (void*)&tsp);
-
-       /*vypisovani lokalni pozice*//*nizka priorita*/
-       tsp.sch_prior = PRIOR_LOW;
-       sem_wait(&thd_par_sem);
-       pthread_create(&tid, &attr, pos_monitor, (void*)&tsp);
-
-
-
        /*
         * Note:
         * pri pouziti scanf("%u",&simple_hall_duty); dochazelo
@@ -377,7 +583,13 @@ int main(){
                case 6:
                        scanf("%d",&tmp);
                        sem_wait(&thd_par_sem);
-                       rps.simple_hall_duty=tmp;
+                       rps.duty=tmp;
+                       sem_post(&thd_par_sem);
+                       break;
+               case 7:
+                       scanf("%d",&tmp);
+                       sem_wait(&thd_par_sem);
+                       rps.desired_pos=tmp;
                        sem_post(&thd_par_sem);
                        break;
 
@@ -386,5 +598,31 @@ int main(){
                }
 
        }
+       return ;
+}
+/**
+ * \brief Main function.
+ */
+
+int main(){
+       pthread_t base_thread_id;
+       clk_init();             /* inicializace gpio hodin */
+       spi_init();             /* iniicializace spi*/
+
+       /*semafor pro detekci zpracovani parametru vlaken*/
+       sem_init(&thd_par_sem,THREAD_SHARED,INIT_VALUE);
+       setup_environment();
+
+       base_thread_id=pthread_self();
+
+       /*main control loop*/
+       create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
+
+       /*monitor of current state*/
+       create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
+
+       /*wait for commands*/
+       poll_cmd();
+
        return 0;
 }