]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control.git/blobdiff - pmsm-control/test_sw/main_pmsm.c
Speed regulation with zero error.
[fpga/rpi-motor-control.git] / pmsm-control / test_sw / main_pmsm.c
index 8b65c74d3e74254a67c2260d7ceffb41344455a9..3d13ff28a484b11ea43c62f6d61b7287d26ff4e5 100644 (file)
 #include "pmsm_state.h"
 #include "cmd_proc.h"
 
-#define MAX_DUTY       170
+
 #define PID_P          0.3
-#define PID_P_S                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
@@ -54,7 +55,9 @@ struct rpi_state rps={
        .desired_pos=0,         /* desired position */
        .pos_reg_ena=0,
        .desired_spd=0,
-       .spd_reg_ena=0
+       .spd_reg_ena=0,
+       .old_pos={0},
+       .spd_err_sum=0
 };
 
 /**
@@ -180,8 +183,7 @@ void appl_stop(){
 }
 
 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
-       data->pozice_raw=data->pozice;
-       data->pozice-=offset->pozice;
+       data->pozice=data->pozice_raw-offset->pozice_raw;
        return;
 }
 /*
@@ -539,8 +541,10 @@ inline void pos_pid(){
  */
 inline void spd_pid(){
        int duty_tmp;
-       signed long int speed=rps.spi_dat->pozice-rps.old_pos;
-       duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
+       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){
@@ -549,6 +553,17 @@ inline void spd_pid(){
                rps.duty = duty_tmp;
        }
 }
+
+/*
+ * \brief
+ * Computate speed.
+ */
+void compSpeed(){
+       signed long int spd;
+       spd=rps.spi_dat->pozice-rps.old_pos[rps.tf_count%OLD_POS_NUM];
+       rps.speed=(int32_t)spd;
+}
+
 /*
  * \brief
  * Feedback loop.
@@ -571,12 +586,14 @@ void * read_data(void* param){
                        clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
                        sem_wait(&rps.thd_par_sem);             /*---take semaphore---*/
 
-                       rps.old_pos=rps.spi_dat->pozice;        /*old position*/
+                       /*old positions*/
+                       rps.old_pos[rps.tf_count%OLD_POS_NUM]=rps.spi_dat->pozice;
                        prepare_tx(tx);                 /*save the data to send*/
                        data = spi_read(tx);            /*exchange data*/
                        /*subtract initiate postion */
                        rps.tf_count++;
                        substractOffset(&data,&pocatek);
+                       compSpeed();                    /*spocita rychlost*/
 
                        if (!rps.index_ok){
                                if (first){
@@ -589,10 +606,14 @@ void * read_data(void* param){
                        }else{ /*index je v poradku*/
                                comIndDist();           /*vypocet vzdalenosti indexu*/
                        }
+
                        /* pocitame sirku plneni podle potreb rizeni*/
-                       if (rps.pos_reg_ena){
+                       if (rps.pos_reg_ena){           /*pozicni rizeni*/
                                pos_pid();
+                       }else if(rps.spd_reg_ena){      /*rizeni na rychlost*/
+                               spd_pid();
                        }
+
                        /* sirka plneni prepoctena na jednotlive pwm */
                        if (rps.index_ok && rps.commutate){
                                /*simple_ind_dist_commutator(rps.duty);*/
@@ -616,7 +637,6 @@ void * read_data(void* param){
 }
 
 
-
 /**
  * \brief Main function.
  */