Now it is possible to log state data.
authorMartin Prudek <prudemar@fel.cvut.cz>
Sat, 16 May 2015 19:47:57 +0000 (21:47 +0200)
committerMartin Prudek <prudemar@fel.cvut.cz>
Sat, 16 May 2015 19:47:57 +0000 (21:47 +0200)
pmsm-control/test_sw/cmd_proc.c
pmsm-control/test_sw/main_pmsm.c
pmsm-control/test_sw/pmsm_state.h

index 82d5390..a0b0592 100644 (file)
@@ -21,6 +21,7 @@ static void printHelp(){
        puts("ga:[hodnota] - Zapne rizeni na zvolenou absolutni pozici.");
        puts("duty:[hodnota] - Nastavi pevnou sirku plneni.");
        puts("spd:[hodnota] - Zapne rizeni na danou rychlost.");
+       puts("log - Spusti nebo ulozi logovani.");
 
        puts("print - Zapne nebo vypne pravidelne vypisovani hodnot.");
        puts("help - Vypne vypisovani hodnot a zobrazi tuto napovedu.");
@@ -114,9 +115,9 @@ static void exitApp(struct rpi_state* state){
  * 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*/
+       sem_wait(&state->thd_par_sem);
        state->pos_reg_ena=0;
        state->spd_reg_ena=1;
        state->commutate=1;
@@ -124,6 +125,68 @@ static void setSpeed(struct rpi_state* state, int speed){
        sem_post(&state->thd_par_sem);
 }
 
+/*
+ * \brief
+ * Initialize logs
+ */
+static void logInit(struct rpi_state* state){
+       int r;
+       state->log_col=0;
+       state->log_col_count=LOG_DEF_COL;
+       for (r=0;r<LOG_ROWS;r++){
+               state->logs[r]=malloc(state->log_col_count*sizeof(int));
+       }
+}
+
+/*
+ * \brief
+ * Save logs
+ */
+static void saveLogs(struct rpi_state* state){
+       FILE *f;
+       int r,s;
+
+       f = fopen("logs.txt", "w");
+       if (f == NULL){
+               printf("Error opening file!\n");
+               return;
+       }
+
+       for (r=0;r<LOG_ROWS;r++){
+               for(s=0;s<state->log_col-1;s++){        /*posledni sloupec je vevyplneny*/
+                       if (s==state->log_col-2){
+                               fprintf(f,"%d ",state->logs[r][s]);
+                       }else{
+                               fprintf(f,"%d, ",state->logs[r][s]);
+                       }
+               }
+               fprintf(f,"\r");
+       }
+       fclose(f);
+       freeLogs();
+}
+
+/**
+ * \brief
+ * SetLog
+ * if logs are being logged, save them
+ * if they are not, start log them
+ */
+static void setLogSEM(struct rpi_state* state){
+       sem_wait(&state->thd_par_sem);
+       /* ulozim logy a vypnu zachytavani*/
+       if (state->log_col_count){
+               state->doLogs=0;
+               sem_post(&state->thd_par_sem);
+               saveLogs(state);
+       /* spustim zachytavani logu */
+       }else{
+               logInit(state);
+               state->doLogs=1;
+               sem_post(&state->thd_par_sem);
+       }
+}
+
 /**
  * \brief
  * Commands detection.
@@ -158,6 +221,8 @@ void poll_cmd(struct rpi_state* state){
                         exitApp(state);
                 }else if (!strcmp(cmd,"spd")){
                         setSpeed(state, val);
+                }else if (!strcmp(cmd,"log")){
+                        setLogSEM(state);
                 }
         }
 
@@ -166,13 +231,13 @@ void poll_cmd(struct rpi_state* state){
 /*
  * pocita procentualni odchylku od prumerneho proudu
  */
-float diff_p(float value){
+static float diff_p(float value){
        return ((float)value-PRUM_PROUD)*100/PRUM_PROUD;
 }
 /*
  * pocita procentualni odchylku od prumerneho souctu proudu
  */
-float diff_s(float value){
+static float diff_s(float value){
        return ((float)value-PRUM_SOUC)*100/PRUM_SOUC;
 }
 
index 3d13ff2..bd676e4 100644 (file)
@@ -57,7 +57,10 @@ struct rpi_state rps={
        .desired_spd=0,
        .spd_reg_ena=0,
        .old_pos={0},
-       .spd_err_sum=0
+       .spd_err_sum=0,
+       .log_col_count=0,       /* pocet radku zaznamu */
+       .log_col=0,
+       .doLogs=0
 };
 
 /**
@@ -95,6 +98,46 @@ int32_t min(int32_t x, int32_t y, int32_t z){
 
         return x;
 }
+
+/*
+ * \brief
+ * Free logs
+ */
+void freeLogs(){
+       int r;
+       if (rps.log_col_count){
+               for (r=0;r<LOG_ROWS;r++){
+                       free(rps.logs[r]);
+               }
+       }
+       rps.log_col_count=0;
+       rps.doLogs=0;
+}
+
+/*
+ * \brief
+ * Makes log.
+ */
+void makeLog(){
+       int r;
+       if (rps.log_col==MAX_LOGS-1){
+               rps.doLogs=0;
+               return;
+       }
+       rps.logs[0][rps.log_col]=(int)rps.tf_count;
+       rps.logs[1][rps.log_col]=(int)rps.spi_dat->pozice;
+
+       rps.log_col++;
+
+        if (rps.log_col==rps.log_col_count-1){
+               rps.log_col_count*=2;
+               rps.log_col_count%=MAX_LOGS;
+               for (r=0;r<LOG_ROWS;r++){
+                       rps.logs[r]=realloc(rps.logs[r],rps.log_col_count*sizeof(int));
+               }
+        }
+}
+
 /*
  * \brief
  * Pripravi psi buffer
@@ -177,6 +220,7 @@ void appl_stop(){
 
        spi_disable();
        clk_disable();
+       freeLogs();
        /*muzeme zavrit semafor*/
        sem_destroy(&rps.thd_par_sem);
         printf("\nprogram bezpecne ukoncen\n");
@@ -623,6 +667,12 @@ void * read_data(void* param){
                        }else if(!rps.index_ok && rps.commutate){
                                simple_hall_commutator(rps.duty);
                        }
+
+                       /*zalogujeme hodnoty*/
+                       if (rps.doLogs && !(rps.tf_count%LOG_PERIOD)){
+                               makeLog();
+                       }
+
                        sem_post(&rps.thd_par_sem);             /*--post semaphore---*/
 
                        /* calculate next shot */
index 32c8d74..a148e1c 100644 (file)
@@ -8,6 +8,11 @@
 #define MAX_DUTY       170
 #define MAX_SPEED      (7*OLD_POS_NUM)
 
+#define LOG_ROWS       2
+#define LOG_DEF_COL    500
+#define MAX_LOGS       1000
+#define LOG_PERIOD     10
+
 struct rpi_in;
 
 struct rpi_state{
@@ -33,6 +38,13 @@ struct rpi_state{
        char spd_reg_ena;               /* speed rugulation enable */
 
        int spd_err_sum;                /* for speed pid regulator */
+
+       int *logs[LOG_ROWS];            /* logs */
+       int log_col_count;              /* number of log columns */
+       int log_col;                    /* current colimn */
+       char doLogs;                    /* schall we make logs? */
 };
 
+void freeLogs();
+
 #endif /*PMSM_STATE*/