Eines de l'usuari

Eines del lloc


tutorials:arduino:05_arduino_motors:25_accelstepper

AccelStepper

Amb la llibreria AccelStepper podem fer que vagin dos motors simultaniament i amb acceleracions.

http://www.airspayce.com/mikem/arduino/AccelStepper/

http://www.airspayce.com/mikem/arduino/AccelStepper/AccelStepper-1.59.zip

Resum(xulla). accelstepper_cheat.pdf

Amb acceleració

#include <AccelStepper.h>
#define HALFSTEP 8

// motor pins
#define motorPin1  A2     // IN1 on the ULN2003 driver 1
#define motorPin2  A3     // IN2 on the ULN2003 driver 1
#define motorPin3  A4     // IN3 on the ULN2003 driver 1
#define motorPin4  A5    // IN4 on the ULN2003 driver 1

#define motorPin5  8     // IN1 on the ULN2003 driver 2
#define motorPin6  9     // IN2 on the ULN2003 driver 2
#define motorPin7  10    // IN3 on the ULN2003 driver 2
#define motorPin8  11    // IN4 on the ULN2003 driver 2

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);


void setup() {
  // Units: position functions have units of STEPS, speed related functions have units of STEPS/SEC

  stepper1.setMaxSpeed(200.0);
  stepper2.setMaxSpeed(200.0);
  stepper1.setAcceleration(100.0);
  stepper2.setAcceleration(100.0);
  
  stepper1.moveTo(2400);
  stepper2.moveTo(2400);
 
}

void loop() {
  // put your main code here, to run repeatedly:
 
    stepper1.run();
    stepper2.run();
}

Altre exemple:

#include <AccelStepper.h>
#define HALFSTEP 8

// motor pins
#define motorPin1  A2     // IN1 on the ULN2003 driver 1
#define motorPin2  A3     // IN2 on the ULN2003 driver 1
#define motorPin3  A4     // IN3 on the ULN2003 driver 1
#define motorPin4  A5    // IN4 on the ULN2003 driver 1

#define motorPin5  8     // IN1 on the ULN2003 driver 2
#define motorPin6  9     // IN2 on the ULN2003 driver 2
#define motorPin7  10    // IN3 on the ULN2003 driver 2
#define motorPin8  11    // IN4 on the ULN2003 driver 2

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);


void setup()
{  
    stepper1.setMaxSpeed(200.0);
    stepper1.setAcceleration(200.0);
    stepper1.moveTo(1000);
    
    stepper2.setMaxSpeed(200.0);
    stepper2.setAcceleration(200.0);
    stepper2.moveTo(1000);
}
void loop()
{
    // Change direction at the limits
    if (stepper1.distanceToGo() == 0)
        stepper1.moveTo(-stepper1.currentPosition());
    if (stepper2.distanceToGo() == 0)
        stepper2.moveTo(-stepper2.currentPosition());
    stepper1.run();
    stepper2.run();
}

Sense acceleració

run() es amb acceleració i runSpeed() es sense acceleració. El setSpeed() cal posar-lo al loop() i el setMaxSpeeed() al setup()

Exemple sense acceleració:

senseAccel.ino
#include <AccelStepper.h>
#define HALFSTEP 8
 
// motor pins
#define motorPin1  A2     // IN1 on the ULN2003 driver 1
#define motorPin2  A3     // IN2 on the ULN2003 driver 1
#define motorPin3  A4     // IN3 on the ULN2003 driver 1
#define motorPin4  A5    // IN4 on the ULN2003 driver 1
 
#define motorPin5  8     // IN1 on the ULN2003 driver 2
#define motorPin6  9     // IN2 on the ULN2003 driver 2
#define motorPin7  10    // IN3 on the ULN2003 driver 2
#define motorPin8  11    // IN4 on the ULN2003 driver 2
 
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
 
 
void setup() {
  // Units: position functions have units of STEPS, speed related functions have units of STEPS/SEC
 
  stepper1.setMaxSpeed(500.0);
  stepper2.setMaxSpeed(500.0);
 // stepper1.setAcceleration(100.0);
 // stepper2.setAcceleration(100.0);
 
 
  stepper1.moveTo(4096);
  stepper2.moveTo(4096);  
 
}
 
void loop() {
  // put your main code here, to run repeatedly:
   stepper1.setSpeed(10.0);
    stepper2.setSpeed(10.0);
    stepper1.runSpeed();
    stepper2.runSpeed();
}

Sense acceleració però amb destí

Amb runSpeed() la velocitat es constant però no para al destí, per parar al destí cal runSpeedToPosition

runSpeedToPosition.ino
#include <AccelStepper.h>
#define HALFSTEP 8
 
// motor pins
#define motorPin1  A2     // IN1 on the ULN2003 driver 1
#define motorPin2  A3     // IN2 on the ULN2003 driver 1
#define motorPin3  A4     // IN3 on the ULN2003 driver 1
#define motorPin4  A5    // IN4 on the ULN2003 driver 1
 
#define motorPin5  8     // IN1 on the ULN2003 driver 2
#define motorPin6  9     // IN2 on the ULN2003 driver 2
#define motorPin7  10    // IN3 on the ULN2003 driver 2
#define motorPin8  11    // IN4 on the ULN2003 driver 2
 
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
 
 
void setup() {
  // Units: position functions have units of STEPS, speed related functions have units of STEPS/SEC
 
  stepper1.setMaxSpeed(500.0);
  stepper2.setMaxSpeed(500.0);
 // stepper1.setAcceleration(100.0);
 // stepper2.setAcceleration(100.0);
 
 
  stepper1.moveTo(100);
  stepper2.moveTo(100);
 
}
 
void loop() {
  // put your main code here, to run repeatedly:
   stepper1.setSpeed(10.0);
    stepper2.setSpeed(10.0);
    stepper1.runSpeedToPosition();
    stepper2.runSpeedToPosition();
}
tutorials/arduino/05_arduino_motors/25_accelstepper.txt · Darrera modificació: 2019/05/19 13:54 per crevert