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 * \brief Signal handler pro Ctrl+C
79 /*muzeme zavrit semafor*/
80 sem_destroy(&rps.thd_par_sem);
81 printf("\nprogram bezpecne ukoncen\n");
84 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
85 data->pozice_raw=data->pozice;
86 data->pozice-=offset->pozice;
91 * Transformace pro uhel pocitany po smeru hodinovych rucicek
93 void dq2alphabeta(int32_t *alpha, int32_t *beta, int d, int q, int32_t sin, int32_t cos){
98 void alphabeta2pwm3(int32_t * pwma, int32_t * pwmb, int32_t *pwmc,int32_t alpha, int32_t beta){
100 *pwmb=-alpha/2+beta*887/1024;
101 *pwmc=-alpha/2-beta*887/1024;
103 void inv_trans_comm(int duty){
107 int32_t pwma,pwmb,pwmc;
109 /*melo by byt urceno co nejpresneji, aby faze 'a' splyvala s osou 'alpha'*/
111 /*use it as cyclic 32-bit logic*/
113 pxmc_sincos_fixed_inline(&sin, &cos, pos, 16);
114 dq2alphabeta(&alpha, &beta,0,duty, sin, cos);
117 alphabeta2pwm3(&pwma,&pwmb, &pwmc,alpha,beta);
124 rps.pwm1=(uint16_t)pwma;
125 rps.pwm3=(uint16_t)pwmb;
126 rps.pwm2=(uint16_t)pwmc;
129 void prepare_tx(uint8_t * tx){
132 * tx[4] - bity 95 downto 88 - bits that are sent first
133 * tx[5] - bity 87 downto 80
134 * tx[6] - bity 79 downto 72
135 * tx[7] - bity 71 downto 64
136 * tx[8] - bity 63 downto 56
137 * tx[9] - bity 55 downto 48
138 * tx[10] - bity 47 downto 40
139 * tx[11] - bity 39 downto 32
140 * tx[12] - bity 31 downto 24
141 * tx[13] - bity 23 downto 16
142 * tx[14] - bity 15 downto 8
143 * tx[15] - bity 7 downto 0
146 * bit 94 - enable PWM1
147 * bit 93 - enable PWM2
148 * bit 92 - enable PWM3
157 * bits 47 .. 32 - match PWM1
158 * bits 31 .. 16 - match PWM2
159 * bits 15 .. 0 - match PWM3
165 /* keep the 11-bit cap*/
167 if (rps.pwm1>2047) rps.pwm1=2047;
168 if (rps.pwm2>2047) rps.pwm2=2047;
169 if (rps.pwm3>2047) rps.pwm3=2047;
171 tx[0]=rps.test; /*bit 94 - enable PWM1*/
173 /*now we have to switch the bytes due to endianess */
174 /* ARMv6 & ARMv7 instructions are little endian */
176 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
177 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
180 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
181 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
184 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
185 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
190 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
192 void * pos_monitor(void* param){
195 usleep(1000000); /*1 Hz*/
201 * Multiplication of 11 bit
202 * Zaporne vysledky prvede na nulu.
204 inline uint16_t mult_cap(int32_t s,int d){
208 /* multiplicate as if maximum sinus value was unity */
209 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
214 int sin_commutator(int duty){
215 #define DEGREE_60 715827883
216 #define DEGREE_120 1431655765
217 #define DEGREE_180 2147483648
218 #define DEGREE_240 2863311531
219 #define DEGREE_300 3579139413
224 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
226 /*use it as cyclic 32-bit logic*/
228 if (duty>=0){ /*clockwise rotation*/
230 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
233 rps.pwm1=(uint16_t)pwm;
236 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
239 rps.pwm2=(uint16_t)pwm;
242 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
245 rps.pwm3=(uint16_t)pwm;
250 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
253 rps.pwm1=(uint16_t)pwm;
256 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
259 rps.pwm2=(uint16_t)pwm;
262 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
265 rps.pwm3=(uint16_t)pwm;
271 * Test function to be placed in controll loop.
272 * Switches PWM's at point where they produce same force.
273 * This points are found thanks to IRC position,
276 void simple_ind_dist_commutator(int duty){
277 if (duty>=0){ /* clockwise - so that position increase */
279 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
280 (rps.index_dist>=1048 && rps.index_dist<=1377)){
285 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
286 (rps.index_dist>=1377 && rps.index_dist<=1711)){
291 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
292 (rps.index_dist>=711 && rps.index_dist<=1048) ||
293 (rps.index_dist>=1711 && rps.index_dist<=1999)){
298 }else{ /*counter-clockwise - position decrease */
300 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
301 (rps.index_dist>=1544 && rps.index_dist<=1878)){
306 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
307 (rps.index_dist>=881 && rps.index_dist<=1210) ||
308 (rps.index_dist>=1878 && rps.index_dist<=1999)){
313 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
314 (rps.index_dist>=1210 && rps.index_dist<=1544)){
323 * Test function to be placed in controll loop.
324 * Switches PWM's at point where they produce same force
326 inline void simple_hall_commutator(int duty){
327 if (duty>=0){ /* clockwise - so that position increase */
329 if (data.hal2 && !data.hal3){
334 }else if (data.hal1 && !data.hal2){
339 }else if (!data.hal1 && data.hal3){
344 }else{ /*counter-clockwise - position decrease */
346 if (!data.hal2 && data.hal3){
351 }else if (!data.hal1 && data.hal2){
356 }else if (data.hal1 && !data.hal3){
365 * Computation of distance to index.
367 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
368 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
370 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
374 uint16_t pos = 0x0FFF & data.pozice_raw;
376 uint16_t index = data.index_position;
378 if (index<1999){ /*index e<0,1998> */
379 if (pos<index){ /*pozice e<0,index-1> */
380 /*proti smeru h.r. od indexu*/
382 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
383 /*po smeru h.r. od indexu*/
385 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
387 }else{ /*pozice e<index+2096,4095> */
388 /*proti smeru h.r. od indexu - podtecena pozice*/
391 }else if (index<=2096){ /*index e<1999,2096>*/
392 if (pos<index-1999){ /*pozice e<0,index-2000> */
394 }else if (pos<index){ /*pozice e<index-1999,index-1> */
395 /*proti smeru h.r. od indexu*/
397 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
398 /*po smeru h.r. od indexu*/
400 }else { /*pozice e<index+2000,4095> */
403 }else{ /*index e<2097,4095> */
404 if (pos<=index-2097){ /*pozice e<0,index-2097> */
405 /*po smeru h.r. od indexu - pretecena pozice*/
407 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
409 }else if (pos<index){ /*pozice e<index-1999,index-1> */
410 /*proti smeru h.r. od indexu*/
412 }else{ /*pozice e<index,4095> */
413 /*po smeru h.r. od indexu*/
418 rps.index_dist = dist;
427 * Very simple PID regulator.
428 * Now only with P-part so that the error doesnt go to zero.
429 * TODO: add anti-wind up and I and D parts
433 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
434 if (duty_tmp>MAX_DUTY){
436 }else if (duty_tmp<-MAX_DUTY){
446 void * read_data(void* param){
448 struct rpi_in pocatek;
450 int interval = 1000000; /* 1ms ~ 1kHz*/
451 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
453 uint16_t last_index; /*we have index up-to date*/
454 pocatek = spi_read(tx);
455 clock_gettime(CLOCK_MONOTONIC ,&t);
456 /* start after one second */
459 /* wait until next shot */
460 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
461 sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
462 prepare_tx(tx); /*save the data to send*/
463 data = spi_read(tx); /*exchange data*/
464 /*subtract initiate postion */
466 substractOffset(&data,&pocatek);
470 last_index=data.index_position;
472 }else if (last_index!=data.index_position){
477 if (rps.index_ok && rps.commutate){
478 /*simple_ind_dist_commutator(rps.duty);*/
479 /*sin_commutator(rps.duty);*/
480 inv_trans_comm(rps.duty);
481 }else if(!rps.index_ok && rps.commutate){
482 simple_hall_commutator(rps.duty);
484 sem_post(&rps.thd_par_sem); /*--post semaphore---*/
486 /* calculate next shot */
487 t.tv_nsec += interval;
489 while (t.tv_nsec >= NSEC_PER_SEC) {
490 t.tv_nsec -= NSEC_PER_SEC;
500 * \brief Main function.
504 pthread_t base_thread_id;
505 clk_init(); /* inicializace gpio hodin */
506 spi_init(); /* iniicializace spi*/
508 /*semafor pro detekci zpracovani parametru vlaken*/
509 sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
512 base_thread_id=pthread_self();
514 /*main control loop*/
515 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
517 /*monitor of current state*/
518 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
520 /*wait for commands*/