0

AccelStepper / Step motor accidentally changes direction
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

AccelStepper / Step motor accidentally changes direction

by haddorp on Mon Apr 09, 2018 3:11 am

Hello, we are facing a strange issue that I do not really understand. The step motor is controlled by the AccelStepper lib. Basically the implementation looks ok.

But whenever the platform moved by motor hits an obstacle the motor changes the direction. My expectation would be that the motor used max. power in this situation or just stops. But why changing the direction?

The code for moving to the initial parking position looks quite simple (see below). I don't see anything that may cause the direction change.

Any idea?

Code: Select all | TOGGLE FULL SIZE
void setup() {
  Serial.begin(9600); // Serial port for debugging
  maestroSerial.begin(9600); // Servo controller
  Serial2.begin(9600); // Bluetooth module
  stepper.setMaxSpeed(X_MAX_SPEED); // Sets the maximum speed the X axis accelerate up to
  pinMode(X_ENDSTOP_PIN, INPUT_PULLUP); // Initialize endstop pin with the internal pull-up resistor enabled

  int endStopState = digitalRead(X_ENDSTOP_PIN);
 
  while (endStopState == HIGH) {
    stepper.moveTo(100);
    stepper.setSpeed(X_HOME_SPEED);
    stepper.runSpeed();
    endStopState = digitalRead(X_ENDSTOP_PIN);
  }
}

haddorp
 
Posts: 4
Joined: Tue Nov 17, 2015 7:08 pm

Re: AccelStepper / Step motor accidentally changes direction

by adafruit_support_bill on Mon Apr 09, 2018 6:32 am

What is in your loop() function?

adafruit_support_bill
 
Posts: 66835
Joined: Sat Feb 07, 2009 10:11 am

Re: AccelStepper / Step motor accidentally changes direction

by haddorp on Mon Apr 09, 2018 6:48 am

Hi, the code is based on...
https://github.com/sidlauskaslukas/barbot

But as far as I could see so far everything happens within the while loop (means all inside setup function).
To be sure we will add further debug statements and check again.

haddorp
 
Posts: 4
Joined: Tue Nov 17, 2015 7:08 pm

Re: AccelStepper / Step motor accidentally changes direction

by adafruit_support_bill on Mon Apr 09, 2018 7:09 am

But as far as I could see so far everything happens within the while loop (means all inside setup function).


... Until you hit that end stop and exit the 'while' loop. At that point you will start executing whatever is in your loop() function.

Code: Select all | TOGGLE FULL SIZE
while (endStopState == HIGH) {

adafruit_support_bill
 
Posts: 66835
Joined: Sat Feb 07, 2009 10:11 am

Re: AccelStepper / Step motor accidentally changes direction

by haddorp on Mon Apr 09, 2018 7:20 am

Hi, I simplified the sketch and removed/cleared the loop() function.

The step motor does strange movements... Going into one direction, then suddenly changing direction.
We also added some log statements. But I still do not understand what is going wrong.

When pressing the switch the while loop is done and the motor stops.

Looks weird. I don't see an error on the code. Perhaps wrong wiring, motor defect or controller defect...

Arduino Sketch
Code: Select all | TOGGLE FULL SIZE
#include "AccelStepper.h"
#include "PololuMaestro.h"
#include "Configuration.h"

String serialBuffer = "";
String actions[TOTAL_ACTIONS];

int counter = 0;
int lastIndex = 0;

AccelStepper stepper(X_INTERFACE_TYPE, X_STEP_PIN, X_DIR_PIN); // Define a stepper and the pins it will use
MicroMaestro maestro(maestroSerial); // Define a servo controller

void setup() {
  Serial.begin(9600); // Serial port for debugging
  maestroSerial.begin(9600); // Servo controller
  Serial2.begin(9600); // Bluetooth module
  stepper.setMaxSpeed(X_MAX_SPEED); // Sets the maximum speed the X axis accelerate up to
  pinMode(X_ENDSTOP_PIN, INPUT_PULLUP); // Initialize endstop pin with the internal pull-up resistor enabled
  homeXAxis(); // Return the X axis to it's home position at the startup
}

void homeXAxis() {
  int endStopState = digitalRead(X_ENDSTOP_PIN);
  long lastLog = millis();

  Serial.println("Home X axis 1");
 
  while (endStopState == HIGH) {
    if (millis() - lastLog > 1000) {
      lastLog = millis();
      Serial.print("Current position: ");
      Serial.println(stepper.currentPosition());
      Serial.print("Distance to go: ");
      Serial.println(stepper.distanceToGo());
    }
    stepper.moveTo(100);
    stepper.setSpeed(X_HOME_SPEED);
    stepper.runSpeed();
    endStopState = digitalRead(X_ENDSTOP_PIN);
  }

  Serial.println("Home X axis 2");

  stepper.moveTo(stepper.currentPosition() - 50);
  while (stepper.distanceToGo() != 0) {
    stepper.setSpeed(X_PARK_SPEED * -1);
    stepper.runSpeed();
  }

  Serial.println("Home X axis 3");

  endStopState = digitalRead(X_ENDSTOP_PIN);

  while (endStopState == HIGH) {
    stepper.moveTo(100);
    stepper.setSpeed(X_PARK_SPEED);
    stepper.runSpeed();
    endStopState = digitalRead(X_ENDSTOP_PIN);
  }
  stepper.setCurrentPosition(0);

  Serial.println("Home X axis 4");
}

void loop() {
}



Log view
Code: Select all | TOGGLE FULL SIZE
Home X axis 1
Current position: 696
Distance to go: -596
Current position: 1392
Distance to go: -1292
Current position: 2088
Distance to go: -1988
Current position: 2784
Distance to go: -2684
Current position: 3479
Distance to go: -3379
Current position: 4175
Distance to go: -4075
Current position: 4870
Distance to go: -4770

...

haddorp
 
Posts: 4
Joined: Tue Nov 17, 2015 7:08 pm

Re: AccelStepper / Step motor accidentally changes direction

by adafruit_support_bill on Mon Apr 09, 2018 7:48 am

I'd check with Pololu to see if you have the controller wired properly. Most of their boards use 'step' and 'direction' pins. It is possible that there is something wrong with your 'direction/ connection.

adafruit_support_bill
 
Posts: 66835
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.


cron