Logging function moved to separate file.
authorMartin Prudek <prudemar@fel.cvut.cz>
Wed, 20 May 2015 17:27:38 +0000 (19:27 +0200)
committerMartin Prudek <prudemar@fel.cvut.cz>
Wed, 20 May 2015 17:27:38 +0000 (19:27 +0200)
pmsm-control/test_sw/Makefile
pmsm-control/test_sw/cmd_proc.c
pmsm-control/test_sw/logs.c [new file with mode: 0644]
pmsm-control/test_sw/logs.h [new file with mode: 0644]
pmsm-control/test_sw/main_pmsm.c

index 778b336..5a8aa24 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
-       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 -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
+       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
index cc92a0b..8f49e91 100644 (file)
@@ -3,6 +3,7 @@
 #include <string.h>
 
 #include "cmd_proc.h"
+#include "logs.h"
 
 #define PRUM_PROUD     2061
 #define PRUM_SOUC      6183
@@ -161,33 +162,7 @@ static void logInit(struct rpi_state* state){
        state->doLogs=1;
 }
 
-/*
- * \brief
- * Save logs
- */
-static void saveLogs(struct rpi_state* state){
-       FILE *f;
-       int r,s;
-
-       f = fopen("logs.log", "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
diff --git a/pmsm-control/test_sw/logs.c b/pmsm-control/test_sw/logs.c
new file mode 100644 (file)
index 0000000..f6c2a5f
--- /dev/null
@@ -0,0 +1,91 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "logs.h"
+#include "pmsm_state.h"
+#include "rp_spi.h"
+
+/*
+ * \brief
+ * Free logs memory.
+ */
+void freeLogs(struct rpi_state* this){
+       int r;
+       if (this->log_col_count){
+               for (r=0;r<LOG_ROWS;r++){
+                       free(this->logs[r]);
+               }
+       }
+       this->log_col_count=0;
+       this->doLogs=0;
+}
+
+/*
+ * \brief
+ * Makes log.
+ */
+void makeLog(struct rpi_state* this){
+       int r;
+       if (this->log_col==MAX_LOGS-1){
+               this->doLogs=0;
+               return;
+       }
+       this->logs[0][this->log_col]=(int)this->tf_count;
+       this->logs[1][this->log_col]=(int)this->spi_dat->pozice;
+
+       this->logs[2][this->log_col]=(int)this->pwm1;
+       this->logs[3][this->log_col]=(int)this->pwm2;
+       this->logs[4][this->log_col]=(int)this->pwm3;
+       this->logs[5][this->log_col]=this->duty;
+
+       this->logs[6][this->log_col]=this->desired_spd;
+       this->logs[7][this->log_col]=this->speed;
+
+       this->logs[8][this->log_col]=(int)(this->spi_dat->ch1/this->spi_dat->adc_m_count);
+       this->logs[9][this->log_col]=(int)(this->spi_dat->ch2/this->spi_dat->adc_m_count);
+       this->logs[10][this->log_col]=(int)(this->spi_dat->ch0/this->spi_dat->adc_m_count);
+
+       this->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));
+                       if (rps.logs[r]==NULL){
+                               rps.doLogs=0;
+                               rps.error=1;
+                       }
+               }
+        }
+     */
+}
+
+/*
+ * \brief
+ * Save logs
+ */
+void saveLogs(struct rpi_state* this){
+       FILE *f;
+       int r,s;
+
+       f = fopen("logs.log", "w");
+       if (f == NULL){
+               printf("Error opening file!\n");
+               return;
+       }
+
+       for (r=0;r<LOG_ROWS;r++){
+               for(s=0;s<this->log_col-1;s++){ /*posledni sloupec je vevyplneny*/
+                       if (s==this->log_col-2){
+                               fprintf(f,"%d ",this->logs[r][s]);
+                       }else{
+                               fprintf(f,"%d, ",this->logs[r][s]);
+                       }
+               }
+               fprintf(f,"\r");
+       }
+       fclose(f);
+       freeLogs(this);
+}
diff --git a/pmsm-control/test_sw/logs.h b/pmsm-control/test_sw/logs.h
new file mode 100644 (file)
index 0000000..c0d1996
--- /dev/null
@@ -0,0 +1,29 @@
+/**
+ * \brief
+ * Making data logs.
+ */
+
+#ifndef LOGS
+#define LOGS
+
+struct rpi_state;
+
+/*
+ * \brief
+ * Makes log.
+ */
+void makeLog(struct rpi_state*);
+
+/*
+ * \brief
+ * Free logs memory.
+ */
+void freeLogs(struct rpi_state*);
+
+/*
+ * \brief
+ * Save logs to text file.
+ */
+void saveLogs(struct rpi_state*);
+
+#endif /*LOGS*/
index 56b6544..25b0acc 100644 (file)
@@ -26,9 +26,7 @@
 #include "controllers.h"
 #include "commutators.h"
 #include "comp.h"
-
-
-
+#include "logs.h"
 
 #define PRIOR_KERN     50
 #define PRIOR_HIGH     49
@@ -83,66 +81,6 @@ inline void clk_disable(){
        termClock(0);
 }
 
-
-/*
- * \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.logs[2][rps.log_col]=(int)rps.pwm1;
-       rps.logs[3][rps.log_col]=(int)rps.pwm2;
-       rps.logs[4][rps.log_col]=(int)rps.pwm3;
-       rps.logs[5][rps.log_col]=rps.duty;
-
-       rps.logs[6][rps.log_col]=rps.desired_spd;
-       rps.logs[7][rps.log_col]=rps.speed;
-
-       rps.logs[8][rps.log_col]=(int)(rps.spi_dat->ch1/rps.spi_dat->adc_m_count);
-       rps.logs[9][rps.log_col]=(int)(rps.spi_dat->ch2/rps.spi_dat->adc_m_count);
-       rps.logs[10][rps.log_col]=(int)(rps.spi_dat->ch0/rps.spi_dat->adc_m_count);
-
-       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));
-                       if (rps.logs[r]==NULL){
-                               rps.doLogs=0;
-                               rps.error=1;
-                       }
-               }
-        }
-     */
-}
-
-
-
-
 /**
  * \brief Signal handler pro Ctrl+C
  */
@@ -158,7 +96,7 @@ void appl_stop(){
 
        spi_disable();
        clk_disable();
-       freeLogs();
+       freeLogs(&rps);
        /*muzeme zavrit semafor*/
        sem_destroy(&rps.thd_par_sem);
         printf("\nprogram bezpecne ukoncen\n");
@@ -240,7 +178,7 @@ void * read_data(void* param){
 
                        /*zalogujeme hodnoty*/
                        if (rps.doLogs && !(rps.tf_count%LOG_PERIOD)){
-                               makeLog();
+                               makeLog(&rps);
                        }
 
                        sem_post(&rps.thd_par_sem);             /*--post semaphore---*/