3 * Implementation of commutators and transformations.
6 #include "commutators.h"
7 #include "pxmc_sin_fixed.h" /*sinus function*/
11 * Computes minimum value of three numbers.
12 * Input values must be in range <-2^28;2^28>.
14 static int32_t min(int32_t x, int32_t y, int32_t z){
18 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze y je vetsi*/
19 x=y+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
22 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze z je vetsi*/
23 x=z+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
30 * Transformace pro uhel pocitany po smeru hodinovych rucicek
32 static void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){
40 * Zpetna Clarkova transformace
42 static void alphabeta2pwm3(int32_t * ia, int32_t * ib, int32_t *ic,int32_t alpha, int32_t beta){
44 *ib=-alpha/2+beta*887/1024;
45 *ic=-alpha/2-beta*887/1024;
50 * Preocita napeti na jednotlivych civkach na napeti,
51 * ktera budou privedena na svorky motoru.
52 * Tedy na A(yel)-pwm1, B(red)-pwm2, C(blk)-pwm3
54 static void transDelta(int32_t * u1, int32_t * u2, int32_t *u3, int32_t ub , int32_t uc){
57 /*vypocte napeti tak, aby odpovidaly rozdily*/
62 /*najde zaporne napeti*/
65 /*dorovna zaporna napeti na nulu*/
73 * Simple vector-control commutator without delta-transformation.
74 * Nearly same as sin_commuatator.
76 void inv_trans_comm(struct rpi_state* this){
80 int32_t pwma,pwmb,pwmc;
82 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
84 /*use it as cyclic 32-bit logic*/
86 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
87 dq2alphabeta(&alpha, &beta,0,this->duty, sin, cos);
90 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
96 /*debugovaci vystupy - pouze vypisy hodnot*/
97 this->t_pwm1=(uint16_t)pwma;
98 this->t_pwm3=(uint16_t)pwmb;
99 this->t_pwm2=(uint16_t)pwmc;
104 * Robust vector-control commuator with Delta-transformation.
106 void inv_trans_comm_2(struct rpi_state* this){
113 pos=this->index_dist;
115 pos+=this->alpha_offset; /*zarovnani faze 'a' s osou 'alpha'*/
117 /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/
119 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
121 dq2alphabeta(&alpha, &beta,0,this->duty, sin, cos);
125 alphabeta2pwm3(&ia,&ib, &ic,alpha,beta);
131 transDelta(&u1,&u2, &u3,ub,uc);
133 this->pwm1=(uint16_t)u1;
134 this->pwm2=(uint16_t)u2;
135 this->pwm3=(uint16_t)u3;
140 * Simple voltage commutation, takes use of sin finction.
142 void sin_commutator(struct rpi_state* this){
143 #define DEGREE_60 715827883
144 #define DEGREE_120 1431655765
145 #define DEGREE_180 2147483648
146 #define DEGREE_240 2863311531
147 #define DEGREE_300 3579139413
152 pos=this->index_dist;
153 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
155 /*use it as cyclic 32-bit logic*/
157 if (this->duty>=0){ /*clockwise rotation*/
159 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
162 this->pwm1=(uint16_t)pwm;
165 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
168 this->pwm2=(uint16_t)pwm;
171 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
174 this->pwm3=(uint16_t)pwm;
179 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
182 this->pwm1=(uint16_t)pwm;
185 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
188 this->pwm2=(uint16_t)pwm;
191 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
194 this->pwm3=(uint16_t)pwm;
200 * Test function to be placed in controll loop.
201 * Switches PWM's at point where they produce same force.
202 * This points are found thanks to IRC position,
204 void simple_ind_dist_commutator(struct rpi_state* this){
206 uint16_t index_dist=this->index_dist;
207 if (duty>=0){ /* clockwise - so that position increase */
209 if ((index_dist>=45 && index_dist<=373) ||
210 (index_dist>=1048 && index_dist<=1377)){
215 }else if ((index_dist>=373 && index_dist<=711) ||
216 (index_dist>=1377 && index_dist<=1711)){
221 }else if ((index_dist>=0 && index_dist<=45) ||
222 (index_dist>=711 && index_dist<=1048) ||
223 (index_dist>=1711 && index_dist<=1999)){
228 }else{ /*counter-clockwise - position decrease */
230 if ((index_dist>=544 && index_dist<=881) ||
231 (index_dist>=1544 && index_dist<=1878)){
236 }else if ((index_dist>=0 && index_dist<=211) ||
237 (index_dist>=881 && index_dist<=1210) ||
238 (index_dist>=1878 && index_dist<=1999)){
243 }else if ((index_dist>=211 && index_dist<=544) ||
244 (index_dist>=1210 && index_dist<=1544)){
254 * Test function to be placed in controll loop.
255 * Switches PWM's at point where they produce same force
257 void simple_hall_commutator(struct rpi_state* this){
259 int8_t hal1=this->spi_dat->hal1;
260 int8_t hal2=this->spi_dat->hal2;
261 int8_t hal3=this->spi_dat->hal3;
262 if (duty>=0){ /* clockwise - so that position increase */
269 }else if (hal1 && !hal2){
274 }else if (!hal1 && hal3){
279 }else{ /*counter-clockwise - position decrease */
286 }else if (!hal1 && hal2){
291 }else if (hal1 && !hal3){