]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control.git/blobdiff - pmsm-control/test_sw/main_pmsm.c
Controllers moved to separate file.
[fpga/rpi-motor-control.git] / pmsm-control / test_sw / main_pmsm.c
index 65df8f1d4df4644ca96f724e8054d4287548215e..84b1cdde31fc44fb73dee0859a3fbcde02606636 100644 (file)
 #include "pxmc_sin_fixed.h"    /*to test sin commutation */
 #include "pmsm_state.h"
 #include "cmd_proc.h"
+#include "controllers.h"
+
 
 
-#define PID_P          0.3
-#define PID_P_S                0.9     /*2.3 kmita*/ /*1.4 vhodne jen pro P regulator*/
-#define PID_I_S                0.01
 
 #define PRIOR_KERN     50
 #define PRIOR_HIGH     49
@@ -43,6 +42,7 @@
 
 struct rpi_in data;
 struct rpi_state rps={
+       //.MAX_DUTY=170,
        .spi_dat=&data,
        .test=0,
        .pwm1=0,.pwm2=0, .pwm3=0,
@@ -60,7 +60,8 @@ struct rpi_state rps={
        .spd_err_sum=0,
        .log_col_count=0,       /* pocet radku zaznamu */
        .log_col=0,
-       .doLogs=0
+       .doLogs=0,
+       .alpha_offset=960
 };
 
 /**
@@ -126,9 +127,18 @@ void makeLog(){
        }
        rps.logs[0][rps.log_col]=(int)rps.tf_count;
        rps.logs[1][rps.log_col]=(int)rps.spi_dat->pozice;
+
        rps.logs[2][rps.log_col]=(int)rps.pwm1;
        rps.logs[3][rps.log_col]=(int)rps.pwm2;
        rps.logs[4][rps.log_col]=(int)rps.pwm3;
+       rps.logs[5][rps.log_col]=rps.duty;
+
+       rps.logs[6][rps.log_col]=rps.desired_spd;
+       rps.logs[7][rps.log_col]=rps.speed;
+
+       rps.logs[8][rps.log_col]=(int)(rps.spi_dat->ch1/rps.spi_dat->adc_m_count);
+       rps.logs[9][rps.log_col]=(int)(rps.spi_dat->ch2/rps.spi_dat->adc_m_count);
+       rps.logs[10][rps.log_col]=(int)(rps.spi_dat->ch0/rps.spi_dat->adc_m_count);
 
        rps.log_col++;
      /*
@@ -309,7 +319,7 @@ void inv_trans_comm_2(int duty){
        int32_t u1,u2,u3;
        pos=rps.index_dist;
 
-       pos+=960; /*zarovnani faze 'a' s osou 'alpha'*/
+       pos+=rps.alpha_offset; /*zarovnani faze 'a' s osou 'alpha'*/
 
        /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/
        pos*=4294967;
@@ -356,6 +366,7 @@ inline uint16_t mult_cap(int32_t s,int d){
        }
        return res;
 }
+
 inline
 int sin_commutator(int duty){
        #define DEGREE_60        715827883
@@ -568,43 +579,7 @@ void comIndDist(){
                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 pos_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
- * Very simple PID regulator.
- * Now only with P-part so that the error doesnt go to zero.
- * FIXME: make better
- */
-inline void spd_pid(){
-       int duty_tmp;
-       int error;
-       error=rps.desired_spd - rps.speed;
-       rps.spd_err_sum+=error;
-       duty_tmp = PID_P_S*error+PID_I_S*rps.spd_err_sum;
-       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
@@ -661,9 +636,9 @@ void * read_data(void* param){
 
                        /* pocitame sirku plneni podle potreb rizeni*/
                        if (rps.pos_reg_ena){           /*pozicni rizeni*/
-                               pos_pid();
+                               pos_pid(&rps);
                        }else if(rps.spd_reg_ena){      /*rizeni na rychlost*/
-                               spd_pid();
+                               spd_pid(&rps);
                        }
 
                        /* sirka plneni prepoctena na jednotlive pwm */