Pages: [1]   Go Down
 Author Topic: Moving motors to a reset (0,0) point  (Read 1171 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 32
 « on: April 09, 2012, 10:10:10 pm » Bigger Smaller Reset

Hi all,

Been getting a lot of help here, and I really appreciate it!

I am building a project using an XY plotter bed to move around a magnet under a table, which will be moving a corresponding magnet-attached object on the top of the table.

I have no way besides mathematically to determine a coordinate system for the header. So I have installed 4 limit switches (0, 0, xMax, yMax), and want to have a way to keep track of its position and create a "reset/recalibrate" function that sends the head back to 0,0 and resets the XY values accordingly.

I am not 100% sure how to do this; to make the motors alternate pulses (so that I get a more diagonal-looking interpolation) until they reach 00? Do I do a some kind of while statement? Or is there a smarter way to do this?

CODE:
Code:
/* Stepper Motor Controller ;  language: Wiring/Arduino
This program drives a unipolar or bipolar stepper motor.  by Tom Igoe
and the Spline library by Kerinin
*/

/* significant help from arduino forum superstars: sirbow2, stimmer
*/
#include <spline.h>
#include <Stepper.h>

#define motorSteps 200     // change this depending on the number of steps
#define motor2Steps 200     // change this depending on the number of steps
// per revolution of your motor

#define motorPin1 2

#define motorPin2 3
#define motorPin3 4
#define motorPin4 5
#define motorPin6 8
#define motorPin7 9
#define motorPin8 10
#define motorPin9 11

int previous = LOW;
long time = 0;         // the last time the output pin was toggled
long debounce = 50;   // the debounce time, increase if the output flickers
int state = HIGH;

//set pin numbers

int xStopMin = A0;
int xStopMax = A1;
int yStopMin = A2;
int yStopMax = A3;

int xPos;
int yPos;
boolean xIs0;
boolean xIsMax;
boolean yIs0;
boolean yIsMax;

// initialize of the Stepper library:
Stepper yAxis(motorSteps, motorPin1,motorPin2, motorPin3, motorPin4);
Stepper xAxis(motor2Steps, motorPin6,motorPin7, motorPin8, motorPin9);

void setup() {

xAxis.setSpeed(60); //x
yAxis.setSpeed(60); //y

pinMode(xStopMin, INPUT);
pinMode(xStopMax, INPUT);
pinMode(yStopMin, INPUT);
pinMode(yStopMax, INPUT);

xPos = 0;
yPos = 0;

// Initialize the Serial port:
Serial.begin(9600);
}

void loop() {
//check limit switches
xIs0 = true;
xPos = 0;
Serial.println("xMin");
}
else xIs0 = false;

xIsMax = true;
Serial.println("xMax");
}
else xIsMax = false;

yIs0 = true;
Serial.println("yMin");
yPos = 0;

}
else yIs0 = false;

yIsMax = true;
Serial.println("yMax");
}
else yIsMax = false;

char val=0;

//keyboard control
switch(val){
case 'N':
moveN(200);
break;

case 'S':
moveS(200);
break;

case 'E':
moveE(200);
break;

case 'W':
moveW(200);
break;

//diagonal (moving motors almost at the same time)
case 'J':
moveNE(400);
Serial.println("I am moving North East");
break;

case 'H': //NW
moveNW(400);
break;

case 'X': //SE
moveSE(400);
break;

case 'Z': //SW
moveSW(400);
break;

case 'C': //SW
moveCirc();
break;

//resets
case 'Q': //reset to 0,0
moveN(100);
moveE(100);
break;

//debugs
case 'P':
Serial.print("x position: ");
Serial.println(xPos);
Serial.println("y position: ");
Serial.println(yPos);
break;

default:
delay(10);
break;
}
}
//movement functions

