Regulation function now implemented with use of polymorphism.
authorMartin Prudek <prudemar@fel.cvut.cz>
Wed, 20 May 2015 19:43:24 +0000 (21:43 +0200)
committerMartin Prudek <prudemar@fel.cvut.cz>
Wed, 20 May 2015 19:43:24 +0000 (21:43 +0200)
pmsm-control/test_sw/cmd_proc.c
pmsm-control/test_sw/controllers.c
pmsm-control/test_sw/controllers.h
pmsm-control/test_sw/main_pmsm.c
pmsm-control/test_sw/pmsm_state.c
pmsm-control/test_sw/pmsm_state.h

index 6846df5..66e9be8 100644 (file)
@@ -62,8 +62,7 @@ static void start(struct rpi_state* state){
 static void stop(struct rpi_state* state){
        sem_wait(&state->thd_par_sem);
        setCommutationOff(state);
-       state->pos_reg_ena=0;
-       state->spd_reg_ena=0;
+       setRegulationOff(state);
        state->duty=0;
        state->pwm1=0;
        state->pwm2=0;
@@ -80,8 +79,7 @@ static void dutySet(struct rpi_state* state, int duty){
        if (duty>MAX_DUTY) duty=MAX_DUTY;
        if (duty<-MAX_DUTY) duty=-MAX_DUTY;/*paranoia*/
        state->duty=duty;
-       state->pos_reg_ena=0;
-       state->spd_reg_ena=0;
+       setRegulationOff(state);
        setCommutationOn(state);
        sem_post(&state->thd_par_sem);
 }
@@ -92,8 +90,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;
+       setRegulationPos(state);
        setCommutationOn(state);
        state->desired_pos=pos;
        sem_post(&state->thd_par_sem);
@@ -124,8 +121,7 @@ static void setSpeed(struct rpi_state* state, int speed){
        if (speed>MAX_SPEED) speed=MAX_SPEED;
        if (speed<-MAX_SPEED) speed=-MAX_SPEED;/*paranoia*/
        sem_wait(&state->thd_par_sem);
-       state->pos_reg_ena=0;
-       state->spd_reg_ena=1;
+       setRegulationSpeed(state);
        setCommutationOn(state);
        state->desired_spd=speed;
        sem_post(&state->thd_par_sem);
index 059ade6..bdf5ad9 100644 (file)
@@ -1,6 +1,13 @@
 
 #include "controllers.h"
 
+/**
+ * \brief No regulation.
+ */
+inline void zero_controller(struct rpi_state* state){
+       return;
+}
+
 /*
  * \brief
  * Very simple PID regulator.
index 520745e..5b24148 100644 (file)
 #define PID_I_S                0.0134
 
 /**
+ * \brief No regulation.
+ */
+inline void zero_controller(struct rpi_state*);
+
+/**
  * \brief
  * Very simple position P regulator.
  * Now only with P-part so that the error doesnt go to zero.
  */
-void pos_pid(struct rpi_state* state);
+void pos_pid(struct rpi_state*);
 
 /**
  * \brief
  * Very simple PI speed regulator.
  */
-void spd_pid(struct rpi_state* state);
+void spd_pid(struct rpi_state*);
 
 
 #endif /*CONTROLLERS*/
index ac3563d..f0fc982 100644 (file)
@@ -61,7 +61,8 @@ struct rpi_state rps={
        .log_col=0,
        .doLogs=0,
        .alpha_offset=960,
-       .main_commutator=zero_commutator        /*komutace vypnuta*/
+       .main_commutator=zero_commutator,       /* komutace vypnuta */
+       .main_controller=zero_controller        /* rizeni vypnuto */
 };
 
 
@@ -154,11 +155,7 @@ void * control_loop(void* param){
                        }
 
                        /* pocitame sirku plneni podle potreb rizeni*/
-                       if (rps.pos_reg_ena){           /*pozicni rizeni*/
-                               pos_pid(&rps);
-                       }else if(rps.spd_reg_ena){      /*rizeni na rychlost*/
-                               spd_pid(&rps);
-                       }
+                       rps.main_controller(&rps);
 
                        /* sirku plneni prepocteme na jednotlive pwm */
                        rps.main_commutator(&rps);
index 3938dc7..acfd347 100644 (file)
@@ -1,5 +1,6 @@
 #include "pmsm_state.h"
 #include "commutators.h"
+#include "controllers.h"
 
 /**
  * Index Lost.
@@ -36,3 +37,32 @@ inline void setCommutationOff(struct rpi_state* this){
        this->commutate=0;
        this->main_commutator=zero_commutator;
 }
+
+/**
+ * Turn on speed regulation.
+ */
+inline void setRegulationSpeed(struct rpi_state* this){
+       this->spd_reg_ena=1;
+       this->pos_reg_ena=0;
+       this->main_controller=spd_pid;
+}
+
+/**
+ * \brief Turn on position regulation
+ */
+inline void setRegulationPos(struct rpi_state* this){
+       this->spd_reg_ena=0;
+       this->pos_reg_ena=1;
+       this->main_controller=pos_pid;
+}
+
+/**
+ * \brief Turn off regulation.
+ */
+inline void setRegulationOff(struct rpi_state* this){
+       this->spd_reg_ena=0;
+       this->pos_reg_ena=0;
+       this->main_controller=zero_controller;
+
+
+}
index 583eb73..6fc77f4 100644 (file)
@@ -73,4 +73,19 @@ inline void setCommutationOn(struct rpi_state*);
  */
 inline void setCommutationOff(struct rpi_state*);
 
+/**
+ * Turn on speed regulation.
+ */
+inline void setRegulationSpeed(struct rpi_state*);
+
+/**
+ * \brief Turn on position regulation
+ */
+inline void setRegulationPos(struct rpi_state*);
+
+/**
+ * \brief Turn off regulation.
+ */
+inline void setRegulationOff(struct rpi_state*);
+
 #endif /*PMSM_STATE*/