3 * \author Martin Prudek
4 * \brief Mainfile pro pmsm control.
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*/
19 #include "rpin.h" /*gpclk*/
20 #include "rp_spi.h" /*spi*/
21 #include "misc.h" /*structure for priorities*/
22 #include "pxmc_sin_fixed.h" /*to test sin commutation */
26 #define PRUM_PROUD 2061
27 #define PRUM_SOUC 6183
35 #define THREAD_SHARED 0
36 #define INIT_VALUE 0 /*init value for semaphor*/
39 #define PXMC_SIN_FIX_TAB_BITS 9
40 #define PXMC_SIN_FIX_IDX_SLR 23
41 #define PXMC_SIN_FIX_XD_MASK 0x007fffff
42 #define PXMC_SIN_FIX_XD_SLR 8
43 #define PXMC_SIN_FIX_A_MASK 0xffffc000
44 #define PXMC_SIN_FIX_B_SLL 19
45 #define PXMC_SIN_FIX_B_SAR 16
46 #define PXMC_SIN_FIX_B_XD_SAR 6
47 #define PXMC_SIN_FIX_ZIC_MASK 0x00002000
48 #define PXMC_SIN_FIX_ZIC_BIT 13
50 #define PXMC_SIN_FIX_PI2 0x40000000
51 #define PXMC_SIN_FIX_2PI3 0x55555555
53 struct sigaction sighnd; /*struktura pro signal handler*/
57 uint16_t pwm1, pwm2, pwm3;
58 uint16_t t_pwm1, t_pwm2, t_pwm3;
60 int duty; /* duty cycle of pwm */
61 uint16_t index_dist; /* distance to index position */
62 unsigned char index_ok;
63 uint32_t tf_count; /*number of transfer*/
64 int desired_pos; /* desired position */
69 * \brief Initilizes GPCLK.
73 initialise(); /*namapovani gpio*/
74 initClock(PLLD_500_MHZ, 10, 0);
75 gpioSetMode(4, FSEL_ALT0);
79 * \brief Terminates GPCLK.
82 inline void clk_disable(){
87 * \brief Signal handler pro Ctrl+C
92 /*muzeme zavrit semafor*/
93 sem_destroy(&thd_par_sem);
94 printf("\nprogram bezpecne ukoncen\n");
98 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
99 data->pozice_raw=data->pozice;
100 data->pozice-=offset->pozice;
104 * pocita procentualni odchylku od prumerneho proudu
106 float diff_p(float value){
107 return ((float)value-PRUM_PROUD)*100/PRUM_PROUD;
110 * pocita procentualni odchylku od prumerneho souctu proudu
112 float diff_s(float value){
113 return ((float)value-PRUM_SOUC)*100/PRUM_SOUC;
116 * tiskne potrebna data
119 struct rpi_in data_p;
120 struct rpi_state s; /*state*/
121 float cur0, cur1, cur2;
124 sem_wait(&thd_par_sem);
127 sem_post(&thd_par_sem);
129 if (data_p.adc_m_count){
130 cur0=data_p.ch0/data_p.adc_m_count;
131 cur1=data_p.ch1/data_p.adc_m_count;
132 cur2=data_p.ch2/data_p.adc_m_count;
134 for (i = 0; i < 16; i++) {
137 printf("%.2X ", data_p.debug_rx[i]);
140 printf("\npozice=%d\n",(int32_t)data_p.pozice);
141 printf("chtena pozice=%d\n",s.desired_pos);
142 printf("transfer count=%u\n",s.tf_count);
143 printf("raw_pozice=%d\n",(int32_t)data_p.pozice_raw);
144 printf("raw_pozice last12=%u\n",(data_p.pozice_raw&0x0FFF));
145 printf("index position=%u\n",data_p.index_position);
146 printf("hal1=%d, hal2=%d, hal3=%d\n",data_p.hal1,data_p.hal2,data_p.hal3);
147 printf("en1=%d, en2=%d, en3=%d (Last sent)\n",!!(0x40&s.test),!!(0x20&s.test),!!(0x10&s.test));
148 printf("shdn1=%d, shdn2=%d, shdn3=%d (L.s.)\n",!!(0x08&s.test),!!(0x04&s.test),!!(0x02&s.test));
149 printf("PWM1=%u(L.s.)\n",s.pwm1);
150 printf("PWM2=%u(L.s.)\n",s.pwm2);
151 printf("PWM3=%u(L.s.)\n",s.pwm3);
152 printf("distance to index=%u\n",s.index_dist);
153 printf("T_PWM1=%u T_PWM2=%u T_PWM3=%u\n",s.t_pwm1,s.t_pwm2, s.t_pwm3);
154 printf("Pocet namerenych proudu=%u\n",data_p.adc_m_count);
155 printf("(pwm1) (ch1)=%d (avg=%4.0f) (%2.2f%%)\n",data_p.ch1,cur1,diff_p(cur1));
156 printf("(pwm2) (ch2)=%d (avg=%4.0f)(%2.2f%%)\n",data_p.ch2,cur2,diff_p(cur2));
157 printf("(pwm3) (ch0)=%d (avg=%4.0f)(%2.2f%%)\n",data_p.ch0,cur0,diff_p(cur0));
158 printf("soucet prumeru=%5.0f (%2.2f%%)\n",cur0+cur1+cur2,diff_s(cur0+cur1+cur2));
159 printf("duty=%d\n",s.duty);
160 if (s.index_ok) printf("index ok\n");
161 if (s.commutate) printf("commutation in progress\n");
163 void prepare_tx(uint8_t * tx){
166 * tx[4] - bity 95 downto 88 - bits that are sent first
167 * tx[5] - bity 87 downto 80
168 * tx[6] - bity 79 downto 72
169 * tx[7] - bity 71 downto 64
170 * tx[8] - bity 63 downto 56
171 * tx[9] - bity 55 downto 48
172 * tx[10] - bity 47 downto 40
173 * tx[11] - bity 39 downto 32
174 * tx[12] - bity 31 downto 24
175 * tx[13] - bity 23 downto 16
176 * tx[14] - bity 15 downto 8
177 * tx[15] - bity 7 downto 0
180 * bit 94 - enable PWM1
181 * bit 93 - enable PWM2
182 * bit 92 - enable PWM3
189 * bits 66 .. 56 - match PWM1
190 * bits 55 .. 45 - match PWM2
192 * bits 42 .. 32 - match PWM3
200 if (rps.pwm1>2047) rps.pwm1=2047;
201 if (rps.pwm2>2047) rps.pwm2=2047;
202 if (rps.pwm3>2047) rps.pwm3=2047;
204 tx[0]=rps.test; /*bit 94 - enable PWM1*/
207 tx[7]=(tx[7] & 0xF8) | (0x07 & ((uint8_t*)&rps.pwm1)[1]); /*MSB*/
208 tx[8]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
213 tx[9]=((uint8_t*)&tmp)[1]; /*MSB*/
214 tx[10]=(tx[10] & 0x1F) | (0xE0 & ((uint8_t*)&tmp)[0]); /*LSB*/
217 tx[10]=(tx[10] & 0xF8) | (0x07 & ((uint8_t*)&rps.pwm3)[1]); /*MSB*/
218 tx[11]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
223 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
225 void * pos_monitor(void* param){
226 set_priority(param); /*set priority*/
229 usleep(1000000); /*1 Hz*/
235 int sin_commutator(int duty){
236 #define DEGREE_60 715827883
237 #define DEGREE_120 1431655765
238 #define DEGREE_180 2147483648
239 #define DEGREE_240 2863311531
240 #define DEGREE_300 3579139413
244 pos=rps.index_dist*4294967;
245 if (duty>=0){ /*clockwise rotation*/
247 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
250 /* multiplicate as if maximum sinus value was unity */
251 pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
256 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
259 /* multiplicate as if maximum sinus value was unity */
260 pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
265 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
268 /* multiplicate as if maximum sinus value was unity */
269 pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
276 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
279 /* multiplicate as if maximum sinus value was unity */
280 pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
285 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
288 /* multiplicate as if maximum sinus value was unity */
289 pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
294 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
297 /* multiplicate as if maximum sinus value was unity */
298 pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
306 * Test function to be placed in controll loop.
307 * Switches PWM's at point where they produce same force.
308 * This points are found thanks to IRC position,
311 void simple_ind_dist_commutator(int duty){
312 if (duty>=0){ /* clockwise - so that position increase */
314 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
315 (rps.index_dist>=1048 && rps.index_dist<=1377)){
320 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
321 (rps.index_dist>=1377 && rps.index_dist<=1711)){
326 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
327 (rps.index_dist>=711 && rps.index_dist<=1048) ||
328 (rps.index_dist>=1711 && rps.index_dist<=1999)){
333 }else{ /*counter-clockwise - position decrease */
335 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
336 (rps.index_dist>=1544 && rps.index_dist<=1878)){
341 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
342 (rps.index_dist>=881 && rps.index_dist<=1210) ||
343 (rps.index_dist>=1878 && rps.index_dist<=1999)){
348 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
349 (rps.index_dist>=1210 && rps.index_dist<=1544)){
358 * Test function to be placed in controll loop.
359 * Switches PWM's at point where they produce same force
361 inline void simple_hall_commutator(int duty){
362 if (duty>=0){ /* clockwise - so that position increase */
364 if (data.hal2 && !data.hal3){
369 }else if (data.hal1 && !data.hal2){
374 }else if (!data.hal1 && data.hal3){
379 }else{ /*counter-clockwise - position decrease */
381 if (!data.hal2 && data.hal3){
386 }else if (!data.hal1 && data.hal2){
391 }else if (data.hal1 && !data.hal3){
399 * Funkce pravidelne vycita data z motoru
401 inline void comIndDist(){
402 rps.index_dist=0x0FFF & (data.pozice_raw - data.index_position);
404 * if distance is bigger than 2047, the distance underflown
405 * -> if 12th bit is set, substract 2096
407 rps.index_dist-=((rps.index_dist & 0x0800)>>11)*2096;
411 * Very simple PID regulator.
412 * Now only with P-part so that the error doesnt go to zero.
413 * TODO: add anti-wind up and I and D parts
417 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
418 if (duty_tmp>MAX_DUTY){
420 }else if (duty_tmp<-MAX_DUTY){
429 * TODO: replace usleep with real-time wait
432 void * read_data(void* param){
434 struct rpi_in pocatek;
435 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
437 uint16_t last_index; /*we have index up-to date*/
438 set_priority(param); /*set priority*/
439 pocatek = spi_read(tx);
441 prepare_tx(tx); /*save the data to send*/
442 sem_wait(&thd_par_sem); /*---take semaphore---*/
443 data = spi_read(tx); /*exchange data*/
444 /*subtract initiate postion */
446 substractOffset(&data,&pocatek);
450 last_index=data.index_position;
452 }else if (last_index!=data.index_position){
457 if (rps.index_ok && rps.commutate){
458 /*simple_ind_dist_commutator(rps.duty);*/
459 sin_commutator(rps.duty);
460 }else if(!rps.index_ok && rps.commutate){
461 simple_hall_commutator(rps.duty);
463 sem_post(&thd_par_sem); /*--post semaphore---*/
464 usleep(1000); /*1kHz*/
469 * \brief Main function.
475 /*nastaveni priorit vlaken*/
476 struct thread_param tsp;
477 tsp.sch_policy = SCHED_FIFO;
479 /*nastaveni signalu pro vypnuti pomoci Ctrl+C*/
480 sighnd.sa_handler=&sighnd_fnc;
481 sigaction(SIGINT, &sighnd, NULL );
483 clk_init(); /* inicializace gpio hodin */
484 spi_init(); /* iniicializace spi*/
486 /*semafor pro detekci zpracovani parametru vlaken*/
487 sem_init(&thd_par_sem,THREAD_SHARED,INIT_VALUE);
490 pthread_t tid; /*identifikator vlakna*/
491 pthread_attr_t attr; /*atributy vlakna*/
492 pthread_attr_init(&attr); /*inicializuj implicitni atributy*/
496 /*ziskavani dat z motoru*//*vysoka priorita*/
497 tsp.sch_prior = PRIOR_HIGH;
498 pthread_create(&tid, &attr, read_data, (void*)&tsp);
500 /*vypisovani lokalni pozice*//*nizka priorita*/
501 tsp.sch_prior = PRIOR_LOW;
502 sem_wait(&thd_par_sem);
503 pthread_create(&tid, &attr, pos_monitor, (void*)&tsp);
509 * pri pouziti scanf("%u",&simple_hall_duty); dochazelo
510 * k preukladani hodnot na promenne test. Dost divne.
514 printf("volba=%u\n",tmp);
518 sem_wait(&thd_par_sem);
520 sem_post(&thd_par_sem);
524 sem_wait(&thd_par_sem);
526 sem_post(&thd_par_sem);
530 sem_wait(&thd_par_sem);
532 sem_post(&thd_par_sem);
536 sem_wait(&thd_par_sem);
538 sem_post(&thd_par_sem);
541 sem_wait(&thd_par_sem);
542 rps.commutate=!rps.commutate;
543 /* switch off pwms at the end of commutation */
544 rps.pwm1&=rps.commutate*0xFFFF;
545 rps.pwm2&=rps.commutate*0xFFFF;
546 rps.pwm3&=rps.commutate*0xFFFF;
547 sem_post(&thd_par_sem);
551 sem_wait(&thd_par_sem);
553 sem_post(&thd_par_sem);
557 sem_wait(&thd_par_sem);
559 sem_post(&thd_par_sem);