]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control.git/blob - pmsm-control/test_sw/main_pmsm.c
7d707da09e85d4ccf97e81a67ad1b37ce2bf5672
[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_raw=data->pozice;
71         data->pozice-=offset->pozice;
72         return;
73 }
74 /*
75  * pocita procentualni odchylku od prumerneho proudu
76  */
77 float diff_p(float value){
78         return ((float)value-PRUM_PROUD)*100/PRUM_PROUD;
79 }
80 /*
81  * pocita procentualni odchylku od prumerneho souctu proudu
82  */
83 float diff_s(float value){
84         return ((float)value-PRUM_SOUC)*100/PRUM_SOUC;
85 }
86 /*
87  * tiskne potrebna data
88  */
89 void printData(struct rpi_in data){
90         float cur0, cur1, cur2;
91         int i;
92         if (data.adc_m_count){
93                 cur0=data.ch0/data.adc_m_count;
94                 cur1=data.ch1/data.adc_m_count;
95                 cur2=data.ch2/data.adc_m_count;
96         }
97         for (i = 0; i < 16; i++) {
98                         if (!(i % 6))
99                                 puts("");
100                         printf("%.2X ", data.debug_rx[i]);
101         }
102         puts("");
103         printf("\npozice=%d\n",(int32_t)data.pozice);
104         printf("raw_pozice=%d\n",(int32_t)data.pozice_raw);
105         printf("raw_pozice last11=%u\n",(data.pozice_raw&0x7FF));
106         printf("index position=%d\n",(int16_t)data.index_position);
107         printf("hal1=%d, hal2=%d, hal3=%d\n",data.hal1,data.hal2,data.hal3);
108         printf("en1=%d, en2=%d, en3=%d (Last sent)\n",!!(0x40&test),!!(0x20&test),!!(0x10&test));
109         printf("shdn1=%d, shdn2=%d, shdn3=%d (L.s.)\n",!!(0x08&test),!!(0x04&test),!!(0x02&test));
110         printf("PWM1=%u(L.s.)\n",pwm1);
111         printf("PWM2=%u(L.s.)\n",pwm2);
112         printf("PWM3=%u(L.s.)\n",pwm3);
113         printf("Pocet namerenych proudu=%u\n",data.adc_m_count);
114         printf("(pwm1) (ch1)=%d (avg=%4.0f) (%2.2f%%)\n",data.ch1,cur1,diff_p(cur1));
115         printf("(pwm2) (ch2)=%d (avg=%4.0f)(%2.2f%%)\n",data.ch2,cur2,diff_p(cur2));
116         printf("(pwm3) (ch0)=%d (avg=%4.0f)(%2.2f%%)\n",data.ch0,cur0,diff_p(cur0));
117         printf("soucet prumeru=%5.0f (%2.2f%%)\n",cur0+cur1+cur2,diff_s(cur0+cur1+cur2));
118 }
119 void prepare_tx(uint8_t * tx){
120
121         /*Data format:
122          * tx[4] - bity 95 downto 88 - bits that are sent first
123          * tx[5] - bity 87 downto 80
124          * tx[6] - bity 79 downto 72
125          * tx[7] - bity 71 downto 64
126          * tx[8] - bity 63 downto 56
127          * tx[9] - bity 55 downto 48
128          * tx[10] - bity 47 downto 40
129          * tx[11] - bity 39 downto 32
130          * tx[12] - bity 31 downto 24
131          * tx[13] - bity 23 downto 16
132          * tx[14] - bity 15 downto 8
133          * tx[15] - bity 7 downto 0
134          *
135          * bit 95 - ADC reset
136          * bit 94 - enable PWM1
137          * bit 93 - enable PWM2
138          * bit 92 - enable PWM3
139          * bit 91 - shutdown1
140          * bit 90 - shutdown2
141          * bit 89 - shutdown3
142          *      .
143          *      .
144          *      .
145          * bits 66 .. 56 - match PWM1
146          * bits 55 .. 45 - match PWM2
147          * bit 11,12 - Unused
148          * bits 42 .. 32  - match PWM3
149          */
150
151
152         uint16_t tmp;
153
154         /* keep the cap*/
155         if (pwm1>2047) pwm1=2047;
156         if (pwm2>2047) pwm2=2047;
157         if (pwm3>2047) pwm3=2047;
158
159         tx[0]=test; /*bit 94 - enable PWM1*/
160
161         /*pwm1*/
162         tx[7]=(tx[7] & 0xF8) | (0x07 & ((uint8_t*)&pwm1)[1]); /*MSB*/
163         tx[8]=((uint8_t*)&pwm1)[0]; /*LSB*/
164
165         /*pwm2*/
166         tmp=pwm2;
167         tmp<<=5;
168         tx[9]=((uint8_t*)&tmp)[1]; /*MSB*/
169         tx[10]=(tx[10] & 0x1F) | (0xE0 & ((uint8_t*)&tmp)[0]); /*LSB*/
170
171         /*pwm3*/
172         tx[10]=(tx[10] & 0xF8) | (0x07 & ((uint8_t*)&pwm3)[1]); /*MSB*/
173         tx[11]=((uint8_t*)&pwm3)[0]; /*LSB*/
174
175
176 }
177 /**
178  * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
179  */
180 void * pos_monitor(void* param){
181         set_priority(param);            /*set priority*/
182         while(1){
183                 printData(data);
184                 usleep(1000000);        /*1 Hz*/
185         }
186         return (void*)0;
187 }
188
189 /**
190  * Funkce pravidelne vycita data z motoru
191  */
192 void * read_data(void* param){
193         int i;
194         struct rpi_in pocatek;
195         uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
196         set_priority(param);                            /*set priority*/
197         pocatek = spi_read(tx);
198                 while(1){
199                         prepare_tx(tx);
200                         data = spi_read(tx);
201                         substractOffset(&data,&pocatek);
202                         usleep(1000);                           /*1kHz*/
203                 }
204 }
205
206 /**
207  * \brief Main function.
208  */
209
210 int main(){
211         uint16_t tmp;
212
213         /*nastaveni priorit vlaken*/
214         struct thread_param tsp;
215         tsp.sch_policy = SCHED_FIFO;
216
217         /*nastaveni signalu pro vypnuti pomoci Ctrl+C*/
218         sighnd.sa_handler=&sighnd_fnc;
219         sigaction(SIGINT, &sighnd, NULL );
220
221         clk_init();             /* inicializace gpio hodin */
222         spi_init();             /* iniicializace spi*/
223
224         /*semafor pro detekci zpracovani parametru vlaken*/
225         sem_init(&thd_par_sem,THREAD_SHARED,INIT_VALUE);
226
227         /*vlakna*/
228         pthread_t tid;                  /*identifikator vlakna*/
229         pthread_attr_t attr;            /*atributy vlakna*/
230         pthread_attr_init(&attr);       /*inicializuj implicitni atributy*/
231
232
233
234         /*ziskavani dat z motoru*//*vysoka priorita*/
235         tsp.sch_prior = PRIOR_HIGH;
236         pthread_create(&tid, &attr, read_data, (void*)&tsp);
237
238         /*vypisovani lokalni pozice*//*nizka priorita*/
239         tsp.sch_prior = PRIOR_LOW;
240         pthread_create(&tid, &attr, pos_monitor, (void*)&tsp);
241
242
243         /*muzeme zavrit semafor*/
244         sem_destroy(&thd_par_sem);
245
246         while (1){
247                 scanf("%u",&tmp);
248                 printf("volba=%x\n",tmp);
249                 switch (tmp){
250                 case 1:
251                         scanf("%u",&pwm1);
252                         break;
253                 case 2:
254                         scanf("%u",&pwm2);
255                         break;
256                 case 3:
257                         scanf("%u",&pwm3);
258                         break;
259                 case 4:
260                         scanf("%u",&test);
261                         break;
262
263                 default:
264                         break;
265                 }
266
267         }
268         return 0;
269 }