From: Martin Prudek Date: Wed, 20 May 2015 16:19:41 +0000 (+0200) Subject: Commutators moved to separate file. X-Git-Url: https://rtime.felk.cvut.cz/gitweb/fpga/rpi-motor-control.git/commitdiff_plain/f747a68dd84aab260a8553829ae5da1ee4bf321a Commutators moved to separate file. --- diff --git a/pmsm-control/test_sw/Makefile b/pmsm-control/test_sw/Makefile index d2e19af..24d2ea5 100644 --- a/pmsm-control/test_sw/Makefile +++ b/pmsm-control/test_sw/Makefile @@ -49,5 +49,5 @@ blikej: howto_gpio.o rpi_hw.o spi: rp_spi.o gcc -o spi rp_spi.c #pro rpi -pmsm: main_pmsm.o rp_spi.o rpi_hw.o misc.o pxmc_sin_fixtab.o cmd_proc.o controllers.o - gcc -o pmsm_controll main_pmsm.o rp_spi.o rpi_hw.o misc.o pxmc_sin_fixtab.o cmd_proc.o controllers.o -lpthread -lrt +pmsm: main_pmsm.o rp_spi.o rpi_hw.o misc.o pxmc_sin_fixtab.o cmd_proc.o controllers.o commutators.o + gcc -o pmsm_controll main_pmsm.o rp_spi.o rpi_hw.o misc.o pxmc_sin_fixtab.o cmd_proc.o controllers.o commutators.o -lpthread -lrt diff --git a/pmsm-control/test_sw/commutators.c b/pmsm-control/test_sw/commutators.c new file mode 100644 index 0000000..1a63724 --- /dev/null +++ b/pmsm-control/test_sw/commutators.c @@ -0,0 +1,297 @@ +/** + * \brief + * Implementation of commutators and transformations. + */ + +#include "commutators.h" +#include "pxmc_sin_fixed.h" /*sinus function*/ + +/* + * \brief + * Computes minimum value of three numbers. + * Input values must be in range <-2^28;2^28>. + */ +static int32_t min(int32_t x, int32_t y, int32_t z){ + int32_t diff,sign; + + diff=x-y; /*rozdil*/ + sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze y je vetsi*/ + x=y+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */ + + diff=x-z; /*rozdil*/ + sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze z je vetsi*/ + x=z+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */ + + return x; +} + +/* + * \brief + * Transformace pro uhel pocitany po smeru hodinovych rucicek + */ +static void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){ + *alpha=cos*d+sin*q; + *beta=-sin*d+cos*q; + return; +} + +/** + * \brief + * Zpetna Clarkova transformace + */ +static void alphabeta2pwm3(int32_t * ia, int32_t * ib, int32_t *ic,int32_t alpha, int32_t beta){ + *ia=alpha; + *ib=-alpha/2+beta*887/1024; + *ic=-alpha/2-beta*887/1024; +} + +/* + * \brief + * Preocita napeti na jednotlivych civkach na napeti, + * ktera budou privedena na svorky motoru. + * Tedy na A(yel)-pwm1, B(red)-pwm2, C(blk)-pwm3 + */ +static void transDelta(int32_t * u1, int32_t * u2, int32_t *u3, int32_t ub , int32_t uc){ + int32_t t; + + /*vypocte napeti tak, aby odpovidaly rozdily*/ + *u1=uc; + *u2=uc+ub; + *u3=0; + + /*najde zaporne napeti*/ + t=min(*u1,*u2,*u3); + + /*dorovna zaporna napeti na nulu*/ + *u1-=t; + *u2-=t; + *u3-=t; +} + +/** + * \brief + * Simple vector-control commutator without delta-transformation. + * Nearly same as sin_commuatator. + */ +void inv_trans_comm(struct rpi_state* this){ + uint32_t pos; + int32_t sin, cos; + int32_t alpha, beta; + int32_t pwma,pwmb,pwmc; + pos=this->index_dist; + /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/ + pos+=717; + /*use it as cyclic 32-bit logic*/ + pos*=4294967; + pxmc_sincos_fixed_inline(&sin, &cos, pos, 16); + dq2alphabeta(&alpha, &beta,0,this->duty, sin, cos); + alpha>>=16; + beta>>=16; + alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta); + + if (pwma<0) pwma=0; + if (pwmb<0) pwmb=0; + if (pwmc<0) pwmc=0; + + /*debugovaci vystupy - pouze vypisy hodnot*/ + this->t_pwm1=(uint16_t)pwma; + this->t_pwm3=(uint16_t)pwmb; + this->t_pwm2=(uint16_t)pwmc; +} + +/* + * \brief + * Robust vector-control commuator with Delta-transformation. + */ +void inv_trans_comm_2(struct rpi_state* this){ + uint32_t pos; + int32_t sin, cos; + int32_t alpha, beta; + int32_t ua,ub,uc; + int32_t ia,ib,ic; + int32_t u1,u2,u3; + pos=this->index_dist; + + pos+=this->alpha_offset; /*zarovnani faze 'a' s osou 'alpha'*/ + + /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/ + pos*=4294967; + pxmc_sincos_fixed_inline(&sin, &cos, pos, 16); + + dq2alphabeta(&alpha, &beta,0,this->duty, sin, cos); + alpha>>=16; + beta>>=16; + + alphabeta2pwm3(&ia,&ib, &ic,alpha,beta); + + ua=ia; + ub=ib; + uc=ic; + + transDelta(&u1,&u2, &u3,ub,uc); + + this->pwm1=(uint16_t)u1; + this->pwm2=(uint16_t)u2; + this->pwm3=(uint16_t)u3; +} + +/** + * \brief + * Simple voltage commutation, takes use of sin finction. + */ +void sin_commutator(struct rpi_state* this){ + #define DEGREE_60 715827883 + #define DEGREE_120 1431655765 + #define DEGREE_180 2147483648 + #define DEGREE_240 2863311531 + #define DEGREE_300 3579139413 + uint32_t j,pos; + int32_t sin; + int32_t pwm; + int duty=this->duty; + pos=this->index_dist; + /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/ + pos+=38; + /*use it as cyclic 32-bit logic*/ + pos*=4294967; + if (this->duty>=0){ /*clockwise rotation*/ + /* 1st phase */ + sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/ + pwm=sin*duty/1024; + if (pwm<0) pwm=0; + this->pwm1=(uint16_t)pwm; + + /* 2nd phase */ + sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/ + pwm=sin*duty/1024; + if (pwm<0) pwm=0; + this->pwm2=(uint16_t)pwm; + + /* 3rd phase */ + sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/ + pwm=sin*duty/1024; + if (pwm<0) pwm=0; + this->pwm3=(uint16_t)pwm; + }else{ + duty=-duty; + + /* 1st phase */ + sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/ + pwm=sin*duty/1024; + if (pwm<0) pwm=0; + this->pwm1=(uint16_t)pwm; + + /* 2nd phase */ + sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/ + pwm=sin*duty/1024; + if (pwm<0) pwm=0; + this->pwm2=(uint16_t)pwm; + + /* 3rd phase */ + sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/ + pwm=sin*duty/1024; + if (pwm<0) pwm=0; + this->pwm3=(uint16_t)pwm; + } +} + +/* + * \brief + * Test function to be placed in controll loop. + * Switches PWM's at point where they produce same force. + * This points are found thanks to IRC position, + */ +void simple_ind_dist_commutator(struct rpi_state* this){ + int duty=this->duty; + uint16_t index_dist=this->index_dist; + if (duty>=0){ /* clockwise - so that position increase */ + /* pwm3 */ + if ((index_dist>=45 && index_dist<=373) || + (index_dist>=1048 && index_dist<=1377)){ + this->pwm1=0; + this->pwm2=0; + this->pwm3=duty; + /* pwm1 */ + }else if ((index_dist>=373 && index_dist<=711) || + (index_dist>=1377 && index_dist<=1711)){ + this->pwm1=duty; + this->pwm2=0; + this->pwm3=0; + /* pwm2 */ + }else if ((index_dist>=0 && index_dist<=45) || + (index_dist>=711 && index_dist<=1048) || + (index_dist>=1711 && index_dist<=1999)){ + this->pwm1=0; + this->pwm2=duty; + this->pwm3=0; + } + }else{ /*counter-clockwise - position decrease */ + /* pwm3 */ + if ((index_dist>=544 && index_dist<=881) || + (index_dist>=1544 && index_dist<=1878)){ + this->pwm1=0; + this->pwm2=0; + this->pwm3=-duty; + /* pwm1 */ + }else if ((index_dist>=0 && index_dist<=211) || + (index_dist>=881 && index_dist<=1210) || + (index_dist>=1878 && index_dist<=1999)){ + this->pwm1=-duty; + this->pwm2=0; + this->pwm3=0; + /* pwm2 */ + }else if ((index_dist>=211 && index_dist<=544) || + (index_dist>=1210 && index_dist<=1544)){ + this->pwm1=0; + this->pwm2=-duty; + this->pwm3=0; + } + } +} + +/* + * \brief + * Test function to be placed in controll loop. + * Switches PWM's at point where they produce same force + */ +void simple_hall_commutator(struct rpi_state* this){ + int duty=this->duty; + int8_t hal1=this->spi_dat->hal1; + int8_t hal2=this->spi_dat->hal2; + int8_t hal3=this->spi_dat->hal3; + if (duty>=0){ /* clockwise - so that position increase */ + /* pwm3 */ + if (hal2 && !hal3){ + this->pwm1=0; + this->pwm2=0; + this->pwm3=duty; + /* pwm1 */ + }else if (hal1 && !hal2){ + this->pwm1=duty; + this->pwm2=0; + this->pwm3=0; + /* pwm2 */ + }else if (!hal1 && hal3){ + this->pwm1=0; + this->pwm2=duty; + this->pwm3=0; + } + }else{ /*counter-clockwise - position decrease */ + /* pwm3 */ + if (!hal2 && hal3){ + this->pwm1=0; + this->pwm2=0; + this->pwm3=-duty; + /* pwm1 */ + }else if (!hal1 && hal2){ + this->pwm1=-duty; + this->pwm2=0; + this->pwm3=0; + /* pwm2 */ + }else if (hal1 && !hal3){ + this->pwm1=0; + this->pwm2=-duty; + this->pwm3=0; + } + } +} diff --git a/pmsm-control/test_sw/commutators.h b/pmsm-control/test_sw/commutators.h new file mode 100644 index 0000000..59905d3 --- /dev/null +++ b/pmsm-control/test_sw/commutators.h @@ -0,0 +1,47 @@ +/** + * \brief + * Duty cycle commutation. + * \author Martin Prudek + * \file commutators.h + * + */ +#ifndef COMMUTATORS +#define COMMUTATORS + +#include "pmsm_state.h" +#include "rp_spi.h" + +/** + * \brief + * Simple vector-control commutator without delta-transformation. + * Nearly same as sin_commuatator. + */ +void inv_trans_comm(struct rpi_state*); + +/* + * \brief + * Robust vector-control commuator with Delta-transformation. + */ +void inv_trans_comm_2(struct rpi_state*); + +/** + * \brief + * Simple voltage commutation, takes use of sin function. + */ +void sin_commutator(struct rpi_state*); + +/* + * \brief + * Switches PWM's at point where they produce same force. + * This points are found from IRC position, + */ +void simple_ind_dist_commutator(struct rpi_state*); + +/* + * \brief + * Switches PWM's at point where they produce same force + */ +void simple_hall_commutator(struct rpi_state*); + + +#endif /*COMMUTATORS*/ diff --git a/pmsm-control/test_sw/main_pmsm.c b/pmsm-control/test_sw/main_pmsm.c index 84b1cdd..a9c6329 100644 --- a/pmsm-control/test_sw/main_pmsm.c +++ b/pmsm-control/test_sw/main_pmsm.c @@ -21,10 +21,10 @@ #include "rpin.h" /*gpclk*/ #include "rp_spi.h" /*spi*/ #include "misc.h" /*structure for priorities*/ -#include "pxmc_sin_fixed.h" /*to test sin commutation */ #include "pmsm_state.h" #include "cmd_proc.h" #include "controllers.h" +#include "commutators.h" @@ -81,24 +81,7 @@ int clk_init() inline void clk_disable(){ termClock(0); } -/* - * \brief - * Count minimum value of three numbers. - * Input values must be in range <-2^28;2^28>. - */ -int32_t min(int32_t x, int32_t y, int32_t z){ - int32_t diff,sign; - diff=x-y; /*rozdil*/ - sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze y je vetsi*/ - x=y+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */ - - diff=x-z; /*rozdil*/ - sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze z je vetsi*/ - x=z+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */ - - return x; -} /* * \brief @@ -248,99 +231,8 @@ void substractOffset(struct rpi_in* data, struct rpi_in* offset){ data->pozice=data->pozice_raw-offset->pozice_raw; return; } -/* - * \brief - * Transformace pro uhel pocitany po smeru hodinovych rucicek - */ -void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){ - *alpha=cos*d+sin*q; - *beta=-sin*d+cos*q; - return; -} -void alphabeta2pwm3(int32_t * ia, int32_t * ib, int32_t *ic,int32_t alpha, int32_t beta){ - *ia=alpha; - *ib=-alpha/2+beta*887/1024; - *ic=-alpha/2-beta*887/1024; -} -/* - * \brief - * Preocita napeti na jednotlivych civkach na napeti, - * ktera budou privedena na svorky motoru. - * Tedy na A(yel)-pwm1, B(red)-pwm2, C(blk)-pwm3 - */ -void transDelta(int32_t * u1, int32_t * u2, int32_t *u3, int32_t ub , int32_t uc){ - int32_t t; - - /*vypocte napeti tak, aby odpovidaly rozdily*/ - *u1=uc; - *u2=uc+ub; - *u3=0; - - /*najde zaporne napeti*/ - t=min(*u1,*u2,*u3); - - /*dorovna zaporna napeti na nulu*/ - *u1-=t; - *u2-=t; - *u3-=t; -} -void inv_trans_comm(int duty){ - uint32_t pos; - int32_t sin, cos; - int32_t alpha, beta; - int32_t pwma,pwmb,pwmc; - pos=rps.index_dist; - /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/ - pos+=717; - /*use it as cyclic 32-bit logic*/ - pos*=4294967; - pxmc_sincos_fixed_inline(&sin, &cos, pos, 16); - dq2alphabeta(&alpha, &beta,0,duty, sin, cos); - alpha>>=16; - beta>>=16; - alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta); - - if (pwma<0) pwma=0; - if (pwmb<0) pwmb=0; - if (pwmc<0) pwmc=0; - - - rps.t_pwm1=(uint16_t)pwma; - rps.t_pwm3=(uint16_t)pwmb; - rps.t_pwm2=(uint16_t)pwmc; -} - -void inv_trans_comm_2(int duty){ - uint32_t pos; - int32_t sin, cos; - int32_t alpha, beta; - int32_t ua,ub,uc; - int32_t ia,ib,ic; - int32_t u1,u2,u3; - pos=rps.index_dist; - - pos+=rps.alpha_offset; /*zarovnani faze 'a' s osou 'alpha'*/ - /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/ - pos*=4294967; - pxmc_sincos_fixed_inline(&sin, &cos, pos, 16); - dq2alphabeta(&alpha, &beta,0,duty, sin, cos); - alpha>>=16; - beta>>=16; - - alphabeta2pwm3(&ia,&ib, &ic,alpha,beta); - - ua=ia; - ub=ib; - uc=ic; - - transDelta(&u1,&u2, &u3,ub,uc); - - rps.pwm1=(uint16_t)u1; - rps.pwm2=(uint16_t)u2; - rps.pwm3=(uint16_t)u3; -} /** * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru @@ -367,156 +259,7 @@ inline uint16_t mult_cap(int32_t s,int d){ return res; } -inline -int sin_commutator(int duty){ - #define DEGREE_60 715827883 - #define DEGREE_120 1431655765 - #define DEGREE_180 2147483648 - #define DEGREE_240 2863311531 - #define DEGREE_300 3579139413 - uint32_t j,pos; - int32_t sin; - pos=rps.index_dist; - int32_t pwm; - /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/ - pos+=38; - /*use it as cyclic 32-bit logic*/ - pos*=4294967; - if (duty>=0){ /*clockwise rotation*/ - /* 1st phase */ - sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/ - pwm=sin*duty/1024; - if (pwm<0) pwm=0; - rps.pwm1=(uint16_t)pwm; - - /* 2nd phase */ - sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/ - pwm=sin*duty/1024; - if (pwm<0) pwm=0; - rps.pwm2=(uint16_t)pwm; - - /* 3rd phase */ - sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/ - pwm=sin*duty/1024; - if (pwm<0) pwm=0; - rps.pwm3=(uint16_t)pwm; - }else{ - duty=-duty; - - /* 1st phase */ - sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/ - pwm=sin*duty/1024; - if (pwm<0) pwm=0; - rps.pwm1=(uint16_t)pwm; - - /* 2nd phase */ - sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/ - pwm=sin*duty/1024; - if (pwm<0) pwm=0; - rps.pwm2=(uint16_t)pwm; - - /* 3rd phase */ - sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/ - pwm=sin*duty/1024; - if (pwm<0) pwm=0; - rps.pwm3=(uint16_t)pwm; - } - return 0; -} -/* - * \brief - * Test function to be placed in controll loop. - * Switches PWM's at point where they produce same force. - * This points are found thanks to IRC position, - */ -inline -void simple_ind_dist_commutator(int duty){ - if (duty>=0){ /* clockwise - so that position increase */ - /* pwm3 */ - if ((rps.index_dist>=45 && rps.index_dist<=373) || - (rps.index_dist>=1048 && rps.index_dist<=1377)){ - rps.pwm1=0; - rps.pwm2=0; - rps.pwm3=duty; - /* pwm1 */ - }else if ((rps.index_dist>=373 && rps.index_dist<=711) || - (rps.index_dist>=1377 && rps.index_dist<=1711)){ - rps.pwm1=duty; - rps.pwm2=0; - rps.pwm3=0; - /* pwm2 */ - }else if ((rps.index_dist>=0 && rps.index_dist<=45) || - (rps.index_dist>=711 && rps.index_dist<=1048) || - (rps.index_dist>=1711 && rps.index_dist<=1999)){ - rps.pwm1=0; - rps.pwm2=duty; - rps.pwm3=0; - } - }else{ /*counter-clockwise - position decrease */ - /* pwm3 */ - if ((rps.index_dist>=544 && rps.index_dist<=881) || - (rps.index_dist>=1544 && rps.index_dist<=1878)){ - rps.pwm1=0; - rps.pwm2=0; - rps.pwm3=-duty; - /* pwm1 */ - }else if ((rps.index_dist>=0 && rps.index_dist<=211) || - (rps.index_dist>=881 && rps.index_dist<=1210) || - (rps.index_dist>=1878 && rps.index_dist<=1999)){ - rps.pwm1=-duty; - rps.pwm2=0; - rps.pwm3=0; - /* pwm2 */ - }else if ((rps.index_dist>=211 && rps.index_dist<=544) || - (rps.index_dist>=1210 && rps.index_dist<=1544)){ - rps.pwm1=0; - rps.pwm2=-duty; - rps.pwm3=0; - } - } -} -/* - * \brief - * Test function to be placed in controll loop. - * Switches PWM's at point where they produce same force - */ -inline void simple_hall_commutator(int duty){ - if (duty>=0){ /* clockwise - so that position increase */ - /* pwm3 */ - if (data.hal2 && !data.hal3){ - rps.pwm1=0; - rps.pwm2=0; - rps.pwm3=duty; - /* pwm1 */ - }else if (data.hal1 && !data.hal2){ - rps.pwm1=duty; - rps.pwm2=0; - rps.pwm3=0; - /* pwm2 */ - }else if (!data.hal1 && data.hal3){ - rps.pwm1=0; - rps.pwm2=duty; - rps.pwm3=0; - } - }else{ /*counter-clockwise - position decrease */ - /* pwm3 */ - if (!data.hal2 && data.hal3){ - rps.pwm1=0; - rps.pwm2=0; - rps.pwm3=-duty; - /* pwm1 */ - }else if (!data.hal1 && data.hal2){ - rps.pwm1=-duty; - rps.pwm2=0; - rps.pwm3=0; - /* pwm2 */ - }else if (data.hal1 && !data.hal3){ - rps.pwm1=0; - rps.pwm2=-duty; - rps.pwm3=0; - } - } -} + /** * \brief * Computation of distance to index. @@ -645,10 +388,10 @@ void * read_data(void* param){ if (rps.index_ok && rps.commutate){ /*simple_ind_dist_commutator(rps.duty);*/ /*sin_commutator(rps.duty);*/ - inv_trans_comm(rps.duty); - inv_trans_comm_2(rps.duty); + inv_trans_comm(&rps); + inv_trans_comm_2(&rps); }else if(!rps.index_ok && rps.commutate){ - simple_hall_commutator(rps.duty); + simple_hall_commutator(&rps); } /*zalogujeme hodnoty*/