Minor changes to status monitor.
[fpga/rpi-motor-control.git] / pmsm-control / test_sw / cmd_proc.c
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include "cmd_proc.h"
5
6 #define PRUM_PROUD      2061
7 #define PRUM_SOUC       6183
8
9 /**
10  * \brief
11  * Commands detection.
12  */
13 void poll_cmd(struct rpi_state* state){
14         unsigned int tmp;
15         /*
16          * Note:
17          * pri pouziti scanf("%u",&simple_hall_duty); dochazelo
18          * k preukladani hodnot na promenne test. Dost divne.
19          */
20         while (1){
21                 scanf("%u",&tmp);
22                 printf("volba=%u\n",tmp);
23                 switch (tmp){
24                 case 1:
25                         scanf("%u",&tmp);
26                         sem_wait(&state->thd_par_sem);
27                         state->pwm1=tmp&0xFFF;
28                         sem_post(&state->thd_par_sem);
29                         break;
30                 case 2:
31                         scanf("%u",&tmp);
32                         sem_wait(&state->thd_par_sem);
33                         state->pwm2=tmp&0xFFF;
34                         sem_post(&state->thd_par_sem);
35                         break;
36                 case 3:
37                         scanf("%u",&tmp);
38                         sem_wait(&state->thd_par_sem);
39                         state->pwm3=tmp&0xFFF;
40                         sem_post(&state->thd_par_sem);
41                         break;
42                 case 4:
43                         scanf("%u",&tmp);
44                         sem_wait(&state->thd_par_sem);
45                         state->test=tmp&0xFF;
46                         sem_post(&state->thd_par_sem);
47                         break;
48                 case 5:
49                         sem_wait(&state->thd_par_sem);
50                         state->commutate=!state->commutate;
51                         /* switch off pwms at the end of commutation */
52                         state->pwm1&=state->commutate*0xFFFF;
53                         state->pwm2&=state->commutate*0xFFFF;
54                         state->pwm3&=state->commutate*0xFFFF;
55                         sem_post(&state->thd_par_sem);
56                         break;
57                 case 6:
58                         scanf("%d",&tmp);
59                         sem_wait(&state->thd_par_sem);
60                         state->duty=tmp;
61                         sem_post(&state->thd_par_sem);
62                         break;
63                 case 7:
64                         scanf("%d",&tmp);
65                         sem_wait(&state->thd_par_sem);
66                         state->desired_pos=tmp;
67                         sem_post(&state->thd_par_sem);
68                         break;
69
70                 default:
71                         break;
72                 }
73
74         }
75         return ;
76 }
77 /*
78  * pocita procentualni odchylku od prumerneho proudu
79  */
80 float diff_p(float value){
81         return ((float)value-PRUM_PROUD)*100/PRUM_PROUD;
82 }
83 /*
84  * pocita procentualni odchylku od prumerneho souctu proudu
85  */
86 float diff_s(float value){
87         return ((float)value-PRUM_SOUC)*100/PRUM_SOUC;
88 }
89
90 /*
91  * tiskne potrebna data
92  */
93 void printData(struct rpi_state* state){
94         struct rpi_in data_p;
95         struct rpi_state s;     /*state*/
96         float cur0, cur1, cur2;
97         int i;
98         /* copy the data */
99         sem_wait(&state->thd_par_sem);
100         data_p = *state->spi_dat;
101         s=*state;
102         sem_post(&state->thd_par_sem);
103
104         if (data_p.adc_m_count){
105                 cur0=data_p.ch0/data_p.adc_m_count;
106                 cur1=data_p.ch1/data_p.adc_m_count;
107                 cur2=data_p.ch2/data_p.adc_m_count;
108         }
109         for (i = 0; i < 16; i++) {
110                         if (!(i % 6))
111                                 puts("");
112                         printf("%.2X ", data_p.debug_rx[i]);
113         }
114         puts("");
115         printf("\npozice=%d\n",(int32_t)data_p.pozice);
116         printf("chtena pozice=%d\n",s.desired_pos);
117         printf("transfer count=%u\n",s.tf_count);
118         printf("raw_pozice=%u\n",data_p.pozice_raw);
119         printf("raw_pozice last12=%u\n",(data_p.pozice_raw&0x0FFF));
120         printf("index position=%u\n",data_p.index_position);
121         printf("distance to index=%u\n",s.index_dist);
122         printf("hal1=%d, hal2=%d, hal3=%d\n",data_p.hal1,data_p.hal2,data_p.hal3);
123         printf("en1=%d, en2=%d, en3=%d (Last sent)\n",!!(0x40&s.test),!!(0x20&s.test),!!(0x10&s.test));
124         printf("shdn1=%d, shdn2=%d, shdn3=%d (L.s.)\n",!!(0x08&s.test),!!(0x04&s.test),!!(0x02&s.test));
125         printf("  PWM1=%u   PWM2=%u   PWM3=%u\n",s.pwm1,s.pwm2,s.pwm3);
126         printf("T_PWM1=%u T_PWM2=%u T_PWM3=%u\n",s.t_pwm1,s.t_pwm2, s.t_pwm3);
127         printf("Pocet namerenych proudu=%u\n",data_p.adc_m_count);
128         printf("(pwm1) (ch1)=%d (avg=%4.0f) (%2.2f%%)\n",data_p.ch1,cur1,diff_p(cur1));
129         printf("(pwm2) (ch2)=%d (avg=%4.0f)(%2.2f%%)\n",data_p.ch2,cur2,diff_p(cur2));
130         printf("(pwm3) (ch0)=%d (avg=%4.0f)(%2.2f%%)\n",data_p.ch0,cur0,diff_p(cur0));
131         printf("soucet prumeru=%5.0f (%2.2f%%)\n",cur0+cur1+cur2,diff_s(cur0+cur1+cur2));
132         printf("duty=%d\n",s.duty);
133         if (s.index_ok) printf("index ok\n");
134         if (s.commutate) printf("commutation in progress\n");
135 }