gin66 / FastAccelStepper

A high speed stepper library for Atmega 168/328p (nano), Atmega32u4, Atmega 2560, ESP32, ESP32S2, ESP32S3, ESP32C3 and Atmel SAM Due
MIT License
286 stars 67 forks source link

Wrong direction for one stepper #143

Closed cezarg1410 closed 1 year ago

cezarg1410 commented 1 year ago

Hello, I am trying to build a robot with four stepper engines. They will work in pairs (left and right).

The thing is: i develoepd very simple programm to controll my robot. But the problem is that only one stepper is controller well - the second one always moves forward (no chance to move it back).

So i uplaoded my code into ESP32 WROOOM and removed that esp from robot. Here is what i can see:

Here is my code:


#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

#include "FastAccelStepper.h"
#include "AVRStepperPins.h" // Only required for AVR controllers

const int DIR_LEFT_PIN = 32;
const int STEP_LEFT_PIN = 12;

const int DIR_RIGHT_PIN = 14;
const int STEP_RIGHT_PIN = 13;

const int ENABLE_PIN = 26;

FastAccelStepperEngine leftEngine = FastAccelStepperEngine();
FastAccelStepper *leftStepper = NULL;

FastAccelStepperEngine rightEngine = FastAccelStepperEngine();
FastAccelStepper *rightStepper = NULL;

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");

  leftEngine.init();
  leftStepper = leftEngine.stepperConnectToPin(STEP_LEFT_PIN);

  rightEngine.init();
  rightStepper = rightEngine.stepperConnectToPin(STEP_RIGHT_PIN);

  if (leftStepper && rightStepper) {
    leftStepper->setDirectionPin(DIR_LEFT_PIN);
    leftStepper->setEnablePin(ENABLE_PIN);
    leftStepper->setAutoEnable(true);
    leftStepper->setSpeedInHz(1000);       // 500 steps/s
    leftStepper->setAcceleration(500);    // 100 steps/s²

    rightStepper->setDirectionPin(DIR_RIGHT_PIN);
    rightStepper->setEnablePin(ENABLE_PIN);
    rightStepper->setAutoEnable(true);
    rightStepper->setSpeedInHz(1000);       // 500 steps/s
    rightStepper->setAcceleration(500);    // 100 steps/s²

    //    digitalWrite(DIR_LEFT_PIN, LOW);
    //    digitalWrite(DIR_RIGHT_PIN, LOW);
  }
}

void loop() {
  if (SerialBT.available()) {

    String text = SerialBT.readStringUntil('<<');

    if (text.startsWith(">>")) {
      text = text.substring(2);

      int splitterIndex = text.indexOf("--");

      String buttonName = text.substring(0, splitterIndex);
      String actionName = text.substring(splitterIndex + 2, text.length());

      Serial.print("Button ");
      Serial.print(buttonName);

      Serial.print(", Action: ");
      Serial.println(actionName);

      if (buttonName == "UP" && actionName == "DOWN") {
        goForward();
      } else if (buttonName == "UP" && actionName == "UP") {
        stop();
      }

      if (buttonName == "DOWN" && actionName == "DOWN") {
        goBackward();
      } else if (buttonName == "DOWN" && actionName == "UP") {
        stop();
      }
      if (buttonName == "LEFT" && actionName == "DOWN") {
        left();
      } else if (buttonName == "LEFT" && actionName == "UP") {
        stop();
      }
      if (buttonName == "RIGHT" && actionName == "DOWN") {
        right();
      } else if (buttonName == "RIGHT" && actionName == "UP") {
        stop();
      }
    }
  }
}

void goForward() {
  leftStepper->runForward();
  rightStepper->runForward();
}
void goBackward() {
  leftStepper->runBackward();
  rightStepper->runBackward();
}

void left() {
  leftStepper->runBackward();
  rightStepper->runForward();
}
void right() {
  leftStepper->runForward();
  rightStepper->runBackward();
}

void stop() {
  leftStepper->stopMove();
  rightStepper->stopMove();
}```

Will be very glad for any help
cezarg1410 commented 1 year ago

I've just done futher checks and here is the interesting part. Consider following part of code

if (leftStepper && rightStepper) {
    leftStepper->setDirectionPin(14);
    leftStepper->setEnablePin(ENABLE_PIN);
    leftStepper->setAutoEnable(true);
    leftStepper->setSpeedInHz(1000);       // 500 steps/s
    leftStepper->setAcceleration(500);    // 100 steps/s²

    rightStepper->setDirectionPin(27);
    rightStepper->setEnablePin(ENABLE_PIN);
    rightStepper->setAutoEnable(true);
    rightStepper->setSpeedInHz(1000);       // 500 steps/s
    rightStepper->setAcceleration(500);    // 100 steps/s²

    Serial.println("Direction pins: ");
    Serial.println(leftStepper->getDirectionPin());
    Serial.println(rightStepper->getDirectionPin());
  }

The output should be 27 and 14. But it its:

Direction pins: 
27
27
cezarg1410 commented 1 year ago

The code was wrong. It should't create separate FastAccelStepperEngine Instead it should be:

FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *leftStepper = NULL;
FastAccelStepper *rightStepper = NULL;

void setup() {
  SerialBT.begin("ESP32test"); //Bluetooth device name

  engine.init();
  leftStepper = engine.stepperConnectToPin(STEP_LEFT_PIN);
  rightStepper = engine.stepperConnectToPin(STEP_RIGHT_PIN);

  if (leftStepper && rightStepper) {
    leftStepper->setEnablePin(ENABLE_PIN);
    leftStepper->setAutoEnable(true);
    leftStepper->setSpeedInHz(2000);
    leftStepper->setAcceleration(500);
    leftStepper->setDirectionPin(DIR_LEFT_PIN);

    rightStepper->setEnablePin(ENABLE_PIN);
    rightStepper->setAutoEnable(true);
    rightStepper->setSpeedInHz(2000);
    rightStepper->setAcceleration(500);
    rightStepper->setDirectionPin(DIR_RIGHT_PIN);
  }
}