Hello,
I am working on a delta robot controlled with stepper motors through arduino uno.
I found a code online (Code) that allows me to use Arduino processing IDE but this code was written for servo motors.
So, I am trying to re-adjust this code to work with my stepper motors but I am very new to using arduino and programming so I am facing some difficulties.
I think my main problem is not knowing how to write angle values into stepper motors which is this part of their code:
servoWriteFloat(&servo1, t1); //write angles value into the servo
servoWriteFloat(&servo2, t2+10); //write angles value into the servo - the 10 value is due to not perfect alignement of the servo
servoWriteFloat(&servo3, t3+5); //write angles value into the servo - the 5 value is due to not perfect alignement of the servo
The Following is my attempt to readjust the code to work for stepper motors:
#include <Stepper.h>
float xPos = 0; // x axis position
float yPos = 0; // y axis position
float zPos = -190; // z axis position, negative is up, positive is down. Delta bots are usually used up side down...
//float xPos = 78.231;
//float yPos = -78.231;
//float zPos = -377.824;
float t1; //stepper angle t for 'theta', 1 for stepper 1
float t2;
float t3;
int result;
int data = 0;
int serialTeller=0;
#define MAXANGLE 98.73 //maximum angle allowed by the Steppers
#define MINANGLE -42.92 //minimum angle alloweb by the Steppers
// defines pins numbers
const int stepX = 2;
const int dirX = 5;
const int stepY = 3;
const int dirY = 6;
const int stepZ = 4;
const int dirZ = 7;
const int enPin = 8;
void setup() {
// initialize the serial port:
Serial.begin(9600);
// Sets the two pins as Outputs
pinMode(stepX,OUTPUT);
pinMode(dirX,OUTPUT);
pinMode(stepY,OUTPUT);
pinMode(dirY,OUTPUT);
pinMode(stepZ,OUTPUT);
pinMode(dirZ,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
digitalWrite(dirX,HIGH);
digitalWrite(dirY,HIGH);
digitalWrite(dirZ,HIGH);
}
void loop(){
//int variable1 = int(random(100));
//Serial.print(variable1);
char xxPos[4]; // x position taken from processing
char yyPos[4]; // y position taken from processing
char zzPos[4]; // z position taken from processing
char tempo; // temp variable
if (Serial.available() > 12) {
tempo=Serial.read(); //read from serial
int conta=0;
while (tempo != 'X') // wait for the X position
{
tempo=Serial.read();
}
if (Serial.available() >= 11) {
xxPos[0]=Serial.read(); //read X byte
xxPos[1]=Serial.read(); //read X byte
xxPos[2]=Serial.read(); //read X byte
xxPos[3]=Serial.read(); //read X byte
xPos=atoi(xxPos); //transfor bytes in integer
tempo=Serial.read(); //read Y char
yyPos[0]=Serial.read(); //read Y byte
yyPos[1]=Serial.read(); //read Y byte
yyPos[2]=Serial.read(); //read Y byte
yyPos[3]=Serial.read(); //read Y byte
yPos=atoi(yyPos); //transform bytes in integer
tempo=Serial.read(); //read Z Char
zzPos[0]=Serial.read(); //read Z byte
zzPos[1]=Serial.read(); //read Z byte
zzPos[2]=Serial.read(); //read Z byte
zzPos[3]=Serial.read(); //read Z byte
zPos=atoi(zzPos); //transform bytes in integer
}
}
result = delta_calcInverse(xPos, yPos, zPos, t1, t2, t3);
// stepperWriteFloat(&stepX, t1); //How should I rewrite servoWriteFloat for stepper motors?
// stepperWriteFloat(&stepY, t2+10);
// stepperWriteFloat(&stepZ, t3+5);
//Should I write it this way?
digitalWrite(stepX, t1);
digitalWrite(stepY, t2+10);
digitalWrite(stepZ, t2+5);
//or should I write it similare to this ?
//// initialize the stepper library on pins:
//Stepper myStepper1(t1, 2, 5);
//Stepper myStepper2(t2+10, 3, 6);
//Stepper myStepper3(t3+5, 4, 7);
// // set the speed at 60 rpm:
//myStepper1.setSpeed(60);
//// set the speed at 60 rpm:
//myStepper2.setSpeed(60);
//// set the speed at 60 rpm:
//myStepper3.setSpeed(60);
//
//// Serial.println("myStepper1 t1");
// myStepper1.step(t1);
// Serial.println("myStepper1 t2");
// myStepper1.step(t2+10);
// Serial.println("myStepper1 t3");
// myStepper1.step(t3+5);
}
//////////////////// I think this part should remain the same////////////////////
//Note: I copied this part in the same file as the above code because otherwise it gives me and error saying that result is not indicated in this scope//
// print some values in the serial
void printSerial(){
Serial.print(result == 0 ? "ok" : "no");
char buf[10];
dtostrf(xPos, 4, 0, buf);
Serial.print(" X");
Serial.print(buf);
dtostrf(yPos, 4, 0, buf);
Serial.print(" Y");
Serial.print(buf);
dtostrf(zPos, 4, 0, buf);
Serial.print(" Z");
Serial.print(buf);
dtostrf(t1, 6, 2, buf);
Serial.print(" T1");
Serial.print(buf);
dtostrf(t2, 6, 2, buf);
Serial.print(" T2");
Serial.print(buf);
dtostrf(t3, 6, 2, buf);
Serial.print(" T3");
Serial.print(buf);
Serial.println("");
}
The Following is the processing IDE code which I believe works just fine:
import processing.serial.*;
Serial myPort; // The serial port:
int XXX = 0; // x zero
int YYY = 0; // y zero
int ZZZ = -180; // z zero (the robot is upsidedown)
int serialBegin = 255;
void setup() {
size(600,600);
// open serial port
myPort = new Serial(this, Serial.list()[1], 19200);
frameRate(100);
noCursor();
}
void draw() {
background(255);
//triangle draw
triangle(width/2, height, 0, 200, width, 200);
strokeWeight(3);
//line draw
line(300,200,mouseX,mouseY);
line(150,400,mouseX,mouseY);
line(450,400,mouseX,mouseY);
// X and y value are scaled in order to fit the robot space work
XXX=round(float((mouseX-300))*2/5);
YYY=round(float((mouseY-300))*2/5);
// if mouse is pressed the Z value change
if (mousePressed && (mouseButton == LEFT)) {
ZZZ -= 1;
}
if (mousePressed && (mouseButton == RIGHT)) {
ZZZ += 1;
}
String xPos;
String yPos;
String zPos;
//write X value in serial port
xPos="X";
if (XXX > 0)
xPos += '+';
else
xPos += '-';
if (abs(XXX) < 100)
xPos += '0';
if (abs(XXX) < 10)
xPos += '0';
xPos += abs(XXX);
// write Y value in serial port
yPos="Y";
if (YYY > 0)
yPos += '+';
else
yPos += '-';
if (abs(YYY) < 100)
yPos += '0';
if (abs(YYY) < 10)
yPos += '0';
yPos += abs(YYY);
// write Z value in serial port
zPos="Z";
if (ZZZ > 0)
zPos += '+';
else
zPos += '-';
if (abs(ZZZ) < 100)
zPos += '0';
if (abs(ZZZ) < 10)
zPos += '0';
zPos += abs(ZZZ);
// write x,y,z in the serial port
myPort.write(xPos);
myPort.write(yPos);
myPort.write(zPos);
// write x,y,z in the monitor
print(xPos);
print(yPos);
println(zPos);
}
I also read this (Forum) and found out the following: "A stepper - in contrast to a servo - does not know where it is. If you want it to move to an absoulte angle, you must fist determine a starting position - e.g. with an end switch." but I am still not sure how to move on from here, How should I wrtie angle values into stepper motor?
Note: I believe the kinematics code should remain the same in both cases.