I am new to this forum and thaught I should jump right in with a question about a project I am working on.
The idea is to build a wirebender, something along the lines of this:
Now I've finnished the machanical part of the project, now it's a matter of programming.
I'm running with NEMA 17 stepper motors driven by pololu stepper drivers as often used in reprap 3D printers.
All this controlled by an arduino (at the moment a UNO because the mega 2560 which I originally intended on using doesn't work with the processing progeam).
Now where the problem arises is that I have the code from PENSA (the guys that made the original DIWIRE) unfortunately this does not seem to be working as expected. What seems to happen is the stepper motors twitch and don't seem to be receiving the signals from the processing program. Interestingly enough with a simple sketch to test the stepper motors controlled through the serial port they run beautifully and can be controlled from the Processing program (this should rule out a wiring problem and problems with the serial connection. I am completely lost at tyhe moment and help will definately be much appreciated! (see bottom of the post for the code on the arduino and the processing sketch).
It would be great if someone helped out! Thanks in advance!
Arduino Code:
* Feed Motor 1 - drives wire
* z Bend Motor - performs 3D bends by rotating entire assembly
* bend Motor - performs 2D bends by bending wire over
* benderPin - solenoid
*/
// pin assignments
// Motor pulse and solenoid pins
const int bendMotorPls = 9;
const int zMotorPls = 10;
const int feedMotorPls = 11;
const int benderPin = 12;
// AWO pins to allow motor shaft to free spin
const int bendMotorAWO = 3;
const int zMotorAWO = 4;
const int feedMotorAWO = 5;
// Direction pins to select drive direction
const int bendMotorDir = 6;
const int zMotorDir = 7;
const int feedMotorDir = 8;
//misc program constants
const int pulseWidth = 20;
const int pulseDelay = 700;
// Drive directions in english
boolean ccw = true; // counter-clockwise drive direction
boolean cw = false; // clockwise drive direction
boolean curDir = cw; // resets direction before next angle command
int lastbend = 0;
int fieldindex=0;
int values[300]; //creates array
void setup() {
Serial.begin (9600); //com port communication
pinMode (bendMotorPls, OUTPUT); //Declaring motor pins as out
pinMode (zMotorPls, OUTPUT);
pinMode (feedMotorPls, OUTPUT);
pinMode (bendMotorAWO, OUTPUT);
pinMode (zMotorAWO, OUTPUT);
pinMode (feedMotorAWO, OUTPUT);
pinMode (bendMotorDir, OUTPUT);
pinMode (zMotorDir, OUTPUT);
pinMode (feedMotorDir, OUTPUT);
pinMode (benderPin, OUTPUT);
digitalWrite (bendMotorPls, LOW); //starts with everything off
digitalWrite (zMotorPls, LOW);
digitalWrite (feedMotorPls, LOW);
digitalWrite (benderPin, LOW);
digitalWrite (zMotorAWO, HIGH);
digitalWrite (feedMotorAWO, HIGH);
digitalWrite (bendMotorAWO, HIGH);
}
void bend (float angle) { //bender pin movement
if (angle!=0){ //sets direction of bend based on + or - angle
Serial.println("bending");
Serial.println(angle);
boolean dir=cw;
boolean back=ccw;
if (angle<0){
dir = ccw;
back = cw;
}
float rotations = 0;
angle = abs(angle);
if (angle <= 90){
angle = -.0012*angle*angle+.5959*angle+.2452; //converts angle into calibrated motor steps
angle = 6000 * (angle/360)+220;
}
else if (91 <= angle <= 120){
angle = .0044*angle*angle-.5481*angle+57.981; //converts angle into calibrated motor steps
angle = 5960 * (angle/360)+220;
}
else if (121<=angle<=180){
angle = angle-63.26; //converts angle into calibrated motor steps
angle = 5960 * (angle/360)+220;
} //calibration will differ depending on set up
rotations = angle;
// Serial.println (angle);
digitalWrite (bendMotorDir, dir);
for (int i=0; i <=rotations ; i++){ //moves bender bin the desired angle
digitalWrite(bendMotorPls, HIGH);
delayMicroseconds (pulseWidth);
digitalWrite(bendMotorPls, LOW);
delayMicroseconds (pulseDelay);
}
delay (100);
digitalWrite (bendMotorDir, back); //moves bender pin back to home position ready for next feed
for (int i=0; i <=rotations ; i++){
digitalWrite(bendMotorPls, HIGH);
delayMicroseconds (pulseWidth);
digitalWrite(bendMotorPls, LOW);
delayMicroseconds (pulseDelay);
}
}
}
void rotatePin (boolean dir, float angle) { //moves bender pin during duck. direction specified from duck subroutine
float rotations = 0;
angle = 6000 * (angle/360); //converts angle to steps
rotations = angle;
Serial.println (dir);
digitalWrite (bendMotorDir, dir);
for (int i=0; i <=rotations ; i++){ //rotates bender motor appropriate number of steps
digitalWrite(bendMotorPls, HIGH);
delayMicroseconds (pulseWidth);
digitalWrite(bendMotorPls, LOW);
delayMicroseconds (pulseDelay);
}
}
void zbend (float angle) { //rotates z motor
if (angle!=0){
Serial.println("Z bending");
Serial.println(angle);
boolean dir=cw;
if (angle<0){ //+ angle is clockwise - angle is counter clockwise
dir = ccw;
}
float rotations = 0;
angle = abs(angle);
angle = (2000 * angle)/360; //converts angle to motor steps
rotations = angle;
digitalWrite (zMotorDir, dir); //sets motor direction
for (int i=0; i <=rotations ; i++){ //rotates z motor appropriate number of steps
digitalWrite(zMotorPls, HIGH);
delayMicroseconds (pulseWidth);
digitalWrite(zMotorPls, LOW);
delayMicroseconds (2000);
}
}
}
void feed (float dist) { //feeds wire
if (dist != 0){
Serial.println("feeding");
Serial.println(dist);
float rotations = 0;
float feedCalib = 25.4*PI; //feed mm per revolution of motor
dist = 2000 * dist/feedCalib; //dist converted from mm to steps
rotations = dist;
digitalWrite (feedMotorDir, 1); //feed motor only moves forward
for (int i=0; i <=rotations ; i++){ //rotate feed motor appropriate number of steps
digitalWrite(feedMotorPls, HIGH);
delayMicroseconds (pulseWidth);
digitalWrite(feedMotorPls, LOW);
delayMicroseconds (pulseDelay);
}
}
}
void duck (){ //ducks bender pin under wire
digitalWrite (benderPin, HIGH);
delay (100);
rotatePin (curDir, 45);
digitalWrite (benderPin, LOW); //pin down move under wire
curDir = !curDir; //direction reversed for next duck
}
void loop() {
int copies = 0;
while (Serial.available ()){ //starts once serial entry made
digitalWrite (bendMotorAWO, LOW);
digitalWrite (zMotorAWO, LOW);
digitalWrite (feedMotorAWO, LOW);
int in = Serial.read();
byte incoming = in+128; //converts bytes from processing
if (incoming != 255){ //255 signifies end of incoming array
Serial.println (Serial.read());
values[fieldindex] = values[fieldindex]*10+incoming; //fills array fieldindex from incoming processing
fieldindex++;
}
else{
Serial.println("END"); //if array end marker inputs from processing end
for (int i=0; i<=fieldindex;i++){
Serial.println(values[i]-128);
}
copies=copies+1;
}
}
if (copies==1){ //once bend is complete eject shape
delay (1000);
motorrun();
feed(50); //eject
copies=copies+1;
}
}
void motorrun(){
int lastbend=0;
for (int i=0; i<= fieldindex;i++){
delay (100);
if ((values[i]-128)==126){ //convert bytes from processing and look for feed motor marker
feed (values[i+1]-128); //if feed motor marker detected next value in array is a feed length
}
else if ((values[i]-128)==125){ //convert bytes from processing and look for bend motor marker
int bendAng = (values[i+1]-128); //if bend motor marker detected next value in array is a bend angle
if ((bendAng<0&&curDir==cw) || (bendAng>0 && curDir ==ccw)){ //if incoming bend angle is opposite direction from previous angle duck pin
duck ();
delay (100);
}
bend (bendAng);
lastbend = bendAng;
}
else if ((values[i]-128)==124){ //convert bytes from processing and look for z motor marker
zbend (values[i+1]-128); //if z motor marker detected next value in array is z bend angle
}
}
}
All credit for the above code goes to PENSA