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*/
21 #include "rpin.h" /*gpclk*/
22 #include "rp_spi.h" /*spi*/
23 #include "misc.h" /*structure for priorities*/
24 #include "pxmc_sin_fixed.h" /*to test sin commutation */
25 #include "pmsm_state.h"
27 #include "controllers.h"
36 #define THREAD_SHARED 0
37 #define INIT_VALUE 1 /*init value for semaphor*/
40 #define NSEC_PER_SEC (1000000000) /* The number of nsecs per sec. */
44 struct rpi_state rps={
48 .pwm1=0,.pwm2=0, .pwm3=0,
49 .pwm1=0, .t_pwm2=0, .t_pwm3=0,
51 .duty=0, /* duty cycle of pwm */
52 .index_dist=0, /* distance to index position */
54 .tf_count=0, /*number of transfer*/
55 .desired_pos=0, /* desired position */
61 .log_col_count=0, /* pocet radku zaznamu */
68 * \brief Initilizes GPCLK.
72 initialise(); /*namapovani gpio*/
73 initClock(PLLD_500_MHZ, 10, 0);
74 gpioSetMode(4, FSEL_ALT0);
78 * \brief Terminates GPCLK.
81 inline void clk_disable(){
86 * Count minimum value of three numbers.
87 * Input values must be in range <-2^28;2^28>.
89 int32_t min(int32_t x, int32_t y, int32_t z){
93 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze y je vetsi*/
94 x=y+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
97 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze z je vetsi*/
98 x=z+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
109 if (rps.log_col_count){
110 for (r=0;r<LOG_ROWS;r++){
124 if (rps.log_col==MAX_LOGS-1){
128 rps.logs[0][rps.log_col]=(int)rps.tf_count;
129 rps.logs[1][rps.log_col]=(int)rps.spi_dat->pozice;
131 rps.logs[2][rps.log_col]=(int)rps.pwm1;
132 rps.logs[3][rps.log_col]=(int)rps.pwm2;
133 rps.logs[4][rps.log_col]=(int)rps.pwm3;
134 rps.logs[5][rps.log_col]=rps.duty;
136 rps.logs[6][rps.log_col]=rps.desired_spd;
137 rps.logs[7][rps.log_col]=rps.speed;
139 rps.logs[8][rps.log_col]=(int)(rps.spi_dat->ch1/rps.spi_dat->adc_m_count);
140 rps.logs[9][rps.log_col]=(int)(rps.spi_dat->ch2/rps.spi_dat->adc_m_count);
141 rps.logs[10][rps.log_col]=(int)(rps.spi_dat->ch0/rps.spi_dat->adc_m_count);
145 if (rps.log_col==rps.log_col_count-1){
146 rps.log_col_count*=2;
147 rps.log_col_count%=MAX_LOGS;
148 for (r=0;r<LOG_ROWS;r++){
149 rps.logs[r]=realloc(rps.logs[r],rps.log_col_count*sizeof(int));
150 if (rps.logs[r]==NULL){
161 * Pripravi psi buffer
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
191 * bits 47 .. 32 - match PWM1
192 * bits 31 .. 16 - match PWM2
193 * bits 15 .. 0 - match PWM3
199 /* keep the 11-bit cap*/
201 if (rps.pwm1>2047) rps.pwm1=2047;
202 if (rps.pwm2>2047) rps.pwm2=2047;
203 if (rps.pwm3>2047) rps.pwm3=2047;
205 tx[0]=rps.test; /*bit 94 - enable PWM1*/
207 /*now we have to switch the bytes due to endianess */
208 /* ARMv6 & ARMv7 instructions are little endian */
210 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
211 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
214 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
215 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
218 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
219 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
226 * \brief Signal handler pro Ctrl+C
230 sem_wait(&rps.thd_par_sem);
232 memset(tx,0,16*sizeof(int));
236 prepare_tx(tx); /*save the data to send*/
242 /*muzeme zavrit semafor*/
243 sem_destroy(&rps.thd_par_sem);
244 printf("\nprogram bezpecne ukoncen\n");
247 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
248 data->pozice=data->pozice_raw-offset->pozice_raw;
253 * Transformace pro uhel pocitany po smeru hodinovych rucicek
255 void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){
260 void alphabeta2pwm3(int32_t * ia, int32_t * ib, int32_t *ic,int32_t alpha, int32_t beta){
262 *ib=-alpha/2+beta*887/1024;
263 *ic=-alpha/2-beta*887/1024;
267 * Preocita napeti na jednotlivych civkach na napeti,
268 * ktera budou privedena na svorky motoru.
269 * Tedy na A(yel)-pwm1, B(red)-pwm2, C(blk)-pwm3
271 void transDelta(int32_t * u1, int32_t * u2, int32_t *u3, int32_t ub , int32_t uc){
274 /*vypocte napeti tak, aby odpovidaly rozdily*/
279 /*najde zaporne napeti*/
282 /*dorovna zaporna napeti na nulu*/
287 void inv_trans_comm(int duty){
291 int32_t pwma,pwmb,pwmc;
293 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
295 /*use it as cyclic 32-bit logic*/
297 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
298 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
301 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
308 rps.t_pwm1=(uint16_t)pwma;
309 rps.t_pwm3=(uint16_t)pwmb;
310 rps.t_pwm2=(uint16_t)pwmc;
313 void inv_trans_comm_2(int duty){
322 pos+=rps.alpha_offset; /*zarovnani faze 'a' s osou 'alpha'*/
324 /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/
326 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
328 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
332 alphabeta2pwm3(&ia,&ib, &ic,alpha,beta);
338 transDelta(&u1,&u2, &u3,ub,uc);
340 rps.pwm1=(uint16_t)u1;
341 rps.pwm2=(uint16_t)u2;
342 rps.pwm3=(uint16_t)u3;
346 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
348 void * pos_monitor(void* param){
351 usleep(1000000); /*1 Hz*/
357 * Multiplication of 11 bit
358 * Zaporne vysledky prvede na nulu.
360 inline uint16_t mult_cap(int32_t s,int d){
364 /* multiplicate as if maximum sinus value was unity */
365 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
371 int sin_commutator(int duty){
372 #define DEGREE_60 715827883
373 #define DEGREE_120 1431655765
374 #define DEGREE_180 2147483648
375 #define DEGREE_240 2863311531
376 #define DEGREE_300 3579139413
381 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
383 /*use it as cyclic 32-bit logic*/
385 if (duty>=0){ /*clockwise rotation*/
387 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
390 rps.pwm1=(uint16_t)pwm;
393 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
396 rps.pwm2=(uint16_t)pwm;
399 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
402 rps.pwm3=(uint16_t)pwm;
407 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
410 rps.pwm1=(uint16_t)pwm;
413 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
416 rps.pwm2=(uint16_t)pwm;
419 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
422 rps.pwm3=(uint16_t)pwm;
428 * Test function to be placed in controll loop.
429 * Switches PWM's at point where they produce same force.
430 * This points are found thanks to IRC position,
433 void simple_ind_dist_commutator(int duty){
434 if (duty>=0){ /* clockwise - so that position increase */
436 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
437 (rps.index_dist>=1048 && rps.index_dist<=1377)){
442 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
443 (rps.index_dist>=1377 && rps.index_dist<=1711)){
448 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
449 (rps.index_dist>=711 && rps.index_dist<=1048) ||
450 (rps.index_dist>=1711 && rps.index_dist<=1999)){
455 }else{ /*counter-clockwise - position decrease */
457 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
458 (rps.index_dist>=1544 && rps.index_dist<=1878)){
463 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
464 (rps.index_dist>=881 && rps.index_dist<=1210) ||
465 (rps.index_dist>=1878 && rps.index_dist<=1999)){
470 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
471 (rps.index_dist>=1210 && rps.index_dist<=1544)){
480 * Test function to be placed in controll loop.
481 * Switches PWM's at point where they produce same force
483 inline void simple_hall_commutator(int duty){
484 if (duty>=0){ /* clockwise - so that position increase */
486 if (data.hal2 && !data.hal3){
491 }else if (data.hal1 && !data.hal2){
496 }else if (!data.hal1 && data.hal3){
501 }else{ /*counter-clockwise - position decrease */
503 if (!data.hal2 && data.hal3){
508 }else if (!data.hal1 && data.hal2){
513 }else if (data.hal1 && !data.hal3){
522 * Computation of distance to index.
524 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
525 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
527 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
531 uint16_t pos = 0x0FFF & data.pozice_raw;
533 uint16_t index = data.index_position;
535 if (index<1999){ /*index e<0,1998> */
536 if (pos<index){ /*pozice e<0,index-1> */
537 /*proti smeru h.r. od indexu*/
539 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
540 /*po smeru h.r. od indexu*/
542 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
544 }else{ /*pozice e<index+2096,4095> */
545 /*proti smeru h.r. od indexu - podtecena pozice*/
548 }else if (index<=2096){ /*index e<1999,2096>*/
549 if (pos<index-1999){ /*pozice e<0,index-2000> */
551 }else if (pos<index){ /*pozice e<index-1999,index-1> */
552 /*proti smeru h.r. od indexu*/
554 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
555 /*po smeru h.r. od indexu*/
557 }else { /*pozice e<index+2000,4095> */
560 }else{ /*index e<2097,4095> */
561 if (pos<=index-2097){ /*pozice e<0,index-2097> */
562 /*po smeru h.r. od indexu - pretecena pozice*/
564 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
566 }else if (pos<index){ /*pozice e<index-1999,index-1> */
567 /*proti smeru h.r. od indexu*/
569 }else{ /*pozice e<index,4095> */
570 /*po smeru h.r. od indexu*/
575 rps.index_dist = dist;
590 spd=rps.spi_dat->pozice-rps.old_pos[rps.tf_count%OLD_POS_NUM];
591 rps.speed=(int32_t)spd;
597 * TODO: replace bunch of 'IFs' with Object-like pattern
599 void * read_data(void* param){
601 struct rpi_in pocatek;
603 int interval = 1000000; /* 1ms ~ 1kHz*/
604 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
606 uint16_t last_index; /*we have index up-to date*/
607 pocatek = spi_read(tx);
608 clock_gettime(CLOCK_MONOTONIC ,&t);
609 /* start after one second */
612 /* wait until next shot */
613 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
614 sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
617 rps.old_pos[rps.tf_count%OLD_POS_NUM]=rps.spi_dat->pozice;
618 prepare_tx(tx); /*save the data to send*/
619 data = spi_read(tx); /*exchange data*/
620 /*subtract initiate postion */
622 substractOffset(&data,&pocatek);
623 compSpeed(); /*spocita rychlost*/
627 last_index=data.index_position;
629 }else if (last_index!=data.index_position){
631 comIndDist(); /*vypocet vzdalenosti indexu*/
633 }else{ /*index je v poradku*/
634 comIndDist(); /*vypocet vzdalenosti indexu*/
637 /* pocitame sirku plneni podle potreb rizeni*/
638 if (rps.pos_reg_ena){ /*pozicni rizeni*/
640 }else if(rps.spd_reg_ena){ /*rizeni na rychlost*/
644 /* sirka plneni prepoctena na jednotlive pwm */
645 if (rps.index_ok && rps.commutate){
646 /*simple_ind_dist_commutator(rps.duty);*/
647 /*sin_commutator(rps.duty);*/
648 inv_trans_comm(rps.duty);
649 inv_trans_comm_2(rps.duty);
650 }else if(!rps.index_ok && rps.commutate){
651 simple_hall_commutator(rps.duty);
654 /*zalogujeme hodnoty*/
655 if (rps.doLogs && !(rps.tf_count%LOG_PERIOD)){
659 sem_post(&rps.thd_par_sem); /*--post semaphore---*/
661 /* calculate next shot */
662 t.tv_nsec += interval;
664 while (t.tv_nsec >= NSEC_PER_SEC) {
665 t.tv_nsec -= NSEC_PER_SEC;
674 * \brief Main function.
678 pthread_t base_thread_id;
679 clk_init(); /* inicializace gpio hodin */
680 spi_init(); /* iniicializace spi*/
682 /*semafor pro detekci zpracovani parametru vlaken*/
683 sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
686 base_thread_id=pthread_self();
688 /*main control loop*/
689 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
691 /*monitor of current state*/
692 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
694 /*wait for commands*/