]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control.git/blob - pmsm-control/test_sw/main_pmsm.c
Correction of position of subroutines.
[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 #include <time.h>       /*nanosleep*/
19
20 #include "rpin.h"       /*gpclk*/
21 #include "rp_spi.h"     /*spi*/
22 #include "misc.h"       /*structure for priorities*/
23 #include "pxmc_sin_fixed.h"     /*to test sin commutation */
24 #include "pmsm_state.h"
25 #include "cmd_proc.h"
26
27 #define MAX_DUTY        128
28 #define PID_P           0.1
29
30 #define PRIOR_KERN      50
31 #define PRIOR_HIGH      49
32 #define PRIOR_LOW       20
33
34 #define THREAD_SHARED   0
35 #define INIT_VALUE      1       /*init value for semaphor*/
36
37
38 #define NSEC_PER_SEC    (1000000000) /* The number of nsecs per sec. */
39
40
41 struct rpi_in data;
42 struct rpi_state rps={
43         .spi_dat=&data,
44         .thd_par_sem=NULL,
45         .test=0,
46         .pwm1=0,.pwm2=0, .pwm3=0,
47         .pwm1=0, .t_pwm2=0, .t_pwm3=0,
48         .commutate=0,
49         .duty=0,                        /* duty cycle of pwm */
50         .index_dist=0,          /* distance to index position */
51         .index_ok=0,
52         .tf_count=0,            /*number of transfer*/
53         .desired_pos=0          /* desired position */
54 };
55
56 /**
57  * \brief Initilizes GPCLK.
58  */
59 int clk_init()
60 {
61         initialise(); /*namapovani gpio*/
62         initClock(PLLD_500_MHZ, 10, 0);
63         gpioSetMode(4, FSEL_ALT0);
64         return 0;
65 }
66 /*
67  * \brief Terminates GPCLK.
68  */
69
70 inline void clk_disable(){
71         termClock(0);
72 }
73
74 /**
75  * \brief Signal handler pro Ctrl+C
76  */
77 void appl_stop(){
78         spi_disable();
79         clk_disable();
80         /*muzeme zavrit semafor*/
81         sem_destroy(&rps.thd_par_sem);
82         printf("\nprogram bezpecne ukoncen\n");
83 }
84
85 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
86         data->pozice_raw=data->pozice;
87         data->pozice-=offset->pozice;
88         return;
89 }
90
91
92 void prepare_tx(uint8_t * tx){
93
94         /*Data format:
95          * tx[4] - bity 95 downto 88 - bits that are sent first
96          * tx[5] - bity 87 downto 80
97          * tx[6] - bity 79 downto 72
98          * tx[7] - bity 71 downto 64
99          * tx[8] - bity 63 downto 56
100          * tx[9] - bity 55 downto 48
101          * tx[10] - bity 47 downto 40
102          * tx[11] - bity 39 downto 32
103          * tx[12] - bity 31 downto 24
104          * tx[13] - bity 23 downto 16
105          * tx[14] - bity 15 downto 8
106          * tx[15] - bity 7 downto 0
107          *
108          * bit 95 - ADC reset
109          * bit 94 - enable PWM1
110          * bit 93 - enable PWM2
111          * bit 92 - enable PWM3
112          * bit 91 - shutdown1
113          * bit 90 - shutdown2
114          * bit 89 - shutdown3
115          *      .
116          *      .
117          *      Unused
118          *      .
119          *      .
120          * bits 47 .. 32 - match PWM1
121          * bits 31 .. 16 - match PWM2
122          * bits 15 .. 0  - match PWM3
123          */
124
125
126         uint16_t tmp;
127
128         /* keep the 11-bit cap*/
129
130         if (rps.pwm1>2047) rps.pwm1=2047;
131         if (rps.pwm2>2047) rps.pwm2=2047;
132         if (rps.pwm3>2047) rps.pwm3=2047;
133
134         tx[0]=rps.test; /*bit 94 - enable PWM1*/
135
136         /*now we have to switch the bytes due to endianess */
137         /* ARMv6 & ARMv7 instructions are little endian */
138         /*pwm1*/
139         tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
140         tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
141
142         /*pwm2*/
143         tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
144         tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
145
146         /*pwm3*/
147         tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
148         tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
149
150
151 }
152 /**
153  * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
154  */
155 void * pos_monitor(void* param){
156         while(1){
157                 printData(&rps);
158                 usleep(1000000);        /*1 Hz*/
159         }
160         return (void*)0;
161 }
162 /*
163  * \brief
164  * Multiplication of 11 bit
165  * Zaporne vysledky prvede na nulu.
166  */
167 inline uint16_t mult_cap(int32_t s,int d){
168         int j;
169         int res=0;
170         for(j=0;j!=11;j++){
171                 /* multiplicate as if maximum sinus value was unity */
172                 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
173         }
174         return res;
175 }
176 inline
177 int sin_commutator(int duty){
178         #define DEGREE_60        715827883
179         #define DEGREE_120      1431655765
180         #define DEGREE_180      2147483648
181         #define DEGREE_240      2863311531
182         #define DEGREE_300      3579139413
183         uint32_t j,pos;
184         int32_t sin;
185         pos=rps.index_dist;
186         int32_t pwm;
187         /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
188         pos+=38;
189         /*use it as cyclic 32-bit logic*/
190         pos*=4294967;
191         if (duty>=0){   /*clockwise rotation*/
192                 /* 1st phase */
193                 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
194                 pwm=sin*duty/1024;
195                 if (pwm<0) pwm=0;
196                 rps.pwm1=(uint16_t)pwm;
197
198                 /* 2nd phase */
199                 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
200                 pwm=sin*duty/1024;
201                 if (pwm<0) pwm=0;
202                 rps.pwm2=(uint16_t)pwm;
203
204                 /* 3rd phase */
205                 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
206                 pwm=sin*duty/1024;
207                 if (pwm<0) pwm=0;
208                 rps.pwm3=(uint16_t)pwm;
209         }else{
210                 duty=-duty;
211
212                 /* 1st phase */
213                 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
214                 pwm=sin*duty/1024;
215                 if (pwm<0) pwm=0;
216                 rps.pwm1=(uint16_t)pwm;
217
218                 /* 2nd phase */
219                 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
220                 pwm=sin*duty/1024;
221                 if (pwm<0) pwm=0;
222                 rps.pwm2=(uint16_t)pwm;
223
224                 /* 3rd phase */
225                 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
226                 pwm=sin*duty/1024;
227                 if (pwm<0) pwm=0;
228                 rps.pwm3=(uint16_t)pwm;
229         }
230         return 0;
231 }
232 /*
233  * \brief
234  * Test function to be placed in controll loop.
235  * Switches PWM's at point where they produce same force.
236  * This points are found thanks to IRC position,
237  */
238 inline
239 void simple_ind_dist_commutator(int duty){
240         if (duty>=0){ /* clockwise - so that position increase */
241                 /* pwm3 */
242                 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
243                 (rps.index_dist>=1048 && rps.index_dist<=1377)){
244                         rps.pwm1=0;
245                         rps.pwm2=0;
246                         rps.pwm3=duty;
247                         /* pwm1 */
248                 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
249                 (rps.index_dist>=1377 && rps.index_dist<=1711)){
250                         rps.pwm1=duty;
251                         rps.pwm2=0;
252                         rps.pwm3=0;
253                         /* pwm2 */
254                 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
255                 (rps.index_dist>=711 && rps.index_dist<=1048) ||
256                 (rps.index_dist>=1711 && rps.index_dist<=1999)){
257                         rps.pwm1=0;
258                         rps.pwm2=duty;
259                         rps.pwm3=0;
260                 }
261         }else{  /*counter-clockwise - position decrease */
262                 /* pwm3 */
263                 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
264                 (rps.index_dist>=1544 && rps.index_dist<=1878)){
265                         rps.pwm1=0;
266                         rps.pwm2=0;
267                         rps.pwm3=-duty;
268                         /* pwm1 */
269                 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
270                 (rps.index_dist>=881 && rps.index_dist<=1210) ||
271                 (rps.index_dist>=1878 && rps.index_dist<=1999)){
272                         rps.pwm1=-duty;
273                         rps.pwm2=0;
274                         rps.pwm3=0;
275                         /* pwm2 */
276                 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
277                 (rps.index_dist>=1210 && rps.index_dist<=1544)){
278                         rps.pwm1=0;
279                         rps.pwm2=-duty;
280                         rps.pwm3=0;
281                 }
282         }
283 }
284 /*
285  * \brief
286  * Test function to be placed in controll loop.
287  * Switches PWM's at point where they produce same force
288  */
289 inline void simple_hall_commutator(int duty){
290         if (duty>=0){ /* clockwise - so that position increase */
291                 /* pwm3 */
292                 if (data.hal2 && !data.hal3){
293                         rps.pwm1=0;
294                         rps.pwm2=0;
295                         rps.pwm3=duty;
296                         /* pwm1 */
297                 }else if (data.hal1 && !data.hal2){
298                         rps.pwm1=duty;
299                         rps.pwm2=0;
300                         rps.pwm3=0;
301                         /* pwm2 */
302                 }else if (!data.hal1 && data.hal3){
303                         rps.pwm1=0;
304                         rps.pwm2=duty;
305                         rps.pwm3=0;
306                 }
307         }else{  /*counter-clockwise - position decrease */
308                 /* pwm3 */
309                 if (!data.hal2 && data.hal3){
310                         rps.pwm1=0;
311                         rps.pwm2=0;
312                         rps.pwm3=-duty;
313                         /* pwm1 */
314                 }else if (!data.hal1 && data.hal2){
315                         rps.pwm1=-duty;
316                         rps.pwm2=0;
317                         rps.pwm3=0;
318                         /* pwm2 */
319                 }else if (data.hal1 && !data.hal3){
320                         rps.pwm1=0;
321                         rps.pwm2=-duty;
322                         rps.pwm3=0;
323                 }
324         }
325 }
326 /**
327  * \brief
328  * Computation of distance to index.
329  *
330  * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
331  * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
332  *      to je 3999 bodu
333  *      =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
334  *              signalu indexu
335  */
336 void comIndDist(){
337         uint16_t pos = 0x0FFF & data.pozice_raw;
338         uint16_t dist;
339         uint16_t index = data.index_position;
340
341         if (index<1999){                /*index e<0,1998> */
342                 if (pos<index){                 /*pozice e<0,index-1> */
343                         /*proti smeru h.r. od indexu*/
344                         dist=pos+2000-index;
345                 }else if (pos<=index+1999){     /*pozice e<index,index+1999> */
346                         /*po smeru h.r. od indexu*/
347                         dist=pos-index;
348                 }else if (pos<index+2096){      /*pozice e<index+2000,index+2095> */
349                         goto index_lost;
350                 }else{                          /*pozice e<index+2096,4095> */
351                         /*proti smeru h.r. od indexu - podtecena pozice*/
352                         dist=pos-index-2096;
353                 }
354         }else if (index<=2096){         /*index e<1999,2096>*/
355                 if (pos<index-1999){            /*pozice e<0,index-2000> */
356                         goto index_lost;
357                 }else if (pos<index){           /*pozice e<index-1999,index-1> */
358                         /*proti smeru h.r. od indexu*/
359                         dist=pos+2000-index;
360                 }else if (pos<=index+1999){     /*pozice e<index,index+1999> */
361                         /*po smeru h.r. od indexu*/
362                         dist=pos-index;
363                 }else {                         /*pozice e<index+2000,4095> */
364                         goto index_lost;
365                 }
366         }else{                          /*index e<2097,4095> */
367                 if (pos<=index-2097){           /*pozice e<0,index-2097> */
368                         /*po smeru h.r. od indexu - pretecena pozice*/
369                         dist=4096+pos-index;
370                 }else if (pos<index-1999){      /*pozice e<index-2096,index-2000> */
371                         goto index_lost;
372                 }else if (pos<index){           /*pozice e<index-1999,index-1> */
373                         /*proti smeru h.r. od indexu*/
374                         dist=pos+2000-index;
375                 }else{                          /*pozice e<index,4095> */
376                         /*po smeru h.r. od indexu*/
377                         dist=pos-index;
378                 }
379         }
380
381         rps.index_dist = dist;
382         return;
383
384         index_lost:
385                 rps.index_ok=0;
386                 return;
387 }
388 /*
389  * \brief
390  * Very simple PID regulator.
391  * Now only with P-part so that the error doesnt go to zero.
392  * TODO: add anti-wind up and I and D parts
393  */
394 inline void pid(){
395         int duty_tmp;
396         duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
397         if (duty_tmp>MAX_DUTY){
398                 rps.duty=MAX_DUTY;
399         }else if (duty_tmp<-MAX_DUTY){
400                 rps.duty=-MAX_DUTY;
401         }else{
402                 rps.duty = duty_tmp;
403         }
404 }
405 /*
406  * \brief
407  * Feedback loop.
408  */
409 void * read_data(void* param){
410         int i;
411         struct rpi_in pocatek;
412         struct timespec t;
413         int interval = 1000000; /* 1ms ~ 1kHz*/
414         uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
415         char first=1;
416         uint16_t last_index;                            /*we have index up-to date*/
417         pocatek = spi_read(tx);
418         clock_gettime(CLOCK_MONOTONIC ,&t);
419         /* start after one second */
420         t.tv_sec++;
421                 while(1){
422                         /* wait until next shot */
423                         clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
424                         sem_wait(&rps.thd_par_sem);             /*---take semaphore---*/
425                         prepare_tx(tx);                 /*save the data to send*/
426                         data = spi_read(tx);            /*exchange data*/
427                         /*subtract initiate postion */
428                         rps.tf_count++;
429                         substractOffset(&data,&pocatek);
430                         comIndDist();
431                         if (!rps.index_ok){
432                                 if (first){
433                                         last_index=data.index_position;
434                                         first=0;
435                                 }else if (last_index!=data.index_position){
436                                         rps.index_ok=1;
437                                 }
438                         }
439                         pid();
440                         if (rps.index_ok && rps.commutate){
441                                 /*simple_ind_dist_commutator(rps.duty);*/
442                                 sin_commutator(rps.duty);
443                         }else if(!rps.index_ok && rps.commutate){
444                                 simple_hall_commutator(rps.duty);
445                         }
446                         sem_post(&rps.thd_par_sem);             /*--post semaphore---*/
447
448                         /* calculate next shot */
449                         t.tv_nsec += interval;
450
451                         while (t.tv_nsec >= NSEC_PER_SEC) {
452                                 t.tv_nsec -= NSEC_PER_SEC;
453                                 t.tv_sec++;
454                         }
455
456                 }
457 }
458
459
460
461 /**
462  * \brief Main function.
463  */
464
465 int main(){
466         pthread_t base_thread_id;
467         clk_init();             /* inicializace gpio hodin */
468         spi_init();             /* iniicializace spi*/
469
470         /*semafor pro detekci zpracovani parametru vlaken*/
471         sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
472         setup_environment();
473
474         base_thread_id=pthread_self();
475
476         /*main control loop*/
477         create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
478
479         /*monitor of current state*/
480         create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
481
482         /*wait for commands*/
483         poll_cmd(&rps);
484
485         return 0;
486 }