2016-05-23 110 views
-4

我正在使用linux並寫入了一個.c文件。我想使用void類型的一些函數來執行電機的運動。我的目標是向前移動2秒,然後向右轉2秒等等。我該如何實現它?函數MOVE_FORW,MOVE_RIGHT等都是無效的。在特定時間運行void函數

該函數的返回類型爲void。我的問題是我如何讓系統執行我想要的實時任務2秒。我應該使用什麼樣的延遲?

這裏是代碼: 我想通過電機控制兩個車輪。名字是象徵性的:

/* 
DEFINITIONS 

FLEN FILE LEFT ENABLE GPIO177 
FREN FILE RIGHT ENABLE GPIO168 
FLC FILE LEFT C GPIO166 
FRC FILE RIGHT C GPIO169 
FLD FILE LEFT D GPIO167 
FRD FILE RIGHT D GPIO170 

LEH LEFT ENABLE HIGH 
LEL LEFT ENABLE LOW 
LCH LEFT C HIGH 
LCL LEFT C LOW 
LDH LEFT D HIGH 
LDL LEFT D LOW 

REH RIGHT ENABLE HIGH 
REL RIGHT ENABLE LOW 
RCH RIGHT C HIGH 
RCL RIGHT C LOW 
RDH RIGHT D HIGH 
RDL RIGHT D LOW 
*/ 

FILE *FLEN,*FREN,*FLC,*FRC,*FLD,*FRD; 

FILE *DIR_FLEN,*DIR_FREN,*DIR_FLC,*DIR_FRC,*DIR_FLD,*DIR_FRD; 

//LEFT WHEEL 
#define LEH() fprintf(FLEN,"1") 
#define LEL() fprintf(FLEN,"0") 
#define LCH() fprintf(FLC,"1") 
#define LCL() fprintf(FLC,"0") 
#define LDH() fprintf(FLD,"1") 
#define LDL() fprintf(FLD,"0") 

//RIGHT WHEEL 
#define REH() fprintf(FREN,"1") 
#define REL() fprintf(FREN,"0") 
#define RCH() fprintf(FRC,"1") 
#define RCL() fprintf(FRC,"0") 
#define RDH() fprintf(FRD,"1") 
#define RDL() fprintf(FRD,"0") 


void MOVE_FORW(){ 
LEH(); 
LCH(); 
LDL(); 
REH(); 
RCH(); 
RDL(); 
} 



void MOVE_BCKW(){ 
LEH(); 
LCL(); 
LDH(); 
REH(); 
RCL(); 
RDH(); 
} 

void MOVE_RIGHT(){ 
LEH(); 
LCH(); 
LDL(); 
REH(); 
RCL(); 
RDH(); 
} 


void MOVE_LEFT(){ 
LEH(); 
LCL(); 
LDH(); 
REH(); 
RCH(); 
RDL(); 
} 



int main(void){ 

//HANDLING FILES - opening files fow writing (overwriting) 

//EXPORTING AS OUTPUT - DEFINING DIRECTION (out) 

system("echo 177 > /sys/class/gpio/export"); 
printf("FINE\n"); 

DIR_FLEN=fopen("/sys/class/gpio/gpio177/direction","w+"); //GPIO 177 
fprintf(DIR_FLEN,"out"); 
FLEN=fopen("/sys/class/gpio/gpio177/value","w+"); //GPIO 177 



system("echo 168 > /sys/class/gpio/export"); 
DIR_FREN=fopen("/sys/class/gpio/gpio168/direction","w+"); //GPIO 168 
fprintf(DIR_FREN,"out"); 
FREN=fopen("/sys/class/gpio/gpio168/value","w+"); //GPIO 168 


system("echo 166 > /sys/class/gpio/export"); 
DIR_FLC=fopen("/sys/class/gpio/gpio166/direction","w+"); 
fprintf(DIR_FLC,"out"); 
FLC=fopen("/sys/class/gpio/gpio166/value","w+"); 


system("echo 169 > /sys/class/gpio/export"); 
DIR_FRC=fopen("/sys/class/gpio/gpio169/direction","w+"); 
fprintf(DIR_FRC,"out"); 
FRC=fopen("/sys/class/gpio/gpio169/value","w+"); 


system("echo 167 > /sys/class/gpio/export"); 
DIR_FLD=fopen("/sys/class/gpio/gpio167/direction","w+"); 
fprintf(DIR_FLD,"out"); 
FLD=fopen("/sys/class/gpio/gpio167/value","w+"); 


system("echo 170> /sys/class/gpio/export"); 
DIR_FRD=fopen("/sys/class/gpio/gpio170/direction","w+"); 
fprintf(DIR_FRD,"out"); 
FRD=fopen("/sys/class/gpio/gpio170/value","w+"); 


MOVE_FORW(); 
+1

_請澄清你的具體問題或添加額外的細節,以確切地突出你所需要的。正如目前所寫,很難確切地說出你在問什麼。請參閱如何問問頁面以獲取幫助,以澄清此問題._ –

+1

您不清楚您要問什麼。通過「void類型的函數」,你的意思是函數的返回類型是「void」還是採用'void'參數?無論哪種情況,爲什麼「空白」是一個問題?只需調用它。請澄清你的問題。如果你展示一些具體代碼以使其更清晰,那麼最好。 – kaylum

+0

在你的問題中有一個巨大的空白 - 代碼應該是的空間:( –

回答

1

我解決了它簡單地說:

void MOVE_FORW(){ 
LEH(); 
LCH(); 
LDL(); 
REH(); 
RCH(); 
RDL(); 
system("sleep 3"); 
} 



void MOVE_BCKW(){ 
LEH(); 
LCL(); 
LDH(); 
REH(); 
RCL(); 
RDH(); 
system("sleep 3"); 
} 

void MOVE_RIGHT(){ 
LEH(); 
LCH(); 
LDL(); 
REH(); 
RCL(); 
RDH(); 
system("sleep 3"); 
} 


void MOVE_LEFT(){ 
LEH(); 
LCL(); 
LDH(); 
REH(); 
RCH(); 
RDL(); 
system("sleep 3"); 
} 

感謝您的嘗試和思考什麼辦法!