Hi, I'm doing a project with a robot arm(with three steppers) and processing. Actually, it's a kind of drawing machine, the robot will be controlled by a webcam. A moving dot is captured by the cam and then transit the coordinate of dot to Arduino.
For now, I just want to control one stepper, and let it rotate by xy position. The problem is it always move by the same angle and same frequency. I guess there is something wrong with headToPoin function, is anybody could help me, please? Thank you.
This is the code:
#include <AccelStepper.h>
//#include <MultiStepper.h>
const byte MOVETO = 0;
const byte LINETO = 1;
const byte ORIGIN = 2;
const float MOTOR_DIFF = 1.3;
const int MICROSTEPS = 16;
#define STEPPERX_DIR_PIN 5
#define STEPPERX_STEP_PIN 2
#define STEPPERY_DIR_PIN 6
#define STEPPERY_STEP_PIN 3
#define STEPPERZ_DIR_PIN 7
#define STEPPERZ_STEP_PIN 4
#define en 8
AccelStepper stepper1(1, STEPPERX_STEP_PIN, STEPPERX_DIR_PIN);
AccelStepper stepper2(1, STEPPERY_STEP_PIN, STEPPERY_DIR_PIN);
AccelStepper stepper3(1, STEPPERZ_STEP_PIN, STEPPERZ_DIR_PIN);
int led = 13;
int xPos = 0;
int yPos = 0;
void setup() {
Serial.begin(9600);
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(100.0);
// stepper1.moveTo(100000);
stepper2.setMaxSpeed(1000.0);
stepper2.setAcceleration(100.0);
// stepper2.moveTo(1000000);
stepper3.setMaxSpeed(5000.0);
stepper3.setAcceleration(100.0);
// stepper3.moveTo(-1000000);
pinMode(en, OUTPUT);
digitalWrite(en, LOW);
pinMode(led, OUTPUT);
}
void loop( ) {
//receiving data (bytearrays) from Serial,
if (Serial.available() >= 9) {
byte type = Serial.read();
byte xByte = Serial.read();
byte xByte2 = Serial.read();
byte xByte3 = Serial.read();
byte xByte4 = Serial.read();
byte yByte = Serial.read();
byte yByte2 = Serial.read();
byte yByte3 = Serial.read();
byte yByte4 = Serial.read();
//adding bytes for x and y parameter
int x = (int) xByte + (int) xByte2 + (int) xByte3 + (int) xByte4;
int y = (int) yByte + (int) yByte2 + (int) yByte3 + (int) yByte4;
headToPoint(x, y);
}
stepper3.run();
}
void headToPoint(int x, int y) {
long theta = atan2(y, x);
long thetaPos;
long thetaDiff = theta - thetaPos;
if (stepper3.distanceToGo() == 0) {
stepper3.moveTo( thetaDiff * (180 / PI));
thetaPos = theta;
delay(100);
}
}