Checking the limit switches when moving


#1

Before attempting this change you should have checked the Stepper motors and limit switches as described in the post “Making the XY-Plotter and mDraw work together”, and also added the debounce method to reading limit switches as described in the post “How to debounce the limit switches”.

Quit mDraw and make a back-up copy of your xybot.ino.

Double-click xybot.ino and get the Arduino IDE open.

Scroll down to the function doMove()

Locate the lines setting dA and dB from the values of the current position and the intended target position.

  int d;
  dA = tarA - posA;
  dB = tarB - posB;

Immediately after the declaration of “int d” insert the following lines:

int xLow;
int xHigh;
int yLow;
int yHigh;

Remember how we had to correct the assignments to tarA and tarB? Without those errors being addressed, this next piece of code would not work. The values derived from tarA and tarB tell us something as well as the distance to be moved, they tell us, by the sign, which direction the move is making, allowing us to work out which limit pin we should be keeping an eye on. We want to stop an intended move if the limit switch for that direction has gone from high to low, meaning the carriage has met it and is pressing rather closely against it.

So, scroll through the code to the comment

// Move A

immediately before the line “posA+=(dA>0?1:-1)” insert

yLow = debouncePin(ylimit_pin1);
yHigh = debouncePin(ylimit_pin2);

then we make a test of the direction and the limit results, and only allow the call to the stepper move function if the limit switch for the intended direction is still high

if( (dA > 0 && yHigh == 1) || (dA < 0 && yLow == 1) )
{
      posA+=(dA>0?1:-1)
      stepperMoveA(d);
      cntA-=1;
}
else
{
    if( yLow == 0) Serial.println("Y- reached");
    else Serial.println("Y+ reached");
    tarA = posA;
}

Before moving down to do the same thing to the StepperB move, a quick explanation.
We only come to this branch in the code if there is a change in Y-position to be made (see the earlier line "if( posA != tarA)"
We only need to worry if dA is positive AND yHigh has gone to zero, or if dA is negative AND yLow has gone to zero (if yHigh is 0 but we move in a negative Y direction that will clear the Y+ limit switch)
Once we know we’ve hit a limit, we stop any further attempt to move in that direction by changing tarA to be where we are now (Which was posA BEFORE altering it).

So, for the X move, we need to find the line “posB+=(dB>0?1:-1);”

before it we insert

xLow = debouncePin(xlimit_pin1);
xHigh = debouncePin(xlimit_pin2);

And around the three lines of code that update posB and actually move the stepper we add the test and the subsequent reporting if we touched a limit

if( (dB > 0 && xHigh == 1) || (dB < 0 && xLow == 1) )
{
    posB+=(dB>0?1:-1);`
    stepperMoveB(d);
    cntB-=1;
}
else
{
    if( xLow == 0) Serial.println("X- Reached");
    else Serial.println("X+ Reached");
    tarB = posB;
}

And that’s all that is needed, but without debounced switches, known assignments of switches and motors, or reliable tarA and tarB values, it wouldn’t have been half that simple.


Makeblock XY - Mdraw and Gremote problems
#2