]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control.git/commitdiff
Commutation function now implemented with use of polymorphism.
authorMartin Prudek <prudemar@fel.cvut.cz>
Wed, 20 May 2015 19:16:00 +0000 (21:16 +0200)
committerMartin Prudek <prudemar@fel.cvut.cz>
Wed, 20 May 2015 19:16:00 +0000 (21:16 +0200)
pmsm-control/test_sw/Makefile
pmsm-control/test_sw/cmd_proc.c
pmsm-control/test_sw/commutators.c
pmsm-control/test_sw/commutators.h
pmsm-control/test_sw/comp.c
pmsm-control/test_sw/main_pmsm.c
pmsm-control/test_sw/pmsm_state.c [new file with mode: 0644]
pmsm-control/test_sw/pmsm_state.h

index 5a8aa24d1f6fdb9bb13eabc664a0aaa0829c44a2..8684cd795c113d1ceea2684237fbc70f232df8fe 100644 (file)
@@ -49,5 +49,5 @@ blikej: howto_gpio.o rpi_hw.o
 spi: rp_spi.o
        gcc -o spi rp_spi.c
 #pro rpi
-pmsm: main_pmsm.o rp_spi.o rpi_hw.o misc.o pxmc_sin_fixtab.o cmd_proc.o controllers.o commutators.o comp.o logs.o
-       gcc -o pmsm_controll main_pmsm.o rp_spi.o rpi_hw.o  misc.o pxmc_sin_fixtab.o cmd_proc.o controllers.o commutators.o comp.o logs.o -lpthread -lrt
+pmsm: main_pmsm.o rp_spi.o rpi_hw.o misc.o pxmc_sin_fixtab.o cmd_proc.o controllers.o commutators.o comp.o logs.o pmsm_state.o
+       gcc -o pmsm_controll main_pmsm.o rp_spi.o rpi_hw.o  misc.o pxmc_sin_fixtab.o cmd_proc.o controllers.o commutators.o comp.o logs.o pmsm_state.o -lpthread -lrt
index 8f49e91595b677a47377f8183a1844ab0adc2c7d..bb3be078ba6978eee202e7c7fa8bc01328b1974d 100644 (file)
@@ -61,7 +61,7 @@ static void start(struct rpi_state* state){
  */
 static void stop(struct rpi_state* state){
        sem_wait(&state->thd_par_sem);
-       state->commutate=0;
+       setCommutationOff(state);
        state->pos_reg_ena=0;
        state->spd_reg_ena=0;
        state->duty=0;
@@ -82,7 +82,7 @@ static void dutySet(struct rpi_state* state, int duty){
        state->duty=duty;
        state->pos_reg_ena=0;
        state->spd_reg_ena=0;
-       state->commutate=1;
+       setCommutationOn(state);
        sem_post(&state->thd_par_sem);
 }
 
@@ -94,7 +94,7 @@ 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;
+       setCommutationOn(state);
        state->desired_pos=pos;
        sem_post(&state->thd_par_sem);
 }
@@ -126,7 +126,7 @@ static void setSpeed(struct rpi_state* state, int speed){
        sem_wait(&state->thd_par_sem);
        state->pos_reg_ena=0;
        state->spd_reg_ena=1;
-       state->commutate=1;
+       setCommutationOn(state);
        state->desired_spd=speed;
        sem_post(&state->thd_par_sem);
 }
index 1a6372417be911a9f57110377aabeb440a65e1e9..f20824e66cb44f8d3c3e3c5d9229a4c3b2d5a931 100644 (file)
@@ -6,6 +6,15 @@
 #include "commutators.h"
 #include "pxmc_sin_fixed.h"    /*sinus function*/
 
+
+/*
+ * \brief
+ * No commutation.
+ */
+void zero_commutator(struct rpi_state* this){
+       return;
+}
+
 /*
  * \brief
  * Computes minimum value of three numbers.
index 59905d33d62d16de18ae5ef14afa0004e50109f2..ca4c59b97fd8b480737de4190d0046dc13bee134 100644 (file)
@@ -43,5 +43,11 @@ void simple_ind_dist_commutator(struct rpi_state*);
  */
 void simple_hall_commutator(struct rpi_state*);
 
+/*
+ * \brief
+ * No commutation.
+ */
+void zero_commutator(struct rpi_state*);
+
 
 #endif /*COMMUTATORS*/
index 49d354659af0054a91f8f0d0679b38bc4f58b462..764726acc4ae46c09a1dc2848e1447517988a009 100644 (file)
@@ -92,7 +92,7 @@ void comIndDist(struct rpi_state* this){
        return;
 
        index_lost:
-               this->index_ok=0;
+               setIndexLost(this);
                return;
 }
 
index 2eb92627ea55c8174b4e7661b201c86c2e0f71cc..ac3563d59b578396991ba6f0088415bb28668929 100644 (file)
@@ -60,7 +60,8 @@ struct rpi_state rps={
        .log_col_count=0,       /* pocet radku zaznamu */
        .log_col=0,
        .doLogs=0,
-       .alpha_offset=960
+       .alpha_offset=960,
+       .main_commutator=zero_commutator        /*komutace vypnuta*/
 };
 
 
@@ -112,7 +113,7 @@ void * pos_monitor(void* param){
  * Feedback loop.
  * TODO: replace bunch of 'IFs' with Object-like pattern
  */
-void * read_data(void* param){
+void * control_loop(void* param){
        int i;
        struct rpi_in pocatek;
        struct rpi_state poc={
@@ -135,10 +136,9 @@ void * read_data(void* param){
 
                        /*old positions*/
                        rps.old_pos[rps.tf_count%OLD_POS_NUM]=rps.spi_dat->pozice;
-                       spi_read(&rps);         /*exchange data*/
-                       /*subtract initiate postion */
+                       spi_read(&rps);                         /*exchange data*/
                        rps.tf_count++;
-                       substractOffset(&data,poc.spi_dat);
+                       substractOffset(&data,poc.spi_dat);     /*subtract initiate postion */
                        compSpeed(&rps);                        /*spocita rychlost*/
 
                        if (!rps.index_ok){
@@ -146,7 +146,7 @@ void * read_data(void* param){
                                        last_index=data.index_position;
                                        first=0;
                                }else if (last_index!=data.index_position){
-                                       rps.index_ok=1;
+                                       setIndexOK(&rps);
                                        comIndDist(&rps);       /*vypocet vzdalenosti indexu*/
                                }
                        }else{ /*index je v poradku*/
@@ -160,15 +160,8 @@ void * read_data(void* param){
                                spd_pid(&rps);
                        }
 
-                       /* sirka plneni prepoctena na jednotlive pwm */
-                       if (rps.index_ok && rps.commutate){
-                               /*simple_ind_dist_commutator(rps.duty);*/
-                               /*sin_commutator(rps.duty);*/
-                               inv_trans_comm(&rps);
-                               inv_trans_comm_2(&rps);
-                       }else if(!rps.index_ok && rps.commutate){
-                               simple_hall_commutator(&rps);
-                       }
+                       /* sirku plneni prepocteme na jednotlive pwm */
+                       rps.main_commutator(&rps);
 
                        /*zalogujeme hodnoty*/
                        if (rps.doLogs && !(rps.tf_count%LOG_PERIOD)){
@@ -203,7 +196,7 @@ int main(){
        base_thread_id=pthread_self();
 
        /*main control loop*/
-       create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
+       create_rt_task(&base_thread_id,PRIOR_HIGH,control_loop,NULL);
 
        /*monitor of current state*/
        create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
diff --git a/pmsm-control/test_sw/pmsm_state.c b/pmsm-control/test_sw/pmsm_state.c
new file mode 100644 (file)
index 0000000..9721490
--- /dev/null
@@ -0,0 +1,38 @@
+#include "pmsm_state.h"
+#include "commutators.h"
+
+/**
+ * Index Lost.
+ */
+void setIndexLost(struct rpi_state* this){
+       this->index_ok=0;
+       this->main_commutator=simple_hall_commutator;
+}
+
+/**
+ * Index OK.
+ */
+void setIndexOK(struct rpi_state* this){
+       this->index_ok=1;
+       this->main_commutator=inv_trans_comm_2;
+}
+
+/**
+ * Turn commutation on.
+ */
+void setCommutationOn(struct rpi_state* this){
+       this->commutate=1;
+       if (this->index_ok){
+               this->main_commutator=inv_trans_comm_2;
+       }else{
+               this->main_commutator=simple_hall_commutator;
+       }
+}
+
+/**
+ * Turn commutation off.
+ */
+void setCommutationOff(struct rpi_state* this){
+       this->commutate=0;
+       this->main_commutator=zero_commutator;
+}
index 5d02be26b62328c7503cdb75fb2f10a4ca5d7de6..376e2f8482e779a75f22960a6583a81dd5556952 100644 (file)
@@ -46,8 +46,29 @@ struct rpi_state{
        int log_col_count;              /* number of log columns */
        int log_col;                    /* current colimn */
        char doLogs;                    /* schall we make logs? */
+
+       void (*main_commutator)(struct rpi_state*);     /* primarni komutator */
+       void (*main_controller)(struct rpi_state*);     /* primarni regulator */
 };
 
-void freeLogs();
+/**
+ * Index OK.
+ */
+void setIndexOK(struct rpi_state*);
+
+/**
+ * Index Lost.
+ */
+void setIndexLost(struct rpi_state*);
+
+/**
+ * Turn commutation on.
+ */
+void setCommutationOn(struct rpi_state*);
+
+/**
+ * Turn commutation off.
+ */
+void setCommutationOff(struct rpi_state*);
 
 #endif /*PMSM_STATE*/