#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"
+#include "controllers.h"
+#include "commutators.h"
+
+
-#define PRUM_PROUD 2061
-#define PRUM_SOUC 6183
-#define MAX_DUTY 128
-#define PID_P 0.1
#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
};
/**
termClock(0);
}
-/**
- * \brief Signal handler pro Ctrl+C
- */
-void appl_stop(){
- spi_disable();
- clk_disable();
- /*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
+ * \brief
+ * Free logs
*/
-float diff_p(float value){
- return ((float)value-PRUM_PROUD)*100/PRUM_PROUD;
+void freeLogs(){
+ int r;
+ if (rps.log_col_count){
+ for (r=0;r<LOG_ROWS;r++){
+ free(rps.logs[r]);
+ }
+ }
+ rps.log_col_count=0;
+ rps.doLogs=0;
}
+
/*
- * pocita procentualni odchylku od prumerneho souctu proudu
+ * \brief
+ * Makes log.
*/
-float diff_s(float value){
- return ((float)value-PRUM_SOUC)*100/PRUM_SOUC;
+void makeLog(){
+ int r;
+ if (rps.log_col==MAX_LOGS-1){
+ rps.doLogs=0;
+ return;
+ }
+ rps.logs[0][rps.log_col]=(int)rps.tf_count;
+ rps.logs[1][rps.log_col]=(int)rps.spi_dat->pozice;
+
+ rps.logs[2][rps.log_col]=(int)rps.pwm1;
+ rps.logs[3][rps.log_col]=(int)rps.pwm2;
+ rps.logs[4][rps.log_col]=(int)rps.pwm3;
+ rps.logs[5][rps.log_col]=rps.duty;
+
+ rps.logs[6][rps.log_col]=rps.desired_spd;
+ rps.logs[7][rps.log_col]=rps.speed;
+
+ rps.logs[8][rps.log_col]=(int)(rps.spi_dat->ch1/rps.spi_dat->adc_m_count);
+ rps.logs[9][rps.log_col]=(int)(rps.spi_dat->ch2/rps.spi_dat->adc_m_count);
+ rps.logs[10][rps.log_col]=(int)(rps.spi_dat->ch0/rps.spi_dat->adc_m_count);
+
+ rps.log_col++;
+ /*
+ if (rps.log_col==rps.log_col_count-1){
+ rps.log_col_count*=2;
+ rps.log_col_count%=MAX_LOGS;
+ for (r=0;r<LOG_ROWS;r++){
+ rps.logs[r]=realloc(rps.logs[r],rps.log_col_count*sizeof(int));
+ if (rps.logs[r]==NULL){
+ rps.doLogs=0;
+ rps.error=1;
+ }
+ }
+ }
+ */
}
+/*
+ * \brief
+ * Pripravi psi buffer
+ */
void prepare_tx(uint8_t * tx){
/*Data format:
}
+
+
+/**
+ * \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;
+ prepare_tx(tx); /*save the data to send*/
+ data=spi_read(tx);
+
+ spi_disable();
+ clk_disable();
+ freeLogs();
+ /*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=data->pozice_raw-offset->pozice_raw;
+ return;
+}
+
+
+
/**
* Funkce pravidelne vypisuje posledni zjistenou pozici lokalniho motoru
*/
}
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.
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
+ * Computate speed.
*/
-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;
- }
+void compSpeed(){
+ signed long int spd;
+ spd=rps.spi_dat->pozice-rps.old_pos[rps.tf_count%OLD_POS_NUM];
+ rps.speed=(int32_t)spd;
}
+
/*
* \brief
* Feedback loop.
+ * TODO: replace bunch of 'IFs' with Object-like pattern
*/
void * read_data(void* param){
int i;
/* wait until next shot */
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
sem_wait(&rps.thd_par_sem); /*---take semaphore---*/
+
+ /*old positions*/
+ rps.old_pos[rps.tf_count%OLD_POS_NUM]=rps.spi_dat->pozice;
prepare_tx(tx); /*save the data to send*/
data = spi_read(tx); /*exchange data*/
/*subtract initiate postion */
rps.tf_count++;
substractOffset(&data,&pocatek);
- comIndDist();
+ compSpeed(); /*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;
+ comIndDist(); /*vypocet vzdalenosti indexu*/
}
+ }else{ /*index je v poradku*/
+ comIndDist(); /*vypocet vzdalenosti indexu*/
}
- pid();
+
+ /* pocitame sirku plneni podle potreb rizeni*/
+ if (rps.pos_reg_ena){ /*pozicni rizeni*/
+ pos_pid(&rps);
+ }else if(rps.spd_reg_ena){ /*rizeni na rychlost*/
+ spd_pid(&rps);
+ }
+
+ /* sirka plneni prepoctena na jednotlive pwm */
if (rps.index_ok && rps.commutate){
/*simple_ind_dist_commutator(rps.duty);*/
- sin_commutator(rps.duty);
+ /*sin_commutator(rps.duty);*/
+ inv_trans_comm(&rps);
+ inv_trans_comm_2(&rps);
}else if(!rps.index_ok && rps.commutate){
- simple_hall_commutator(rps.duty);
+ simple_hall_commutator(&rps);
}
+
+ /*zalogujeme hodnoty*/
+ if (rps.doLogs && !(rps.tf_count%LOG_PERIOD)){
+ makeLog();
+ }
+
sem_post(&rps.thd_par_sem); /*--post semaphore---*/
/* calculate next shot */
}
-
/**
* \brief Main function.
*/