Arduino ile Step Motor, Joystick ve Limit Switch nasıl kullanılır?

Katılım
1 Şub 2012
Mesajlar
1
Puanları
1
Yaş
38
Merhabalar. Arduino'da Step motoru joystick ile kontrol edebiliyorum ancak limit switch bağlamak istiyorum. Birkaç şey denedim ancak başaramadım. Mevcutta kullandığım kodu ekleyeceğim aşağıya. Bu kodlara nasıl limit switch kodu entegre edebilirim? Limit switch aktif edildiğinde motor ters yönde gitsin belirleyeceğim adım sayısı kadar şeklinde bir isteğim var. Yardımcı olabilecek varsa şimdiden teşekkürler.

Sürücü olarak CWD556, motor olarak ise Nema 23 kullanıyorum.

Bu kodu normal kullanımda kullanıyorum. İstediğim yönde istediğim hızda kullanabiliyorum.

Kod:
#include <AccelStepper.h>
 //accelstepper library
AccelStepper stepper(1, 8, 9); // direction Digital 9 (CCW), pulses Digital 8 (CLK)


//Pins
const byte Analog_X_pin = A0; //x-axis readings

int Analog_X = 0; //x-axis value

int Analog_X_AVG = 0; //x-axis value average

void setup()
{

  //SERIAL
  Serial.begin(9600);
  //----------------------------------------------------------------------------   
  //PINS
  pinMode(Analog_X_pin, INPUT);
 
  //---------------------------------------------------------------------------- 
  InitialValues(); //averaging the values of the 3 analog pins (values from potmeters)
  //---------------------------------------------------------------------------- 
  //Stepper parameters
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(5000); //SPEED = Steps / second 
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2   
  stepper.setSpeed(500);
  delay(500);

}

void loop()
{
  ReadAnalog(); 
  stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
}

void ReadAnalog()
{
  //Reading the 3 potentiometers in the joystick: x
  Analog_X = analogRead(Analog_X_pin); 

  //if the value is 25 "value away" from the average (midpoint), we allow the update of the speed
  //This is a sort of a filter for the inaccuracy of the reading
  if(abs(Analog_X-Analog_X_AVG)>25)
  {
  stepper.setSpeed(5*(Analog_X-Analog_X_AVG));   
  }
  else
  {
    stepper.setSpeed(0);
  }
}

void InitialValues()
{
  //Set the values to zero before averaging
  float tempX = 0;
  //---------------------------------------------------------------------------- 
  //read the analog 50x, then calculate an average.
  //they will be the reference values
  for(int i = 0; i<50; i++)
  {
     tempX += analogRead(Analog_X_pin); 
     delay(10); //allowing a little time between two readings
  }
  //---------------------------------------------------------------------------- 
  Analog_X_AVG = tempX/50;
  //---------------------------------------------------------------------------- 
  Serial.print("AVG_X: ");
  Serial.println(Analog_X_AVG);
  Serial.println("Calibration finished"); 
}


Aşağıya ekleyeceğim kodda ise limit switch ile denediğimde motor çok çok çok yavaş şekilde sadece yön belirleyebiliyorum. Hızını artıramıyorum. Sabit ve oldukça yavaş dönüyor.

Kod:
#include <AccelStepper.h>
 //accelstepper library
AccelStepper stepper(1, 8, 9); // direction Digital 9 (CCW), pulses Digital 8 (CLK)

//Pins
const byte Analog_X_pin = A0; //x-axis readings
const byte interruptPin = 2; //pin for the microswitch using attachInterrupt();
int Analog_X = 0; //x-axis value
int Analog_X_AVG = 0; //x-axis value average
char receivedCommand; //character for commands
void setup()
{

  //SERIAL
  Serial.begin(9600);
  //----------------------------------------------------------------------------   
  //PINS
  pinMode(Analog_X_pin, INPUT);
  pinMode(interruptPin, INPUT_PULLUP); // internal pullup resistor (debouncing)
  attachInterrupt(digitalPinToInterrupt(interruptPin), stopMotor, FALLING);

  //---------------------------------------------------------------------------- 
  InitialValues(); //averaging the values of the 3 analog pins (values from potmeters)
  //---------------------------------------------------------------------------- 
  //Stepper parameters
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(5000); //SPEED = Steps / second 
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2   
  stepper.setSpeed(500);
  delay(500);

}

void loop()
{
  ReadAnalog(); 
  stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
  checkSerial(); //check serial port for new commands

}

