Hello everyone, I am new to arduino programming. My project includes 3 stepper motor , A4988 motor driver , CNC shield and arduino uno. I have able to achieve the desired steps of motor required for my application but then the motor is not holding position at all. Any help / guidance would be appreciated.
pinMode(MOTOR_X_ENABLE_PIN, OUTPUT);
motorX.setEnablePin(MOTOR_X_ENABLE_PIN);
motorX.setPinsInverted(false, false, false); // Remove inversion if not needed
motorX.setAcceleration(1000); // Adjust acceleration value
motorX.setMaxSpeed(100); // Adjust max speed value
motorX.setSpeed(50); // Adjust speed value
motorX.enableOutputs();
pinMode(MOTOR_Y_ENABLE_PIN, OUTPUT);
motorY.setEnablePin(MOTOR_Y_ENABLE_PIN);
motorY.setPinsInverted(false, false, false); // Remove inversion if not needed
motorY.setAcceleration(1000); // Adjust acceleration value
motorY.setMaxSpeed(100); // Adjust max speed value
motorY.setSpeed(50); // Adjust speed value
motorY.enableOutputs();
pinMode(MOTOR_Z_ENABLE_PIN, OUTPUT);
motorZ.setEnablePin(MOTOR_Z_ENABLE_PIN);
motorZ.setPinsInverted(false, false, false); // Remove inversion if not needed
motorZ.setAcceleration(1000); // Adjust acceleration value
motorZ.setMaxSpeed(100); // Adjust max speed value
motorZ.setSpeed(50); // Adjust speed value
motorZ.enableOutputs();
}
void loop() {
// // Achieveing the Home position
// // Calculate the steps for each motor based on the joint angles
// int stepX = (Theta_1 / steps_angles);
// Serial.println(stepX);
// int stepY = (Theta_2 / steps_angles);
// Serial.println(stepY);
// int stepZ = (Theta_3 / steps_angles);
// Serial.println(stepZ);
// int acceleration = 100; // steps per second per second
// int deceleration = 100; // steps per second per second
// // Move the motors to the desired positions
// motorX.moveTo(stepX);
// motorY.moveTo(stepY);
// motorZ.moveTo(stepZ);
// while (motorX.distanceToGo() != 0 || motorY.distanceToGo() != 0 || motorZ.distanceToGo() != 0) {
// motorX.run();
// motorY.run();
// motorZ.run();
// }
// Calculate the steps for each motor based on the joint angles
int stepsX = (theta1 / steps_angles);
Serial.println(stepsX);
int stepsY = (theta2 / steps_angles);
Serial.println(stepsY);
int stepsZ = (theta3 / steps_angles);
Serial.println(stepsZ);
int acceleration = 10; // steps per second per second
int deceleration = 10; // steps per second per second
// Move the motors to the desired positions
motorX.moveTo(stepsX);
motorY.moveTo(stepsY);
// motorZ.moveTo(stepsZ);
while (motorX.distanceToGo() != 0 || motorY.distanceToGo() != 0) {
motorX.run();
motorY.run();
// motorZ.run();
}
delay(5000000);
// Reset motor positions to zero
motorX.setCurrentPosition(0);
motorY.setCurrentPosition(0);
motorZ.setCurrentPosition(0);
// // Run motors
// motorX.run();
// motorY.run();
// motorZ.run();
// Optionally add any other continuous operations here
}
This is my code before this is the inverse kinematics calculation to achieve the desired position.