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. Code initially based on… https://github.com/sidlauskaslukas/barbot
Any idea?
#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() {
}