2014-05-19 84 views
0

我使用Arduino製作項目的相撲機器人。我沒有使用Arduino代碼的經驗,但我確實有Java經驗。這就是說這個機器人有一個邊緣檢測器來防止它脫落,一個L298驅動器,等等。 由於缺乏經驗,我不確切知道Arduino的代碼如何與方法等一起工作。這就是說,我的主要問題是它如何執行我的方法的延遲?它是否陷入了沒有回報的方法?不進入?執行結束後?目前,似乎只是在沒有打開或關閉開關的情況下向前運行電機。我也檢查了接線。 (我對可疑的問題 - 堆棧溢出處女道歉)。旋轉電機Arduinosumo機器人

int motor_forward = 10; 
int motor_reverse = 9; 
int motor2_forward = 13; 
int motor2_reverse = 12; 
int edgeDec1 = 7; 
//int edgeDec2 = 6; 

//the setup routine runs once when you press reset; 
void setup(){ 
//initialize the digital pin as an output. 
pinMode(motor_forward, OUTPUT); 
pinMode(motor_reverse,OUTPUT); 
pinMode(motor2_forward,OUTPUT); 
pinMode(motor2_reverse,OUTPUT); 
pinMode(edgeDec1,INPUT); 

} 
//the loop routine runs over and over again forever 
void loop(){ 
    int indicator = random(2); 

    delay(5000);//5 second delay 
    //loop to prevent another 5 second delay 
    while(true){ 
    if (indicator = 0){ 
     for(int x = 0; x < random(200); x++){ 
     Forward(); 
     EdgeDec(); 
     delay(10); 
     } 
    } 
    else if (indicator = 1){ 
     for(int x = 0; x < random(200); x++){ 
     TurnLeft(); 
     EdgeDec(); 
     delay(10); 
     } 
    } 
    else if (indicator = 2){ 
     for(int x = 0; x < random(200); x++){ 
     TurnRight(); 
     EdgeDec(); 
     delay(10); 
     } 
    } 

    } 

} 
void Forward(){ 
    //right motor 
    digitalWrite(motor_forward,1);//terminal d1 will be high 
    digitalWrite(motor_reverse,0);//terminal d2 will be low 

    //left motor 
    digitalWrite(motor2_forward,1);//terminal d1 will be high 
    digitalWrite(motor2_reverse,0);//terminal d2 will be low 

} 
//going in reverse 
void Reverse(){ 
    //right motor 
    digitalWrite(motor_forward,0);//terminal d1 will be low 
    digitalWrite(motor_reverse,1);//terminal d2 will be high 

    //left motor 
    digitalWrite(motor2_forward,0);//terminal d1 will be low 
    digitalWrite(motor2_reverse,1);//terminal d2 will be high 

} 
//rotating left 
void TurnLeft(){ 
    //right motor 
    digitalWrite(motor_forward,0);//terminal d1 will be high 
    digitalWrite(motor_reverse,1);//terminal d2 will be low 

    //left motor 
    digitalWrite(motor2_forward,1);//terminal d1 will be high 
    digitalWrite(motor2_reverse,0);//terminal d2 will be low 

} 
void TurnRight(){ 
    //right motor 
    digitalWrite(motor_forward,1);//terminal d1 will be high 
    digitalWrite(motor_reverse,0);//terminal d2 will be low 

    //left motor 
    digitalWrite(motor2_forward,0);//terminal d1 will be high 
    digitalWrite(motor2_reverse,1);//terminal d2 will be low 

} 
void EdgeDec(){ 
    if(edgeDec1 == 1){ 
    Reverse(); 
    delay(700); 
    TurnLeft(); 
    delay(1000); 
    Forward(); 

    } 
} 

回答

0

您的代碼中存在一些錯誤/誤解,所以我添加了一些簡單的調試,在您下載代碼後打開Tools> Serial Monitor即可工作。在您的EdgeDec()函數(函數= Java方法)中,您有if(EdgeDec = 1),但EdgeDec只是一個值爲7的整數。您想要讀取的編號爲7的引腳的值 - if(digitalRead (edgeDec1)== LOW)。由於您沒有將digitalRead引腳連接到LOW(使用外部電阻)或HIGH(使用Arduino的內部電阻),所以我將邏輯從HIGH(1)轉換爲LOW(0) - 閱讀我的評論在代碼中。不知道你在while(true)循環中做了什麼 - 可能是一些進一步的調試?無論如何,希望它可以幫助...

int motor_forward = 10; 
int motor_reverse = 9; 
int motor2_forward = 13; 
int motor2_reverse = 12; 
int edgeDec1 = 7; 
//int edgeDec2 = 6; 
int indicator; // pulled this out of your loop - askchipbug 

String repeatstring; //debugging variable, stops a debug string from repeating in Serial monitor when watching a loop - askchipbug 

