runspeed and run

2,071 views
Skip to first unread message

Paulo Silva

unread,
Aug 25, 2017, 12:47:38 PM8/25/17
to accelstepper
Hello i dont understand the diference of runspeed and run; but runspeed in my code works better then run

#include <AccelStepper.h>

//pins
#define encoderPinA 2
#define encoderPinB 3
#define ZIGZAG_PIN 4
#define LEFT_PIN 5
#define STOP_PIN 6
#define RIGHT_PIN 7
#define enabledriver 8
AccelStepper stepper1(AccelStepper::DRIVER, 9, 10);


//vals
const int encoderMax = 24;
static char sign = 0;  // Holds -1, 1 or 0 to turn the motor on/off and control direction
boolean stateRun = false;
boolean stateOutputs = false;
const int sliderTotal = 26000;
const int sliderHome = 0;
int currentBasePos;
int current_speed = 0;
const int MAX_SPEED = 5000;
const int MAX_ACCEL = 5000;
int encoderPos = 0;
int encoderPosTemp = 0;
unsigned int lastReportedPos = 1;   // change management
static boolean rotating = false;    // debounce management
boolean A_set = false;
boolean B_set = false;


void setup() {
  /*debug
    Serial.begin(9600);  // output
    Serial.println("start");
  */


  //encoder
  pinMode(encoderPinA, INPUT_PULLUP);
  pinMode(encoderPinB, INPUT_PULLUP);
  attachInterrupt(0, doEncoderA, CHANGE);
  attachInterrupt(1, doEncoderB, CHANGE);
  

  //button
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
  pinMode(ZIGZAG_PIN, INPUT_PULLUP);

  //stepper setup
  delay(3000);
  stepper1.setPinsInverted(false, false, true);
  stepper1.setEnablePin(enabledriver);
  stepper1.disableOutputs();

}

void loop() {
  rotating = true;  // reset the debouncer
  currentBasePos = stepper1.currentPosition();

  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) {
    delay(250);
        stateRun = true;       
    stepper1.stop();
    stepper1.enableOutputs();
    stepper1.setMaxSpeed(MAX_SPEED);
    stepper1.setAcceleration(MAX_ACCEL);
    stepper1.moveTo(sliderTotal);
    sign = 1;
    encoderPos = 0;
    encoderPosTemp = 0;

  }

  else if (digitalRead(RIGHT_PIN) == 0) {
    delay(250);
    stateRun = true;
    stepper1.stop();
    stepper1.enableOutputs();
    stepper1.setMaxSpeed(MAX_SPEED);
    stepper1.setAcceleration(MAX_ACCEL);
    stepper1.moveTo(sliderHome);
    sign = -1;
    encoderPos = 0;
    encoderPosTemp = 0;

  }

  else if (digitalRead(STOP_PIN) == 0) {
    delay(250);
    stepper1.stop();
    stepper1.disableOutputs();
    encoderPos = 0;
    encoderPosTemp = 0;
    sign = 0;
 
  }

  else if (digitalRead(ZIGZAG_PIN) == 0) {
    delay(250);
    stateRun = true;
    stepper1.stop();
    stepper1.enableOutputs();
    stepper1.setMaxSpeed(MAX_SPEED);
    stepper1.setAcceleration(750);
    stepper1.runToNewPosition(0);
    delay(2000);
    stepper1.runToNewPosition(26000);
    stepper1.runToNewPosition(0);
    stepper1.disableOutputs();
    sign = 0;
    encoderPos = 0;
    encoderPosTemp = 0;     
  }
 
  /*debug
  if (lastReportedPos != encoderPos) {
    //Serial.println(-encoderPos);
    Serial.println(stepper1.speed());
    //Serial.printm(stepper1.currentPosition());
        lastReportedPos = encoderPos;
  }
  */
  

  // run motor speed 
  encoderPosTemp = -encoderPos;
  if ( encoderPosTemp > encoderMax ){
        encoderPosTemp = encoderMax;
      }
  if ( encoderPosTemp < 0 ){
        encoderPosTemp = 0;
      }
  current_speed = pow( 2, encoderPosTemp * 0.53) * sign;
  stepper1.setSpeed(current_speed);
  stepper1.runSpeed();
 
  if ( stepper1.currentPosition() == stepper1.targetPosition() && stateRun == true){
    encoderPosTemp = 0;
    encoderPos = 0;
    sign = 0;
    stepper1.stop();
    stepper1.disableOutputs();
    stateRun = false;
  }

  if ( stepper1.currentPosition() < 0){
    encoderPosTemp = 0;
    encoderPos = 0;
    sign = 0; 
    stepper1.stop();
    stepper1.disableOutputs();
    stateRun = false;
  }
  
}

// Interrupt on A changing state
void doEncoderA() {
  // debounce
  if ( rotating ) delayMicroseconds(500);  // wait a little until the bouncing is done

  // Test transition, did things really change?
  if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
    A_set = !A_set;

    // adjust counter + if A leads B
    if ( A_set && !B_set )
      encoderPos += 1;

    rotating = false;  // no more debouncing until loop() hits again
  }
}

// Interrupt on B changing state, same as A above
void doEncoderB() {
  if ( rotating ) delayMicroseconds(500);
  if ( digitalRead(encoderPinB) != B_set ) {
    B_set = !B_set;
    //  adjust counter - 1 if B leads A
    if ( B_set && !A_set )
      encoderPos -= 1;

    rotating = false;
  }
}



gregor christandl

unread,
Aug 25, 2017, 2:00:36 PM8/25/17
to accelstepper
Run() supports acceleration, runSpeed() steps at a constant speed. The computational overhead of run() results in a lower maximum speed.

--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages