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 * ia, int32_t * ib, int32_t *ic,int32_t alpha, int32_t beta){
118 *ib=-alpha/2+beta*887/1024;
119 *ic=-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 transDelta(int32_t * u1, int32_t * u2, int32_t *u3, int32_t ub , int32_t uc){
130 /*vypocte napeti tak, aby odpovidaly rozdily*/
135 /*najde zaporne napeti*/
138 /*dorovna zaporna napeti na nulu*/
143 void inv_trans_comm(int duty){
147 int32_t pwma,pwmb,pwmc;
149 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
151 /*use it as cyclic 32-bit logic*/
153 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
154 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
157 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
164 rps.t_pwm1=(uint16_t)pwma;
165 rps.t_pwm3=(uint16_t)pwmb;
166 rps.t_pwm2=(uint16_t)pwmc;
169 void inv_trans_comm_2(int duty){
178 pos+=960; /*zarovnani faze 'a' s osou 'alpha'*/
180 /*pro výpočet sin a cos je pouzita 32-bit cyklicka logika*/
182 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
184 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
188 alphabeta2pwm3(&ia,&ib, &ic,alpha,beta);
194 transDelta(&u1,&u2, &u3,ub,uc);
196 rps.pwm1=(uint16_t)u1;
197 rps.pwm2=(uint16_t)u2;
198 rps.pwm3=(uint16_t)u3;
200 void prepare_tx(uint8_t * tx){
203 * tx[4] - bity 95 downto 88 - bits that are sent first
204 * tx[5] - bity 87 downto 80
205 * tx[6] - bity 79 downto 72
206 * tx[7] - bity 71 downto 64
207 * tx[8] - bity 63 downto 56
208 * tx[9] - bity 55 downto 48
209 * tx[10] - bity 47 downto 40
210 * tx[11] - bity 39 downto 32
211 * tx[12] - bity 31 downto 24
212 * tx[13] - bity 23 downto 16
213 * tx[14] - bity 15 downto 8
214 * tx[15] - bity 7 downto 0
217 * bit 94 - enable PWM1
218 * bit 93 - enable PWM2
219 * bit 92 - enable PWM3
228 * bits 47 .. 32 - match PWM1
229 * bits 31 .. 16 - match PWM2
230 * bits 15 .. 0 - match PWM3
236 /* keep the 11-bit cap*/
238 if (rps.pwm1>2047) rps.pwm1=2047;
239 if (rps.pwm2>2047) rps.pwm2=2047;
240 if (rps.pwm3>2047) rps.pwm3=2047;
242 tx[0]=rps.test; /*bit 94 - enable PWM1*/
244 /*now we have to switch the bytes due to endianess */
245 /* ARMv6 & ARMv7 instructions are little endian */
247 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
248 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
251 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
252 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
255 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
256 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
261 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
263 void * pos_monitor(void* param){
266 usleep(1000000); /*1 Hz*/
272 * Multiplication of 11 bit
273 * Zaporne vysledky prvede na nulu.
275 inline uint16_t mult_cap(int32_t s,int d){
279 /* multiplicate as if maximum sinus value was unity */
280 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
285 int sin_commutator(int duty){
286 #define DEGREE_60 715827883
287 #define DEGREE_120 1431655765
288 #define DEGREE_180 2147483648
289 #define DEGREE_240 2863311531
290 #define DEGREE_300 3579139413
295 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
297 /*use it as cyclic 32-bit logic*/
299 if (duty>=0){ /*clockwise rotation*/
301 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
304 rps.pwm1=(uint16_t)pwm;
307 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
310 rps.pwm2=(uint16_t)pwm;
313 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
316 rps.pwm3=(uint16_t)pwm;
321 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
324 rps.pwm1=(uint16_t)pwm;
327 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
330 rps.pwm2=(uint16_t)pwm;
333 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
336 rps.pwm3=(uint16_t)pwm;
342 * Test function to be placed in controll loop.
343 * Switches PWM's at point where they produce same force.
344 * This points are found thanks to IRC position,
347 void simple_ind_dist_commutator(int duty){
348 if (duty>=0){ /* clockwise - so that position increase */
350 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
351 (rps.index_dist>=1048 && rps.index_dist<=1377)){
356 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
357 (rps.index_dist>=1377 && rps.index_dist<=1711)){
362 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
363 (rps.index_dist>=711 && rps.index_dist<=1048) ||
364 (rps.index_dist>=1711 && rps.index_dist<=1999)){
369 }else{ /*counter-clockwise - position decrease */
371 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
372 (rps.index_dist>=1544 && rps.index_dist<=1878)){
377 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
378 (rps.index_dist>=881 && rps.index_dist<=1210) ||
379 (rps.index_dist>=1878 && rps.index_dist<=1999)){
384 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
385 (rps.index_dist>=1210 && rps.index_dist<=1544)){
394 * Test function to be placed in controll loop.
395 * Switches PWM's at point where they produce same force
397 inline void simple_hall_commutator(int duty){
398 if (duty>=0){ /* clockwise - so that position increase */
400 if (data.hal2 && !data.hal3){
405 }else if (data.hal1 && !data.hal2){
410 }else if (!data.hal1 && data.hal3){
415 }else{ /*counter-clockwise - position decrease */
417 if (!data.hal2 && data.hal3){
422 }else if (!data.hal1 && data.hal2){
427 }else if (data.hal1 && !data.hal3){
436 * Computation of distance to index.
438 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
439 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
441 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
445 uint16_t pos = 0x0FFF & data.pozice_raw;
447 uint16_t index = data.index_position;
449 if (index<1999){ /*index e<0,1998> */
450 if (pos<index){ /*pozice e<0,index-1> */
451 /*proti smeru h.r. od indexu*/
453 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
454 /*po smeru h.r. od indexu*/
456 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
458 }else{ /*pozice e<index+2096,4095> */
459 /*proti smeru h.r. od indexu - podtecena pozice*/
462 }else if (index<=2096){ /*index e<1999,2096>*/
463 if (pos<index-1999){ /*pozice e<0,index-2000> */
465 }else if (pos<index){ /*pozice e<index-1999,index-1> */
466 /*proti smeru h.r. od indexu*/
468 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
469 /*po smeru h.r. od indexu*/
471 }else { /*pozice e<index+2000,4095> */
474 }else{ /*index e<2097,4095> */
475 if (pos<=index-2097){ /*pozice e<0,index-2097> */
476 /*po smeru h.r. od indexu - pretecena pozice*/
478 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
480 }else if (pos<index){ /*pozice e<index-1999,index-1> */
481 /*proti smeru h.r. od indexu*/
483 }else{ /*pozice e<index,4095> */
484 /*po smeru h.r. od indexu*/
489 rps.index_dist = dist;
498 * Very simple PID regulator.
499 * Now only with P-part so that the error doesnt go to zero.
500 * TODO: add anti-wind up and I and D parts
504 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
505 if (duty_tmp>MAX_DUTY){
507 }else if (duty_tmp<-MAX_DUTY){
517 void * read_data(void* param){
519 struct rpi_in pocatek;
521 int interval = 1000000; /* 1ms ~ 1kHz*/
522 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
524 uint16_t last_index; /*we have index up-to date*/
525 pocatek = spi_read(tx);
526 clock_gettime(CLOCK_MONOTONIC ,&t);
527 /* start after one second */
530 /* wait until next shot */
531 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
532 sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
533 prepare_tx(tx); /*save the data to send*/
534 data = spi_read(tx); /*exchange data*/
535 /*subtract initiate postion */
537 substractOffset(&data,&pocatek);
541 last_index=data.index_position;
543 }else if (last_index!=data.index_position){
548 if (rps.index_ok && rps.commutate){
549 /*simple_ind_dist_commutator(rps.duty);*/
550 /*sin_commutator(rps.duty);*/
551 inv_trans_comm(rps.duty);
552 inv_trans_comm_2(rps.duty);
553 }else if(!rps.index_ok && rps.commutate){
554 simple_hall_commutator(rps.duty);
556 sem_post(&rps.thd_par_sem); /*--post semaphore---*/
558 /* calculate next shot */
559 t.tv_nsec += interval;
561 while (t.tv_nsec >= NSEC_PER_SEC) {
562 t.tv_nsec -= NSEC_PER_SEC;
572 * \brief Main function.
576 pthread_t base_thread_id;
577 clk_init(); /* inicializace gpio hodin */
578 spi_init(); /* iniicializace spi*/
580 /*semafor pro detekci zpracovani parametru vlaken*/
581 sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
584 base_thread_id=pthread_self();
586 /*main control loop*/
587 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
589 /*monitor of current state*/
590 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
592 /*wait for commands*/