void ReadAnalog()
{
  //Reading the 3 potentiometers in the joystick: x
  Analog_X = analogRead(Analog_X_pin); 

  //if the value is 25 "value away" from the average (midpoint), we allow the update of the speed
  //This is a sort of a filter for the inaccuracy of the reading
  if(abs(Analog_X-Analog_X_AVG)>25)
  {
  stepper.setSpeed(5*(Analog_X-Analog_X_AVG));   
  }
  else
  {
    stepper.setSpeed(0);
  }
}
void InitialValues()
{
  //Set the values to zero before averaging
  float tempX = 0;
  //---------------------------------------------------------------------------- 
  //read the analog 50x, then calculate an average.
  //they will be the reference values
  for(int i = 0; i<50; i++)
  {
     tempX += analogRead(Analog_X_pin); 
     delay(10); //allowing a little time between two readings
  }
  //---------------------------------------------------------------------------- 
  Analog_X_AVG = tempX/50;
  //---------------------------------------------------------------------------- 
  Serial.print("AVG_X: ");
  Serial.println(Analog_X_AVG);
  Serial.println("Calibration finished"); 
}
void checkSerial() //method for receiving the commands
{ 
  //switch-case would also work, and maybe more elegant
 
  if (Serial.available() > 0) //if something comes
  {
    receivedCommand = Serial.read(); // this will read the command character
  }

  {
    //START - MEASURE
    if (receivedCommand == 'n') //immediately stops the motor
    {       
      stepper.setCurrentPosition(0); // reset position
      Serial.println("STOP "); //print action
      stepper.stop(); //stop motor
      stepper.disableOutputs(); //disable power

    }

    //HOMING
    if (receivedCommand == 'h') //homing, this movement will be interrupted via the attachInterrupt() triggered by the microswitch.
    {       

      Serial.println("HOMING"); //print action
      stepper.setAcceleration(100); //defining some low acceleration
      stepper.setMaxSpeed(100); //set speed, 100 for test purposes
      stepper.move(-1 * 20000); ////set distance - negative value flips the direction
      //distance should be larger than the length of the whole path.
      //I don't think that this is a safe way of homing. if the switch fails, the motor will keep running anyway
    }

  }
  //after we went through the above tasks, newData becomes false again, so we are ready to receive new commands again.

}
void stopMotor()//function activated by the pressed microswitch
{
  //Stop motor, disable outputs; here we should also reset the numbers if there are any
      
      stepper.setCurrentPosition(0); // reset position
      Serial.println("STOP "); //print action
      stepper.stop(); //stop motor
      stepper.disableOutputs(); //disable power


  Serial.println("Pressed."); //feedback towards the serial port
}
 
Merhabalar. Arduino'da Step motoru joystick ile kontrol edebiliyorum ancak limit switch bağlamak istiyorum. Birkaç şey denedim ancak başaramadım. Mevcutta kullandığım kodu ekleyeceğim aşağıya. Bu kodlara nasıl limit switch kodu entegre edebilirim? Limit switch aktif edildiğinde motor ters yönde gitsin belirleyeceğim adım sayısı kadar şeklinde bir isteğim var. Yardımcı olabilecek varsa şimdiden teşekkürler.

Sürücü olarak CWD556, motor olarak ise Nema 23 kullanıyorum.

Bu kodu normal kullanımda kullanıyorum. İstediğim yönde istediğim hızda kullanabiliyorum.

Kod:
#include <AccelStepper.h>
 //accelstepper library
AccelStepper stepper(1, 8, 9); // direction Digital 9 (CCW), pulses Digital 8 (CLK)


//Pins
const byte Analog_X_pin = A0; //x-axis readings

int Analog_X = 0; //x-axis value

int Analog_X_AVG = 0; //x-axis value average

void setup()
{

  //SERIAL
  Serial.begin(9600);
  //----------------------------------------------------------------------------  
  //PINS
  pinMode(Analog_X_pin, INPUT);
 
  //----------------------------------------------------------------------------
  InitialValues(); //averaging the values of the 3 analog pins (values from potmeters)
  //----------------------------------------------------------------------------
  //Stepper parameters
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(5000); //SPEED = Steps / second
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2  
  stepper.setSpeed(500);
  delay(500);

}

void loop()
{
  ReadAnalog();
  stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
}

void ReadAnalog()
{
  //Reading the 3 potentiometers in the joystick: x
  Analog_X = analogRead(Analog_X_pin);

  //if the value is 25 "value away" from the average (midpoint), we allow the update of the speed
  //This is a sort of a filter for the inaccuracy of the reading
  if(abs(Analog_X-Analog_X_AVG)>25)
  {
  stepper.setSpeed(5*(Analog_X-Analog_X_AVG));  
  }
  else
  {
    stepper.setSpeed(0);
  }
}

void InitialValues()
{
  //Set the values to zero before averaging
  float tempX = 0;
  //----------------------------------------------------------------------------
  //read the analog 50x, then calculate an average.
  //they will be the reference values
  for(int i = 0; i<50; i++)
  {
     tempX += analogRead(Analog_X_pin);
     delay(10); //allowing a little time between two readings
  }
  //----------------------------------------------------------------------------
  Analog_X_AVG = tempX/50;
  //----------------------------------------------------------------------------
  Serial.print("AVG_X: ");
  Serial.println(Analog_X_AVG);
  Serial.println("Calibration finished");
}


