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"
36 #define THREAD_SHARED 0
37 #define INIT_VALUE 1 /*init value for semaphor*/
40 #define NSEC_PER_SEC (1000000000) /* The number of nsecs per sec. */
44 struct rpi_state rps={
47 .pwm1=0,.pwm2=0, .pwm3=0,
48 .pwm1=0, .t_pwm2=0, .t_pwm3=0,
50 .duty=0, /* duty cycle of pwm */
51 .index_dist=0, /* distance to index position */
53 .tf_count=0, /*number of transfer*/
54 .desired_pos=0, /* desired position */
61 * \brief Initilizes GPCLK.
65 initialise(); /*namapovani gpio*/
66 initClock(PLLD_500_MHZ, 10, 0);
67 gpioSetMode(4, FSEL_ALT0);
71 * \brief Terminates GPCLK.
74 inline void clk_disable(){
79 * Count minimum value of three numbers.
80 * Input values must be in range <-2^28;2^28>.
82 int32_t min(int32_t x, int32_t y, int32_t z){
86 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze y je vetsi*/
87 x=y+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
90 sign=(*((uint32_t*)&diff))>>31; /*znamenko -> detekuje, ze z je vetsi*/
91 x=z+sign*diff; /*ulozime mensi cislo, pokud sign>0, pak diff<0 */
99 void prepare_tx(uint8_t * tx){
102 * tx[4] - bity 95 downto 88 - bits that are sent first
103 * tx[5] - bity 87 downto 80
104 * tx[6] - bity 79 downto 72
105 * tx[7] - bity 71 downto 64
106 * tx[8] - bity 63 downto 56
107 * tx[9] - bity 55 downto 48
108 * tx[10] - bity 47 downto 40
109 * tx[11] - bity 39 downto 32
110 * tx[12] - bity 31 downto 24
111 * tx[13] - bity 23 downto 16
112 * tx[14] - bity 15 downto 8
113 * tx[15] - bity 7 downto 0
116 * bit 94 - enable PWM1
117 * bit 93 - enable PWM2
118 * bit 92 - enable PWM3
127 * bits 47 .. 32 - match PWM1
128 * bits 31 .. 16 - match PWM2
129 * bits 15 .. 0 - match PWM3
135 /* keep the 11-bit cap*/
137 if (rps.pwm1>2047) rps.pwm1=2047;
138 if (rps.pwm2>2047) rps.pwm2=2047;
139 if (rps.pwm3>2047) rps.pwm3=2047;
141 tx[0]=rps.test; /*bit 94 - enable PWM1*/
143 /*now we have to switch the bytes due to endianess */
144 /* ARMv6 & ARMv7 instructions are little endian */
146 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
147 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
150 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
151 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
154 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
155 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
162 * \brief Signal handler pro Ctrl+C
166 sem_wait(&rps.thd_par_sem);
168 memset(tx,0,16*sizeof(int));
172 prepare_tx(tx); /*save the data to send*/
177 /*muzeme zavrit semafor*/
178 sem_destroy(&rps.thd_par_sem);
179 printf("\nprogram bezpecne ukoncen\n");
182 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
183 data->pozice_raw=data->pozice;
184 data->pozice-=offset->pozice;
189 * Transformace pro uhel pocitany po smeru hodinovych rucicek
191 void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){
196 void alphabeta2pwm3(int32_t * ia, int32_t * ib, int32_t *ic,int32_t alpha, int32_t beta){
198 *ib=-alpha/2+beta*887/1024;
199 *ic=-alpha/2-beta*887/1024;
203 * Preocita napeti na jednotlivych civkach na napeti,
204 * ktera budou privedena na svorky motoru.
205 * Tedy na A(yel)-pwm1, B(red)-pwm2, C(blk)-pwm3
207 void transDelta(int32_t * u1, int32_t * u2, int32_t *u3, int32_t ub , int32_t uc){
210 /*vypocte napeti tak, aby odpovidaly rozdily*/
215 /*najde zaporne napeti*/
218 /*dorovna zaporna napeti na nulu*/
223 void inv_trans_comm(int duty){
227 int32_t pwma,pwmb,pwmc;
229 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
231 /*use it as cyclic 32-bit logic*/
233 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
234 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
237 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
244 rps.t_pwm1=(uint16_t)pwma;
245 rps.t_pwm3=(uint16_t)pwmb;
246 rps.t_pwm2=(uint16_t)pwmc;
249 void inv_trans_comm_2(int duty){
258 pos+=960; /*zarovnani faze 'a' s osou 'alpha'*/
260 /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/
262 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
264 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
268 alphabeta2pwm3(&ia,&ib, &ic,alpha,beta);
274 transDelta(&u1,&u2, &u3,ub,uc);
276 rps.pwm1=(uint16_t)u1;
277 rps.pwm2=(uint16_t)u2;
278 rps.pwm3=(uint16_t)u3;
282 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
284 void * pos_monitor(void* param){
287 usleep(1000000); /*1 Hz*/
293 * Multiplication of 11 bit
294 * Zaporne vysledky prvede na nulu.
296 inline uint16_t mult_cap(int32_t s,int d){
300 /* multiplicate as if maximum sinus value was unity */
301 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
306 int sin_commutator(int duty){
307 #define DEGREE_60 715827883
308 #define DEGREE_120 1431655765
309 #define DEGREE_180 2147483648
310 #define DEGREE_240 2863311531
311 #define DEGREE_300 3579139413
316 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
318 /*use it as cyclic 32-bit logic*/
320 if (duty>=0){ /*clockwise rotation*/
322 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
325 rps.pwm1=(uint16_t)pwm;
328 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
331 rps.pwm2=(uint16_t)pwm;
334 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
337 rps.pwm3=(uint16_t)pwm;
342 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
345 rps.pwm1=(uint16_t)pwm;
348 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
351 rps.pwm2=(uint16_t)pwm;
354 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
357 rps.pwm3=(uint16_t)pwm;
363 * Test function to be placed in controll loop.
364 * Switches PWM's at point where they produce same force.
365 * This points are found thanks to IRC position,
368 void simple_ind_dist_commutator(int duty){
369 if (duty>=0){ /* clockwise - so that position increase */
371 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
372 (rps.index_dist>=1048 && rps.index_dist<=1377)){
377 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
378 (rps.index_dist>=1377 && rps.index_dist<=1711)){
383 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
384 (rps.index_dist>=711 && rps.index_dist<=1048) ||
385 (rps.index_dist>=1711 && rps.index_dist<=1999)){
390 }else{ /*counter-clockwise - position decrease */
392 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
393 (rps.index_dist>=1544 && rps.index_dist<=1878)){
398 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
399 (rps.index_dist>=881 && rps.index_dist<=1210) ||
400 (rps.index_dist>=1878 && rps.index_dist<=1999)){
405 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
406 (rps.index_dist>=1210 && rps.index_dist<=1544)){
415 * Test function to be placed in controll loop.
416 * Switches PWM's at point where they produce same force
418 inline void simple_hall_commutator(int duty){
419 if (duty>=0){ /* clockwise - so that position increase */
421 if (data.hal2 && !data.hal3){
426 }else if (data.hal1 && !data.hal2){
431 }else if (!data.hal1 && data.hal3){
436 }else{ /*counter-clockwise - position decrease */
438 if (!data.hal2 && data.hal3){
443 }else if (!data.hal1 && data.hal2){
448 }else if (data.hal1 && !data.hal3){
457 * Computation of distance to index.
459 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
460 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
462 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
466 uint16_t pos = 0x0FFF & data.pozice_raw;
468 uint16_t index = data.index_position;
470 if (index<1999){ /*index e<0,1998> */
471 if (pos<index){ /*pozice e<0,index-1> */
472 /*proti smeru h.r. od indexu*/
474 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
475 /*po smeru h.r. od indexu*/
477 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
479 }else{ /*pozice e<index+2096,4095> */
480 /*proti smeru h.r. od indexu - podtecena pozice*/
483 }else if (index<=2096){ /*index e<1999,2096>*/
484 if (pos<index-1999){ /*pozice e<0,index-2000> */
486 }else if (pos<index){ /*pozice e<index-1999,index-1> */
487 /*proti smeru h.r. od indexu*/
489 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
490 /*po smeru h.r. od indexu*/
492 }else { /*pozice e<index+2000,4095> */
495 }else{ /*index e<2097,4095> */
496 if (pos<=index-2097){ /*pozice e<0,index-2097> */
497 /*po smeru h.r. od indexu - pretecena pozice*/
499 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
501 }else if (pos<index){ /*pozice e<index-1999,index-1> */
502 /*proti smeru h.r. od indexu*/
504 }else{ /*pozice e<index,4095> */
505 /*po smeru h.r. od indexu*/
510 rps.index_dist = dist;
519 * Very simple PID regulator.
520 * Now only with P-part so that the error doesnt go to zero.
521 * TODO: add anti-wind up and I and D parts
523 inline void pos_pid(){
525 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
526 if (duty_tmp>MAX_DUTY){
528 }else if (duty_tmp<-MAX_DUTY){
536 * Very simple PID regulator.
537 * Now only with P-part so that the error doesnt go to zero.
540 inline void spd_pid(){
542 signed long int speed=rps.spi_dat->pozice-rps.old_pos;
543 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
544 if (duty_tmp>MAX_DUTY){
546 }else if (duty_tmp<-MAX_DUTY){
555 * TODO: replace bunch of 'IFs' with Object-like pattern
557 void * read_data(void* param){
559 struct rpi_in pocatek;
561 int interval = 1000000; /* 1ms ~ 1kHz*/
562 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
564 uint16_t last_index; /*we have index up-to date*/
565 pocatek = spi_read(tx);
566 clock_gettime(CLOCK_MONOTONIC ,&t);
567 /* start after one second */
570 /* wait until next shot */
571 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
572 sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
574 rps.old_pos=rps.spi_dat->pozice; /*old position*/
575 prepare_tx(tx); /*save the data to send*/
576 data = spi_read(tx); /*exchange data*/
577 /*subtract initiate postion */
579 substractOffset(&data,&pocatek);
583 last_index=data.index_position;
585 }else if (last_index!=data.index_position){
587 comIndDist(); /*vypocet vzdalenosti indexu*/
589 }else{ /*index je v poradku*/
590 comIndDist(); /*vypocet vzdalenosti indexu*/
592 /* pocitame sirku plneni podle potreb rizeni*/
593 if (rps.pos_reg_ena){
596 /* sirka plneni prepoctena na jednotlive pwm */
597 if (rps.index_ok && rps.commutate){
598 /*simple_ind_dist_commutator(rps.duty);*/
599 /*sin_commutator(rps.duty);*/
600 inv_trans_comm(rps.duty);
601 inv_trans_comm_2(rps.duty);
602 }else if(!rps.index_ok && rps.commutate){
603 simple_hall_commutator(rps.duty);
605 sem_post(&rps.thd_par_sem); /*--post semaphore---*/
607 /* calculate next shot */
608 t.tv_nsec += interval;
610 while (t.tv_nsec >= NSEC_PER_SEC) {
611 t.tv_nsec -= NSEC_PER_SEC;
621 * \brief Main function.
625 pthread_t base_thread_id;
626 clk_init(); /* inicializace gpio hodin */
627 spi_init(); /* iniicializace spi*/
629 /*semafor pro detekci zpracovani parametru vlaken*/
630 sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
633 base_thread_id=pthread_self();
635 /*main control loop*/
636 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
638 /*monitor of current state*/
639 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
641 /*wait for commands*/