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"
30 #define PID_P_S 0.8 /*2.3 kmita*/ /*1.4 vhodne jen pro P regulator*/
37 #define THREAD_SHARED 0
38 #define INIT_VALUE 1 /*init value for semaphor*/
41 #define NSEC_PER_SEC (1000000000) /* The number of nsecs per sec. */
45 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 */
67 * \brief Initilizes GPCLK.
71 initialise(); /*namapovani gpio*/
72 initClock(PLLD_500_MHZ, 10, 0);
73 gpioSetMode(4, FSEL_ALT0);
77 * \brief Terminates GPCLK.
80 inline void clk_disable(){
85 * Count minimum value of three numbers.
86 * Input values must be in range <-2^28;2^28>.
88 int32_t min(int32_t x, int32_t y, int32_t z){
92 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze y je vetsi*/
93 x=y+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
96 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze z je vetsi*/
97 x=z+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
108 if (rps.log_col_count){
109 for (r=0;r<LOG_ROWS;r++){
123 if (rps.log_col==MAX_LOGS-1){
127 rps.logs[0][rps.log_col]=(int)rps.tf_count;
128 rps.logs[1][rps.log_col]=(int)rps.spi_dat->pozice;
130 rps.logs[2][rps.log_col]=(int)rps.pwm1;
131 rps.logs[3][rps.log_col]=(int)rps.pwm2;
132 rps.logs[4][rps.log_col]=(int)rps.pwm3;
133 rps.logs[5][rps.log_col]=rps.duty;
135 rps.logs[6][rps.log_col]=rps.desired_spd;
136 rps.logs[7][rps.log_col]=rps.speed;
138 rps.logs[8][rps.log_col]=(int)(rps.spi_dat->ch1/rps.spi_dat->adc_m_count);
139 rps.logs[9][rps.log_col]=(int)(rps.spi_dat->ch2/rps.spi_dat->adc_m_count);
140 rps.logs[10][rps.log_col]=(int)(rps.spi_dat->ch0/rps.spi_dat->adc_m_count);
144 if (rps.log_col==rps.log_col_count-1){
145 rps.log_col_count*=2;
146 rps.log_col_count%=MAX_LOGS;
147 for (r=0;r<LOG_ROWS;r++){
148 rps.logs[r]=realloc(rps.logs[r],rps.log_col_count*sizeof(int));
149 if (rps.logs[r]==NULL){
160 * Pripravi psi buffer
162 void prepare_tx(uint8_t * tx){
165 * tx[4] - bity 95 downto 88 - bits that are sent first
166 * tx[5] - bity 87 downto 80
167 * tx[6] - bity 79 downto 72
168 * tx[7] - bity 71 downto 64
169 * tx[8] - bity 63 downto 56
170 * tx[9] - bity 55 downto 48
171 * tx[10] - bity 47 downto 40
172 * tx[11] - bity 39 downto 32
173 * tx[12] - bity 31 downto 24
174 * tx[13] - bity 23 downto 16
175 * tx[14] - bity 15 downto 8
176 * tx[15] - bity 7 downto 0
179 * bit 94 - enable PWM1
180 * bit 93 - enable PWM2
181 * bit 92 - enable PWM3
190 * bits 47 .. 32 - match PWM1
191 * bits 31 .. 16 - match PWM2
192 * bits 15 .. 0 - match PWM3
198 /* keep the 11-bit cap*/
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*/
206 /*now we have to switch the bytes due to endianess */
207 /* ARMv6 & ARMv7 instructions are little endian */
209 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
210 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
213 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
214 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
217 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
218 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
225 * \brief Signal handler pro Ctrl+C
229 sem_wait(&rps.thd_par_sem);
231 memset(tx,0,16*sizeof(int));
235 prepare_tx(tx); /*save the data to send*/
241 /*muzeme zavrit semafor*/
242 sem_destroy(&rps.thd_par_sem);
243 printf("\nprogram bezpecne ukoncen\n");
246 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
247 data->pozice=data->pozice_raw-offset->pozice_raw;
252 * Transformace pro uhel pocitany po smeru hodinovych rucicek
254 void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){
259 void alphabeta2pwm3(int32_t * ia, int32_t * ib, int32_t *ic,int32_t alpha, int32_t beta){
261 *ib=-alpha/2+beta*887/1024;
262 *ic=-alpha/2-beta*887/1024;
266 * Preocita napeti na jednotlivych civkach na napeti,
267 * ktera budou privedena na svorky motoru.
268 * Tedy na A(yel)-pwm1, B(red)-pwm2, C(blk)-pwm3
270 void transDelta(int32_t * u1, int32_t * u2, int32_t *u3, int32_t ub , int32_t uc){
273 /*vypocte napeti tak, aby odpovidaly rozdily*/
278 /*najde zaporne napeti*/
281 /*dorovna zaporna napeti na nulu*/
286 void inv_trans_comm(int duty){
290 int32_t pwma,pwmb,pwmc;
292 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
294 /*use it as cyclic 32-bit logic*/
296 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
297 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
300 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
307 rps.t_pwm1=(uint16_t)pwma;
308 rps.t_pwm3=(uint16_t)pwmb;
309 rps.t_pwm2=(uint16_t)pwmc;
312 void inv_trans_comm_2(int duty){
321 pos+=960; /*zarovnani faze 'a' s osou 'alpha'*/
323 /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/
325 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
327 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
331 alphabeta2pwm3(&ia,&ib, &ic,alpha,beta);
337 transDelta(&u1,&u2, &u3,ub,uc);
339 rps.pwm1=(uint16_t)u1;
340 rps.pwm2=(uint16_t)u2;
341 rps.pwm3=(uint16_t)u3;
345 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
347 void * pos_monitor(void* param){
350 usleep(1000000); /*1 Hz*/
356 * Multiplication of 11 bit
357 * Zaporne vysledky prvede na nulu.
359 inline uint16_t mult_cap(int32_t s,int d){
363 /* multiplicate as if maximum sinus value was unity */
364 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
369 int sin_commutator(int duty){
370 #define DEGREE_60 715827883
371 #define DEGREE_120 1431655765
372 #define DEGREE_180 2147483648
373 #define DEGREE_240 2863311531
374 #define DEGREE_300 3579139413
379 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
381 /*use it as cyclic 32-bit logic*/
383 if (duty>=0){ /*clockwise rotation*/
385 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
388 rps.pwm1=(uint16_t)pwm;
391 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
394 rps.pwm2=(uint16_t)pwm;
397 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
400 rps.pwm3=(uint16_t)pwm;
405 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
408 rps.pwm1=(uint16_t)pwm;
411 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
414 rps.pwm2=(uint16_t)pwm;
417 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
420 rps.pwm3=(uint16_t)pwm;
426 * Test function to be placed in controll loop.
427 * Switches PWM's at point where they produce same force.
428 * This points are found thanks to IRC position,
431 void simple_ind_dist_commutator(int duty){
432 if (duty>=0){ /* clockwise - so that position increase */
434 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
435 (rps.index_dist>=1048 && rps.index_dist<=1377)){
440 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
441 (rps.index_dist>=1377 && rps.index_dist<=1711)){
446 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
447 (rps.index_dist>=711 && rps.index_dist<=1048) ||
448 (rps.index_dist>=1711 && rps.index_dist<=1999)){
453 }else{ /*counter-clockwise - position decrease */
455 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
456 (rps.index_dist>=1544 && rps.index_dist<=1878)){
461 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
462 (rps.index_dist>=881 && rps.index_dist<=1210) ||
463 (rps.index_dist>=1878 && rps.index_dist<=1999)){
468 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
469 (rps.index_dist>=1210 && rps.index_dist<=1544)){
478 * Test function to be placed in controll loop.
479 * Switches PWM's at point where they produce same force
481 inline void simple_hall_commutator(int duty){
482 if (duty>=0){ /* clockwise - so that position increase */
484 if (data.hal2 && !data.hal3){
489 }else if (data.hal1 && !data.hal2){
494 }else if (!data.hal1 && data.hal3){
499 }else{ /*counter-clockwise - position decrease */
501 if (!data.hal2 && data.hal3){
506 }else if (!data.hal1 && data.hal2){
511 }else if (data.hal1 && !data.hal3){
520 * Computation of distance to index.
522 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
523 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
525 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
529 uint16_t pos = 0x0FFF & data.pozice_raw;
531 uint16_t index = data.index_position;
533 if (index<1999){ /*index e<0,1998> */
534 if (pos<index){ /*pozice e<0,index-1> */
535 /*proti smeru h.r. od indexu*/
537 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
538 /*po smeru h.r. od indexu*/
540 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
542 }else{ /*pozice e<index+2096,4095> */
543 /*proti smeru h.r. od indexu - podtecena pozice*/
546 }else if (index<=2096){ /*index e<1999,2096>*/
547 if (pos<index-1999){ /*pozice e<0,index-2000> */
549 }else if (pos<index){ /*pozice e<index-1999,index-1> */
550 /*proti smeru h.r. od indexu*/
552 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
553 /*po smeru h.r. od indexu*/
555 }else { /*pozice e<index+2000,4095> */
558 }else{ /*index e<2097,4095> */
559 if (pos<=index-2097){ /*pozice e<0,index-2097> */
560 /*po smeru h.r. od indexu - pretecena pozice*/
562 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
564 }else if (pos<index){ /*pozice e<index-1999,index-1> */
565 /*proti smeru h.r. od indexu*/
567 }else{ /*pozice e<index,4095> */
568 /*po smeru h.r. od indexu*/
573 rps.index_dist = dist;
582 * Very simple PID regulator.
583 * Now only with P-part so that the error doesnt go to zero.
584 * TODO: add anti-wind up and I and D parts
586 inline void pos_pid(){
588 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
589 if (duty_tmp>MAX_DUTY){
591 }else if (duty_tmp<-MAX_DUTY){
599 * Very simple PID regulator.
600 * Now only with P-part so that the error doesnt go to zero.
603 inline void spd_pid(){
606 error=rps.desired_spd - rps.speed;
607 rps.spd_err_sum+=error;
608 duty_tmp = PID_P_S*error+PID_I_S*rps.spd_err_sum;
609 if (duty_tmp>MAX_DUTY){
611 }else if (duty_tmp<-MAX_DUTY){
624 spd=rps.spi_dat->pozice-rps.old_pos[rps.tf_count%OLD_POS_NUM];
625 rps.speed=(int32_t)spd;
631 * TODO: replace bunch of 'IFs' with Object-like pattern
633 void * read_data(void* param){
635 struct rpi_in pocatek;
637 int interval = 1000000; /* 1ms ~ 1kHz*/
638 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
640 uint16_t last_index; /*we have index up-to date*/
641 pocatek = spi_read(tx);
642 clock_gettime(CLOCK_MONOTONIC ,&t);
643 /* start after one second */
646 /* wait until next shot */
647 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
648 sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
651 rps.old_pos[rps.tf_count%OLD_POS_NUM]=rps.spi_dat->pozice;
652 prepare_tx(tx); /*save the data to send*/
653 data = spi_read(tx); /*exchange data*/
654 /*subtract initiate postion */
656 substractOffset(&data,&pocatek);
657 compSpeed(); /*spocita rychlost*/
661 last_index=data.index_position;
663 }else if (last_index!=data.index_position){
665 comIndDist(); /*vypocet vzdalenosti indexu*/
667 }else{ /*index je v poradku*/
668 comIndDist(); /*vypocet vzdalenosti indexu*/
671 /* pocitame sirku plneni podle potreb rizeni*/
672 if (rps.pos_reg_ena){ /*pozicni rizeni*/
674 }else if(rps.spd_reg_ena){ /*rizeni na rychlost*/
678 /* sirka plneni prepoctena na jednotlive pwm */
679 if (rps.index_ok && rps.commutate){
680 /*simple_ind_dist_commutator(rps.duty);*/
681 /*sin_commutator(rps.duty);*/
682 inv_trans_comm(rps.duty);
683 inv_trans_comm_2(rps.duty);
684 }else if(!rps.index_ok && rps.commutate){
685 simple_hall_commutator(rps.duty);
688 /*zalogujeme hodnoty*/
689 if (rps.doLogs && !(rps.tf_count%LOG_PERIOD)){
693 sem_post(&rps.thd_par_sem); /*--post semaphore---*/
695 /* calculate next shot */
696 t.tv_nsec += interval;
698 while (t.tv_nsec >= NSEC_PER_SEC) {
699 t.tv_nsec -= NSEC_PER_SEC;
708 * \brief Main function.
712 pthread_t base_thread_id;
713 clk_init(); /* inicializace gpio hodin */
714 spi_init(); /* iniicializace spi*/
716 /*semafor pro detekci zpracovani parametru vlaken*/
717 sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
720 base_thread_id=pthread_self();
722 /*main control loop*/
723 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
725 /*monitor of current state*/
726 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
728 /*wait for commands*/