繁体   English   中英

Arduino步进电机如何前后移动?

[英]How to move forward and backward with the Arduino Stepper motor?

我想为具有三个按钮和一个步进电机的系统创建一个 Arduino 程序。 如果按下按钮 1,步进器应为 go,例如向前 50 步。 如果按下按钮 2,步进器应向后 go 50 步。 如果按下按钮 3,则步进器应该 go 在向后 50 步之后向前 50 步。

我使用了 Arduino 的步进器库并编写了以下代码。 函数Forward()Backward()Continuous()实现要为每个按钮执行的操作。 每个 function 一步步移动电机,并将动作记录在序列号 output 上。

但我无法达到我想要的结果:步进器不会向后 go 而是只会向前。 更确切地说:

  • Forward()按预期工作
  • Backward()产生预期的日志记录 output(计算步数最多为 50),但电机仅向前移动而不是向后移动。
  • Continuous() function 也不工作:在向前 50 步(移动并记录步数)后,它继续向前移动,只为计数器记录 1。

我需要你的帮助。 如何让电机在Backward()中倒退? 以及如何纠正Continuous()以实现前进和后退,并产生正确的后退步数?

这是我的代码:

#include <Stepper.h>

int forward_button = 2;
int backward_button = 3;
int cont_button = 4;

int button_cond1;
int button_cond2;
int button_cond3;

int del = 50;

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0;         // number of steps the motor has taken
int steps;

void Forward() { // should go forward by 50 steps
  int stepCount = 0; 
  while (stepCount < 50) {
    steps = 1;
    myStepper.step(steps);
    stepCount++;
    delay(del);
    Serial.print("Forward steps :");
    Serial.println(stepCount);
  }
}

void Backward() { // should go backward by 50 steps
   int stepCount = 0;
   while (stepCount < 50) {
    steps = 1;
    myStepper.step(steps);
    stepCount++;
    delay(del);
    Serial.print("Backward steps :");
    Serial.println(stepCount);
  }
}

void Continuous() {  // should go forward by 50 steps, then backwards
  int stepCount = 0; 
  while (stepCount < 50) {
    steps = 1;
    myStepper.step(steps);
    stepCount++;
    delay(del);
    Serial.print("Continuous steps :");
    Serial.println(stepCount);
  }
  while (50 < stepCount <= 200) {
    int stepCount = 0;
    steps = 1;
    myStepper.step(steps);
    stepCount++;
    delay(del);
    Serial.print("Continuous steps :");
    Serial.println(stepCount);
  }
}

void setup() {
  // initialize the serial port:
  Serial.begin(9600);

  pinMode(forward_button, INPUT_PULLUP);
  pinMode(backward_button, INPUT_PULLUP);
  pinMode(cont_button, INPUT_PULLUP);

  //myStepper.setSpeed(60);
}

void loop() {
  // step one step:

  button_cond1 = digitalRead(forward_button);
  button_cond2 = digitalRead(backward_button);
  button_cond3 = digitalRead(cont_button);

  if ((button_cond1 == LOW) && (button_cond2 == HIGH) && (button_cond3 == HIGH))  {
    Forward();
  }
  else if ((button_cond1 == HIGH) && (button_cond2 == LOW) && (button_cond3 == HIGH))  {
    Backward();
  }
  else if ((button_cond1 == HIGH) && (button_cond2 == HIGH) && (button_cond3 == LOW))  {
    Continuous();
  } 
}

步进器的方向问题:

根据文档, myStepper.step()转动电机:

以最近调用 setSpeed() 确定的速度转动电机特定的步数。

更重要的是,为了双向移动,它使用了一个带符号的参数:

转动电机的步数 -正向转动一个方向,向转动另一个方向(int)

在您的代码中, Backward()Forward()之间没有区别:您在这两种情况下都提供正值。 所以我建议将Backwards()更改为使用负数:

myStepper.step(-steps); 

Continuous()的逻辑问题

还需要在您的Continuous() function 的第二个循环中更正方向,您希望 go 向后。
但首先,您需要更正此循环的条件:

while (50 < stepCount <= 200) {  //OUCH compares a boolean and an integer

C++ 并没有像您想象的那样链接比较运算符。 因此,将其分解为两个不同的比较,并用逻辑分组:

while (50 < stepCount && stepCount <= 200) { 

不幸的是,在第一个循环结束时, stepCount正好是50 并且 50 并不严格小于( < ) 小于 50。您可以改用更小或等于( <= ),但由于循环不会递减计数器,您可以将条件简化为:

while (stepCount <= 200) { // we know that it will stay 50 or above

您还需要删除此循环块中stepCount的重新定义,因为它创建了一个隐藏初始定义的新局部变量(即一个名称,两个变量,这非常混乱)。

如果还是不行

经过长时间的意见交流,这里有一些建议(解决问题的建议以粗体显示):

  • 确保最新版本的代码在 arduino 上运行并且没有编译错误或警告。
  • 确保不同的按钮执行预期的 function。
  • 如果电机运动不符合预期,请确保您的Stepper器 object 配置了与电机兼容的每转步数。
  • 如果运动不稳定,或者如果正步和负步之间没有区别(根据文档应该朝相反的方向移动),则交叉检查Stepper的构造函数是否以正确的顺序获取引脚号:这是很重要,因为Stepper会按照给定的顺序向不同的引脚发送一系列信号,以实现正确的运动。

如果它仍然不起作用,则要么是硬件问题,要么是与库不兼容(例如,参见此处,作者通过提供另一个信号序列解决了问题,这不是最简单的方法)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM