]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control.git/blob - pmsm-control/test_sw/logs.c
Function Init_logs moved to right place.
[fpga/rpi-motor-control.git] / pmsm-control / test_sw / logs.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "logs.h"
6 #include "pmsm_state.h"
7 #include "rp_spi.h"
8
9 /*
10  * \brief
11  * Free logs memory.
12  */
13 void freeLogs(struct rpi_state* this){
14         int r;
15         if (this->log_col_count){
16                 for (r=0;r<LOG_ROWS;r++){
17                         free(this->logs[r]);
18                 }
19         }
20         this->log_col_count=0;
21         this->doLogs=0;
22 }
23
24 /*
25  * \brief
26  * Initialize logs
27  */
28 void logInit(struct rpi_state* state){
29         int r;
30         state->log_col=0;
31         state->log_col_count=LOG_DEF_COL;
32         for (r=0;r<LOG_ROWS;r++){
33                 state->logs[r]=malloc(state->log_col_count*sizeof(int));
34                 if (state->logs[r]==NULL){
35                         state->error=1;
36                         state->log_col_count=-1;
37                         return;
38                 }
39         }
40         state->doLogs=1;
41 }
42
43 /*
44  * \brief
45  * Makes log.
46  */
47 void makeLog(struct rpi_state* this){
48         int r;
49         if (this->log_col==MAX_LOGS-1){
50                 this->doLogs=0;
51                 return;
52         }
53         this->logs[0][this->log_col]=(int)this->tf_count;
54         this->logs[1][this->log_col]=(int)this->spi_dat->pozice;
55
56         this->logs[2][this->log_col]=(int)this->pwm1;
57         this->logs[3][this->log_col]=(int)this->pwm2;
58         this->logs[4][this->log_col]=(int)this->pwm3;
59         this->logs[5][this->log_col]=this->duty;
60
61         this->logs[6][this->log_col]=this->desired_spd;
62         this->logs[7][this->log_col]=this->speed;
63
64         this->logs[8][this->log_col]=(int)(this->spi_dat->ch1/this->spi_dat->adc_m_count);
65         this->logs[9][this->log_col]=(int)(this->spi_dat->ch2/this->spi_dat->adc_m_count);
66         this->logs[10][this->log_col]=(int)(this->spi_dat->ch0/this->spi_dat->adc_m_count);
67
68         this->log_col++;
69      /*
70         if (rps.log_col==rps.log_col_count-1){
71                 rps.log_col_count*=2;
72                 rps.log_col_count%=MAX_LOGS;
73                 for (r=0;r<LOG_ROWS;r++){
74                         rps.logs[r]=realloc(rps.logs[r],rps.log_col_count*sizeof(int));
75                         if (rps.logs[r]==NULL){
76                                 rps.doLogs=0;
77                                 rps.error=1;
78                         }
79                 }
80         }
81      */
82 }
83
84 /*
85  * \brief
86  * Save logs
87  */
88 void saveLogs(struct rpi_state* this){
89         FILE *f;
90         int r,s;
91
92         f = fopen("logs.log", "w");
93         if (f == NULL){
94                 printf("Error opening file!\n");
95                 return;
96         }
97
98         for (r=0;r<LOG_ROWS;r++){
99                 for(s=0;s<this->log_col-1;s++){ /*posledni sloupec je vevyplneny*/
100                         if (s==this->log_col-2){
101                                 fprintf(f,"%d ",this->logs[r][s]);
102                         }else{
103                                 fprintf(f,"%d, ",this->logs[r][s]);
104                         }
105                 }
106                 fprintf(f,"\r");
107         }
108         fclose(f);
109         freeLogs(this);
110 }