簡體   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