forked from diwire/DIWire-Bender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino.txt
231 lines (211 loc) · 7.38 KB
/
Arduino.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* DIWire Bender
* 3D Wire Bender by Pensa - www.PensaNYC.com
* Written by Marco Perry. Call (718) 855 - 5354 for questions or support.
* Drives on 3 Stepper Motors to bender wire in 3D space
*
*/
/*This program is to be uploaded onto the DIWire arduino.
Bend angles and feed lengths are imported from processing running on a computer plugged into the arduino.
This program takes these lengths and angles and moves the motors and solenoid accordingly.
*/
/*
* 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
}
}
}