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;
129 rps.logs[2][rps.log_col]=(int)rps.pwm1;
130 rps.logs[3][rps.log_col]=(int)rps.pwm2;
131 rps.logs[4][rps.log_col]=(int)rps.pwm3;
135 if (rps.log_col==rps.log_col_count-1){
136 rps.log_col_count*=2;
137 rps.log_col_count%=MAX_LOGS;
138 for (r=0;r<LOG_ROWS;r++){
139 rps.logs[r]=realloc(rps.logs[r],rps.log_col_count*sizeof(int));
140 if (rps.logs[r]==NULL){
151 * Pripravi psi buffer
153 void prepare_tx(uint8_t * tx){
156 * tx[4] - bity 95 downto 88 - bits that are sent first
157 * tx[5] - bity 87 downto 80
158 * tx[6] - bity 79 downto 72
159 * tx[7] - bity 71 downto 64
160 * tx[8] - bity 63 downto 56
161 * tx[9] - bity 55 downto 48
162 * tx[10] - bity 47 downto 40
163 * tx[11] - bity 39 downto 32
164 * tx[12] - bity 31 downto 24
165 * tx[13] - bity 23 downto 16
166 * tx[14] - bity 15 downto 8
167 * tx[15] - bity 7 downto 0
170 * bit 94 - enable PWM1
171 * bit 93 - enable PWM2
172 * bit 92 - enable PWM3
181 * bits 47 .. 32 - match PWM1
182 * bits 31 .. 16 - match PWM2
183 * bits 15 .. 0 - match PWM3
189 /* keep the 11-bit cap*/
191 if (rps.pwm1>2047) rps.pwm1=2047;
192 if (rps.pwm2>2047) rps.pwm2=2047;
193 if (rps.pwm3>2047) rps.pwm3=2047;
195 tx[0]=rps.test; /*bit 94 - enable PWM1*/
197 /*now we have to switch the bytes due to endianess */
198 /* ARMv6 & ARMv7 instructions are little endian */
200 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
201 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
204 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
205 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
208 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
209 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
216 * \brief Signal handler pro Ctrl+C
220 sem_wait(&rps.thd_par_sem);
222 memset(tx,0,16*sizeof(int));
226 prepare_tx(tx); /*save the data to send*/
232 /*muzeme zavrit semafor*/
233 sem_destroy(&rps.thd_par_sem);
234 printf("\nprogram bezpecne ukoncen\n");
237 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
238 data->pozice=data->pozice_raw-offset->pozice_raw;
243 * Transformace pro uhel pocitany po smeru hodinovych rucicek
245 void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){
250 void alphabeta2pwm3(int32_t * ia, int32_t * ib, int32_t *ic,int32_t alpha, int32_t beta){
252 *ib=-alpha/2+beta*887/1024;
253 *ic=-alpha/2-beta*887/1024;
257 * Preocita napeti na jednotlivych civkach na napeti,
258 * ktera budou privedena na svorky motoru.
259 * Tedy na A(yel)-pwm1, B(red)-pwm2, C(blk)-pwm3
261 void transDelta(int32_t * u1, int32_t * u2, int32_t *u3, int32_t ub , int32_t uc){
264 /*vypocte napeti tak, aby odpovidaly rozdily*/
269 /*najde zaporne napeti*/
272 /*dorovna zaporna napeti na nulu*/
277 void inv_trans_comm(int duty){
281 int32_t pwma,pwmb,pwmc;
283 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
285 /*use it as cyclic 32-bit logic*/
287 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
288 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
291 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
298 rps.t_pwm1=(uint16_t)pwma;
299 rps.t_pwm3=(uint16_t)pwmb;
300 rps.t_pwm2=(uint16_t)pwmc;
303 void inv_trans_comm_2(int duty){
312 pos+=960; /*zarovnani faze 'a' s osou 'alpha'*/
314 /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/
316 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
318 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
322 alphabeta2pwm3(&ia,&ib, &ic,alpha,beta);
328 transDelta(&u1,&u2, &u3,ub,uc);
330 rps.pwm1=(uint16_t)u1;
331 rps.pwm2=(uint16_t)u2;
332 rps.pwm3=(uint16_t)u3;
336 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
338 void * pos_monitor(void* param){
341 usleep(1000000); /*1 Hz*/
347 * Multiplication of 11 bit
348 * Zaporne vysledky prvede na nulu.
350 inline uint16_t mult_cap(int32_t s,int d){
354 /* multiplicate as if maximum sinus value was unity */
355 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
360 int sin_commutator(int duty){
361 #define DEGREE_60 715827883
362 #define DEGREE_120 1431655765
363 #define DEGREE_180 2147483648
364 #define DEGREE_240 2863311531
365 #define DEGREE_300 3579139413
370 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
372 /*use it as cyclic 32-bit logic*/
374 if (duty>=0){ /*clockwise rotation*/
376 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
379 rps.pwm1=(uint16_t)pwm;
382 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
385 rps.pwm2=(uint16_t)pwm;
388 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
391 rps.pwm3=(uint16_t)pwm;
396 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
399 rps.pwm1=(uint16_t)pwm;
402 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
405 rps.pwm2=(uint16_t)pwm;
408 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
411 rps.pwm3=(uint16_t)pwm;
417 * Test function to be placed in controll loop.
418 * Switches PWM's at point where they produce same force.
419 * This points are found thanks to IRC position,
422 void simple_ind_dist_commutator(int duty){
423 if (duty>=0){ /* clockwise - so that position increase */
425 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
426 (rps.index_dist>=1048 && rps.index_dist<=1377)){
431 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
432 (rps.index_dist>=1377 && rps.index_dist<=1711)){
437 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
438 (rps.index_dist>=711 && rps.index_dist<=1048) ||
439 (rps.index_dist>=1711 && rps.index_dist<=1999)){
444 }else{ /*counter-clockwise - position decrease */
446 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
447 (rps.index_dist>=1544 && rps.index_dist<=1878)){
452 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
453 (rps.index_dist>=881 && rps.index_dist<=1210) ||
454 (rps.index_dist>=1878 && rps.index_dist<=1999)){
459 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
460 (rps.index_dist>=1210 && rps.index_dist<=1544)){
469 * Test function to be placed in controll loop.
470 * Switches PWM's at point where they produce same force
472 inline void simple_hall_commutator(int duty){
473 if (duty>=0){ /* clockwise - so that position increase */
475 if (data.hal2 && !data.hal3){
480 }else if (data.hal1 && !data.hal2){
485 }else if (!data.hal1 && data.hal3){
490 }else{ /*counter-clockwise - position decrease */
492 if (!data.hal2 && data.hal3){
497 }else if (!data.hal1 && data.hal2){
502 }else if (data.hal1 && !data.hal3){
511 * Computation of distance to index.
513 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
514 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
516 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
520 uint16_t pos = 0x0FFF & data.pozice_raw;
522 uint16_t index = data.index_position;
524 if (index<1999){ /*index e<0,1998> */
525 if (pos<index){ /*pozice e<0,index-1> */
526 /*proti smeru h.r. od indexu*/
528 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
529 /*po smeru h.r. od indexu*/
531 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
533 }else{ /*pozice e<index+2096,4095> */
534 /*proti smeru h.r. od indexu - podtecena pozice*/
537 }else if (index<=2096){ /*index e<1999,2096>*/
538 if (pos<index-1999){ /*pozice e<0,index-2000> */
540 }else if (pos<index){ /*pozice e<index-1999,index-1> */
541 /*proti smeru h.r. od indexu*/
543 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
544 /*po smeru h.r. od indexu*/
546 }else { /*pozice e<index+2000,4095> */
549 }else{ /*index e<2097,4095> */
550 if (pos<=index-2097){ /*pozice e<0,index-2097> */
551 /*po smeru h.r. od indexu - pretecena pozice*/
553 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
555 }else if (pos<index){ /*pozice e<index-1999,index-1> */
556 /*proti smeru h.r. od indexu*/
558 }else{ /*pozice e<index,4095> */
559 /*po smeru h.r. od indexu*/
564 rps.index_dist = dist;
573 * Very simple PID regulator.
574 * Now only with P-part so that the error doesnt go to zero.
575 * TODO: add anti-wind up and I and D parts
577 inline void pos_pid(){
579 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
580 if (duty_tmp>MAX_DUTY){
582 }else if (duty_tmp<-MAX_DUTY){
590 * Very simple PID regulator.
591 * Now only with P-part so that the error doesnt go to zero.
594 inline void spd_pid(){
597 error=rps.desired_spd - rps.speed;
598 rps.spd_err_sum+=error;
599 duty_tmp = PID_P_S*error+PID_I_S*rps.spd_err_sum;
600 if (duty_tmp>MAX_DUTY){
602 }else if (duty_tmp<-MAX_DUTY){
615 spd=rps.spi_dat->pozice-rps.old_pos[rps.tf_count%OLD_POS_NUM];
616 rps.speed=(int32_t)spd;
622 * TODO: replace bunch of 'IFs' with Object-like pattern
624 void * read_data(void* param){
626 struct rpi_in pocatek;
628 int interval = 1000000; /* 1ms ~ 1kHz*/
629 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
631 uint16_t last_index; /*we have index up-to date*/
632 pocatek = spi_read(tx);
633 clock_gettime(CLOCK_MONOTONIC ,&t);
634 /* start after one second */
637 /* wait until next shot */
638 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
639 sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
642 rps.old_pos[rps.tf_count%OLD_POS_NUM]=rps.spi_dat->pozice;
643 prepare_tx(tx); /*save the data to send*/
644 data = spi_read(tx); /*exchange data*/
645 /*subtract initiate postion */
647 substractOffset(&data,&pocatek);
648 compSpeed(); /*spocita rychlost*/
652 last_index=data.index_position;
654 }else if (last_index!=data.index_position){
656 comIndDist(); /*vypocet vzdalenosti indexu*/
658 }else{ /*index je v poradku*/
659 comIndDist(); /*vypocet vzdalenosti indexu*/
662 /* pocitame sirku plneni podle potreb rizeni*/
663 if (rps.pos_reg_ena){ /*pozicni rizeni*/
665 }else if(rps.spd_reg_ena){ /*rizeni na rychlost*/
669 /* sirka plneni prepoctena na jednotlive pwm */
670 if (rps.index_ok && rps.commutate){
671 /*simple_ind_dist_commutator(rps.duty);*/
672 /*sin_commutator(rps.duty);*/
673 inv_trans_comm(rps.duty);
674 inv_trans_comm_2(rps.duty);
675 }else if(!rps.index_ok && rps.commutate){
676 simple_hall_commutator(rps.duty);
679 /*zalogujeme hodnoty*/
680 if (rps.doLogs && !(rps.tf_count%LOG_PERIOD)){
684 sem_post(&rps.thd_par_sem); /*--post semaphore---*/
686 /* calculate next shot */
687 t.tv_nsec += interval;
689 while (t.tv_nsec >= NSEC_PER_SEC) {
690 t.tv_nsec -= NSEC_PER_SEC;
699 * \brief Main function.
703 pthread_t base_thread_id;
704 clk_init(); /* inicializace gpio hodin */
705 spi_init(); /* iniicializace spi*/
707 /*semafor pro detekci zpracovani parametru vlaken*/
708 sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
711 base_thread_id=pthread_self();
713 /*main control loop*/
714 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
716 /*monitor of current state*/
717 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
719 /*wait for commands*/