//first, str  aight up down left right
void moveE(int numSteps){
int s = 0;
while(!digitalRead(xStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
xAxis.step(1);
s++;
}
//delay(100);

xPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveW(int numSteps){
int s = 0;
while(!digitalRead(xStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
xAxis.step(-1);
s++;
}
//delay(100);
xPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveN(int numSteps){
int s = 0;
while(!digitalRead(yStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
yAxis.step(-1);
s++;
}
//delay(100);
yPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);

void moveS(int numSteps){
int s = 0;
while(!digitalRead(yStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
yAxis.step(1);
s++;
}
//delay(100);
yPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

//diagonals
void moveNE(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(-1);
xAxis.step(1);
}
xPos += numSteps;
yPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveSE(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(1);
xAxis.step(1);
}
xPos += numSteps;
yPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}
}

void moveSW(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(1);
xAxis.step(-1);
}
xPos -= numSteps;
yPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveNW(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(-1);
xAxis.step(-1);
}
xPos -= numSteps;
yPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveCirc(){
//draw circle at x,y 50,50
int CircleXCenter = 2;
int CircleYCenter = 2;
int CurXPos = xPos; //where the platform currently is in X
int CurYPos = yPos; //where the platform currently is in Y

for (int i = 0; i < 360; i++)
{
//it does this for each point of the circle, so you dont have to run them at the same time
float
angle = i*2*3.14/360;
xPos = CircleXCenter + (cos(angle) * Rad);
yPos = CircleYCenter + (sin(angle) * Rad);
if(CurXPos < xPos)
{

xAxis.step(xPos - CurXPos);
//movexnow = Xpos - CurXPos;
}
else
{
xAxis.step(xPos - CurXPos);
//MoveXNow = CurXPos - Xpos;
}

if(CurYPos < yPos)
{
yAxis.step(yPos - CurYPos);
//   MoveYNow = Ypos - CurYPos;
}
else
{
yAxis.step(yPos - CurYPos
);
//MoveYNow = CurYPos - Ypos;
}

// step(xAxis);
//step(yAxis);

//<100, so -1
if(i == (360-1)) //we are at end of for loop, save current position.
{
CurYPos = yPos;

CurXPos = xPos ;
}

}

}

 Logged

UK
Offline
Shannon Member
Karma: 223
Posts: 12631
-
 « Reply #1 on: April 10, 2012, 05:20:08 am » Bigger Smaller Reset

Presumably your sketch provides functions to move in X and Y axes. So you just call functions repeatedly to move in the negative direction on both axes until the limit switch is hit on each axis. If you have got your head around writing code to move to move to an arbitrary position, then the 'reset' part ought to be easy.
 « Last Edit: April 10, 2012, 05:21:52 am by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline
Newbie
Karma: 0
Posts: 32
 « Reply #2 on: April 10, 2012, 10:59:09 am » Bigger Smaller Reset

Hmmmmmm I tried a few things, but I can't seem to get it to stop when it hits the limit switches; it keeps going. What I need is for it to move diagonally until it hits one limit switch and then move in the other direction till both switches have been activated...

Here's what I have so far, the function is called "reset"

Code:
/* Stepper Motor Controller ;  language: Wiring/Arduino
This program drives a unipolar or bipolar stepper motor.  by Tom Igoe
and the Spline library by Kerinin
*/

/* significant help from arduino forum superstars: sirbow2, stimmer
*/
#include <spline.h>
#include <Stepper.h>

#define motorSteps 200     // change this depending on the number of steps
#define motor2Steps 200     // change this depending on the number of steps
// per revolution of your motor

#define motorPin1 2

#define motorPin2 3
#define motorPin3 4
#define motorPin4 5
#define motorPin6 8
#define motorPin7 9
#define motorPin8 10
#define motorPin9 11

int previous = LOW;
long time = 0;         // the last time the output pin was toggled
long debounce = 50;   // the debounce time, increase if the output flickers
int state = HIGH;

//set pin numbers

int xStopMin = A0;
int xStopMax = A1;
int yStopMin = A2;
int yStopMax = A3;

int xPos;
int yPos;
boolean xIs0;
boolean xIsMax;
boolean yIs0;
boolean yIsMax;

// initialize of the Stepper library:
Stepper yAxis(motorSteps, motorPin1,motorPin2, motorPin3, motorPin4);
Stepper xAxis(motor2Steps, motorPin6,motorPin7, motorPin8, motorPin9);

void setup() {

xAxis.setSpeed(60); //x
yAxis.setSpeed(60); //y

pinMode(xStopMin, INPUT);
pinMode(xStopMax, INPUT);
pinMode(yStopMin, INPUT);
pinMode(yStopMax, INPUT);

xPos = 0;
yPos = 0;

// Initialize the Serial port:
Serial.begin(9600);
}

void loop() {
//check limit switches
xIs0 = true;
xPos = 0;
Serial.println("xMin");
}
else xIs0 = false;

xIsMax = true;
Serial.println("xMax");
}
else xIsMax = false;

yIs0 = true;
Serial.println("yMin");
yPos = 0;

}
else yIs0 = false;

yIsMax = true;
Serial.println("yMax");
}
else yIsMax = false;

char val=0;

//keyboard control
switch(val){
case 'N':
moveN(200);
break;

case 'S':
moveS(200);
break;

case 'E':
moveE(200);
break;

case 'W':
moveW(200);
break;

//diagonal (moving motors almost at the same time)
case 'J':
moveNE(400);
Serial.println("I am moving North East");
break;

case 'H': //NW
moveNW(400);
break;

case 'X': //SE
moveSE(400);
break;

case 'Z': //SW
moveSW(400);
break;

case 'C': //SW
moveCirc();
break;

//resets
case 'Q': //reset to 0,0

reset(8000);
break;

//debugs
case 'P':
Serial.print("x position: ");
Serial.println(xPos);
Serial.println("y position: ");
Serial.println(yPos);
break;

default:
delay(10);
break;
}
}
//movement functions

//first, str  aight up down left right
void moveE(int numSteps){
int s = 0;
while(!digitalRead(xStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
xAxis.step(1);
s++;
}
//delay(100);

xPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveW(int numSteps){
int s = 0;
while(!digitalRead(xStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
xAxis.step(-1);
s++;
}
//delay(100);
xPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveN(int numSteps){
int s = 0;
while(!digitalRead(yStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
yAxis.step(-1);
s++;
}
//delay(100);
yPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);

void moveS(int numSteps){
int s = 0;
while(!digitalRead(yStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
yAxis.step(1);
s++;
}
//delay(100);
yPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

//diagonals
void moveNE(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(-1);
xAxis.step(1);
}
xPos += numSteps;
yPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveSE(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(1);
xAxis.step(1);
}
xPos += numSteps;
yPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}
}

void moveSW(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(1);
xAxis.step(-1);
}
xPos -= numSteps;
yPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveNW(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(1);
xAxis.step(-1);
}
xPos -= numSteps;
yPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}
}

void reset(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++){
xAxis.step(-1);
}
yAxis.step(-1);
}
}
xPos = 0;
yPos = 0;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);

void moveCirc(){
//draw circle at x,y 50,50
int CircleXCenter = 2;
int CircleYCenter = 2;
int CurXPos = xPos; //where the platform currently is in X
int CurYPos = yPos; //where the platform currently is in Y

for (int i = 0; i < 360; i++)
{
//it does this for each point of the circle, so you dont have to run them at the same time
float
angle = i*2*3.14/360;
xPos = CircleXCenter + (cos(angle) * Rad);
yPos = CircleYCenter + (sin(angle) * Rad);
if(CurXPos < xPos)
{

xAxis.step(xPos - CurXPos);
//movexnow = Xpos - CurXPos;
}
else
{
xAxis.step(xPos - CurXPos);
//MoveXNow = CurXPos - Xpos;
}

if(CurYPos < yPos)
{
yAxis.step(yPos - CurYPos);
//   MoveYNow = Ypos - CurYPos;
}
else
{
yAxis.step(yPos - CurYPos
);
//MoveYNow = CurYPos - Ypos;
}

// step(xAxis);
//step(yAxis);

//<100, so -1
if(i == (360-1)) //we are at end of for loop, save current position.
{
CurYPos = yPos;

CurXPos = xPos ;
}

}

}

 Logged

UK
Offline
Shannon Member
Karma: 223
Posts: 12631
-
 « Reply #3 on: April 10, 2012, 12:39:24 pm » Bigger Smaller Reset

Are you able to read all the limit switches in your sketch, and get the correct value as the switches are opened and closed?
 Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline
Newbie
Karma: 0
Posts: 32
 « Reply #4 on: April 10, 2012, 01:16:11 pm » Bigger Smaller Reset

Yes, they're all working as far as I know (tested them and they all do their println functions). Also, with normal movement, they stop the movement. I think it's a logic issue, but I can't figure it out :\

Thanks btw
 Logged

Offline
God Member
Karma: 32
Posts: 507
 « Reply #5 on: April 10, 2012, 03:19:10 pm » Bigger Smaller Reset

Double check that the steppers are stepping in the correct direction and you have the correct limit switch for each axis.

If you were to call xAxis.step(-1) repeatedly would it eventually trigger the xStopMin switch or the xStopMax switch? Similarly if you repeatedly call yAxis.step(-1) will it eventually hit yStopMin or yStopMax?

In moveN you have yAxis.step(-1) associated with the yStopMax switch, but in reset yAxis.step(-1) is associated with the yStopMin switch. One or the other must be wrong.
 Logged

Offline
Sr. Member
Karma: 1
Posts: 486
 « Reply #6 on: April 10, 2012, 04:39:19 pm » Bigger Smaller Reset

you could put the switches on the head that moves around on the plane instead of putting the switches at 0,0 on the frame. that way, you can move in -x direction until you hit the x switch on the frame, then move -y until you hit the y switch on the frame.
 Logged

http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

0
Offline
Newbie
Karma: 0
Posts: 46
Krazatchu Design Systems
 « Reply #7 on: April 10, 2012, 08:33:35 pm » Bigger Smaller Reset

In most CNC equipment, there is only one limit/home switch per axis.
Once it's calibrated to home, the machine software (MACH3 for ex) "knows" how far it can go in each axis...

You might try the pin change interrupt library to signal when a limit/home switch is triggered.
It will be more repeatable than polling as the interrupt will fire regardless of what the main loop is doing.

When my CNC mill homes, it pulls the Z up first for safety, then does X and Y.
When it finds the home switch, it then backs up and slow approaches again to negate switch bounce.
 Logged

Michael
----------------------
http://www.krazatchu.ca

Offline
Newbie
Karma: 0
Posts: 32
 « Reply #8 on: April 11, 2012, 08:45:23 am » Bigger Smaller Reset

Ok I double checked my mins and maxes (dyslexia yay).

However, my reset function still doesn't work as expected :\
The switches don't stop it.

I am not sure I want to use interrupt pins, but I might have to. Is there a way to make this logic work?

Quote
Code:
/* Stepper Motor Controller ;  language: Wiring/Arduino
This program drives a unipolar or bipolar stepper motor.  by Tom Igoe
and the Spline library by Kerinin
*/

/* significant help from arduino forum superstars: sirbow2, stimmer
*/
#include <spline.h>
#include <Stepper.h>

#define motorSteps 200     // change this depending on the number of steps
#define motor2Steps 200     // change this depending on the number of steps
// per revolution of your motor

#define motorPin1 2

#define motorPin2 3
#define motorPin3 4
#define motorPin4 5
#define motorPin6 8
#define motorPin7 9
#define motorPin8 10
#define motorPin9 11

int previous = LOW;
long time = 0;         // the last time the output pin was toggled
long debounce = 50;   // the debounce time, increase if the output flickers
int state = HIGH;

//set pin numbers

int xStopMin = A0;
int xStopMax = A1;
int yStopMin = A2;
int yStopMax = A3;

int xPos;
int yPos;
boolean xIs0;
boolean xIsMax;
boolean yIs0;
boolean yIsMax;

// initialize of the Stepper library:
Stepper yAxis(motorSteps, motorPin1,motorPin2, motorPin3, motorPin4);
Stepper xAxis(motor2Steps, motorPin6,motorPin7, motorPin8, motorPin9);

void setup() {

xAxis.setSpeed(60); //x HAS APPX 2200 STEPS FROM 0 TO MAX
yAxis.setSpeed(60); //y HAS APPX 6200 STEPS FROM 0 TO MAX

pinMode(xStopMin, INPUT);
pinMode(xStopMax, INPUT);
pinMode(yStopMin, INPUT);
pinMode(yStopMax, INPUT);

xPos = 0;
yPos = 0;

// Initialize the Serial port:
Serial.begin(9600);
}

void loop() {
//check limit switches
xIs0 = true;
xPos = 0;
Serial.println("xMin");
}
else xIs0 = false;

xIsMax = true;
Serial.println("xMax");
}
else xIsMax = false;

