I'm trying to figure out why my movement stops short when I use the run vs runSpeedToPosition. I prefer to use run, because it allows me to interrupt the movement in the event a limit switch is triggered, but the way I am implementing it may not be right because it moves only momentarily.
Does run need to be placed in void loop, or some other loop that runs continuously, or at least continues to loop until some indication that it has reached its final position?
How do you implement run, so that it is both interruptible, and finishes its move? Is void loop the best place for it?
Here is my problematic code that stops short, and I suspect its only moving while the loop does its computations, and stops when its exits the computation. The number of cycles of this computation loop of course vary, but could be between 10 and 1000 loops.
[code]
void Bresenham(int x1, int y1, int x2, int y2)// x2 and y2 were int const
{
/*
x1=x1*10;
y1=y1*10;
x2=x2*10;
y2=y2*10;
*/
int delta_x = (x2 - x1);
// if x1 == x2, then it does not matter what we set here
signed char const ix((delta_x > 0) - (delta_x < 0));
delta_x = abs(delta_x) << 1;
int delta_y = (y2 - y1);
// if y1 == y2, then it does not matter what we set here
signed char const iy((delta_y > 0) - (delta_y < 0));
delta_y = abs(delta_y) << 1;
move3(x1, y1);
if (delta_x >= delta_y)
{
// error may go below zero
int error = (delta_y - (delta_x >> 1));
while (x1 != x2)
{
// reduce error, while taking into account the corner case of error == 0
if ((error > 0) || (!error && (ix > 0)))
{
error -= delta_x;
y1 += iy;
}
// else do nothing
error += delta_y;
x1 += ix;
move3(x1, y1);
}
}
else
{
// error may go below zero
int error = (delta_x - (delta_y >> 1));
while (y1 != y2)
{
// reduce error, while taking into account the corner case of error == 0
if ((error > 0) || (!error && (iy > 0)))
{
error -= delta_y;
x1 += ix;
}
// else do nothing
error += delta_x;
y1 += iy;
move3(x1, y1);
}
}
} // end void Bresenham
void move3(int x1, int y1) {
float newx1=x1;
float newy1=y1;
// float newx1 = (float)x1 / 10.0;
// float newy1 = (float)y1 / 10.0;
Serial.print ("Move3 newx1= ");
Serial.print (newx1);
Serial.print (" newy1 = ");
Serial.println (newy1);
R=newx1;
S=newy1;
calculateAngles(R,S,T);
calcMoveSteps();
move(J1newPosition, J2newPosition, J3newPosition);
}
void move(int J1newPosition, int J2newPosition, int J3newPosition) {
long positions[3]; // Array of desired stepper positions
positions[0] = J1newPosition; //needs positive movement from home CW
positions[1] = -(J2newPosition); // needs negative movement from home
positions[2] = -(J3newPosition); // needs negative movement from home
steppers.moveTo(positions);
steppers.run(); // preferable to allow checking for reaching limit switches
/*
Serial.print ("J1newPosition");
Serial.println (J1newPosition);
Serial.print ("J2newPosition");
Serial.println (J2newPosition);
Serial.print ("J3newPosition");
Serial.println (J3newPosition);
*/
// stop movement if end / home switch activated
if (digitalRead(J1calPin) == HIGH) {
J1newPosition = J1axis.currentPosition() + 50;
Serial.println ("J1 limit switch activated");
}
if (digitalRead(J2calPin) == HIGH) {
J2newPosition = -(J2axis.currentPosition() - 50);
Serial.println ("J2 limit switch activated");
}
if (digitalRead(J3calPin) == HIGH) {
J3newPosition = -(J3axis.currentPosition() - 100);
J2newPosition = -(J2axis.currentPosition() - 100); //J2 influences J3
Serial.println ("J3 limit switch activated");
}
// stop movement if end / home switch activated
} // end void move
[/code]