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*/
20 #include "rpin.h" /*gpclk*/
21 #include "rp_spi.h" /*spi*/
22 #include "misc.h" /*structure for priorities*/
23 #include "pxmc_sin_fixed.h" /*to test sin commutation */
24 #include "pmsm_state.h"
34 #define THREAD_SHARED 0
35 #define INIT_VALUE 1 /*init value for semaphor*/
38 #define NSEC_PER_SEC (1000000000) /* The number of nsecs per sec. */
42 struct rpi_state rps={
45 .pwm1=0,.pwm2=0, .pwm3=0,
46 .pwm1=0, .t_pwm2=0, .t_pwm3=0,
48 .duty=0, /* duty cycle of pwm */
49 .index_dist=0, /* distance to index position */
51 .tf_count=0, /*number of transfer*/
52 .desired_pos=0 /* desired position */
56 * \brief Initilizes GPCLK.
60 initialise(); /*namapovani gpio*/
61 initClock(PLLD_500_MHZ, 10, 0);
62 gpioSetMode(4, FSEL_ALT0);
66 * \brief Terminates GPCLK.
69 inline void clk_disable(){
74 * Count minimum value of three numbers.
75 * Input values must be in range <-2^28;2^28>.
77 int32_t min(int32_t x, int32_t y, int32_t z){
81 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze y je vetsi*/
82 x=y+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
85 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze z je vetsi*/
86 x=z+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
92 * \brief Signal handler pro Ctrl+C
97 /*muzeme zavrit semafor*/
98 sem_destroy(&rps.thd_par_sem);
99 printf("\nprogram bezpecne ukoncen\n");
102 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
103 data->pozice_raw=data->pozice;
104 data->pozice-=offset->pozice;
109 * Transformace pro uhel pocitany po smeru hodinovych rucicek
111 void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){
116 void alphabeta2pwm3(int32_t * pwma, int32_t * pwmb, int32_t *pwmc,int32_t alpha, int32_t beta){
118 *pwmb=-alpha/2+beta*887/1024;
119 *pwmc=-alpha/2-beta*887/1024;
123 * Preocita napeti na jednotlivych civkach na napeti,
124 * ktera budou privedena na svorky motoru.
125 * Tedy na A(yel)-pwm1, B(red)-pwm2, C(blk)-pwm3
127 void compPhases(int32_t * pwm1, int32_t * pwm2, int32_t *pwm3, int32_t ua , int32_t ub){
131 /*vypocte napeti tak, aby odpovidaly rozdily*/
136 /*najde zaporne napeti*/
139 /*dorovna zaporna napeti napeti na nulu*/
149 void inv_trans_comm(int duty){
153 int32_t pwma,pwmb,pwmc;
155 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
157 /*use it as cyclic 32-bit logic*/
159 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
160 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
163 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
170 rps.t_pwm1=(uint16_t)pwma;
171 rps.t_pwm3=(uint16_t)pwmb;
172 rps.t_pwm2=(uint16_t)pwmc;
175 void inv_trans_comm_2(int duty){
179 int32_t pwma,pwmb,pwmc;
180 int32_t pwm1,pwm2,pwm3;
182 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
184 /*use it as cyclic 32-bit logic*/
186 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
187 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
190 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
192 compPhases(&pwm1,&pwm2, &pwm3,pwma,pwmb);
194 rps.pwm1=(uint16_t)pwm1;
195 rps.pwm2=(uint16_t)pwm2;
196 rps.pwm3=(uint16_t)pwm3;
198 void prepare_tx(uint8_t * tx){
201 * tx[4] - bity 95 downto 88 - bits that are sent first
202 * tx[5] - bity 87 downto 80
203 * tx[6] - bity 79 downto 72
204 * tx[7] - bity 71 downto 64
205 * tx[8] - bity 63 downto 56
206 * tx[9] - bity 55 downto 48
207 * tx[10] - bity 47 downto 40
208 * tx[11] - bity 39 downto 32
209 * tx[12] - bity 31 downto 24
210 * tx[13] - bity 23 downto 16
211 * tx[14] - bity 15 downto 8
212 * tx[15] - bity 7 downto 0
215 * bit 94 - enable PWM1
216 * bit 93 - enable PWM2
217 * bit 92 - enable PWM3
226 * bits 47 .. 32 - match PWM1
227 * bits 31 .. 16 - match PWM2
228 * bits 15 .. 0 - match PWM3
234 /* keep the 11-bit cap*/
236 if (rps.pwm1>2047) rps.pwm1=2047;
237 if (rps.pwm2>2047) rps.pwm2=2047;
238 if (rps.pwm3>2047) rps.pwm3=2047;
240 tx[0]=rps.test; /*bit 94 - enable PWM1*/
242 /*now we have to switch the bytes due to endianess */
243 /* ARMv6 & ARMv7 instructions are little endian */
245 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
246 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
249 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
250 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
253 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
254 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
259 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
261 void * pos_monitor(void* param){
264 usleep(1000000); /*1 Hz*/
270 * Multiplication of 11 bit
271 * Zaporne vysledky prvede na nulu.
273 inline uint16_t mult_cap(int32_t s,int d){
277 /* multiplicate as if maximum sinus value was unity */
278 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
283 int sin_commutator(int duty){
284 #define DEGREE_60 715827883
285 #define DEGREE_120 1431655765
286 #define DEGREE_180 2147483648
287 #define DEGREE_240 2863311531
288 #define DEGREE_300 3579139413
293 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
295 /*use it as cyclic 32-bit logic*/
297 if (duty>=0){ /*clockwise rotation*/
299 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
302 rps.pwm1=(uint16_t)pwm;
305 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
308 rps.pwm2=(uint16_t)pwm;
311 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
314 rps.pwm3=(uint16_t)pwm;
319 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
322 rps.pwm1=(uint16_t)pwm;
325 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
328 rps.pwm2=(uint16_t)pwm;
331 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
334 rps.pwm3=(uint16_t)pwm;
340 * Test function to be placed in controll loop.
341 * Switches PWM's at point where they produce same force.
342 * This points are found thanks to IRC position,
345 void simple_ind_dist_commutator(int duty){
346 if (duty>=0){ /* clockwise - so that position increase */
348 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
349 (rps.index_dist>=1048 && rps.index_dist<=1377)){
354 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
355 (rps.index_dist>=1377 && rps.index_dist<=1711)){
360 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
361 (rps.index_dist>=711 && rps.index_dist<=1048) ||
362 (rps.index_dist>=1711 && rps.index_dist<=1999)){
367 }else{ /*counter-clockwise - position decrease */
369 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
370 (rps.index_dist>=1544 && rps.index_dist<=1878)){
375 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
376 (rps.index_dist>=881 && rps.index_dist<=1210) ||
377 (rps.index_dist>=1878 && rps.index_dist<=1999)){
382 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
383 (rps.index_dist>=1210 && rps.index_dist<=1544)){
392 * Test function to be placed in controll loop.
393 * Switches PWM's at point where they produce same force
395 inline void simple_hall_commutator(int duty){
396 if (duty>=0){ /* clockwise - so that position increase */
398 if (data.hal2 && !data.hal3){
403 }else if (data.hal1 && !data.hal2){
408 }else if (!data.hal1 && data.hal3){
413 }else{ /*counter-clockwise - position decrease */
415 if (!data.hal2 && data.hal3){
420 }else if (!data.hal1 && data.hal2){
425 }else if (data.hal1 && !data.hal3){
434 * Computation of distance to index.
436 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
437 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
439 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
443 uint16_t pos = 0x0FFF & data.pozice_raw;
445 uint16_t index = data.index_position;
447 if (index<1999){ /*index e<0,1998> */
448 if (pos<index){ /*pozice e<0,index-1> */
449 /*proti smeru h.r. od indexu*/
451 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
452 /*po smeru h.r. od indexu*/
454 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
456 }else{ /*pozice e<index+2096,4095> */
457 /*proti smeru h.r. od indexu - podtecena pozice*/
460 }else if (index<=2096){ /*index e<1999,2096>*/
461 if (pos<index-1999){ /*pozice e<0,index-2000> */
463 }else if (pos<index){ /*pozice e<index-1999,index-1> */
464 /*proti smeru h.r. od indexu*/
466 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
467 /*po smeru h.r. od indexu*/
469 }else { /*pozice e<index+2000,4095> */
472 }else{ /*index e<2097,4095> */
473 if (pos<=index-2097){ /*pozice e<0,index-2097> */
474 /*po smeru h.r. od indexu - pretecena pozice*/
476 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
478 }else if (pos<index){ /*pozice e<index-1999,index-1> */
479 /*proti smeru h.r. od indexu*/
481 }else{ /*pozice e<index,4095> */
482 /*po smeru h.r. od indexu*/
487 rps.index_dist = dist;
496 * Very simple PID regulator.
497 * Now only with P-part so that the error doesnt go to zero.
498 * TODO: add anti-wind up and I and D parts
502 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
503 if (duty_tmp>MAX_DUTY){
505 }else if (duty_tmp<-MAX_DUTY){
515 void * read_data(void* param){
517 struct rpi_in pocatek;
519 int interval = 1000000; /* 1ms ~ 1kHz*/
520 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
522 uint16_t last_index; /*we have index up-to date*/
523 pocatek = spi_read(tx);
524 clock_gettime(CLOCK_MONOTONIC ,&t);
525 /* start after one second */
528 /* wait until next shot */
529 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
530 sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
531 prepare_tx(tx); /*save the data to send*/
532 data = spi_read(tx); /*exchange data*/
533 /*subtract initiate postion */
535 substractOffset(&data,&pocatek);
539 last_index=data.index_position;
541 }else if (last_index!=data.index_position){
546 if (rps.index_ok && rps.commutate){
547 /*simple_ind_dist_commutator(rps.duty);*/
548 /*sin_commutator(rps.duty);*/
549 inv_trans_comm(rps.duty);
550 inv_trans_comm_2(rps.duty);
551 }else if(!rps.index_ok && rps.commutate){
552 simple_hall_commutator(rps.duty);
554 sem_post(&rps.thd_par_sem); /*--post semaphore---*/
556 /* calculate next shot */
557 t.tv_nsec += interval;
559 while (t.tv_nsec >= NSEC_PER_SEC) {
560 t.tv_nsec -= NSEC_PER_SEC;
570 * \brief Main function.
574 pthread_t base_thread_id;
575 clk_init(); /* inicializace gpio hodin */
576 spi_init(); /* iniicializace spi*/
578 /*semafor pro detekci zpracovani parametru vlaken*/
579 sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
582 base_thread_id=pthread_self();
584 /*main control loop*/
585 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
587 /*monitor of current state*/
588 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
590 /*wait for commands*/