yIs0 = true;
Serial.println("yMin");
yPos = 0;

}
else yIs0 = false;

yIsMax = true;
Serial.println("yMax");
}
else yIsMax = false;

char val=0;

//keyboard control
switch(val){
case 'N':
moveN(200);
break;

case 'S':
moveS(200);
break;

case 'E':
moveE(200);
break;

case 'W':
moveW(200);
break;

//diagonal (moving motors almost at the same time)
case 'J':
moveNE(400);
Serial.println("I am moving North East");
break;

case 'H': //NW
moveNW(400);
break;

case 'X': //SE
moveSE(400);
break;

case 'Z': //SW
moveSW(400);
break;

case 'C': //SW
moveCirc();
break;

//resets
case 'Q': //reset to 0,0

reset(8000);
break;

//debugs
case 'P':
Serial.print("x position: ");
Serial.println(xPos);
Serial.println("y position: ");
Serial.println(yPos);
break;

default:
delay(10);
break;
}
}
//movement functions

//first, str  aight up down left right
void moveE(int numSteps){
int s = 0;
while(!digitalRead(xStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
xAxis.step(1);
s++;
}
//delay(100);

xPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveW(int numSteps){
int s = 0;
while(!digitalRead(xStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
xAxis.step(-1);
s++;
}
//delay(100);
xPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveN(int numSteps){
int s = 0;
while(!digitalRead(yStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
yAxis.step(-1);
s++;
}
//delay(100);
yPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);

void moveS(int numSteps){
int s = 0;
while(!digitalRead(yStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps
{
yAxis.step(1);
s++;
}
//delay(100);
yPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

//diagonals
void moveNE(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(-1);
xAxis.step(1);
}
xPos += numSteps;
yPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveSE(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(1);
xAxis.step(1);
}
xPos += numSteps;
yPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}
}

void moveSW(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(1);
xAxis.step(-1);
}
xPos -= numSteps;
yPos += numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}

void moveNW(int numSteps){
int s=0;
for(int s=0; s<numSteps; s++)
{
yAxis.step(1);
xAxis.step(-1);
}
xPos -= numSteps;
yPos -= numSteps;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);
}
}

void reset(int numSteps){
for(int s=0; s<numSteps; s++){
xAxis.step(-1);
}

yAxis.step(-1);
}
}
xPos = 0;
yPos = 0;

//check where we are
Serial.print("x position: ");
Serial.println(xPos);
Serial.print("y position: ");
Serial.println(yPos);

void moveCirc(){
//draw circle at x,y 50,50
int CircleXCenter = 2;
int CircleYCenter = 2;
int CurXPos = xPos; //where the platform currently is in X
int CurYPos = yPos; //where the platform currently is in Y

for (int i = 0; i < 360; i++)
{
//it does this for each point of the circle, so you dont have to run them at the same time
float
angle = i*2*3.14/360;
xPos = CircleXCenter + (cos(angle) * Rad);
yPos = CircleYCenter + (sin(angle) * Rad);
if(CurXPos < xPos)
{

xAxis.step(xPos - CurXPos);
//movexnow = Xpos - CurXPos;
}
else
{
xAxis.step(xPos - CurXPos);
//MoveXNow = CurXPos - Xpos;
}

if(CurYPos < yPos)
{
yAxis.step(yPos - CurYPos);
//   MoveYNow = Ypos - CurYPos;
}
else
{
yAxis.step(yPos - CurYPos
);
//MoveYNow = CurYPos - Ypos;
}

// step(xAxis);
//step(yAxis);

//<100, so -1
if(i == (360-1)) //we are at end of for loop, save current position.
{
CurYPos = yPos;

CurXPos = xPos ;
}

}

}

 Logged

New Jersey
Online
Karma: 66
Posts: 3673
 « Reply #9 on: April 11, 2012, 09:15:47 am » Bigger Smaller Reset

The reset routine looks like it should work, assuming that your limit switches do. If you move the plotter head to trigger the limit switches by hand, do you get the xMin and yMin messages on the serial monitor?

Try putting some serial debug inside the for loop in reset, for one thing, to see what the digitalRead is returning when the head hits the stop.
 Logged

Offline
Newbie
Karma: 0
Posts: 32
 « Reply #10 on: April 11, 2012, 10:24:37 am » Bigger Smaller Reset

It seems I can only get the switches to actually function if I handle them separately, but then I can't get that nice diagonal interpolation that I am looking for.

Any thoughts?

Code:

void reset(int numSteps){
for(int s=0; s<numSteps/2; s++){
xAxis.step(-1);
}
else {
Serial.println("STOP x");
xPos = 0;
break;
}
}
for(int s=0; s<numSteps/2; s++){
yAxis.step(-1);
}
else {
Serial.println("STOP y");
yPos = 0;
break;

}
}
}//END RESET FUNCTION
 Logged

