#include <unistd.h> /*usleep*/
#include <pthread.h> /*threads*/
#include <time.h> /*nanosleep*/
+#include <string.h>
#include "rpin.h" /*gpclk*/
#include "rp_spi.h" /*spi*/
#include "misc.h" /*structure for priorities*/
-#include "pxmc_sin_fixed.h" /*to test sin commutation */
#include "pmsm_state.h"
#include "cmd_proc.h"
-
-#define PRUM_PROUD 2061
-#define PRUM_SOUC 6183
-#define MAX_DUTY 128
-#define PID_P 0.1
+#include "controllers.h"
+#include "commutators.h"
+#include "comp.h"
+#include "logs.h"
#define PRIOR_KERN 50
#define PRIOR_HIGH 49
struct rpi_in data;
struct rpi_state rps={
+ //.MAX_DUTY=170,
.spi_dat=&data,
- .thd_par_sem=NULL,
.test=0,
.pwm1=0,.pwm2=0, .pwm3=0,
.pwm1=0, .t_pwm2=0, .t_pwm3=0,
.index_dist=0, /* distance to index position */
.index_ok=0,
.tf_count=0, /*number of transfer*/
- .desired_pos=0 /* desired position */
+ .desired_pos=0, /* desired position */
+ .pos_reg_ena=0,
+ .desired_spd=0,
+ .spd_reg_ena=0,
+ .old_pos={0},
+ .spd_err_sum=0,
+ .log_col_count=0, /* pocet radku zaznamu */
+ .log_col=0,
+ .doLogs=0,
+ .alpha_offset=960,
+ .main_commutator=zero_commutator, /* komutace vypnuta */
+ .main_controller=zero_controller /* rizeni vypnuto */
};
+
/**
* \brief Initilizes GPCLK.
*/
gpioSetMode(4, FSEL_ALT0);
return 0;
}
-/*
- * \brief Terminates GPCLK.
- */
-
-inline void clk_disable(){
- termClock(0);
-}
/**
* \brief Signal handler pro Ctrl+C
*/
void appl_stop(){
+ uint8_t tx[16];
+ sem_wait(&rps.thd_par_sem);
+
+ memset(tx,0,16*sizeof(int));
+ rps.pwm1=0;
+ rps.pwm2=0;
+ rps.pwm3=0;
+ spi_read(&rps);
+
spi_disable();
- clk_disable();
+ termClock(0);
+ freeLogs(&rps);
/*muzeme zavrit semafor*/
sem_destroy(&rps.thd_par_sem);
printf("\nprogram bezpecne ukoncen\n");
}
-void substractOffset(struct rpi_in* data, struct rpi_in* offset){
- data->pozice_raw=data->pozice;
- data->pozice-=offset->pozice;
- return;
-}
-/*
- * pocita procentualni odchylku od prumerneho proudu
- */
-float diff_p(float value){
- return ((float)value-PRUM_PROUD)*100/PRUM_PROUD;
-}
-/*
- * pocita procentualni odchylku od prumerneho souctu proudu
- */
-float diff_s(float value){
- return ((float)value-PRUM_SOUC)*100/PRUM_SOUC;
-}
-
-void prepare_tx(uint8_t * tx){
-
- /*Data format:
- * tx[4] - bity 95 downto 88 - bits that are sent first
- * tx[5] - bity 87 downto 80
- * tx[6] - bity 79 downto 72
- * tx[7] - bity 71 downto 64
- * tx[8] - bity 63 downto 56
- * tx[9] - bity 55 downto 48
- * tx[10] - bity 47 downto 40
- * tx[11] - bity 39 downto 32
- * tx[12] - bity 31 downto 24
- * tx[13] - bity 23 downto 16
- * tx[14] - bity 15 downto 8
- * tx[15] - bity 7 downto 0
- *
- * bit 95 - ADC reset
- * bit 94 - enable PWM1
- * bit 93 - enable PWM2
- * bit 92 - enable PWM3
- * bit 91 - shutdown1
- * bit 90 - shutdown2
- * bit 89 - shutdown3
- * .
- * .
- * Unused
- * .
- * .
- * bits 47 .. 32 - match PWM1
- * bits 31 .. 16 - match PWM2
- * bits 15 .. 0 - match PWM3
- */
-
-
- uint16_t tmp;
-
- /* keep the 11-bit cap*/
-
- if (rps.pwm1>2047) rps.pwm1=2047;
- if (rps.pwm2>2047) rps.pwm2=2047;
- if (rps.pwm3>2047) rps.pwm3=2047;
-
- tx[0]=rps.test; /*bit 94 - enable PWM1*/
-
- /*now we have to switch the bytes due to endianess */
- /* ARMv6 & ARMv7 instructions are little endian */
- /*pwm1*/
- tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/
- tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/
-
- /*pwm2*/
- tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/
- tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/
-
- /*pwm3*/
- tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/
- tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/
-
-
-}
/**
* Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
*/
}
return (void*)0;
}
-/*
- * \brief
- * Multiplication of 11 bit
- * Zaporne vysledky prvede na nulu.
- */
-inline uint16_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_120 1431655765
- #define DEGREE_180 2147483648
- #define DEGREE_240 2863311531
- #define DEGREE_300 3579139413
- uint32_t j,pos;
- int32_t sin;
- pos=rps.index_dist;
- int32_t pwm;
- /*aby prictene uhly mohla byt kulata cisla, musime index posunout*/
- pos+=38;
- /*use it as cyclic 32-bit logic*/
- pos*=4294967;
- if (duty>=0){ /*clockwise rotation*/
- /* 1st phase */
- sin = pxmc_sin_fixed_inline(pos+DEGREE_240,10); /*10+1 bity*/ /*-120*/
- pwm=sin*duty/1024;
- if (pwm<0) pwm=0;
- rps.pwm1=(uint16_t)pwm;
-
- /* 2nd phase */
- sin = pxmc_sin_fixed_inline(pos+DEGREE_120,10); /*10+1 bity*/ /*-240*/
- pwm=sin*duty/1024;
- if (pwm<0) pwm=0;
- rps.pwm2=(uint16_t)pwm;
-
- /* 3rd phase */
- sin = pxmc_sin_fixed_inline(pos,10); /*10+1 bity*/
- pwm=sin*duty/1024;
- if (pwm<0) pwm=0;
- rps.pwm3=(uint16_t)pwm;
- }else{
- duty=-duty;
-
- /* 1st phase */
- sin = pxmc_sin_fixed_inline(pos+DEGREE_60,10); /*10+1 bity*/ /*-300*/
- pwm=sin*duty/1024;
- if (pwm<0) pwm=0;
- rps.pwm1=(uint16_t)pwm;
-
- /* 2nd phase */
- sin = pxmc_sin_fixed_inline(pos+DEGREE_300,10); /*10+1 bity*/ /*-60-*/
- pwm=sin*duty/1024;
- if (pwm<0) pwm=0;
- rps.pwm2=(uint16_t)pwm;
-
- /* 3rd phase */
- sin = pxmc_sin_fixed_inline(pos+DEGREE_180,10); /*10+1 bity*/ /*-180*/
- pwm=sin*duty/1024;
- if (pwm<0) pwm=0;
- rps.pwm3=(uint16_t)pwm;
- }
- return 0;
-}
-/*
- * \brief
- * Test function to be placed in controll loop.
- * Switches PWM's at point where they produce same force.
- * This points are found thanks to IRC position,
- */
-inline
-void simple_ind_dist_commutator(int duty){
- if (duty>=0){ /* clockwise - so that position increase */
- /* pwm3 */
- if ((rps.index_dist>=45 && rps.index_dist<=373) ||
- (rps.index_dist>=1048 && rps.index_dist<=1377)){
- rps.pwm1=0;
- rps.pwm2=0;
- rps.pwm3=duty;
- /* pwm1 */
- }else if ((rps.index_dist>=373 && rps.index_dist<=711) ||
- (rps.index_dist>=1377 && rps.index_dist<=1711)){
- rps.pwm1=duty;
- rps.pwm2=0;
- rps.pwm3=0;
- /* pwm2 */
- }else if ((rps.index_dist>=0 && rps.index_dist<=45) ||
- (rps.index_dist>=711 && rps.index_dist<=1048) ||
- (rps.index_dist>=1711 && rps.index_dist<=1999)){
- rps.pwm1=0;
- rps.pwm2=duty;
- rps.pwm3=0;
- }
- }else{ /*counter-clockwise - position decrease */
- /* pwm3 */
- if ((rps.index_dist>=544 && rps.index_dist<=881) ||
- (rps.index_dist>=1544 && rps.index_dist<=1878)){
- rps.pwm1=0;
- rps.pwm2=0;
- rps.pwm3=-duty;
- /* pwm1 */
- }else if ((rps.index_dist>=0 && rps.index_dist<=211) ||
- (rps.index_dist>=881 && rps.index_dist<=1210) ||
- (rps.index_dist>=1878 && rps.index_dist<=1999)){
- rps.pwm1=-duty;
- rps.pwm2=0;
- rps.pwm3=0;
- /* pwm2 */
- }else if ((rps.index_dist>=211 && rps.index_dist<=544) ||
- (rps.index_dist>=1210 && rps.index_dist<=1544)){
- rps.pwm1=0;
- rps.pwm2=-duty;
- rps.pwm3=0;
- }
- }
-}
-/*
- * \brief
- * Test function to be placed in controll loop.
- * Switches PWM's at point where they produce same force
- */
-inline void simple_hall_commutator(int duty){
- if (duty>=0){ /* clockwise - so that position increase */
- /* pwm3 */
- if (data.hal2 && !data.hal3){
- rps.pwm1=0;
- rps.pwm2=0;
- rps.pwm3=duty;
- /* pwm1 */
- }else if (data.hal1 && !data.hal2){
- rps.pwm1=duty;
- rps.pwm2=0;
- rps.pwm3=0;
- /* pwm2 */
- }else if (!data.hal1 && data.hal3){
- rps.pwm1=0;
- rps.pwm2=duty;
- rps.pwm3=0;
- }
- }else{ /*counter-clockwise - position decrease */
- /* pwm3 */
- if (!data.hal2 && data.hal3){
- rps.pwm1=0;
- rps.pwm2=0;
- rps.pwm3=-duty;
- /* pwm1 */
- }else if (!data.hal1 && data.hal2){
- rps.pwm1=-duty;
- rps.pwm2=0;
- rps.pwm3=0;
- /* pwm2 */
- }else if (data.hal1 && !data.hal3){
- rps.pwm1=0;
- rps.pwm2=-duty;
- rps.pwm3=0;
- }
- }
-}
-/**
- * \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
- */
-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
- * Very simple PID regulator.
- * Now only with P-part so that the error doesnt go to zero.
- * TODO: add anti-wind up and I and D parts
- */
-inline void pid(){
- int duty_tmp;
- duty_tmp = PID_P*(rps.desired_pos - (int32_t)data.pozice);
- if (duty_tmp>MAX_DUTY){
- rps.duty=MAX_DUTY;
- }else if (duty_tmp<-MAX_DUTY){
- rps.duty=-MAX_DUTY;
- }else{
- rps.duty = duty_tmp;
- }
-}
/*
* \brief
* Feedback loop.
+ * TODO: replace bunch of 'IFs' with Object-like pattern
*/
-void * read_data(void* param){
+void * control_loop(void* param){
int i;
struct rpi_in pocatek;
+ struct rpi_state poc={
+ .spi_dat=&pocatek,
+ .test=0,
+ .pwm1=0, .pwm1=0, .pwm3=0
+ };
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*/
- pocatek = spi_read(tx);
+ spi_read(&poc); /*pocatecni informace*/
clock_gettime(CLOCK_MONOTONIC ,&t);
/* start after one second */
t.tv_sec++;
/* wait until next shot */
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
- prepare_tx(tx); /*save the data to send*/
- data = spi_read(tx); /*exchange data*/
- /*subtract initiate postion */
+
+ /*old positions*/
+ rps.old_pos[rps.tf_count%OLD_POS_NUM]=rps.spi_dat->pozice;
+ spi_read(&rps); /*exchange data*/
rps.tf_count++;
- substractOffset(&data,&pocatek);
- comIndDist();
+ substractOffset(&data,poc.spi_dat); /*subtract initiate postion */
+ compSpeed(&rps); /*spocita rychlost*/
+
if (!rps.index_ok){
if (first){
last_index=data.index_position;
first=0;
}else if (last_index!=data.index_position){
- rps.index_ok=1;
+ setIndexOK(&rps);
+ comIndDist(&rps); /*vypocet vzdalenosti indexu*/
}
+ }else{ /*index je v poradku*/
+ comIndDist(&rps); /*vypocet vzdalenosti indexu*/
}
- pid();
- if (rps.index_ok && rps.commutate){
- /*simple_ind_dist_commutator(rps.duty);*/
- sin_commutator(rps.duty);
- }else if(!rps.index_ok && rps.commutate){
- simple_hall_commutator(rps.duty);
+
+ /* pocitame sirku plneni podle potreb rizeni*/
+ rps.main_controller(&rps);
+
+ /* sirku plneni prepocteme na jednotlive pwm */
+ rps.main_commutator(&rps);
+
+ /*zalogujeme hodnoty*/
+ if (rps.doLogs && !(rps.tf_count%LOG_PERIOD)){
+ makeLog(&rps);
}
+
sem_post(&rps.thd_par_sem); /*--post semaphore---*/
/* calculate next shot */
}
}
-
-
/**
* \brief Main function.
*/
-
int main(){
pthread_t base_thread_id;
clk_init(); /* inicializace gpio hodin */
base_thread_id=pthread_self();
/*main control loop*/
- create_rt_task(&base_thread_id,PRIOR_HIGH,read_data,NULL);
+ create_rt_task(&base_thread_id,PRIOR_HIGH,control_loop,NULL);
/*monitor of current state*/
create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);