带旋转马达的Arduino Sumo bot

时间:2014-05-19 04:31:44

标签: c++ arduino arduino-uno

我正在使用Arduino为项目制作一个相扑机器人。我没有使用Arduino代码的经验,但我确实有java经验。也就是说这个机器人有一个边缘探测器来防止它掉落,一个L298驱动器,等等。      由于我缺乏经验,我不知道Arduino的代码究竟是如何与方法等一起工作的。那就是说,我的主要问题是如何用延迟执行我的方法?是否陷入无法返回的方法?没进?执行会在以后结束吗?目前,它似乎只是在没有开关打开或关闭的情况下向前运行电机。我还检查了接线。 (我为糟糕的质疑道歉 - stackoverflow virgin)。

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();

    }
}

2 个答案:

答案 0 :(得分:0)

一般来说,Arduino中的delay方法实现为只在CPU上执行多个nop s足够的周期以弥补所需的延迟时间。它本质上是一个阻塞调用,因为它只是完全耗尽了CPU,因为传递给delay的参数指定了足够的时间。

例如,假设您的处理器以100hz运行,那么基本上延迟1秒将涉及生成处理器将执行的100条nop指令。当然,这样的指令恰好需要1个时钟周期。鉴于Arduino Uno运行ATMEGA328p微处理器,您可以查看该数据表以了解更多信息。

答案 1 :(得分:0)

您的代码中存在一些错误/误解,因此我在您下载代码后打开工具&gt;串行监视器后添加了一些简单的调试。在你的EdgeDec()函数(function = Java方法)中你有if(EdgeDec = 1),但是EdgeDec只是一个值为7的int。你想要的是读取编号为7的引脚的值 - if(digitalRead) (edgeDec1)== LOW)。

另外,我将你的逻辑从HIGH(1)反转为LOW(0),因为你没有将digitalRead引脚连接到LOW(使用外部电阻)或HIGH(使用Arduino的内部电阻) ) - 在代码中阅读我的评论。不知道你用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;
  }
}