自動滑軌項目_參考案例1

對於遇到的問題,進行了查閱,看到一些參考案例進行分析,看看能不能解決自己的問題。

參考連接地址:https://www.brainy-bits.com/dual-axis-camera-slider-using-joystick-in-and-out/

 

OVERVIEW 概述

In our prior tutorial we saw how to control 2 stepper motor simultaneously using the Arduino Serial Monitor.

在之前的教程中,我們瞭解瞭如何使用Arduino串行監控器同時控制2個步進電機。

Continuing from this, we will now add an Analog Joystick to our project to set IN and OUT points for the Steppers to travel to.

在此基礎上,我們現在將添加一個模擬操縱桿到我們的項目中,以設置進和出位置,讓步進電機在其間運行。

We will also be seeing a new way to move both steppers simultaneously and make sure that they reach their destination at the same time.

我們還將看到一種新方法,可以同時移動兩個步進電機,並確保它們同時到達目的地。

Again we will be using the Serial Monitor to input some values like the Speed we want the Steppers to travel but we will also be using the Joystick integrated switch to set the IN and OUT points.

同樣,我們將使用串行監視器來輸入一些值,比如我們希望步進器移動的速度,但我們也將使用操縱桿集成開關來設置輸入和輸出點。

CONNECTIONS 連線

 

 

 

The connection for the Easy Driver boards are almost the same as our prior tutorial:

簡單驅動板的連接幾乎和我們之前的教程一樣:

To power the Stepper Motors connected to the Easy Driver boards we are using a 12V 1A power supply.

爲了給連接到簡易驅動板的步進電機供電,我們使用了12V 1A電源。

The GND and 5V (out) of the Power Supply are connected to the breadboard power rails.

電源的GND和5V (out)連接到麪包板電源線上。
GND and 5V (in) of the Easy Driver boards are connected to the breadboard power rails.

Easy Driver驅動板的GND和5V (in)連接到麪包板電源線上。

Each pair of winding of the Stepper motors are connected to the A-B pins of the Easy Drivers.

步進電機的每一對繞組都連接在Easy Driver驅動板的A-B引腳上。

 

THE CODE

The code we are using is different from our prior tutorial, but we are using some parts of it to read data from the Serial Monitor.

我們使用的代碼與之前的教程不同,但是我們使用其中的一些部分來從串行監視器讀取數據。

We are still using the AccelStepper class of the library to move the Steppers using the Joystick X and Y axis.

我們仍然使用AccelStepper庫來使用操縱桿X和Y軸移動步進電機。

But we are now using the MultiStepper class of the same library to move both Steppers once we have set the IN and OUT points using the Joystick.

但是我們現在使用同一個庫的MultiStepper類來移動兩個Steppers,一旦我們使用操縱桿設置了輸入和輸出點。

The MultiStepper class not only provide us with a way to move both Steppers at once, but also calculates the speed needed to move each Steppers so they end up at their destination (IN or OUT point) at the same time.

MultiStepper類不僅爲我們提供了同時移動兩個步進器的方法,而且還計算了移動每個步進器所需的速度,從而使它們同時到達目的地(輸入或輸出點)。

We use the Serial Monitor window to provide us with information like what the sketch is expecting at a particular point as well as give us the ability to enter information like the desired Speed and what move we want to execute.

我們使用串行監視器窗口來爲我們提供信息,如在特定點上草繪所期望的內容,併爲我們提供輸入信息的能力,如所需的速度和我們想要執行的移動。

As always for more information about the tutorial and explanation of the code please watch our tutorial video.

如往常一樣,更多關於教程的信息和代碼的解釋,請觀看我們的教程視頻。

