Hey guys,
I am doing a little project here where I have 2 pulleys on the sides that are moving the board in order to move through all the books that on the shelves, a bit similar to the polargraph project. I am using Arduino Uno, DRV8825 stepper drivers, CNC shield, and 2 nema 17 steppers. I calculated the grid that i want my object(board) to pass and set the grid tp 4X4.
Long story short, I decided which intervals should the object pass, and calculated the steps accordingly by the calculating LENGHT_TO_PASS/(RADIUS*TETA), and of course, I coordinated the correct motion of the steppers.
Now here is the problem!
The steppers aren't moving the way I tell them to move. they move every time a bit less than it should which at the end looks like a disaster. I tried to print out the steps and they seem to be right.
Now this is the solution that I think i am looking for!
I think the problem is microstepping, that i am not sure how to manage with accelstepper or at all in arduino. I saw a bunch of cnc solutions that use other program than arduino to manage it, but i would like to know if you can do it in the software. And if not what can i do?
It will be great if you help me to understand how to calibrate my project.
And here is my code!
#include <AccelStepper.h>
#define ENABLE 8
#define XDIR 5
#define XSTEP 2
#define YDIR 6
#define YSTEP 3
#define LENGHT 0.089
#define RADIUS 0.0289
#define TETA 0.0314
#define N 4
AccelStepper stepper1(1, 2, 5);
AccelStepper stepper2(1, 3, 6);
int counter1=0;
int counter_down=0;
enum Dir { Right=0, Left, Down};
void setup() {
//put your setup code here, to run once:
Serial.begin(9600);
Serial.println("we are ready\n");
pinMode(XDIR, OUTPUT);
pinMode(XSTEP, OUTPUT);
pinMode(YDIR, OUTPUT);
pinMode(YSTEP, OUTPUT);
pinMode(ENABLE, OUTPUT);
//digitalWrite(ENABLE, LOW);
stepper1.setMaxSpeed(100.0);
stepper1.setAcceleration(100.0);
stepper2.setMaxSpeed(100.0);
stepper2.setAcceleration(100.0);
}
void right(AccelStepper* stepper1,AccelStepper* stepper2, float steps1, float steps2){
stepper1->moveTo(steps1);
stepper2->moveTo(steps2);
while(!(stepper1->distanceToGo()==0 && stepper2->distanceToGo()==0)){
stepper1->run();
stepper2->run();
}
}
double power(float exponent, float power){
double result=1;
for(int i=0; i<power; i++){
result=result*((double)exponent);
}
return result;
}
void loop() {
float y=0, x=0;
float steps1_total=0, steps2_total=0;
double steps1, steps2;
int rightOrLeft=0;
Dir dir=Right;
int iter=9;
while(counter1<iter){
switch(dir){
case Right:
rightOrLeft=1;
steps1 = (sqrt(power(y,2)+power(LENGHT,2))-sqrt(power(y,2))) / (RADIUS * TETA);
steps2 = (sqrt(power(y,2)+power(N*LENGHT,2))-sqrt(power(y,2)+power((N-1)*LENGHT, 2)))/ (RADIUS * TETA);
right( &stepper1, &stepper2, -steps1, -steps2);
Serial.println(steps1, 3);
Serial.println(steps2, 3);
delay(1000);
for(int i=1; i<N; i++){
steps1 = steps1 +((sqrt(power(y,2)+power((i+1)*LENGHT,2)))-sqrt(power(y,2)+power((i)*LENGHT,2))) / (RADIUS * TETA);
steps2 = steps2 +((sqrt(power(y,2)+power((N-i)*LENGHT,2)))-sqrt(power(y,2)+power((N-1-i)*LENGHT, 2)))/ (RADIUS * TETA);
right( &stepper1, &stepper2, -steps1, -steps2);
Serial.println(steps1);
Serial.println(steps2);
delay(1000);
}
steps1_total=steps1_total+N*steps1;
steps2_total=steps2_total+N*steps2;
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
counter1++;
dir=Down;
break;
case Down:
counter_down++;
if(rightOrLeft==1){
steps1 = (sqrtf(power(N * LENGHT,2) + power(counter_down*LENGHT, 2)) - sqrt(power(N * LENGHT,2)+power((counter_down-1)*LENGHT,2))) / (RADIUS * TETA);
steps2 = LENGHT/(RADIUS*TETA);
right( &stepper1, &stepper2, -steps1, steps2);
dir=Left;
Serial.println(steps1);
Serial.println(steps2);
steps1_total=steps1_total-steps1;
steps2_total=steps2_total+steps2;
delay(1000);
} else {
steps2 = (sqrtf(power(N * LENGHT,2) + power(counter_down*LENGHT, 2)) - sqrt(power(N * LENGHT,2)+power((counter_down-1)*LENGHT,2))) / (RADIUS * TETA);
steps1 = LENGHT/(RADIUS*TETA);
right( &stepper1, &stepper2, -steps1, steps2);
dir=Right;
Serial.println(steps1);
Serial.println(steps2);
steps1_total=steps1_total-steps1;
steps2_total=steps2_total+steps2;
delay(1000);
}
y=y+LENGHT;
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
counter1++;
break;
case Left:
rightOrLeft=2;
steps2 = ((sqrt(power(y,2)+power(LENGHT,2)))-sqrt(power(y,2))) / (RADIUS * TETA);
steps1 = ((sqrt(power(y,2)+power(N*LENGHT,2)))-sqrt(power(y,2)+power((N-1)*LENGHT, 2)))/ (RADIUS * TETA);
right( &stepper1, &stepper2, steps1, steps2);
Serial.println(steps1);
Serial.println(steps2);
delay(1000);
for(int i=1; i<N; i++){
steps2 = steps2 + (sqrt(power(y,2)+power((i+1)*LENGHT,2))-sqrt(power(y,2)+power((i)*LENGHT,2))) / (RADIUS * TETA);
steps1 = steps1 +(sqrt(power(y,2)+power((N-i)*LENGHT,2))-sqrt(power(y,2)+power((N-1-i)*LENGHT, 2)))/ (RADIUS * TETA);
right( &stepper1, &stepper2, steps1, steps2);
Serial.println(steps1);
Serial.println(steps2);
delay(1000);
}
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
counter1++;
dir=Down;
break;
}
}
steps1=(sqrt(power(N*LENGHT, 2)+power(N*LENGHT,2)))/(RADIUS*TETA);
stepper1.moveTo(steps1);
stepper1.run();
//delay(60000*15);
}