Added speed regulation.
authorMartin Prudek <prudemar@fel.cvut.cz>
Sat, 16 May 2015 14:40:50 +0000 (16:40 +0200)
committerMartin Prudek <prudemar@fel.cvut.cz>
Sat, 16 May 2015 14:40:50 +0000 (16:40 +0200)
pmsm-control/test_sw/cmd_proc.c
pmsm-control/test_sw/main_pmsm.c
pmsm-control/test_sw/pmsm_state.h

index 69c17b8..640002f 100644 (file)
@@ -19,9 +19,10 @@ static void printHelp(){
        puts("stop - Vypne komutaci, pwm a rizeni.");
        puts("0 - Vypne komutaci, pwm a rizeni.");
        puts("ga:[hodnota] - Zapne rizeni na zvolenou absolutni pozici.");
-       puts("print - Zapne nebo vypne pravidelne vypisovani hodnot.");
        puts("duty:[hodnota] - Nastavi pevnou sirku plneni.");
+       puts("spd:[hodnota] - Zapne rizeni na danou rychlost.");
 
+       puts("print - Zapne nebo vypne pravidelne vypisovani hodnot.");
        puts("help - Vypne vypisovani hodnot a zobrazi tuto napovedu.");
        puts("exit - Bezpecne ukonci program.");
 }
@@ -69,11 +70,13 @@ static void stop(struct rpi_state* state){
  */
 static void dutySet(struct rpi_state* state, int duty){
        sem_wait(&state->thd_par_sem);
-       if (duty>512) duty=512;
-       if (duty<-512) duty=-512;/*paranoia*/
+       if (duty>MAX_DUTY) duty=MAX_DUTY;
+       if (duty<-MAX_DUTY) duty=-MAX_DUTY;/*paranoia*/
        state->duty=duty;
-       state->commutate=1;
        state->pos_reg_ena=0;
+       state->spd_reg_ena=0;
+       state->commutate=1;
+       printf("duty=");
        sem_post(&state->thd_par_sem);
 }
 
@@ -83,6 +86,7 @@ static void dutySet(struct rpi_state* state, int duty){
  */
 static void goAbsolute(struct rpi_state* state, int pos){
        sem_wait(&state->thd_par_sem);
+       state->spd_reg_ena=0;
        state->pos_reg_ena=1;
        state->commutate=1;
        state->desired_pos=pos;
@@ -106,6 +110,20 @@ static void exitApp(struct rpi_state* state){
        /* Note: atexit() is set before*/
        exit(0);
 }
+/*
+ * \brief
+ * Set speed.
+ */
+static void setSpeed(struct rpi_state* state, int speed){
+       sem_wait(&state->thd_par_sem);
+       if (speed>MAX_SPEED) speed=MAX_SPEED;
+       if (speed<-MAX_SPEED) speed=-MAX_SPEED;/*paranoia*/
+       state->pos_reg_ena=0;
+       state->spd_reg_ena=1;
+       state->commutate=1;
+       state->desired_spd=speed;
+       sem_post(&state->thd_par_sem);
+}
 
 /**
  * \brief
@@ -139,6 +157,8 @@ void poll_cmd(struct rpi_state* state){
                         changePrint();
                 }else if (!strcmp(cmd,"exit")){
                         exitApp(state);
+                }else if (!strcmp(cmd,"spd")){
+                        setSpeed(state, val);
                 }
         }
 
@@ -187,6 +207,7 @@ void printData(struct rpi_state* state){
        printf("\npozice=%ld\n",data_p.pozice);
        printf("rychlost=%d\n",s.speed);
        printf("chtena pozice=%d\n",s.desired_pos);
+       printf("chtena rychlost=%d\n",s.desired_spd);
        printf("transfer count=%u\n",s.tf_count);
        printf("raw_pozice=%u\n",data_p.pozice_raw);
        printf("raw_pozice last12=%u\n",(data_p.pozice_raw&0x0FFF));
index 48fcf1e..dd2372d 100644 (file)
@@ -25,9 +25,9 @@
 #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.7
 
 #define PRIOR_KERN     50
 #define PRIOR_HIGH     49
@@ -539,7 +539,7 @@ inline void pos_pid(){
  */
 inline void spd_pid(){
        int duty_tmp;
-       duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
+       duty_tmp = PID_P_S*(rps.desired_spd - rps.speed);
        if (duty_tmp>MAX_DUTY){
                rps.duty=MAX_DUTY;
        }else if (duty_tmp<-MAX_DUTY){
@@ -601,10 +601,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);*/
index 8c80265..3cd648f 100644 (file)
@@ -4,7 +4,9 @@
 #include <stdint.h>
 #include <semaphore.h>
 
-#define OLD_POS_NUM 50
+#define OLD_POS_NUM    50
+#define MAX_DUTY       170
+#define MAX_SPEED      (7*OLD_POS_NUM)
 
 struct rpi_in;