UK
Offline
Shannon Member
Karma: 223
Posts: 12631
-
 « Reply #11 on: April 11, 2012, 10:55:56 am » Bigger Smaller Reset

When you tested your limit switches, did you also test the case where both the 'zero' position switches operated at the same time?
 Logged

I only provide help via the forum - please do not contact me for private consultancy.

Liverpool, UK
Offline
Jr. Member
Karma: 0
Posts: 66
Arduino rocks
 « Reply #12 on: April 11, 2012, 11:12:11 am » Bigger Smaller Reset

Is there a timing issue when reading the pins? Maybe you need a short delay between digitalRead calls (don't see why)?

I'd be tempted to re-write your function thus:

Code:
void reset(int numSteps) {

int xMove = -1;
int yMove = -1;

for(int s=0; s<numSteps; s++){

xAxis.step(xMove);
yAxis.step(yMove);
}

xPos = 0;
yPos = 0;
}

Stick a couple of serial.Write calls in there to show the values in xMove and yMove so you should see exactly when they are (or aren't) being reset.
 Logged

New Jersey
Online
Karma: 66
Posts: 3673
 « Reply #13 on: April 11, 2012, 11:47:59 am » Bigger Smaller Reset

Your NW and SW functions both have the same step instructions:
Code:
yAxis.step(1);
xAxis.step(-1);

Once corrected, what does the version that needs -1 -1 do?

I think PeterH is on to something though, it looks like a wiring issue, not code.

 Logged

Offline
Newbie
Karma: 0
Posts: 32
 « Reply #14 on: April 12, 2012, 02:48:29 pm » Bigger Smaller Reset

Hey everyone, thanks for the help!

Tried every suggestion, then a friend of mine volunteered to help and wrote a new function for me. I think I understand, and it works!

Will keep y'all posted on new issues sure to arise!

Code (if you're curious):

Code:
void resetFully(){

bool bMoveX = true;
bool bMoveY = true;
bool doReset = true;
int count = 0;
while (doReset == true){

count ++;

if (bMoveX == true) xAxis.step(-1);
if (bMoveY == true) yAxis.step(-1);

if (bMoveX == false && bMoveY == false){
doReset = false;
}
if (count == 8000){
doReset = false;
}
}
Serial.println("done!");
xPos = 0;
yPos = 0;

}

void moveTo(int newX, int newY){

int xSteps = newX - xPos;
int ySteps = newY - yPos;
int stepDirectionX = (newX > xPos) ? 1 : -1;
int stepDirectionY = (newY > yPos) ? 1 : -1;
int nStepsX = abs(xSteps);
int nStepsY = abs(ySteps);

int maxSteps = abs(xSteps) > abs(ySteps) ? abs(xSteps) : abs(ySteps);

for (int i = 0; i < maxSteps; i++){
if (i < nStepsX){
bool bMoveX = true;
if (bMoveX == true){
xAxis.step(stepDirectionX);
xPos += stepDirectionX;
}
}

if (i < nStepsY){
bool bMoveY = true;
if (bMoveY == true){
yAxis.step(stepDirectionY);
yPos += stepDirectionY;
}
}

}

}

 Logged

 Pages: [1]   Go Up