【问题标题】:Moving 2 steppers with an adafruit motor shield使用 adafruit 电机护罩移动 2 个步进器
【发布时间】:2013-07-25 18:35:34
【问题描述】:

我正在制作一个项目,我使用串口从我的电脑向 arduino 发送字符串,arduino 读取它们并根据字符串移动一个或另一个电机,这是代码:

#include <AccelStepper.h>
#include <AFMotor.h>


AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);

String steps= "";

void forwardstep1() { 
  motor1.onestep(FORWARD, SINGLE);
}
void backwardstep1() { 
  motor1.onestep(BACKWARD, SINGLE);
}
void forwardstep2() { 
  motor2.onestep(FORWARD, SINGLE);
}
void backwardstep2() { 
  motor2.onestep(BACKWARD, SINGLE);
}

AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

void setup() {

  Serial.begin(9600);
  Serial.println("Stepper program!");
  stepper1.setMaxSpeed(200.0);
  stepper2.setMaxSpeed(200.0);
  stepper1.setAcceleration(100.0);
  stepper2.setAcceleration(100.0);

}

void loop() {

}

void serialEvent() {

  while(Serial.available()) {
    int inChar = Serial.read();
    Serial.print("Echo:");
    Serial.write(inChar);
    if (isDigit(inChar)) {
      steps += (char)inChar;
    }
    if (inChar == 'u') {
      Serial.print("Up:" + steps);
      Serial.println();
      long POSITION = stepper1.currentPosition() + steps.toInt();
      stepper1.runToNewPosition(POSITION);
      steps = "";
      Serial.println();
    }
   if (inChar == 'U') {
      Serial.print("Up:" + steps);
      Serial.println();
      long POSITION = stepper2.currentPosition() + steps.toInt();
      stepper2.runToNewPosition(POSITION);
      steps = "";
      Serial.println();
    }
  }
}   

我遇到的问题是当我移动 stepper1 然后尝试移动 stepper2 时它不再移动,除非我重置 arduino。

谢谢!

【问题讨论】:

    标签: arduino


    【解决方案1】:

    我认为您需要逐步调试它。首先检查您是否能够独立控制每个电机的位置。您可以编写一个简单的函数来检查这一点。只需以固定步长交替移动电机即可。如果两个电机都工作正常,那么您可以继续调试串行通信。但是,如果电机不能正常工作,请检查连接等(硬件调试)。还要检查步进电机的初始化。

    如果这工作正常,那么您可以检查您是否能够在 arduino 上从您的电脑收到正确的响应。每当您收到新角色时,可能会点亮不同的 LED。通过这种方式,您可以确认您的电机和串行通信都可以正常工作。

    此外,由于您从串行接收电机的位置。我建议检查您是否正确接收该职位。可能您应该先尝试在程序中硬编码固定步骤,然后在收到“u”和“U”时查看它是否有效。

    void serialEvent() {
       static int fixed_step_1;
       static int fixed_step_2;
      while(Serial.available()) {
        int inChar = Serial.read();
        Serial.print("Echo:");
        Serial.write(inChar);
    
        if (inChar == 'u') {
          Serial.print("Up:");
          long POSITION = stepper1.currentPosition() + fixed_steps_1;
          stepper1.runToNewPosition(POSITION);
          fixed_steps_1++;
          Serial.println();
        }
       if (inChar == 'U') {
          long POSITION = stepper2.currentPosition() + fixed_steps_2;
          stepper2.runToNewPosition(POSITION);
          fixed_steps_2++;
          Serial.println();
        }
      }
    }   
    

    如果上述工作正常,那么您知道这是可以转换的情况。尽管所有这些步骤都需要时间,但您可以准确指出问题所在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-23
      • 2020-03-16
      • 2021-06-11
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多