Attemp to solve bug. ADC channels association should be pwm1-ch0 pwm2-ch1 pwm3-ch2...
[fpga/rpi-motor-control.git] / pmsm-control / test_sw / main_pmsm.c
1 /**
2  * \file main_pmsm.c
3  * \author Martin Prudek
4  * \brief Mainfile pro pmsm control.
5  */
6
7 #ifndef NULL
8 #define NULL (void*) 0
9 #endif /*NULL*/
10
11
12 #include <stdlib.h>     /*exit*/
13 #include <signal.h>     /*signal handler Ctrl+C*/
14 #include <stdio.h>      /*printf*/
15 #include <sched.h>      /*sheduler*/
16 #include <unistd.h>     /*usleep*/
17 #include <pthread.h>    /*threads*/
18
19 #include "rpin.h"       /*gpclk*/
20 #include "rp_spi.h"     /*spi*/
21 #include "misc.h"       /*structure for priorities*/
22
23
24 #define PRUM_PROUD      2061
25 #define PRUM_SOUC       6183
26
27 #define PRIOR_KERN      50
28 #define PRIOR_HIGH      49
29 #define PRIOR_LOW       20
30
31 #define THREAD_SHARED   0
32 #define INIT_VALUE      0       /*init value for semaphor*/
33
34 struct sigaction sighnd; /*struktura pro signal handler*/
35 struct rpi_in data;
36
37 uint8_t test;
38 uint16_t pwm1, pwm2, pwm3;
39
40
41 /**
42  * \brief Initilizes GPCLK.
43  */
44 int clk_init()
45 {
46         initialise(); /*namapovani gpio*/
47         initClock(PLLD_500_MHZ, 10, 0);
48         gpioSetMode(4, FSEL_ALT0);
49         return 0;
50 }
51 /*
52  * \brief Terminates GPCLK.
53  */
54
55 inline void clk_disable(){
56         termClock(0);
57 }
58
59 /**
60  * \brief Signal handler pro Ctrl+C
61  */
62 void sighnd_fnc(){
63         spi_disable();
64         clk_disable();
65         printf("\nprogram bezpecne ukoncen\n");
66         exit(0);
67 }
68
69 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
70         data->pozice-=offset->pozice;
71         return;
72 }
73 /*
74  * pocita procentualni odchylku od prumerneho proudu
75  */
76 float diff_p(float value){
77         return ((float)value-PRUM_PROUD)*100/PRUM_PROUD;
78 }
79 /*
80  * pocita procentualni odchylku od prumerneho souctu proudu
81  */
82 float diff_s(float value){
83         return ((float)value-PRUM_SOUC)*100/PRUM_SOUC;
84 }
85 /*
86  * tiskne potrebna data
87  */
88 void printData(struct rpi_in data){
89         float cur0, cur1, cur2;
90         int i;
91         if (data.adc_m_count){
92                 cur0=data.ch0/data.adc_m_count;
93                 cur1=data.ch1/data.adc_m_count;
94                 cur2=data.ch2/data.adc_m_count;
95         }
96         for (i = 0; i < 16; i++) {
97                         if (!(i % 6))
98                                 puts("");
99                         printf("%.2X ", data.debug_rx[i]);
100         }
101         puts("");
102         printf("\npozice=%d\n",(int32_t)data.pozice);
103         printf("hal1=%d, hal2=%d, hal3=%d\n",data.hal1,data.hal2,data.hal3);
104         printf("en1=%d, en2=%d, en3=%d (Last sent)\n",!!(0x40&test),!!(0x20&test),!!(0x10&test));
105         printf("shdn1=%d, shdn2=%d, shdn3=%d (L.s.)\n",!!(0x08&test),!!(0x04&test),!!(0x02&test));
106         printf("PWM1=%u(L.s.)\n",pwm1);
107         printf("PWM2=%u(L.s.)\n",pwm2);
108         printf("PWM3=%u(L.s.)\n",pwm3);
109         printf("Pocet namerenych proudu=%u\n",data.adc_m_count);
110         printf("(pwm1) (ch1)=%d (avg=%4.0f) (%2.2f%%)\n",data.ch1,cur1,diff_p(cur1));
111         printf("(pwm2) (ch2)=%d (avg=%4.0f)(%2.2f%%)\n",data.ch2,cur2,diff_p(cur2));
112         printf("(pwm3) (ch0)=%d (avg=%4.0f)(%2.2f%%)\n",data.ch0,cur0,diff_p(cur0));
113         printf("soucet prumeru=%5.0f (%2.2f%%)\n",cur0+cur1+cur2,diff_s(cur0+cur1+cur2));
114 }
115 void prepare_tx(uint8_t * tx){
116
117         /*Data format:
118          * tx[4] - bity 95 downto 88 - bits that are sent first
119          * tx[5] - bity 87 downto 80
120          * tx[6] - bity 79 downto 72
121          * tx[7] - bity 71 downto 64
122          * tx[8] - bity 63 downto 56
123          * tx[9] - bity 55 downto 48
124          * tx[10] - bity 47 downto 40
125          * tx[11] - bity 39 downto 32
126          * tx[12] - bity 31 downto 24
127          * tx[13] - bity 23 downto 16
128          * tx[14] - bity 15 downto 8
129          * tx[15] - bity 7 downto 0
130          *
131          * bit 95 - ADC reset
132          * bit 94 - enable PWM1
133          * bit 93 - enable PWM2
134          * bit 92 - enable PWM3
135          * bit 91 - shutdown1
136          * bit 90 - shutdown2
137          * bit 89 - shutdown3
138          *      .
139          *      .
140          *      .
141          * bits 66 .. 56 - match PWM1
142          * bits 55 .. 45 - match PWM2
143          * bit 11,12 - Unused
144          * bits 42 .. 32  - match PWM3
145          */
146
147
148         uint16_t tmp;
149
150         /* keep the cap*/
151         if (pwm1>2047) pwm1=2047;
152         if (pwm2>2047) pwm2=2047;
153         if (pwm3>2047) pwm3=2047;
154
155         tx[0]=test; /*bit 94 - enable PWM1*/
156
157         /*pwm1*/
158         tx[7]=(tx[7] & 0xF8) | (0x07 & ((uint8_t*)&pwm1)[1]); /*MSB*/
159         tx[8]=((uint8_t*)&pwm1)[0]; /*LSB*/
160
161         /*pwm2*/
162         tmp=pwm2;
163         tmp<<=5;
164         tx[9]=((uint8_t*)&tmp)[1]; /*MSB*/
165         tx[10]=(tx[10] & 0x1F) | (0xE0 & ((uint8_t*)&tmp)[0]); /*LSB*/
166
167         /*pwm3*/
168         tx[10]=(tx[10] & 0xF8) | (0x07 & ((uint8_t*)&pwm3)[1]); /*MSB*/
169         tx[11]=((uint8_t*)&pwm3)[0]; /*LSB*/
170
171
172 }
173 /**
174  * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
175  */
176 void * pos_monitor(void* param){
177         set_priority(param);            /*set priority*/
178         while(1){
179                 printData(data);
180                 usleep(1000000);        /*1 Hz*/
181         }
182         return (void*)0;
183 }
184
185 /**
186  * Funkce pravidelne vycita data z motoru
187  */
188 void * read_data(void* param){
189         int i;
190         struct rpi_in pocatek;
191         uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
192         set_priority(param);                            /*set priority*/
193         pocatek = spi_read(tx);
194                 while(1){
195                         prepare_tx(tx);
196                         data = spi_read(tx);
197                         substractOffset(&data,&pocatek);
198                         usleep(1000);                           /*1kHz*/
199                 }
200 }
201
202 /**
203  * \brief Main function.
204  */
205
206 int main(){
207         uint16_t tmp;
208
209         /*nastaveni priorit vlaken*/
210         struct thread_param tsp;
211         tsp.sch_policy = SCHED_FIFO;
212
213         /*nastaveni signalu pro vypnuti pomoci Ctrl+C*/
214         sighnd.sa_handler=&sighnd_fnc;
215         sigaction(SIGINT, &sighnd, NULL );
216
217         clk_init();             /* inicializace gpio hodin */
218         spi_init();             /* iniicializace spi*/
219
220         /*semafor pro detekci zpracovani parametru vlaken*/
221         sem_init(&thd_par_sem,THREAD_SHARED,INIT_VALUE);
222
223         /*vlakna*/
224         pthread_t tid;                  /*identifikator vlakna*/
225         pthread_attr_t attr;            /*atributy vlakna*/
226         pthread_attr_init(&attr);       /*inicializuj implicitni atributy*/
227
228
229
230         /*ziskavani dat z motoru*//*vysoka priorita*/
231         tsp.sch_prior = PRIOR_HIGH;
232         pthread_create(&tid, &attr, read_data, (void*)&tsp);
233
234         /*vypisovani lokalni pozice*//*nizka priorita*/
235         tsp.sch_prior = PRIOR_LOW;
236         pthread_create(&tid, &attr, pos_monitor, (void*)&tsp);
237
238
239         /*muzeme zavrit semafor*/
240         sem_destroy(&thd_par_sem);
241
242         while (1){
243                 scanf("%u",&tmp);
244                 printf("volba=%x\n",tmp);
245                 switch (tmp){
246                 case 1:
247                         scanf("%u",&pwm1);
248                         break;
249                 case 2:
250                         scanf("%u",&pwm2);
251                         break;
252                 case 3:
253                         scanf("%u",&pwm3);
254                         break;
255                 case 4:
256                         scanf("%u",&test);
257                         break;
258
259                 default:
260                         break;
261                 }
262
263         }
264         return 0;
265 }