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*/
18 #include <time.h> /*nanosleep*/
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 */
27 #define PRUM_PROUD 2061
28 #define PRUM_SOUC 6183
36 #define THREAD_SHARED 0
37 #define INIT_VALUE 1 /*init value for semaphor*/
40 #define PXMC_SIN_FIX_TAB_BITS 9
41 #define PXMC_SIN_FIX_IDX_SLR 23
42 #define PXMC_SIN_FIX_XD_MASK 0x007fffff
43 #define PXMC_SIN_FIX_XD_SLR 8
44 #define PXMC_SIN_FIX_A_MASK 0xffffc000
45 #define PXMC_SIN_FIX_B_SLL 19
46 #define PXMC_SIN_FIX_B_SAR 16
47 #define PXMC_SIN_FIX_B_XD_SAR 6
48 #define PXMC_SIN_FIX_ZIC_MASK 0x00002000
49 #define PXMC_SIN_FIX_ZIC_BIT 13
51 #define PXMC_SIN_FIX_PI2 0x40000000
52 #define PXMC_SIN_FIX_2PI3 0x55555555
54 #define NSEC_PER_SEC (1000000000) /* The number of nsecs per sec. */
61 uint16_t pwm1, pwm2, pwm3;
62 uint16_t t_pwm1, t_pwm2, t_pwm3;
64 int duty; /* duty cycle of pwm */
65 uint16_t index_dist; /* distance to index position */
66 unsigned char index_ok;
67 uint32_t tf_count; /*number of transfer*/
68 int desired_pos; /* desired position */
73 * \brief Initilizes GPCLK.
77 initialise(); /*namapovani gpio*/
78 initClock(PLLD_500_MHZ, 10, 0);
79 gpioSetMode(4, FSEL_ALT0);
83 * \brief Terminates GPCLK.
86 inline void clk_disable(){
91 * \brief Signal handler pro Ctrl+C
96 /*muzeme zavrit semafor*/
97 sem_destroy(&thd_par_sem);
98 printf("\nprogram bezpecne ukoncen\n");
101 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
102 data->pozice_raw=data->pozice;
103 data->pozice-=offset->pozice;
107 * pocita procentualni odchylku od prumerneho proudu
109 float diff_p(float value){
110 return ((float)value-PRUM_PROUD)*100/PRUM_PROUD;
113 * pocita procentualni odchylku od prumerneho souctu proudu
115 float diff_s(float value){
116 return ((float)value-PRUM_SOUC)*100/PRUM_SOUC;
119 * tiskne potrebna data
122 struct rpi_in data_p;
123 struct rpi_state s; /*state*/
124 float cur0, cur1, cur2;
127 sem_wait(&thd_par_sem);
130 sem_post(&thd_par_sem);
132 if (data_p.adc_m_count){
133 cur0=data_p.ch0/data_p.adc_m_count;
134 cur1=data_p.ch1/data_p.adc_m_count;
135 cur2=data_p.ch2/data_p.adc_m_count;
137 for (i = 0; i < 16; i++) {
140 printf("%.2X ", data_p.debug_rx[i]);
143 printf("\npozice=%d\n",(int32_t)data_p.pozice);
144 printf("chtena pozice=%d\n",s.desired_pos);
145 printf("transfer count=%u\n",s.tf_count);
146 printf("raw_pozice=%u\n",data_p.pozice_raw);
147 printf("raw_pozice last12=%u\n",(data_p.pozice_raw&0x0FFF));
148 printf("index position=%u\n",data_p.index_position);
149 printf("hal1=%d, hal2=%d, hal3=%d\n",data_p.hal1,data_p.hal2,data_p.hal3);
150 printf("en1=%d, en2=%d, en3=%d (Last sent)\n",!!(0x40&s.test),!!(0x20&s.test),!!(0x10&s.test));
151 printf("shdn1=%d, shdn2=%d, shdn3=%d (L.s.)\n",!!(0x08&s.test),!!(0x04&s.test),!!(0x02&s.test));
152 printf("PWM1=%u(L.s.)\n",s.pwm1);
153 printf("PWM2=%u(L.s.)\n",s.pwm2);
154 printf("PWM3=%u(L.s.)\n",s.pwm3);
155 printf("distance to index=%u\n",s.index_dist);
156 printf("T_PWM1=%u T_PWM2=%u T_PWM3=%u\n",s.t_pwm1,s.t_pwm2, s.t_pwm3);
157 printf("Pocet namerenych proudu=%u\n",data_p.adc_m_count);
158 printf("(pwm1) (ch1)=%d (avg=%4.0f) (%2.2f%%)\n",data_p.ch1,cur1,diff_p(cur1));
159 printf("(pwm2) (ch2)=%d (avg=%4.0f)(%2.2f%%)\n",data_p.ch2,cur2,diff_p(cur2));
160 printf("(pwm3) (ch0)=%d (avg=%4.0f)(%2.2f%%)\n",data_p.ch0,cur0,diff_p(cur0));
161 printf("soucet prumeru=%5.0f (%2.2f%%)\n",cur0+cur1+cur2,diff_s(cur0+cur1+cur2));
162 printf("duty=%d\n",s.duty);
163 if (s.index_ok) printf("index ok\n");
164 if (s.commutate) printf("commutation in progress\n");
166 void prepare_tx(uint8_t * tx){
169 * tx[4] - bity 95 downto 88 - bits that are sent first
170 * tx[5] - bity 87 downto 80
171 * tx[6] - bity 79 downto 72
172 * tx[7] - bity 71 downto 64
173 * tx[8] - bity 63 downto 56
174 * tx[9] - bity 55 downto 48
175 * tx[10] - bity 47 downto 40
176 * tx[11] - bity 39 downto 32
177 * tx[12] - bity 31 downto 24
178 * tx[13] - bity 23 downto 16
179 * tx[14] - bity 15 downto 8
180 * tx[15] - bity 7 downto 0
183 * bit 94 - enable PWM1
184 * bit 93 - enable PWM2
185 * bit 92 - enable PWM3
194 * bits 47 .. 32 - match PWM1
195 * bits 31 .. 16 - match PWM2
196 * bits 15 .. 0 - match PWM3
202 /* keep the 11-bit cap*/
204 if (rps.pwm1>2047) rps.pwm1=2047;
205 if (rps.pwm2>2047) rps.pwm2=2047;
206 if (rps.pwm3>2047) rps.pwm3=2047;
208 tx[0]=rps.test; /*bit 94 - enable PWM1*/
210 /*now we have to switch the bytes due to endianess */
211 /* ARMv6 & ARMv7 instructions are little endian */
213 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
214 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
217 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
218 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
221 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
222 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
227 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
229 void * pos_monitor(void* param){
232 usleep(1000000); /*1 Hz*/
238 * Multiplication of 11 bit
240 inline int16_t mult_cap(int32_t s,int d){
244 /* multiplicate as if maximum sinus value was unity */
245 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
250 int sin_commutator(int duty){
251 #define DEGREE_60 715827883
252 #define DEGREE_120 1431655765
253 #define DEGREE_180 2147483648
254 #define DEGREE_240 2863311531
255 #define DEGREE_300 3579139413
258 pos=rps.index_dist*4294967;
259 if (duty>=0){ /*clockwise rotation*/
261 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
262 rps.pwm1=mult_cap(sin, duty);
265 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
266 rps.pwm2=mult_cap(sin, duty);;
269 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
270 rps.pwm3=mult_cap(sin, duty);;
275 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
276 rps.pwm1=mult_cap(sin, duty);
279 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
280 rps.pwm2=mult_cap(sin, duty);
283 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
284 rps.pwm3=mult_cap(sin, duty);
290 * Test function to be placed in controll loop.
291 * Switches PWM's at point where they produce same force.
292 * This points are found thanks to IRC position,
295 void simple_ind_dist_commutator(int duty){
296 if (duty>=0){ /* clockwise - so that position increase */
298 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
299 (rps.index_dist>=1048 && rps.index_dist<=1377)){
304 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
305 (rps.index_dist>=1377 && rps.index_dist<=1711)){
310 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
311 (rps.index_dist>=711 && rps.index_dist<=1048) ||
312 (rps.index_dist>=1711 && rps.index_dist<=1999)){
317 }else{ /*counter-clockwise - position decrease */
319 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
320 (rps.index_dist>=1544 && rps.index_dist<=1878)){
325 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
326 (rps.index_dist>=881 && rps.index_dist<=1210) ||
327 (rps.index_dist>=1878 && rps.index_dist<=1999)){
332 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
333 (rps.index_dist>=1210 && rps.index_dist<=1544)){
342 * Test function to be placed in controll loop.
343 * Switches PWM's at point where they produce same force
345 inline void simple_hall_commutator(int duty){
346 if (duty>=0){ /* clockwise - so that position increase */
348 if (data.hal2 && !data.hal3){
353 }else if (data.hal1 && !data.hal2){
358 }else if (!data.hal1 && data.hal3){
363 }else{ /*counter-clockwise - position decrease */
365 if (!data.hal2 && data.hal3){
370 }else if (!data.hal1 && data.hal2){
375 }else if (data.hal1 && !data.hal3){
384 * Computation of distance to index.
386 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
387 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
389 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
393 uint16_t pos = 0x0FFF & data.pozice_raw;
395 uint16_t index = data.index_position;
397 if (index<1999){ /*index e<0,1998> */
398 if (pos<index){ /*pozice e<0,index-1> */
399 /*proti smeru h.r. od indexu*/
401 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
402 /*po smeru h.r. od indexu*/
404 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
406 }else{ /*pozice e<index+2096,4095> */
407 /*proti smeru h.r. od indexu - podtecena pozice*/
410 }else if (index<=2096){ /*index e<1999,2096>*/
411 if (pos<index-1999){ /*pozice e<0,index-2000> */
413 }else if (pos<index){ /*pozice e<index-1999,index-1> */
414 /*proti smeru h.r. od indexu*/
416 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
417 /*po smeru h.r. od indexu*/
419 }else { /*pozice e<index+2000,4095> */
422 }else{ /*index e<2097,4095> */
423 if (pos<=index-2097){ /*pozice e<0,index-2097> */
424 /*po smeru h.r. od indexu - pretecena pozice*/
426 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
428 }else if (pos<index){ /*pozice e<index-1999,index-1> */
429 /*proti smeru h.r. od indexu*/
431 }else{ /*pozice e<index,4095> */
432 /*po smeru h.r. od indexu*/
437 rps.index_dist = dist;
446 * Very simple PID regulator.
447 * Now only with P-part so that the error doesnt go to zero.
448 * TODO: add anti-wind up and I and D parts
452 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
453 if (duty_tmp>MAX_DUTY){
455 }else if (duty_tmp<-MAX_DUTY){
465 void * read_data(void* param){
467 struct rpi_in pocatek;
469 int interval = 1000000; /* 1ms ~ 1kHz*/
470 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
472 uint16_t last_index; /*we have index up-to date*/
473 pocatek = spi_read(tx);
474 clock_gettime(CLOCK_MONOTONIC ,&t);
475 /* start after one second */
478 /* wait until next shot */
479 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
480 sem_wait(&thd_par_sem); /*---take semaphore---*/
481 prepare_tx(tx); /*save the data to send*/
482 data = spi_read(tx); /*exchange data*/
483 /*subtract initiate postion */
485 substractOffset(&data,&pocatek);
489 last_index=data.index_position;
491 }else if (last_index!=data.index_position){
496 if (rps.index_ok && rps.commutate){
497 /*simple_ind_dist_commutator(rps.duty);*/
498 sin_commutator(rps.duty);
499 }else if(!rps.index_ok && rps.commutate){
500 simple_hall_commutator(rps.duty);
502 sem_post(&thd_par_sem); /*--post semaphore---*/
504 /* calculate next shot */
505 t.tv_nsec += interval;
507 while (t.tv_nsec >= NSEC_PER_SEC) {
508 t.tv_nsec -= NSEC_PER_SEC;
518 * Commands detection.
524 * pri pouziti scanf("%u",&simple_hall_duty); dochazelo
525 * k preukladani hodnot na promenne test. Dost divne.
529 printf("volba=%u\n",tmp);
533 sem_wait(&thd_par_sem);
535 sem_post(&thd_par_sem);
539 sem_wait(&thd_par_sem);
541 sem_post(&thd_par_sem);
545 sem_wait(&thd_par_sem);
547 sem_post(&thd_par_sem);
551 sem_wait(&thd_par_sem);
553 sem_post(&thd_par_sem);
556 sem_wait(&thd_par_sem);
557 rps.commutate=!rps.commutate;
558 /* switch off pwms at the end of commutation */
559 rps.pwm1&=rps.commutate*0xFFFF;
560 rps.pwm2&=rps.commutate*0xFFFF;
561 rps.pwm3&=rps.commutate*0xFFFF;
562 sem_post(&thd_par_sem);
566 sem_wait(&thd_par_sem);
568 sem_post(&thd_par_sem);
572 sem_wait(&thd_par_sem);
574 sem_post(&thd_par_sem);
585 * \brief Main function.
589 pthread_t base_thread_id;
590 clk_init(); /* inicializace gpio hodin */
591 spi_init(); /* iniicializace spi*/
593 /*semafor pro detekci zpracovani parametru vlaken*/
594 sem_init(&thd_par_sem,THREAD_SHARED,INIT_VALUE);
597 base_thread_id=pthread_self();
599 /*main control loop*/
600 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
602 /*monitor of current state*/
603 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
605 /*wait for commands*/