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.9 /*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;
132 if (rps.log_col==rps.log_col_count-1){
133 rps.log_col_count*=2;
134 rps.log_col_count%=MAX_LOGS;
135 for (r=0;r<LOG_ROWS;r++){
136 rps.logs[r]=realloc(rps.logs[r],rps.log_col_count*sizeof(int));
143 * Pripravi psi buffer
145 void prepare_tx(uint8_t * tx){
148 * tx[4] - bity 95 downto 88 - bits that are sent first
149 * tx[5] - bity 87 downto 80
150 * tx[6] - bity 79 downto 72
151 * tx[7] - bity 71 downto 64
152 * tx[8] - bity 63 downto 56
153 * tx[9] - bity 55 downto 48
154 * tx[10] - bity 47 downto 40
155 * tx[11] - bity 39 downto 32
156 * tx[12] - bity 31 downto 24
157 * tx[13] - bity 23 downto 16
158 * tx[14] - bity 15 downto 8
159 * tx[15] - bity 7 downto 0
162 * bit 94 - enable PWM1
163 * bit 93 - enable PWM2
164 * bit 92 - enable PWM3
173 * bits 47 .. 32 - match PWM1
174 * bits 31 .. 16 - match PWM2
175 * bits 15 .. 0 - match PWM3
181 /* keep the 11-bit cap*/
183 if (rps.pwm1>2047) rps.pwm1=2047;
184 if (rps.pwm2>2047) rps.pwm2=2047;
185 if (rps.pwm3>2047) rps.pwm3=2047;
187 tx[0]=rps.test; /*bit 94 - enable PWM1*/
189 /*now we have to switch the bytes due to endianess */
190 /* ARMv6 & ARMv7 instructions are little endian */
192 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
193 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
196 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
197 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
200 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
201 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
208 * \brief Signal handler pro Ctrl+C
212 sem_wait(&rps.thd_par_sem);
214 memset(tx,0,16*sizeof(int));
218 prepare_tx(tx); /*save the data to send*/
224 /*muzeme zavrit semafor*/
225 sem_destroy(&rps.thd_par_sem);
226 printf("\nprogram bezpecne ukoncen\n");
229 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
230 data->pozice=data->pozice_raw-offset->pozice_raw;
235 * Transformace pro uhel pocitany po smeru hodinovych rucicek
237 void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){
242 void alphabeta2pwm3(int32_t * ia, int32_t * ib, int32_t *ic,int32_t alpha, int32_t beta){
244 *ib=-alpha/2+beta*887/1024;
245 *ic=-alpha/2-beta*887/1024;
249 * Preocita napeti na jednotlivych civkach na napeti,
250 * ktera budou privedena na svorky motoru.
251 * Tedy na A(yel)-pwm1, B(red)-pwm2, C(blk)-pwm3
253 void transDelta(int32_t * u1, int32_t * u2, int32_t *u3, int32_t ub , int32_t uc){
256 /*vypocte napeti tak, aby odpovidaly rozdily*/
261 /*najde zaporne napeti*/
264 /*dorovna zaporna napeti na nulu*/
269 void inv_trans_comm(int duty){
273 int32_t pwma,pwmb,pwmc;
275 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
277 /*use it as cyclic 32-bit logic*/
279 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
280 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
283 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
290 rps.t_pwm1=(uint16_t)pwma;
291 rps.t_pwm3=(uint16_t)pwmb;
292 rps.t_pwm2=(uint16_t)pwmc;
295 void inv_trans_comm_2(int duty){
304 pos+=960; /*zarovnani faze 'a' s osou 'alpha'*/
306 /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/
308 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
310 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
314 alphabeta2pwm3(&ia,&ib, &ic,alpha,beta);
320 transDelta(&u1,&u2, &u3,ub,uc);
322 rps.pwm1=(uint16_t)u1;
323 rps.pwm2=(uint16_t)u2;
324 rps.pwm3=(uint16_t)u3;
328 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
330 void * pos_monitor(void* param){
333 usleep(1000000); /*1 Hz*/
339 * Multiplication of 11 bit
340 * Zaporne vysledky prvede na nulu.
342 inline uint16_t mult_cap(int32_t s,int d){
346 /* multiplicate as if maximum sinus value was unity */
347 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
352 int sin_commutator(int duty){
353 #define DEGREE_60 715827883
354 #define DEGREE_120 1431655765
355 #define DEGREE_180 2147483648
356 #define DEGREE_240 2863311531
357 #define DEGREE_300 3579139413
362 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
364 /*use it as cyclic 32-bit logic*/
366 if (duty>=0){ /*clockwise rotation*/
368 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
371 rps.pwm1=(uint16_t)pwm;
374 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
377 rps.pwm2=(uint16_t)pwm;
380 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
383 rps.pwm3=(uint16_t)pwm;
388 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
391 rps.pwm1=(uint16_t)pwm;
394 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
397 rps.pwm2=(uint16_t)pwm;
400 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
403 rps.pwm3=(uint16_t)pwm;
409 * Test function to be placed in controll loop.
410 * Switches PWM's at point where they produce same force.
411 * This points are found thanks to IRC position,
414 void simple_ind_dist_commutator(int duty){
415 if (duty>=0){ /* clockwise - so that position increase */
417 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
418 (rps.index_dist>=1048 && rps.index_dist<=1377)){
423 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
424 (rps.index_dist>=1377 && rps.index_dist<=1711)){
429 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
430 (rps.index_dist>=711 && rps.index_dist<=1048) ||
431 (rps.index_dist>=1711 && rps.index_dist<=1999)){
436 }else{ /*counter-clockwise - position decrease */
438 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
439 (rps.index_dist>=1544 && rps.index_dist<=1878)){
444 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
445 (rps.index_dist>=881 && rps.index_dist<=1210) ||
446 (rps.index_dist>=1878 && rps.index_dist<=1999)){
451 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
452 (rps.index_dist>=1210 && rps.index_dist<=1544)){
461 * Test function to be placed in controll loop.
462 * Switches PWM's at point where they produce same force
464 inline void simple_hall_commutator(int duty){
465 if (duty>=0){ /* clockwise - so that position increase */
467 if (data.hal2 && !data.hal3){
472 }else if (data.hal1 && !data.hal2){
477 }else if (!data.hal1 && data.hal3){
482 }else{ /*counter-clockwise - position decrease */
484 if (!data.hal2 && data.hal3){
489 }else if (!data.hal1 && data.hal2){
494 }else if (data.hal1 && !data.hal3){
503 * Computation of distance to index.
505 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
506 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
508 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
512 uint16_t pos = 0x0FFF & data.pozice_raw;
514 uint16_t index = data.index_position;
516 if (index<1999){ /*index e<0,1998> */
517 if (pos<index){ /*pozice e<0,index-1> */
518 /*proti smeru h.r. od indexu*/
520 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
521 /*po smeru h.r. od indexu*/
523 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
525 }else{ /*pozice e<index+2096,4095> */
526 /*proti smeru h.r. od indexu - podtecena pozice*/
529 }else if (index<=2096){ /*index e<1999,2096>*/
530 if (pos<index-1999){ /*pozice e<0,index-2000> */
532 }else if (pos<index){ /*pozice e<index-1999,index-1> */
533 /*proti smeru h.r. od indexu*/
535 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
536 /*po smeru h.r. od indexu*/
538 }else { /*pozice e<index+2000,4095> */
541 }else{ /*index e<2097,4095> */
542 if (pos<=index-2097){ /*pozice e<0,index-2097> */
543 /*po smeru h.r. od indexu - pretecena pozice*/
545 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
547 }else if (pos<index){ /*pozice e<index-1999,index-1> */
548 /*proti smeru h.r. od indexu*/
550 }else{ /*pozice e<index,4095> */
551 /*po smeru h.r. od indexu*/
556 rps.index_dist = dist;
565 * Very simple PID regulator.
566 * Now only with P-part so that the error doesnt go to zero.
567 * TODO: add anti-wind up and I and D parts
569 inline void pos_pid(){
571 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
572 if (duty_tmp>MAX_DUTY){
574 }else if (duty_tmp<-MAX_DUTY){
582 * Very simple PID regulator.
583 * Now only with P-part so that the error doesnt go to zero.
586 inline void spd_pid(){
589 error=rps.desired_spd - rps.speed;
590 rps.spd_err_sum+=error;
591 duty_tmp = PID_P_S*error+PID_I_S*rps.spd_err_sum;
592 if (duty_tmp>MAX_DUTY){
594 }else if (duty_tmp<-MAX_DUTY){
607 spd=rps.spi_dat->pozice-rps.old_pos[rps.tf_count%OLD_POS_NUM];
608 rps.speed=(int32_t)spd;
614 * TODO: replace bunch of 'IFs' with Object-like pattern
616 void * read_data(void* param){
618 struct rpi_in pocatek;
620 int interval = 1000000; /* 1ms ~ 1kHz*/
621 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
623 uint16_t last_index; /*we have index up-to date*/
624 pocatek = spi_read(tx);
625 clock_gettime(CLOCK_MONOTONIC ,&t);
626 /* start after one second */
629 /* wait until next shot */
630 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
631 sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
634 rps.old_pos[rps.tf_count%OLD_POS_NUM]=rps.spi_dat->pozice;
635 prepare_tx(tx); /*save the data to send*/
636 data = spi_read(tx); /*exchange data*/
637 /*subtract initiate postion */
639 substractOffset(&data,&pocatek);
640 compSpeed(); /*spocita rychlost*/
644 last_index=data.index_position;
646 }else if (last_index!=data.index_position){
648 comIndDist(); /*vypocet vzdalenosti indexu*/
650 }else{ /*index je v poradku*/
651 comIndDist(); /*vypocet vzdalenosti indexu*/
654 /* pocitame sirku plneni podle potreb rizeni*/
655 if (rps.pos_reg_ena){ /*pozicni rizeni*/
657 }else if(rps.spd_reg_ena){ /*rizeni na rychlost*/
661 /* sirka plneni prepoctena na jednotlive pwm */
662 if (rps.index_ok && rps.commutate){
663 /*simple_ind_dist_commutator(rps.duty);*/
664 /*sin_commutator(rps.duty);*/
665 inv_trans_comm(rps.duty);
666 inv_trans_comm_2(rps.duty);
667 }else if(!rps.index_ok && rps.commutate){
668 simple_hall_commutator(rps.duty);
671 /*zalogujeme hodnoty*/
672 if (rps.doLogs && !(rps.tf_count%LOG_PERIOD)){
676 sem_post(&rps.thd_par_sem); /*--post semaphore---*/
678 /* calculate next shot */
679 t.tv_nsec += interval;
681 while (t.tv_nsec >= NSEC_PER_SEC) {
682 t.tv_nsec -= NSEC_PER_SEC;
691 * \brief Main function.
695 pthread_t base_thread_id;
696 clk_init(); /* inicializace gpio hodin */
697 spi_init(); /* iniicializace spi*/
699 /*semafor pro detekci zpracovani parametru vlaken*/
700 sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
703 base_thread_id=pthread_self();
705 /*main control loop*/
706 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
708 /*monitor of current state*/
709 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
711 /*wait for commands*/