#include <sched.h> /*sheduler*/
#include <unistd.h> /*usleep*/
#include <pthread.h> /*threads*/
+#include <time.h> /*nanosleep*/
#include "rpin.h" /*gpclk*/
#include "rp_spi.h" /*spi*/
#define PRIOR_LOW 20
#define THREAD_SHARED 0
-#define INIT_VALUE 0 /*init value for semaphor*/
+#define INIT_VALUE 1 /*init value for semaphor*/
#define PXMC_SIN_FIX_TAB_BITS 9
#define PXMC_SIN_FIX_PI2 0x40000000
#define PXMC_SIN_FIX_2PI3 0x55555555
-struct sigaction sighnd; /*struktura pro signal handler*/
+#define NSEC_PER_SEC (1000000000) /* The number of nsecs per sec. */
+
+
+
struct rpi_in data;
struct rpi_state{
uint8_t test;
/**
* \brief Signal handler pro Ctrl+C
*/
-void sighnd_fnc(){
+void appl_stop(){
spi_disable();
clk_disable();
/*muzeme zavrit semafor*/
sem_destroy(&thd_par_sem);
printf("\nprogram bezpecne ukoncen\n");
- exit(0);
}
void substractOffset(struct rpi_in* data, struct rpi_in* offset){
printf("\npozice=%d\n",(int32_t)data_p.pozice);
printf("chtena pozice=%d\n",s.desired_pos);
printf("transfer count=%u\n",s.tf_count);
- printf("raw_pozice=%d\n",(int32_t)data_p.pozice_raw);
+ printf("raw_pozice=%u\n",data_p.pozice_raw);
printf("raw_pozice last12=%u\n",(data_p.pozice_raw&0x0FFF));
printf("index position=%u\n",data_p.index_position);
printf("hal1=%d, hal2=%d, hal3=%d\n",data_p.hal1,data_p.hal2,data_p.hal3);
* Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
*/
void * pos_monitor(void* param){
- set_priority(param); /*set priority*/
while(1){
printData();
usleep(1000000); /*1 Hz*/
}
return (void*)0;
}
-
+/*
+ * \brief
+ * Multiplication of 11 bit
+ */
+inline int16_t mult_cap(int32_t s,int d){
+ int j;
+ int res=0;
+ for(j=0;j!=11;j++){
+ /* multiplicate as if maximum sinus value was unity */
+ res+=(!(s & 0x10000000))*(((1 << j) & s)>>j)*(d>>(10-j));
+ }
+ return res;
+}
inline
int sin_commutator(int duty){
#define DEGREE_60 715827883
#define DEGREE_300 3579139413
uint32_t j,pos;
int32_t sin;
- uint16_t pwm;
pos=rps.index_dist*4294967;
if (duty>=0){ /*clockwise rotation*/
/* 1st phase */
sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
- pwm=0;
- for(j=0;j!=11;j++){
- /* multiplicate as if maximum sinus value was unity */
- pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
- }
- rps.pwm1=pwm;
+ rps.pwm1=mult_cap(sin, duty);
/* 2nd phase */
sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
- pwm=0;
- for(j=0;j!=11;j++){
- /* multiplicate as if maximum sinus value was unity */
- pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
- }
- rps.pwm2=pwm;
+ rps.pwm2=mult_cap(sin, duty);;
/* 3rd phase */
sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
- pwm=0;
- for(j=0;j!=11;j++){
- /* multiplicate as if maximum sinus value was unity */
- pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
- }
- rps.pwm3=pwm;
+ rps.pwm3=mult_cap(sin, duty);;
}else{
duty=-duty;
/* 1st phase */
sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
- pwm=0;
- for(j=0;j!=11;j++){
- /* multiplicate as if maximum sinus value was unity */
- pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
- }
- rps.pwm1=pwm;
+ rps.pwm1=mult_cap(sin, duty);
/* 2nd phase */
sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
- pwm=0;
- for(j=0;j!=11;j++){
- /* multiplicate as if maximum sinus value was unity */
- pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
- }
- rps.pwm2=pwm;
+ rps.pwm2=mult_cap(sin, duty);
/* 3rd phase */
sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
- pwm=0;
- for(j=0;j!=11;j++){
- /* multiplicate as if maximum sinus value was unity */
- pwm+=(!(sin & 0x10000000))*(((1 << j) & sin)>>j)*(duty>>(10-j));
- }
- rps.pwm3=pwm;
+ rps.pwm3=mult_cap(sin, duty);
}
return 0;
}
}
}
/**
- * Funkce pravidelne vycita data z motoru
+ * \brief
+ * Computation of distance to index.
+ *
+ * K dispozici je 12-bit index, to umoznuje ulozit 4096 ruznych bodu
+ * Je nutne vyjadrit 1999 bodu proti i posmeru h.r. od indexu -
+ * to je 3999 bodu
+ * =>12 bitu je dostacujicich, pokud nikdy nedojde ke ztrate
+ * signalu indexu
*/
-inline void comIndDist(){
- rps.index_dist=0x0FFF & (data.pozice_raw - data.index_position);
- /*
- * if distance is bigger than 2047, the distance underflown
- * -> if 12th bit is set, substract 2096
- */
- rps.index_dist-=((rps.index_dist & 0x0800)>>11)*2096;
+void comIndDist(){
+ uint16_t pos = 0x0FFF & data.pozice_raw;
+ uint16_t dist;
+ uint16_t index = data.index_position;
+
+ if (index<1999){ /*index e<0,1998> */
+ if (pos<index){ /*pozice e<0,index-1> */
+ /*proti smeru h.r. od indexu*/
+ dist=pos+2000-index;
+ }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
+ /*po smeru h.r. od indexu*/
+ dist=pos-index;
+ }else if (pos<index+2096){ /*pozice e<index+2000,index+2095> */
+ goto index_lost;
+ }else{ /*pozice e<index+2096,4095> */
+ /*proti smeru h.r. od indexu - podtecena pozice*/
+ dist=pos-index-2096;
+ }
+ }else if (index<=2096){ /*index e<1999,2096>*/
+ if (pos<index-1999){ /*pozice e<0,index-2000> */
+ goto index_lost;
+ }else if (pos<index){ /*pozice e<index-1999,index-1> */
+ /*proti smeru h.r. od indexu*/
+ dist=pos+2000-index;
+ }else if (pos<=index+1999){ /*pozice e<index,index+1999> */
+ /*po smeru h.r. od indexu*/
+ dist=pos-index;
+ }else { /*pozice e<index+2000,4095> */
+ goto index_lost;
+ }
+ }else{ /*index e<2097,4095> */
+ if (pos<=index-2097){ /*pozice e<0,index-2097> */
+ /*po smeru h.r. od indexu - pretecena pozice*/
+ dist=4096+pos-index;
+ }else if (pos<index-1999){ /*pozice e<index-2096,index-2000> */
+ goto index_lost;
+ }else if (pos<index){ /*pozice e<index-1999,index-1> */
+ /*proti smeru h.r. od indexu*/
+ dist=pos+2000-index;
+ }else{ /*pozice e<index,4095> */
+ /*po smeru h.r. od indexu*/
+ dist=pos-index;
+ }
+ }
+
+ rps.index_dist = dist;
+ return;
+
+ index_lost:
+ rps.index_ok=0;
+ return;
}
/*
* \brief
/*
* \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++;
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++;
+ }
+
}
}
+
/**
- * \brief Main function.
+ * \brief
+ * Commands detection.
*/
-
-int main(){
+void poll_cmd(){
unsigned int tmp;
-
- /*nastaveni priorit vlaken*/
- struct thread_param tsp;
- tsp.sch_policy = SCHED_FIFO;
-
- /*nastaveni signalu pro vypnuti pomoci Ctrl+C*/
- sighnd.sa_handler=&sighnd_fnc;
- sigaction(SIGINT, &sighnd, NULL );
-
- clk_init(); /* inicializace gpio hodin */
- spi_init(); /* iniicializace spi*/
-
- /*semafor pro detekci zpracovani parametru vlaken*/
- sem_init(&thd_par_sem,THREAD_SHARED,INIT_VALUE);
-
- /*vlakna*/
- pthread_t tid; /*identifikator vlakna*/
- pthread_attr_t attr; /*atributy vlakna*/
- pthread_attr_init(&attr); /*inicializuj implicitni atributy*/
-
-
-
- /*ziskavani dat z motoru*//*vysoka priorita*/
- tsp.sch_prior = PRIOR_HIGH;
- pthread_create(&tid, &attr, read_data, (void*)&tsp);
-
- /*vypisovani lokalni pozice*//*nizka priorita*/
- tsp.sch_prior = PRIOR_LOW;
- sem_wait(&thd_par_sem);
- pthread_create(&tid, &attr, pos_monitor, (void*)&tsp);
-
-
-
/*
* Note:
* pri pouziti scanf("%u",&simple_hall_duty); dochazelo
}
}
- return 0;
+ return ;
}
+/**
+ * \brief Main function.
+ */
+
+int main(){
+ pthread_t base_thread_id;
+ clk_init(); /* inicializace gpio hodin */
+ spi_init(); /* iniicializace spi*/
+
+ /*semafor pro detekci zpracovani parametru vlaken*/
+ sem_init(&thd_par_sem,THREAD_SHARED,INIT_VALUE);
+ setup_environment();
+
+ base_thread_id=pthread_self();
+
+ /*main control loop*/
+ create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
+ /*monitor of current state*/
+ create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
+
+ /*wait for commands*/
+ poll_cmd();
+
+ return 0;
+}