#include <AccelStepper.h>
#include <MultiStepper.h>
#include <math.h>
// Define pins for the steppers
// stepper 1 truc X drive giữa
#define ENA_PIN1 0
#define STEP_PIN1 1
#define DIR_PIN1 2
//stepper 2 truc Y driver bên trái ngoài cùng
#define ENA_PIN2 3
#define STEP_PIN2 4
#define DIR_PIN2 5
//stepper 3 truc Z drive bên phải ngoài cùng
#define ENA_PIN3 8
#define STEP_PIN3 9
#define DIR_PIN3 10
#define stopMotor 11
#define relayPin 12
#define homeXPin A0
#define homeYPin A1
#define homeZPin A2
// Biến trung gian quá trình
int currentTarget = 0; // 0: A, 1: B
int detectTarget = 0; // 0: ko thay doi tuong, 1: tim thay doi tuong.
int xA, yA, zA, xB, yB, zB; // Biến cho vị trí
//setup stepper
AccelStepper stepper1(AccelStepper::DRIVER, STEP_PIN1, DIR_PIN1); // truc X
AccelStepper stepper2(AccelStepper::DRIVER, STEP_PIN2, DIR_PIN2); // Truc Y
AccelStepper stepper3(AccelStepper::DRIVER, STEP_PIN3, DIR_PIN3); // Truc Z
// object MultiStepper
MultiStepper steppers;
void setup() {
Serial.begin(9600);
// Setup ENABLE pins
pinMode(ENA_PIN1, OUTPUT);
pinMode(ENA_PIN2, OUTPUT);
pinMode(ENA_PIN3, OUTPUT);
// Setup homePin and sensor
pinMode(stopMotor, INPUT_PULLUP);
pinMode(homeXPin, INPUT_PULLUP);
pinMode(homeYPin, INPUT_PULLUP);
pinMode(homeZPin, INPUT_PULLUP);
// Setup relayPin
pinMode(relayPin, OUTPUT);
// Enable drivers (ENABLE LOW)
digitalWrite(ENA_PIN1, LOW);
digitalWrite(ENA_PIN2, LOW);
digitalWrite(ENA_PIN3, LOW);
// add steppers in to multiStepper Steppers
steppers.addStepper(stepper1);
steppers.addStepper(stepper2);
steppers.addStepper(stepper3);
// setup speed for motor
stepper1.setMaxSpeed(1000); // tối đa là 1000 với full bước hoặc 9400 với bước 1/32
stepper1.setAcceleration(500);
stepper2.setMaxSpeed(1000);
stepper2.setAcceleration(500);
stepper3.setMaxSpeed(1000);
stepper3.setAcceleration(500);
// Setup home position XYZ
homing();
}
void loop() {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n'); // Đọc dữ liệu từ Serial
sscanf(input.c_str(), "%d,%d,%d,%d,%d,%d,%d", &detectTarget, &xA, &yA, &zA, &xB, &yB, &zB);
Serial.print("Received: detectTarget: "); Serial.print(detectTarget);
Serial.print(", xA: "); Serial.print(xA);
Serial.print(", yA: "); Serial.print(yA);
Serial.print(", zA: "); Serial.print(zA);
Serial.print(", xB: "); Serial.print(xB);
Serial.print(", yB: "); Serial.print(yB);
Serial.print(", zB: "); Serial.println(zB);
}
// move to A to pick up things
if (detectTarget == 1 && currentTarget == 0) {
if (digitalRead(stopMotor) == HIGH) { // chưa gặp công tắc hành trình thì vẫn sẽ di chuyển
// Move to vị trí đối tượng
moveToPosition(xA, yA, zA);
delay(1000);
currentTarget = 0;
} else { // dừng động cơ khi gặp công tắc và bắt đầu đợi di chuyển tới vị trí thùng chứa B
stopAllMotors();
currentTarget = 1;
digitalWrite(relayPin, HIGH);
delay(500);
}
}
// move to B
if (currentTarget == 1) {
// Move to vị trí thùng chứa
moveToPosition(xB, yB, zB);
delay(500);
digitalWrite(relayPin, LOW);
currentTarget = 0;
}
// move to home
if (currentTarget == 0 && detectTarget == 0) {
moveToPosition(0, 0, 0);
delay(500);
}
}
// calculateSteps with distance
long calculateSteps(float khoangCach) {
long gocQuay = calculateGocQuay(khoangCach);
return (gocQuay / 360.0) * 200; // 200 steps is 1 round (1.8/step)
}
// move all 3 stepper to position
void moveToPosition(float X, float Y, float Z) {
long positions[3];
positions[0] = calculateSteps(X);
positions[1] = calculateSteps(Y);
positions[2] = calculateSteps(Z);
// Di chuyển động cơ đồng bộ
steppers.moveTo(positions);
steppers.runSpeedToPosition(); // Chạy động cơ tới vị trí mục tiêu
}
long calculateGocQuay(float khoangCach) {
return (khoangCach * 360) / 8;
}
// setup homing when start
void homing() {
bool homingXComplete = false;
bool homingYComplete = false;
bool homingZComplete = false;
while (!homingXComplete || !homingYComplete || !homingZComplete) {
// Trục X stepper 1 drive giữa
if (digitalRead(homeXPin) == LOW) {
stepper1.setCurrentPosition(0); // đặt vị trí hiện tại là gốc
homingXComplete = true;
} else {
stepper1.setSpeed(300); // Trục X
stepper1.runSpeed();
}
// Trục Y stepper 2 driver bên trái
if (digitalRead(homeYPin) == LOW) {
stepper2.setCurrentPosition(0); // đặt lại vị trí hiện tại là gốc
homingYComplete = true;
} else {
stepper2.setSpeed(-300); // Trục Y homing trục Y + là về - là tiến
stepper2.runSpeed();
}
// Trục Z stepper 3 drive bên phải
if (digitalRead(homeZPin) == LOW) {
stepper3.setCurrentPosition(0); // đặt lại vị trí hiện tại là gốc
homingZComplete = true; // Sửa lỗi ở đây
} else {
stepper3.setSpeed(500);
stepper3.runSpeed();
}
}
}
// stop all motors
void stopAllMotors() {
stepper1.stop();
stepper2.stop();
stepper3.stop();
}
What is the problem?
Show your devices and wiring diagram.
Device I'm using
2 stepper motors 1.2A , 1.8 degrees /step and 1 stepper motor 2A, 1.8 degrees /step.
Arduino circuit using plug-in chip
3 driver DRV8825
1 power supply 24V-10A
...
Engine running unstable, sometimes 1 engine not working
sometime i got 1 stepper motor not working
Probably power supply is not supplying enough power.
Thank you. I will try another power supply.
According to you, how many A power supply should I use?
What voltage are the three motors?
What stepper driver module are you using?
i tried removing the serial lines in the code and it worked. Is it because my arduino uno is too weak
I saw you posted that information. DRV8825 and 24v10A motors...
...
The datasheet says the voltage is okay, but you need to restrict the current to 2.5A.
8.2-V to 45-V Operating Supply Voltage Range
2.5-A Maximum Drive Current at 24 V
I'm using driver DRV8825 for all
and Vref of driver I setup is 0.6V for stepper motor using 1.2A and 1V for stepper motor 2A
You want to run a 24VDC motor with AA batteries? Okay... 24/1.5 = 16xAA... but maybe you should use a (switching) power supply having 24vdc output.
This shows a typical connection of Arduino, motor, driver and power supply...
Is this a DRV8825 carrier?
yes that's right
// Define pins for the steppers
// stepper 1 truc X drive giữa
#define ENA_PIN1 0
#define STEP_PIN1 1
#define DIR_PIN1 2
I think the reason it was fixed when you removed serial comms is because you are using pins 0 and 1 as stepper1 control pins, they are the pins used for serial comms and programming.
What model Arduino are you using?
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Thanks.. Tom...