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 */
64 * \brief Initilizes GPCLK.
68 initialise(); /*namapovani gpio*/
69 initClock(PLLD_500_MHZ, 10, 0);
70 gpioSetMode(4, FSEL_ALT0);
74 * \brief Terminates GPCLK.
77 inline void clk_disable(){
82 * Count minimum value of three numbers.
83 * Input values must be in range <-2^28;2^28>.
85 int32_t min(int32_t x, int32_t y, int32_t z){
89 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze y je vetsi*/
90 x=y+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
93 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze z je vetsi*/
94 x=z+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
100 * Pripravi psi buffer
102 void prepare_tx(uint8_t * tx){
105 * tx[4] - bity 95 downto 88 - bits that are sent first
106 * tx[5] - bity 87 downto 80
107 * tx[6] - bity 79 downto 72
108 * tx[7] - bity 71 downto 64
109 * tx[8] - bity 63 downto 56
110 * tx[9] - bity 55 downto 48
111 * tx[10] - bity 47 downto 40
112 * tx[11] - bity 39 downto 32
113 * tx[12] - bity 31 downto 24
114 * tx[13] - bity 23 downto 16
115 * tx[14] - bity 15 downto 8
116 * tx[15] - bity 7 downto 0
119 * bit 94 - enable PWM1
120 * bit 93 - enable PWM2
121 * bit 92 - enable PWM3
130 * bits 47 .. 32 - match PWM1
131 * bits 31 .. 16 - match PWM2
132 * bits 15 .. 0 - match PWM3
138 /* keep the 11-bit cap*/
140 if (rps.pwm1>2047) rps.pwm1=2047;
141 if (rps.pwm2>2047) rps.pwm2=2047;
142 if (rps.pwm3>2047) rps.pwm3=2047;
144 tx[0]=rps.test; /*bit 94 - enable PWM1*/
146 /*now we have to switch the bytes due to endianess */
147 /* ARMv6 & ARMv7 instructions are little endian */
149 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
150 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
153 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
154 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
157 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
158 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
165 * \brief Signal handler pro Ctrl+C
169 sem_wait(&rps.thd_par_sem);
171 memset(tx,0,16*sizeof(int));
175 prepare_tx(tx); /*save the data to send*/
180 /*muzeme zavrit semafor*/
181 sem_destroy(&rps.thd_par_sem);
182 printf("\nprogram bezpecne ukoncen\n");
185 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
186 data->pozice=data->pozice_raw-offset->pozice_raw;
191 * Transformace pro uhel pocitany po smeru hodinovych rucicek
193 void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){
198 void alphabeta2pwm3(int32_t * ia, int32_t * ib, int32_t *ic,int32_t alpha, int32_t beta){
200 *ib=-alpha/2+beta*887/1024;
201 *ic=-alpha/2-beta*887/1024;
205 * Preocita napeti na jednotlivych civkach na napeti,
206 * ktera budou privedena na svorky motoru.
207 * Tedy na A(yel)-pwm1, B(red)-pwm2, C(blk)-pwm3
209 void transDelta(int32_t * u1, int32_t * u2, int32_t *u3, int32_t ub , int32_t uc){
212 /*vypocte napeti tak, aby odpovidaly rozdily*/
217 /*najde zaporne napeti*/
220 /*dorovna zaporna napeti na nulu*/
225 void inv_trans_comm(int duty){
229 int32_t pwma,pwmb,pwmc;
231 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
233 /*use it as cyclic 32-bit logic*/
235 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
236 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
239 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
246 rps.t_pwm1=(uint16_t)pwma;
247 rps.t_pwm3=(uint16_t)pwmb;
248 rps.t_pwm2=(uint16_t)pwmc;
251 void inv_trans_comm_2(int duty){
260 pos+=960; /*zarovnani faze 'a' s osou 'alpha'*/
262 /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/
264 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
266 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
270 alphabeta2pwm3(&ia,&ib, &ic,alpha,beta);
276 transDelta(&u1,&u2, &u3,ub,uc);
278 rps.pwm1=(uint16_t)u1;
279 rps.pwm2=(uint16_t)u2;
280 rps.pwm3=(uint16_t)u3;
284 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
286 void * pos_monitor(void* param){
289 usleep(1000000); /*1 Hz*/
295 * Multiplication of 11 bit
296 * Zaporne vysledky prvede na nulu.
298 inline uint16_t mult_cap(int32_t s,int d){
302 /* multiplicate as if maximum sinus value was unity */
303 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
308 int sin_commutator(int duty){
309 #define DEGREE_60 715827883
310 #define DEGREE_120 1431655765
311 #define DEGREE_180 2147483648
312 #define DEGREE_240 2863311531
313 #define DEGREE_300 3579139413
318 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
320 /*use it as cyclic 32-bit logic*/
322 if (duty>=0){ /*clockwise rotation*/
324 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
327 rps.pwm1=(uint16_t)pwm;
330 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
333 rps.pwm2=(uint16_t)pwm;
336 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
339 rps.pwm3=(uint16_t)pwm;
344 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
347 rps.pwm1=(uint16_t)pwm;
350 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
353 rps.pwm2=(uint16_t)pwm;
356 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
359 rps.pwm3=(uint16_t)pwm;
365 * Test function to be placed in controll loop.
366 * Switches PWM's at point where they produce same force.
367 * This points are found thanks to IRC position,
370 void simple_ind_dist_commutator(int duty){
371 if (duty>=0){ /* clockwise - so that position increase */
373 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
374 (rps.index_dist>=1048 && rps.index_dist<=1377)){
379 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
380 (rps.index_dist>=1377 && rps.index_dist<=1711)){
385 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
386 (rps.index_dist>=711 && rps.index_dist<=1048) ||
387 (rps.index_dist>=1711 && rps.index_dist<=1999)){
392 }else{ /*counter-clockwise - position decrease */
394 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
395 (rps.index_dist>=1544 && rps.index_dist<=1878)){
400 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
401 (rps.index_dist>=881 && rps.index_dist<=1210) ||
402 (rps.index_dist>=1878 && rps.index_dist<=1999)){
407 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
408 (rps.index_dist>=1210 && rps.index_dist<=1544)){
417 * Test function to be placed in controll loop.
418 * Switches PWM's at point where they produce same force
420 inline void simple_hall_commutator(int duty){
421 if (duty>=0){ /* clockwise - so that position increase */
423 if (data.hal2 && !data.hal3){
428 }else if (data.hal1 && !data.hal2){
433 }else if (!data.hal1 && data.hal3){
438 }else{ /*counter-clockwise - position decrease */
440 if (!data.hal2 && data.hal3){
445 }else if (!data.hal1 && data.hal2){
450 }else if (data.hal1 && !data.hal3){
459 * Computation of distance to index.
461 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
462 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
464 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
468 uint16_t pos = 0x0FFF & data.pozice_raw;
470 uint16_t index = data.index_position;
472 if (index<1999){ /*index e<0,1998> */
473 if (pos<index){ /*pozice e<0,index-1> */
474 /*proti smeru h.r. od indexu*/
476 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
477 /*po smeru h.r. od indexu*/
479 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
481 }else{ /*pozice e<index+2096,4095> */
482 /*proti smeru h.r. od indexu - podtecena pozice*/
485 }else if (index<=2096){ /*index e<1999,2096>*/
486 if (pos<index-1999){ /*pozice e<0,index-2000> */
488 }else if (pos<index){ /*pozice e<index-1999,index-1> */
489 /*proti smeru h.r. od indexu*/
491 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
492 /*po smeru h.r. od indexu*/
494 }else { /*pozice e<index+2000,4095> */
497 }else{ /*index e<2097,4095> */
498 if (pos<=index-2097){ /*pozice e<0,index-2097> */
499 /*po smeru h.r. od indexu - pretecena pozice*/
501 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
503 }else if (pos<index){ /*pozice e<index-1999,index-1> */
504 /*proti smeru h.r. od indexu*/
506 }else{ /*pozice e<index,4095> */
507 /*po smeru h.r. od indexu*/
512 rps.index_dist = dist;
521 * Very simple PID regulator.
522 * Now only with P-part so that the error doesnt go to zero.
523 * TODO: add anti-wind up and I and D parts
525 inline void pos_pid(){
527 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
528 if (duty_tmp>MAX_DUTY){
530 }else if (duty_tmp<-MAX_DUTY){
538 * Very simple PID regulator.
539 * Now only with P-part so that the error doesnt go to zero.
542 inline void spd_pid(){
545 error=rps.desired_spd - rps.speed;
546 rps.spd_err_sum+=error;
547 duty_tmp = PID_P_S*error+PID_I_S*rps.spd_err_sum;
548 if (duty_tmp>MAX_DUTY){
550 }else if (duty_tmp<-MAX_DUTY){
563 spd=rps.spi_dat->pozice-rps.old_pos[rps.tf_count%OLD_POS_NUM];
564 rps.speed=(int32_t)spd;
570 * TODO: replace bunch of 'IFs' with Object-like pattern
572 void * read_data(void* param){
574 struct rpi_in pocatek;
576 int interval = 1000000; /* 1ms ~ 1kHz*/
577 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
579 uint16_t last_index; /*we have index up-to date*/
580 pocatek = spi_read(tx);
581 clock_gettime(CLOCK_MONOTONIC ,&t);
582 /* start after one second */
585 /* wait until next shot */
586 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
587 sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
590 rps.old_pos[rps.tf_count%OLD_POS_NUM]=rps.spi_dat->pozice;
591 prepare_tx(tx); /*save the data to send*/
592 data = spi_read(tx); /*exchange data*/
593 /*subtract initiate postion */
595 substractOffset(&data,&pocatek);
596 compSpeed(); /*spocita rychlost*/
600 last_index=data.index_position;
602 }else if (last_index!=data.index_position){
604 comIndDist(); /*vypocet vzdalenosti indexu*/
606 }else{ /*index je v poradku*/
607 comIndDist(); /*vypocet vzdalenosti indexu*/
610 /* pocitame sirku plneni podle potreb rizeni*/
611 if (rps.pos_reg_ena){ /*pozicni rizeni*/
613 }else if(rps.spd_reg_ena){ /*rizeni na rychlost*/
617 /* sirka plneni prepoctena na jednotlive pwm */
618 if (rps.index_ok && rps.commutate){
619 /*simple_ind_dist_commutator(rps.duty);*/
620 /*sin_commutator(rps.duty);*/
621 inv_trans_comm(rps.duty);
622 inv_trans_comm_2(rps.duty);
623 }else if(!rps.index_ok && rps.commutate){
624 simple_hall_commutator(rps.duty);
626 sem_post(&rps.thd_par_sem); /*--post semaphore---*/
628 /* calculate next shot */
629 t.tv_nsec += interval;
631 while (t.tv_nsec >= NSEC_PER_SEC) {
632 t.tv_nsec -= NSEC_PER_SEC;
641 * \brief Main function.
645 pthread_t base_thread_id;
646 clk_init(); /* inicializace gpio hodin */
647 spi_init(); /* iniicializace spi*/
649 /*semafor pro detekci zpracovani parametru vlaken*/
650 sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
653 base_thread_id=pthread_self();
655 /*main control loop*/
656 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
658 /*monitor of current state*/
659 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
661 /*wait for commands*/