Aşağıya ekleyeceğim kodda ise limit switch ile denediğimde motor çok çok çok yavaş şekilde sadece yön belirleyebiliyorum. Hızını artıramıyorum. Sabit ve oldukça yavaş dönüyor.

Kod:
#include <AccelStepper.h>
 //accelstepper library
AccelStepper stepper(1, 8, 9); // direction Digital 9 (CCW), pulses Digital 8 (CLK)

//Pins
const byte Analog_X_pin = A0; //x-axis readings
const byte interruptPin = 2; //pin for the microswitch using attachInterrupt();
int Analog_X = 0; //x-axis value
int Analog_X_AVG = 0; //x-axis value average
char receivedCommand; //character for commands
void setup()
{

  //SERIAL
  Serial.begin(9600);
  //----------------------------------------------------------------------------  
  //PINS
  pinMode(Analog_X_pin, INPUT);
  pinMode(interruptPin, INPUT_PULLUP); // internal pullup resistor (debouncing)
  attachInterrupt(digitalPinToInterrupt(interruptPin), stopMotor, FALLING);

  //----------------------------------------------------------------------------
  InitialValues(); //averaging the values of the 3 analog pins (values from potmeters)
  //----------------------------------------------------------------------------
  //Stepper parameters
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(5000); //SPEED = Steps / second
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2  
  stepper.setSpeed(500);
  delay(500);

}

void loop()
{
  ReadAnalog();
  stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
  checkSerial(); //check serial port for new commands

}

void ReadAnalog()
{
  //Reading the 3 potentiometers in the joystick: x
  Analog_X = analogRead(Analog_X_pin);

  //if the value is 25 "value away" from the average (midpoint), we allow the update of the speed
  //This is a sort of a filter for the inaccuracy of the reading
  if(abs(Analog_X-Analog_X_AVG)>25)
  {
  stepper.setSpeed(5*(Analog_X-Analog_X_AVG));  
  }
  else
  {
    stepper.setSpeed(0);
  }
}
void InitialValues()
{
  //Set the values to zero before averaging
  float tempX = 0;
  //----------------------------------------------------------------------------
  //read the analog 50x, then calculate an average.
  //they will be the reference values
  for(int i = 0; i<50; i++)
  {
     tempX += analogRead(Analog_X_pin);
     delay(10); //allowing a little time between two readings
  }
  //----------------------------------------------------------------------------
  Analog_X_AVG = tempX/50;
  //----------------------------------------------------------------------------
  Serial.print("AVG_X: ");
  Serial.println(Analog_X_AVG);
  Serial.println("Calibration finished");
}
void checkSerial() //method for receiving the commands
{
  //switch-case would also work, and maybe more elegant
 
  if (Serial.available() > 0) //if something comes
  {
    receivedCommand = Serial.read(); // this will read the command character
  }

  {
    //START - MEASURE
    if (receivedCommand == 'n') //immediately stops the motor
    {      
      stepper.setCurrentPosition(0); // reset position
      Serial.println("STOP "); //print action
      stepper.stop(); //stop motor
      stepper.disableOutputs(); //disable power

    }

    //HOMING
    if (receivedCommand == 'h') //homing, this movement will be interrupted via the attachInterrupt() triggered by the microswitch.
    {      

      Serial.println("HOMING"); //print action
      stepper.setAcceleration(100); //defining some low acceleration
      stepper.setMaxSpeed(100); //set speed, 100 for test purposes
      stepper.move(-1 * 20000); ////set distance - negative value flips the direction
      //distance should be larger than the length of the whole path.
      //I don't think that this is a safe way of homing. if the switch fails, the motor will keep running anyway
    }

  }
  //after we went through the above tasks, newData becomes false again, so we are ready to receive new commands again.

}
void stopMotor()//function activated by the pressed microswitch
{
  //Stop motor, disable outputs; here we should also reset the numbers if there are any
     
      stepper.setCurrentPosition(0); // reset position
      Serial.println("STOP "); //print action
      stepper.stop(); //stop motor
      stepper.disableOutputs(); //disable power


  Serial.println("Pressed."); //feedback towards the serial port
}
detay verebileceğimi sanmıyorum ama arduino da kesmeler olduğunu biliyorum bazı pinlerin kesme pini oldugunu bunun üzerinden limit switchten gelen bilgi ile yön değiştirme yapabilirsin sanırım araştırabilirsin fikir olması açısından
 

Forum istatistikleri

Konular
128,191
Mesajlar
915,731
Kullanıcılar
449,965
Son üye
Pentium55

Yeni konular

Geri
Üst