//the setup routine runs once when you press reset; 
void setup(){ 
    Serial.begin(9600);   // set up Serial library at 9600 bps, using SerialMonitor to debug - askchipbug 
    //initialize the digital pin as an output. 
    pinMode(motor_forward, OUTPUT); 
    pinMode(motor_reverse,OUTPUT); 
    pinMode(motor2_forward,OUTPUT); 
    pinMode(motor2_reverse,OUTPUT); 
    pinMode(edgeDec1,INPUT_PULLUP); // set this high so it doesn't float about - askchipbug 
    // you can use the internal 20k pullups with INPUT_PULLUP which means 1 = off, 0 = on 
    // or you have to use external pulldown resistors to have 1 = on, 0 = off 
    pr("setup completed"); // askchipbug 

} 
//the loop routine runs over and over again forever 
void loop(){ 
    randomSeed(analogRead(0)); // seeds the random() function with a different number each time the loop runs - askchipbug 
    indicator = random(2); 
    pr("indicator: " + String(indicator)); // askchipbug 
    pr("5 second delay"); 
    delay(5000);//5 second delay 
    //loop to prevent another 5 second delay 
    while(true){ 
    if (indicator == 0){ //you had this set as indicator = 0 - askchipbug 
     pr("indicator: " + String(indicator)); // askchipbug 
     for(int x = 0; x < random(200); x++){ 
     Forward(); 
     EdgeDec(); 
     delay(10); 
     } 
    } 
    else if (indicator = 1){ 
     pr("indicator: " + String(indicator)); //askchipbug 
     for(int x = 0; x < random(200); x++){ 
     TurnLeft(); 
     EdgeDec(); 
     delay(10); 
     } 
    } 
    else if (indicator = 2){ 
     pr("indicator: " + String(indicator));// askchipbug 
     for(int x = 0; x < random(200); x++){ 
     TurnRight(); 
     EdgeDec(); 
     delay(10); 
     } 
    } 
    } 

} 
void Forward(){ 
    //right motor 
    pr("forward"); //askchipbug 
    digitalWrite(motor_forward,1);//terminal d1 will be high 
    digitalWrite(motor_reverse,0);//terminal d2 will be low 

    //left motor 
    digitalWrite(motor2_forward,1);//terminal d1 will be high 
    digitalWrite(motor2_reverse,0);//terminal d2 will be low 
} 
//going in reverse 
void Reverse(){ 
    pr("reverse"); //askchipbug 
    //right motor 
    digitalWrite(motor_forward,0);//terminal d1 will be low 
    digitalWrite(motor_reverse,1);//terminal d2 will be high 

    //left motor 
    digitalWrite(motor2_forward,0);//terminal d1 will be low 
    digitalWrite(motor2_reverse,1);//terminal d2 will be high 
} 
//rotating left 
void TurnLeft(){ 
    pr("turn left"); //askchipbug 
    //right motor 
    digitalWrite(motor_forward,0);//terminal d1 will be high 
    digitalWrite(motor_reverse,1);//terminal d2 will be low 

    //left motor 
    digitalWrite(motor2_forward,1);//terminal d1 will be high 
    digitalWrite(motor2_reverse,0);//terminal d2 will be low 
} 
void TurnRight(){ 
    pr("turn right"); //askchipbug 
    //right motor 
    digitalWrite(motor_forward,1);//terminal d1 will be high 
    digitalWrite(motor_reverse,0);//terminal d2 will be low 

    //left motor 
    digitalWrite(motor2_forward,0);//terminal d1 will be high 
    digitalWrite(motor2_reverse,1);//terminal d2 will be low 
} 
void EdgeDec(){ 
    pr("edge detector: " + String(digitalRead(edgeDec1))); // read pin 7 - askchipbug 
    if(digitalRead(edgeDec1) == LOW){ // remember we're using the internal pullups so LOW = on - askchipbug 
    pr("edge detected!"); // askchipbug 
    Reverse(); 
    delay(700); 
    TurnLeft(); 
    delay(1000); 
    Forward(); 
    } 
} 

//simple debug technique - use pr("something"); in your code - askchipbug 
void pr(String txt){ 
    if(repeatstring != txt){ 
    //if the debug text is different, print it 
    Serial.println(txt); //prints the text and adds a newline 
    Serial.flush(); //waits for all the data to be printed 
    delay(1000); //just pauses the scrolling text for 1 second, make bigger if you want a longer pause 
    repeatstring = txt; 
    } 
} 
+0

我認爲這是把我在正確的軌道,謝謝。調試文本也確實有助於找出問題。我想到如何測試它,但不確定。 – user3651225

+0

不客氣,讓我們張貼並標記爲正確的,如果它適合你!我不想幹涉這個(真實的)一點,因爲我認爲你在那裏做其他事情。 – askchipbug

0

在阿爾杜伊諾的delay方法一般來說是這樣實現的只是CPU進行足夠的週期,以彌補所需要的延遲時間上執行的若干nop第它本質上是一個阻塞的調用,它只是完全耗盡了CPU足夠的時間,由傳遞給delay的參數指定。

例如,假設您的處理器在100hz運行,那麼基本上延遲1秒將涉及生成處理器將執行的100條指令。當然,這樣的指令假設需要1個時鐘週期。鑑於Arduino Uno使用ATMEGA328p微處理器,您需要查看該數據表以瞭解更多信息。