3 * Implementation of commutators and transformations.
6 #include "commutators.h"
7 #include "pxmc_sin_fixed.h" /*sinus function*/
14 void zero_commutator(struct rpi_state* this){
20 * Computes minimum value of three numbers.
21 * Input values must be in range <-2^28;2^28>.
23 static int32_t min(int32_t x, int32_t y, int32_t z){
27 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze y je vetsi*/
28 x=y+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
31 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze z je vetsi*/
32 x=z+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
39 * Transformace pro uhel pocitany po smeru hodinovych rucicek
41 static void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){
49 * Zpetna Clarkova transformace
51 static void alphabeta2pwm3(int32_t * ia, int32_t * ib, int32_t *ic,int32_t alpha, int32_t beta){
53 *ib=-alpha/2+beta*887/1024;
54 *ic=-alpha/2-beta*887/1024;
59 * Preocita napeti na jednotlivych civkach na napeti,
60 * ktera budou privedena na svorky motoru.
61 * Tedy na A(yel)-pwm1, B(red)-pwm2, C(blk)-pwm3
63 static void transDelta(int32_t * u1, int32_t * u2, int32_t *u3, int32_t ub , int32_t uc){
66 /*vypocte napeti tak, aby odpovidaly rozdily*/
71 /*najde zaporne napeti*/
74 /*dorovna zaporna napeti na nulu*/
82 * Simple vector-control commutator without delta-transformation.
83 * Nearly same as sin_commuatator.
85 void inv_trans_comm(struct rpi_state* this){
89 int32_t pwma,pwmb,pwmc;
91 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
93 /*use it as cyclic 32-bit logic*/
95 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
96 dq2alphabeta(&alpha, &beta,0,this->duty, sin, cos);
99 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
105 /*debugovaci vystupy - pouze vypisy hodnot*/
106 this->t_pwm1=(uint16_t)pwma;
107 this->t_pwm3=(uint16_t)pwmb;
108 this->t_pwm2=(uint16_t)pwmc;
113 * Robust vector-control commuator with Delta-transformation.
115 void inv_trans_comm_2(struct rpi_state* this){
122 pos=this->index_dist;
124 pos+=this->alpha_offset; /*zarovnani faze 'a' s osou 'alpha'*/
126 /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/
128 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
130 dq2alphabeta(&alpha, &beta,0,this->duty, sin, cos);
134 alphabeta2pwm3(&ia,&ib, &ic,alpha,beta);
140 transDelta(&u1,&u2, &u3,ub,uc);
142 this->pwm1=(uint16_t)u1;
143 this->pwm2=(uint16_t)u2;
144 this->pwm3=(uint16_t)u3;
149 * Simple voltage commutation, takes use of sin finction.
151 void sin_commutator(struct rpi_state* this){
152 #define DEGREE_60 715827883
153 #define DEGREE_120 1431655765
154 #define DEGREE_180 2147483648
155 #define DEGREE_240 2863311531
156 #define DEGREE_300 3579139413
161 pos=this->index_dist;
162 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
164 /*use it as cyclic 32-bit logic*/
166 if (this->duty>=0){ /*clockwise rotation*/
168 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
171 this->pwm1=(uint16_t)pwm;
174 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
177 this->pwm2=(uint16_t)pwm;
180 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
183 this->pwm3=(uint16_t)pwm;
188 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
191 this->pwm1=(uint16_t)pwm;
194 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
197 this->pwm2=(uint16_t)pwm;
200 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
203 this->pwm3=(uint16_t)pwm;
209 * Test function to be placed in controll loop.
210 * Switches PWM's at point where they produce same force.
211 * This points are found thanks to IRC position,
213 void simple_ind_dist_commutator(struct rpi_state* this){
215 uint16_t index_dist=this->index_dist;
216 if (duty>=0){ /* clockwise - so that position increase */
218 if ((index_dist>=45 && index_dist<=373) ||
219 (index_dist>=1048 && index_dist<=1377)){
224 }else if ((index_dist>=373 && index_dist<=711) ||
225 (index_dist>=1377 && index_dist<=1711)){
230 }else if ((index_dist>=0 && index_dist<=45) ||
231 (index_dist>=711 && index_dist<=1048) ||
232 (index_dist>=1711 && index_dist<=1999)){
237 }else{ /*counter-clockwise - position decrease */
239 if ((index_dist>=544 && index_dist<=881) ||
240 (index_dist>=1544 && index_dist<=1878)){
245 }else if ((index_dist>=0 && index_dist<=211) ||
246 (index_dist>=881 && index_dist<=1210) ||
247 (index_dist>=1878 && index_dist<=1999)){
252 }else if ((index_dist>=211 && index_dist<=544) ||
253 (index_dist>=1210 && index_dist<=1544)){
263 * Test function to be placed in controll loop.
264 * Switches PWM's at point where they produce same force
266 void simple_hall_commutator(struct rpi_state* this){
268 int8_t hal1=this->spi_dat->hal1;
269 int8_t hal2=this->spi_dat->hal2;
270 int8_t hal3=this->spi_dat->hal3;
271 if (duty>=0){ /* clockwise - so that position increase */
278 }else if (hal1 && !hal2){
283 }else if (!hal1 && hal3){
288 }else{ /*counter-clockwise - position decrease */
295 }else if (!hal1 && hal2){
300 }else if (hal1 && !hal3){