/* Arduino Multiple Stepper Control Using Joystick switch
 
Created by Yvan / https://Brainy-Bits.com

This code is in the public domain...

You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/
#include "AccelStepper.h" 
#include "MultiStepper.h" 

// AccelStepper Setup
AccelStepper stepperX(1, 3, 4);   // 1 = Easy Driver interface
                                  // UNO Pin 3 connected to STEP pin of Easy Driver
                                  // UNO Pin 4 connected to DIR pin of Easy Driver
                                  
AccelStepper stepperZ(1, 6, 7);   // 1 = Easy Driver interface
                                  // UNO Pin 6 connected to STEP pin of Easy Driver
                                  // UNO Pin 7 connected to DIR pin of Easy Driver

MultiStepper StepperControl;  // Create instance of MultiStepper
                                  
// Analog Joystick setup
#define JoyX A0  // Joystick X pin connected to A0 on the UNO
#define JoyY A1  // Joystick Y pin connected to A1 on the UNO
#define JoySwitch 2 // Joystick switch connected to interrupt Pin 2 on UNO

long joystepX=0; // Used to move steppers when using the Joystick
long joystepY=0; // Used to move steppers when using the Joystick

// Variables used to store the IN and OUT points
volatile long XInPoint=0;
volatile long ZInPoint=0;
volatile long XOutPoint=0;
volatile long ZOutPoint=0;

int InandOut=0;  // Used to detect if IN and OUT points have been set

long gotoposition[2];  // Used to move steppers using MultiStepper Control



void joyswitchclick ()  {  //  Interrupt runs when clicking the Joystick Switch

  static unsigned long last_call_interrupt = 0;  // Used to debounce to Joystick Switch
  unsigned long interrupt_length = millis();
  
  if (interrupt_length - last_call_interrupt > 300) {  // Check if enough time has passed since clicking switch
    
    switch (InandOut) {  // Keep track of number of times the switch has been clicked
      case 0:
        InandOut=1;
        XInPoint=stepperX.currentPosition();  //  Set the IN points for both steppers
        ZInPoint=stepperZ.currentPosition();
        Serial.print("IN Points set at:  ");
        Serial.print(XInPoint);
        Serial.print(" , ");
        Serial.println(ZInPoint);
        Serial.println("");
        Serial.println("Use Joystick to set OUT Points: ");
        break;

      case 1:
        InandOut=2;
        XOutPoint=stepperX.currentPosition();  //  Set the OUT Points for both steppers
        ZOutPoint=stepperZ.currentPosition();
        Serial.print("OUT Points set at:  ");
        Serial.print(XOutPoint);
        Serial.print(" , ");
        Serial.println(ZOutPoint);
        Serial.println("");
        Serial.println("Enter Stepper Travel Speed: ");
        Serial.println("OR");
        Serial.println("Press Joystick Switch again to Reset IN and OUT Points");
        break;

      case 2:
        Serial.println("");
        Serial.println("IN and OUT Points have been Reset...");
        Serial.println("");
        Serial.println("");
        Serial.println("Set IN points for Steppers using the Joystick ");
        InandOut=0;
        break;

       case 3:
        Serial.println("");
        Serial.println("IN and OUT Points have been Reset...");
        Serial.println("");
        Serial.println("");
        Serial.println("Set IN points for Steppers using the Joystick ");
        InandOut=0;  
        joystepY=stepperZ.currentPosition();  // Reset Positions
        joystepX=stepperX.currentPosition();
        stepperX.setMaxSpeed(350.0);      // Reset Max Speed of X axis
        break;
    }
  }
  last_call_interrupt = interrupt_length;
} 


void setup() {
  
  Serial.begin(9600);  // Start the Serial monitor with speed of 9600 Bauds
  
// Print out Instructions on the Serial Monitor at Start
  Serial.println("Set IN points for Steppers using the Joystick ");

  pinMode(JoySwitch, INPUT_PULLUP);

// Set Max Speed and Acceleration of each Steppers
  stepperX.setMaxSpeed(350.0);      // Set Max Speed of X axis
  stepperX.setAcceleration(5000.0);  // Acceleration of X axis
  
  stepperZ.setMaxSpeed(150.0);      // Set Max Speed of Z axis slower for rotation
  stepperZ.setAcceleration(1000.0);  // Acceleration of Y axis

// Create instances for MultiStepper
  StepperControl.addStepper(stepperX);  // Add Stepper #1 to MultiStepper Control
  StepperControl.addStepper(stepperZ);  // Add Stepper #2 to MultiStepper Control

  attachInterrupt (0,joyswitchclick,FALLING); // interrupt 0 always connected to pin 2 

}


void loop() {

  while ((InandOut != 2) && (InandOut !=3)) {  // Enable moving of steppers using the Joystick

    if (analogRead(JoyX) < 200) {         
        joystepX=stepperX.currentPosition();         
        joystepX=joystepX-20;     }     

    if (analogRead(JoyX) > 900) {
        joystepX=stepperX.currentPosition();
        joystepX=joystepX+20;
    }

    if (analogRead(JoyY) < 200) {         
        joystepY=stepperZ.currentPosition();         
        joystepY=joystepY-20;     }          

    if (analogRead(JoyY) > 900) {
        joystepY=stepperZ.currentPosition();
        joystepY=joystepY+20;
    }

    stepperX.moveTo(joystepX);
    stepperZ.moveTo(joystepY);

    while ((stepperX.distanceToGo() !=0) || (stepperZ.distanceToGo() !=0)) {
      stepperX.runSpeedToPosition();
      stepperZ.runSpeedToPosition();
    }
  }
  
  if (InandOut == 2) {

    while (Serial.available() > 0)  { // Check if values are available in the Serial Buffer
  
      int setspeed = Serial.parseInt();  //  Read entered Speed from Serial Buffer
    
       if (setspeed > 0) {
         stepperX.setMaxSpeed(setspeed);
         Serial.println("");
         Serial.println("Speed set at:  ");
         Serial.println(setspeed);
         Serial.println("");
         Serial.println("Type 'I' to go to the IN points or 'O' to go to the OUT points");
         Serial.println("OR");
         Serial.println("Press Joystick Switch again to Reset IN and OUT Points");
         setspeed=0;
         InandOut=3;
      }
    }
  }
    
  if (InandOut == 3) {

    while (Serial.available() > 0)  { // Check if values are available in the Serial Buffer
  
      char checkinput = Serial.read();  //  Read Character entered in Serial Monitor
      Serial.println(checkinput);
    
    if (checkinput == 'I') {  // Move both steppers to IN points
      gotoposition[0]=XInPoint;
      gotoposition[1]=ZInPoint;
      StepperControl.moveTo(gotoposition);
      StepperControl.runSpeedToPosition(); // Blocks until all are in position
      Serial.println("Steppers are at the IN Position...");
      Serial.println("");
      Serial.println("Type 'I' to go to the IN points or 'O' to go to the OUT points");
      Serial.println("OR");
      Serial.println("Press Joystick Switch again to Reset IN and OUT Points");
    }
    
    if (checkinput == 'O') {  //  Move both steppers to OUT points
      gotoposition[0]=XOutPoint;
      gotoposition[1]=ZOutPoint;
      StepperControl.moveTo(gotoposition);
      StepperControl.runSpeedToPosition(); // Blocks until all are in position
      Serial.println("Steppers are at the OUT Position...");
      Serial.println("");
      Serial.println("Type 'I' to go to the IN points or 'O' to go to the OUT points");
      Serial.println("OR");
      Serial.println("Press Joystick Switch again to Reset IN and OUT Points");
    }

  }
}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章