X-Git-Url: https://rtime.felk.cvut.cz/gitweb/fpga/rpi-motor-control.git/blobdiff_plain/bd18e9490dc33e857c4e857e6efd9be683f6ad21..fa2db689500f7faf91c5127ada91170e6b06f4df:/pmsm-control/test_sw/main_pmsm.c diff --git a/pmsm-control/test_sw/main_pmsm.c b/pmsm-control/test_sw/main_pmsm.c index 4e64f95..5195150 100644 --- a/pmsm-control/test_sw/main_pmsm.c +++ b/pmsm-control/test_sw/main_pmsm.c @@ -15,6 +15,7 @@ #include /*sheduler*/ #include /*usleep*/ #include /*threads*/ +#include /*nanosleep*/ #include "rpin.h" /*gpclk*/ #include "rp_spi.h" /*spi*/ @@ -50,6 +51,8 @@ #define PXMC_SIN_FIX_PI2 0x40000000 #define PXMC_SIN_FIX_2PI3 0x55555555 +#define NSEC_PER_SEC (1000000000) /* The number of nsecs per sec. */ + struct sigaction sighnd; /*struktura pro signal handler*/ struct rpi_in data; struct rpi_state{ @@ -427,20 +430,25 @@ inline void pid(){ /* * \brief * Feedback loop. - * TODO: replace usleep with real-time wait - * measure times */ void * read_data(void* param){ int i; struct rpi_in pocatek; + struct timespec t; + int interval = 1000000; /* 1ms ~ 1kHz*/ uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ; char first=1; uint16_t last_index; /*we have index up-to date*/ set_priority(param); /*set priority*/ pocatek = spi_read(tx); + clock_gettime(CLOCK_MONOTONIC ,&t); + /* start after one second */ + t.tv_sec++; while(1){ - prepare_tx(tx); /*save the data to send*/ + /* wait until next shot */ + clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL); sem_wait(&thd_par_sem); /*---take semaphore---*/ + prepare_tx(tx); /*save the data to send*/ data = spi_read(tx); /*exchange data*/ /*subtract initiate postion */ rps.tf_count++; @@ -462,7 +470,15 @@ void * read_data(void* param){ simple_hall_commutator(rps.duty); } sem_post(&thd_par_sem); /*--post semaphore---*/ - usleep(1000); /*1kHz*/ + + /* calculate next shot */ + t.tv_nsec += interval; + + while (t.tv_nsec >= NSEC_PER_SEC) { + t.tv_nsec -= NSEC_PER_SEC; + t.tv_sec++; + } + } }