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={
46 .pwm1=0,.pwm2=0, .pwm3=0,
47 .pwm1=0, .t_pwm2=0, .t_pwm3=0,
49 .duty=0, /* duty cycle of pwm */
50 .index_dist=0, /* distance to index position */
52 .tf_count=0, /*number of transfer*/
53 .desired_pos=0 /* desired position */
57 * \brief Initilizes GPCLK.
61 initialise(); /*namapovani gpio*/
62 initClock(PLLD_500_MHZ, 10, 0);
63 gpioSetMode(4, FSEL_ALT0);
67 * \brief Terminates GPCLK.
70 inline void clk_disable(){
75 * \brief Signal handler pro Ctrl+C
80 /*muzeme zavrit semafor*/
81 sem_destroy(&rps.thd_par_sem);
82 printf("\nprogram bezpecne ukoncen\n");
85 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
86 data->pozice_raw=data->pozice;
87 data->pozice-=offset->pozice;
92 void prepare_tx(uint8_t * tx){
95 * tx[4] - bity 95 downto 88 - bits that are sent first
96 * tx[5] - bity 87 downto 80
97 * tx[6] - bity 79 downto 72
98 * tx[7] - bity 71 downto 64
99 * tx[8] - bity 63 downto 56
100 * tx[9] - bity 55 downto 48
101 * tx[10] - bity 47 downto 40
102 * tx[11] - bity 39 downto 32
103 * tx[12] - bity 31 downto 24
104 * tx[13] - bity 23 downto 16
105 * tx[14] - bity 15 downto 8
106 * tx[15] - bity 7 downto 0
109 * bit 94 - enable PWM1
110 * bit 93 - enable PWM2
111 * bit 92 - enable PWM3
120 * bits 47 .. 32 - match PWM1
121 * bits 31 .. 16 - match PWM2
122 * bits 15 .. 0 - match PWM3
128 /* keep the 11-bit cap*/
130 if (rps.pwm1>2047) rps.pwm1=2047;
131 if (rps.pwm2>2047) rps.pwm2=2047;
132 if (rps.pwm3>2047) rps.pwm3=2047;
134 tx[0]=rps.test; /*bit 94 - enable PWM1*/
136 /*now we have to switch the bytes due to endianess */
137 /* ARMv6 & ARMv7 instructions are little endian */
139 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
140 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
143 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
144 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
147 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
148 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
153 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
155 void * pos_monitor(void* param){
158 usleep(1000000); /*1 Hz*/
164 * Multiplication of 11 bit
165 * Zaporne vysledky prvede na nulu.
167 inline uint16_t mult_cap(int32_t s,int d){
171 /* multiplicate as if maximum sinus value was unity */
172 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
177 int sin_commutator(int duty){
178 #define DEGREE_60 715827883
179 #define DEGREE_120 1431655765
180 #define DEGREE_180 2147483648
181 #define DEGREE_240 2863311531
182 #define DEGREE_300 3579139413
187 /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
189 /*use it as cyclic 32-bit logic*/
191 if (duty>=0){ /*clockwise rotation*/
193 sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
196 rps.pwm1=(uint16_t)pwm;
199 sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
202 rps.pwm2=(uint16_t)pwm;
205 sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
208 rps.pwm3=(uint16_t)pwm;
213 sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
216 rps.pwm1=(uint16_t)pwm;
219 sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
222 rps.pwm2=(uint16_t)pwm;
225 sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
228 rps.pwm3=(uint16_t)pwm;
234 * Test function to be placed in controll loop.
235 * Switches PWM's at point where they produce same force.
236 * This points are found thanks to IRC position,
239 void simple_ind_dist_commutator(int duty){
240 if (duty>=0){ /* clockwise - so that position increase */
242 if ((rps.index_dist>=45 && rps.index_dist<=373) ||
243 (rps.index_dist>=1048 && rps.index_dist<=1377)){
248 }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
249 (rps.index_dist>=1377 && rps.index_dist<=1711)){
254 }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
255 (rps.index_dist>=711 && rps.index_dist<=1048) ||
256 (rps.index_dist>=1711 && rps.index_dist<=1999)){
261 }else{ /*counter-clockwise - position decrease */
263 if ((rps.index_dist>=544 && rps.index_dist<=881) ||
264 (rps.index_dist>=1544 && rps.index_dist<=1878)){
269 }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
270 (rps.index_dist>=881 && rps.index_dist<=1210) ||
271 (rps.index_dist>=1878 && rps.index_dist<=1999)){
276 }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
277 (rps.index_dist>=1210 && rps.index_dist<=1544)){
286 * Test function to be placed in controll loop.
287 * Switches PWM's at point where they produce same force
289 inline void simple_hall_commutator(int duty){
290 if (duty>=0){ /* clockwise - so that position increase */
292 if (data.hal2 && !data.hal3){
297 }else if (data.hal1 && !data.hal2){
302 }else if (!data.hal1 && data.hal3){
307 }else{ /*counter-clockwise - position decrease */
309 if (!data.hal2 && data.hal3){
314 }else if (!data.hal1 && data.hal2){
319 }else if (data.hal1 && !data.hal3){
328 * Computation of distance to index.
330 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
331 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
333 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
337 uint16_t pos = 0x0FFF & data.pozice_raw;
339 uint16_t index = data.index_position;
341 if (index<1999){ /*index e<0,1998> */
342 if (pos<index){ /*pozice e<0,index-1> */
343 /*proti smeru h.r. od indexu*/
345 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
346 /*po smeru h.r. od indexu*/
348 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
350 }else{ /*pozice e<index+2096,4095> */
351 /*proti smeru h.r. od indexu - podtecena pozice*/
354 }else if (index<=2096){ /*index e<1999,2096>*/
355 if (pos<index-1999){ /*pozice e<0,index-2000> */
357 }else if (pos<index){ /*pozice e<index-1999,index-1> */
358 /*proti smeru h.r. od indexu*/
360 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
361 /*po smeru h.r. od indexu*/
363 }else { /*pozice e<index+2000,4095> */
366 }else{ /*index e<2097,4095> */
367 if (pos<=index-2097){ /*pozice e<0,index-2097> */
368 /*po smeru h.r. od indexu - pretecena pozice*/
370 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
372 }else if (pos<index){ /*pozice e<index-1999,index-1> */
373 /*proti smeru h.r. od indexu*/
375 }else{ /*pozice e<index,4095> */
376 /*po smeru h.r. od indexu*/
381 rps.index_dist = dist;
390 * Very simple PID regulator.
391 * Now only with P-part so that the error doesnt go to zero.
392 * TODO: add anti-wind up and I and D parts
396 duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
397 if (duty_tmp>MAX_DUTY){
399 }else if (duty_tmp<-MAX_DUTY){
409 void * read_data(void* param){
411 struct rpi_in pocatek;
413 int interval = 1000000; /* 1ms ~ 1kHz*/
414 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
416 uint16_t last_index; /*we have index up-to date*/
417 pocatek = spi_read(tx);
418 clock_gettime(CLOCK_MONOTONIC ,&t);
419 /* start after one second */
422 /* wait until next shot */
423 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
424 sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
425 prepare_tx(tx); /*save the data to send*/
426 data = spi_read(tx); /*exchange data*/
427 /*subtract initiate postion */
429 substractOffset(&data,&pocatek);
433 last_index=data.index_position;
435 }else if (last_index!=data.index_position){
440 if (rps.index_ok && rps.commutate){
441 /*simple_ind_dist_commutator(rps.duty);*/
442 sin_commutator(rps.duty);
443 }else if(!rps.index_ok && rps.commutate){
444 simple_hall_commutator(rps.duty);
446 sem_post(&rps.thd_par_sem); /*--post semaphore---*/
448 /* calculate next shot */
449 t.tv_nsec += interval;
451 while (t.tv_nsec >= NSEC_PER_SEC) {
452 t.tv_nsec -= NSEC_PER_SEC;
462 * \brief Main function.
466 pthread_t base_thread_id;
467 clk_init(); /* inicializace gpio hodin */
468 spi_init(); /* iniicializace spi*/
470 /*semafor pro detekci zpracovani parametru vlaken*/
471 sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
474 base_thread_id=pthread_self();
476 /*main control loop*/
477 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
479 /*monitor of current state*/
480 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
482 /*wait for commands*/