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 "pmsm_state.h"
26 #include "controllers.h"
27 #include "commutators.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={
48 .pwm1=0,.pwm2=0, .pwm3=0,
49 .pwm1=0, .t_pwm2=0, .t_pwm3=0,
51 .duty=0, /* duty cycle of pwm */
52 .index_dist=0, /* distance to index position */
54 .tf_count=0, /*number of transfer*/
55 .desired_pos=0, /* desired position */
61 .log_col_count=0, /* pocet radku zaznamu */
68 * \brief Initilizes GPCLK.
72 initialise(); /*namapovani gpio*/
73 initClock(PLLD_500_MHZ, 10, 0);
74 gpioSetMode(4, FSEL_ALT0);
78 * \brief Terminates GPCLK.
81 inline void clk_disable(){
92 if (rps.log_col_count){
93 for (r=0;r<LOG_ROWS;r++){
107 if (rps.log_col==MAX_LOGS-1){
111 rps.logs[0][rps.log_col]=(int)rps.tf_count;
112 rps.logs[1][rps.log_col]=(int)rps.spi_dat->pozice;
114 rps.logs[2][rps.log_col]=(int)rps.pwm1;
115 rps.logs[3][rps.log_col]=(int)rps.pwm2;
116 rps.logs[4][rps.log_col]=(int)rps.pwm3;
117 rps.logs[5][rps.log_col]=rps.duty;
119 rps.logs[6][rps.log_col]=rps.desired_spd;
120 rps.logs[7][rps.log_col]=rps.speed;
122 rps.logs[8][rps.log_col]=(int)(rps.spi_dat->ch1/rps.spi_dat->adc_m_count);
123 rps.logs[9][rps.log_col]=(int)(rps.spi_dat->ch2/rps.spi_dat->adc_m_count);
124 rps.logs[10][rps.log_col]=(int)(rps.spi_dat->ch0/rps.spi_dat->adc_m_count);
128 if (rps.log_col==rps.log_col_count-1){
129 rps.log_col_count*=2;
130 rps.log_col_count%=MAX_LOGS;
131 for (r=0;r<LOG_ROWS;r++){
132 rps.logs[r]=realloc(rps.logs[r],rps.log_col_count*sizeof(int));
133 if (rps.logs[r]==NULL){
144 * Pripravi psi buffer
146 void prepare_tx(uint8_t * tx){
149 * tx[4] - bity 95 downto 88 - bits that are sent first
150 * tx[5] - bity 87 downto 80
151 * tx[6] - bity 79 downto 72
152 * tx[7] - bity 71 downto 64
153 * tx[8] - bity 63 downto 56
154 * tx[9] - bity 55 downto 48
155 * tx[10] - bity 47 downto 40
156 * tx[11] - bity 39 downto 32
157 * tx[12] - bity 31 downto 24
158 * tx[13] - bity 23 downto 16
159 * tx[14] - bity 15 downto 8
160 * tx[15] - bity 7 downto 0
163 * bit 94 - enable PWM1
164 * bit 93 - enable PWM2
165 * bit 92 - enable PWM3
174 * bits 47 .. 32 - match PWM1
175 * bits 31 .. 16 - match PWM2
176 * bits 15 .. 0 - match PWM3
182 /* keep the 11-bit cap*/
184 if (rps.pwm1>2047) rps.pwm1=2047;
185 if (rps.pwm2>2047) rps.pwm2=2047;
186 if (rps.pwm3>2047) rps.pwm3=2047;
188 tx[0]=rps.test; /*bit 94 - enable PWM1*/
190 /*now we have to switch the bytes due to endianess */
191 /* ARMv6 & ARMv7 instructions are little endian */
193 tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
194 tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
197 tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
198 tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
201 tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
202 tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
209 * \brief Signal handler pro Ctrl+C
213 sem_wait(&rps.thd_par_sem);
215 memset(tx,0,16*sizeof(int));
219 prepare_tx(tx); /*save the data to send*/
225 /*muzeme zavrit semafor*/
226 sem_destroy(&rps.thd_par_sem);
227 printf("\nprogram bezpecne ukoncen\n");
230 void substractOffset(struct rpi_in* data, struct rpi_in* offset){
231 data->pozice=data->pozice_raw-offset->pozice_raw;
238 * Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
240 void * pos_monitor(void* param){
243 usleep(1000000); /*1 Hz*/
249 * Multiplication of 11 bit
250 * Zaporne vysledky prvede na nulu.
252 inline uint16_t mult_cap(int32_t s,int d){
256 /* multiplicate as if maximum sinus value was unity */
257 res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
265 * Computation of distance to index.
267 * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
268 * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
270 * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
274 uint16_t pos = 0x0FFF & data.pozice_raw;
276 uint16_t index = data.index_position;
278 if (index<1999){ /*index e<0,1998> */
279 if (pos<index){ /*pozice e<0,index-1> */
280 /*proti smeru h.r. od indexu*/
282 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
283 /*po smeru h.r. od indexu*/
285 }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
287 }else{ /*pozice e<index+2096,4095> */
288 /*proti smeru h.r. od indexu - podtecena pozice*/
291 }else if (index<=2096){ /*index e<1999,2096>*/
292 if (pos<index-1999){ /*pozice e<0,index-2000> */
294 }else if (pos<index){ /*pozice e<index-1999,index-1> */
295 /*proti smeru h.r. od indexu*/
297 }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
298 /*po smeru h.r. od indexu*/
300 }else { /*pozice e<index+2000,4095> */
303 }else{ /*index e<2097,4095> */
304 if (pos<=index-2097){ /*pozice e<0,index-2097> */
305 /*po smeru h.r. od indexu - pretecena pozice*/
307 }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
309 }else if (pos<index){ /*pozice e<index-1999,index-1> */
310 /*proti smeru h.r. od indexu*/
312 }else{ /*pozice e<index,4095> */
313 /*po smeru h.r. od indexu*/
318 rps.index_dist = dist;
333 spd=rps.spi_dat->pozice-rps.old_pos[rps.tf_count%OLD_POS_NUM];
334 rps.speed=(int32_t)spd;
340 * TODO: replace bunch of 'IFs' with Object-like pattern
342 void * read_data(void* param){
344 struct rpi_in pocatek;
346 int interval = 1000000; /* 1ms ~ 1kHz*/
347 uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
349 uint16_t last_index; /*we have index up-to date*/
350 pocatek = spi_read(tx);
351 clock_gettime(CLOCK_MONOTONIC ,&t);
352 /* start after one second */
355 /* wait until next shot */
356 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
357 sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
360 rps.old_pos[rps.tf_count%OLD_POS_NUM]=rps.spi_dat->pozice;
361 prepare_tx(tx); /*save the data to send*/
362 data = spi_read(tx); /*exchange data*/
363 /*subtract initiate postion */
365 substractOffset(&data,&pocatek);
366 compSpeed(); /*spocita rychlost*/
370 last_index=data.index_position;
372 }else if (last_index!=data.index_position){
374 comIndDist(); /*vypocet vzdalenosti indexu*/
376 }else{ /*index je v poradku*/
377 comIndDist(); /*vypocet vzdalenosti indexu*/
380 /* pocitame sirku plneni podle potreb rizeni*/
381 if (rps.pos_reg_ena){ /*pozicni rizeni*/
383 }else if(rps.spd_reg_ena){ /*rizeni na rychlost*/
387 /* sirka plneni prepoctena na jednotlive pwm */
388 if (rps.index_ok && rps.commutate){
389 /*simple_ind_dist_commutator(rps.duty);*/
390 /*sin_commutator(rps.duty);*/
391 inv_trans_comm(&rps);
392 inv_trans_comm_2(&rps);
393 }else if(!rps.index_ok && rps.commutate){
394 simple_hall_commutator(&rps);
397 /*zalogujeme hodnoty*/
398 if (rps.doLogs && !(rps.tf_count%LOG_PERIOD)){
402 sem_post(&rps.thd_par_sem); /*--post semaphore---*/
404 /* calculate next shot */
405 t.tv_nsec += interval;
407 while (t.tv_nsec >= NSEC_PER_SEC) {
408 t.tv_nsec -= NSEC_PER_SEC;
417 * \brief Main function.
421 pthread_t base_thread_id;
422 clk_init(); /* inicializace gpio hodin */
423 spi_init(); /* iniicializace spi*/
425 /*semafor pro detekci zpracovani parametru vlaken*/
426 sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
429 base_thread_id=pthread_self();
431 /*main control loop*/
432 create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
434 /*monitor of current state*/
435 create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
437 /*wait for commands*/