sooo..
kind of a newby in arduino, zo be gentle please)
I'm building a 2-dof arm to draw or print something. right now I'm into my second prototype, understanding how to move steppers (the first one used 9g servos). now it's 28BYJ-48 5v steppers with 1/64 gear with ULN2003 driver. next on the plan are 24v steppers from old printer with cnc shield and dvr8825.
guess the pictures explains best in which state it is
the code homes all motors, gets x and y values from formula (like in here), or u just uses 2 coordinates, then a bit of trigonometry to calculate angles and poof we done (recently added a servo to move a pen).
//debugging LED
const int ledPin = 13;
// Servo Angles
float ServoS_1_Angle = 0;
float ServoS_2_Angle = 0;
// Define arm Constants
const float a = 10; // lower joint length (cm)
const float b = 8.5; // upper joint length (cm)
// Correction factors to align servo values with their respective axis
const float S_1_CorrectionFactor = 10; // Align arm "a" with the horizontal when at 0 degrees
const float S_2_CorrectionFactor = 0; // Align arm "b" with arm "a" when at 0 degrees
// Correction factor to shift origin out to edge of the mount
const float X_CorrectionFactor = 7; // X direction correction factor (cm)
const float Y_CorrectionFactor = 0; // Y direction correction factor (cm)
// Angle Variables
float A; //Angle oppposite side a (between b and c)
float B; //Angle oppposite side b
float C; //Angle oppposite side c
float theta; //Angle formed between line from origin to (x,y) and the horizontal
// Distance variables
float x; // x position (cm)
float y; // y position (cm)
float c; // Hypotenuse legngth in cm
const float pi = M_PI; //Store pi in a less annoying format
//Circle Setup
const float Radius = 2.5; // Radius of circle in centimeters
const int CircleCenterX = 5; // X coordinate of the center of the circle
const int CircleCenterY = 0; // Y coordinate of the center of the circle
//=====================================================================
//===========STEPPERS=================================================
//=====================================================================
#include <AccelStepper.h>
#define HALFSTEP 8
// Определение пинов для управления двигателем 1
#define motor1Pin1 8 // IN1 на 1-м драйвере ULN2003
#define motor1Pin2 9 // IN2 на 1-м драйвере ULN2003
#define motor1Pin3 10 // IN3 на 1-м драйвере ULN2003
#define motor1Pin4 11 // IN4 на 1-м драйвере ULN2003
#define button 7
AccelStepper stepper1(HALFSTEP, motor1Pin1, motor1Pin3, motor1Pin2, motor1Pin4);
const int homeButton1 = 7;
byte hBval;
// Определение пинов для управления двигателем 2
#define motor2Pin1 2 // IN1 на 2-м драйвере ULN2003
#define motor2Pin2 3 // IN2 на 2-м драйвере ULN2003
#define motor2Pin3 4 // IN3 на 2-м драйвере ULN2003
#define motor2Pin4 5 // IN4 на 2-м драйвере ULN2003
#define button 6
AccelStepper stepper2(HALFSTEP, motor2Pin1, motor2Pin3, motor2Pin2, motor2Pin4);
const int homeButton2 = 6;
byte hBva2;
//=========================================
//==============SERVO=======================
//=========================================
#include <Servo.h>
Servo Servo_p; // pen joint
const float serv_ang1 = 140; // angle for up pen position
const float serv_ang2 = 165; // angle for down pen position
//=====================================================================
//==============SETUP=================================================
//=====================================================================
void setup(){
//steppers setup
stepper1.setMaxSpeed(800); //nice and slow for testing
stepper1.setAcceleration(500);
stepper1.setSpeed(600);
pinMode(homeButton1, INPUT_PULLUP);
stepper2.setMaxSpeed(1000); //nice and slow for testing
stepper2.setAcceleration(500);
stepper2.setSpeed(800);
pinMode(homeButton2, INPUT_PULLUP);
//servo setup
Servo_p.attach(12); // pen joint
pinMode(ledPin, OUTPUT); // -For debugging
Serial.begin(9600); // -For debugging
Serial.println(0);
servo_pHome(); //runs routine to home servo_p
stepper2Home(); //runs routine to home motor1
stepper1Home(); //runs routine to home motor2
}
//==============LOOP====================================
void loop()
{
for (float t = 0; t<20; t++) // Get t value
{
x = 4 ;
y = t/2 ;
//parabola
//x = sq(t/2)/10 ;
//y = t/2 ;
// Equation of a circle in parametric form
//x = Radius * cos(t *(pi/180)) + CircleCenterX;
//y = Radius * sin(t *(pi/180)) + CircleCenterY;
FixCoordinates(x,y); // Enter coordinates of point.
CalculateServoAngles(); // Calculate angle of servos
MoveArm(); // Move arm to new position
delay(200);
MoveServo();
delay(200);
}
}
//==============HOMING-RUTINE==============================
void stepper1Home(){ //this routine should run the motor
hBval = digitalRead(homeButton1);
while (hBval == LOW)
{
//backwards slowly till it hits the switch and stops
stepper1.moveTo(4000);
stepper1.run();
hBval = digitalRead(homeButton1);
}
delay(1000);
stepper1.setCurrentPosition(0); //should set motor position to zero and go back to main routine
delay(1000);
}
void stepper2Home(){ //this routine should run the motor
hBva2 = digitalRead(homeButton2);
while (hBva2 == LOW)
{
//backwards slowly till it hits the switch and stops
stepper2.moveTo(4000);
stepper2.run();
hBva2 = digitalRead(homeButton2);
}
delay(1000);
stepper2.setCurrentPosition(0); //should set motor position to zero and go back to main routine
delay(1000);
}
void servo_pHome(){
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
Servo_p.write(serv_ang1); // risen pen
delay(1000);
}
//======================CALCULATING==============================================
//gets x and y measured from the bottom of the base. function corrects for offset
void FixCoordinates(float x_input, float y_input)
{
x = x_input + X_CorrectionFactor;
y = y_input + Y_CorrectionFactor;
}
//calculates necessary servo angles to move arm to desired points
void CalculateServoAngles()
{
c = sqrt( sq(x) + sq(y) ); // pythagorean theorem
B = (acos( (sq(b) - sq(a) - sq(c))/(-2*a*c) )) * (180/pi); // Law of cosines: Angle opposite upper arm section
C = 180-(acos( (sq(c) - sq(b) - sq(a))/(-2*a*b) )) * (180/pi); // Law of cosines: Angle opposite hypotenuse
theta = (asin( y / c )) * (180/pi); // Solve for theta to correct for lower joint's impact on upper joint's angle
ServoS_1_Angle = ( B + theta + S_1_CorrectionFactor ) * 11,37777777777778; // Find necessary angle. Add Correction. Multiply by gear ratio
ServoS_2_Angle = ( C + S_2_CorrectionFactor ) * 11,37777777777778; // Find neceesary angle. Add Correction. Multiply by gear ratio
}
//updates the motors
void MoveArm()
{
Serial.println(22);
stepper1.moveTo(-ServoS_1_Angle);
stepper1.runToPosition();
stepper2.moveTo(-ServoS_2_Angle);
stepper2.runToPosition();
delay(2);
}
soooo the problem now is with the "and poof" part. right now I'm using this to actualy move the arm. MoveArm()
ServoS_1_Angle is a value of an angle when 360=4096
the problem is that second stepper waits for the first to stop. They move one after another.
I actually read the topic on the same subject, but still I don't get how to make 2 steppers to simontaniously move
I could make a some kind of point-drawing bot with this setup, but my goal is to draw lines
Now to the questions
1)How to make them move simontaniously in my setup?
2)Will it be possible to apply this code to my next physical setup? uno+cnc shield+24v steppers+dvr8825?
in advance - I've been trying to run(), but steppers do not move at all. runSpeed() same. Also I am concerned that without RunToPosition and all that relative stuff with angles it is not possible...