Escolar Documentos
Profissional Documentos
Cultura Documentos
*
*
*
*
*
*
*/
You should have received a copy of the GNU General Public License
along with DIWire. If not, see <http://www.gnu.org/licenses/>.
and http://creativecommons.org/licenses/by-sa/3.0/us/legalcode
No portion of this header can be removed from the code.
Now enjoy and start making something!
/* the following program uploads a svg file, draws the 2D shape, allows user to
set the print resolution,
calculates the feed and bend angles to print the 2D shape, and sends these va
lues to the arduino*/
//import these libraries from libraries folder
import processing.serial.*;
import geomerative.*;
Serial arduino;
//Declaring the objects being used to print
RShape shp;
RPoint[] points;
int maxArray = 255;
float[] angles = new float [maxArray];
float[] feeds = new float [maxArray];
float res = 10; //default resolution
int interimSteps = 1; //a counter for the preview
int steps =1;
int lastpty = 0;
boolean chngRes = false;
boolean preview = false;
void setup() {
size (400, 400); //creates window
background (255);
println(Serial.list());
arduino = new Serial(this, Serial.list() [0], 9600); //sets arduino usb port
// VERY IMPORTANT: Allways initialize the library in the setup
RG.init(this);
shp = RG.loadShape("bendMe.svg"); //loads file from folder
steps = int(shp.getCurveLength()/res);
smooth();
frameRate(24);
println ();
println ("SET THE SYSTEM TO HOME POSITION");
println ("PRESS MOUSE BUTTON TO SET RESOLUTION");
println ("PRESS:");
println ("1. PROCESS SHAPE");
println ("2. PREVIEW SHAPE");
println ("3. SEND SHAPE TO ARDUINO");
println ();
println ("RESOLUTION IS CURRENTLY = " + res);
println ("NUMBER OF STEPS = " + steps);
}
void draw() {
translate(width/4, height/4);
noFill();
}
}
else { //draws preview shape based on set resolution
background (255);
stroke (random (255), random (255), random (255));
strokeWeight (5);
pushMatrix();
noFill();
beginShape();
for (int i=1; i<=steps+2; i++) { //preview bend by relative angles
line (0, 0, 0, feeds[i-1]);
translate (0, feeds [i-1]);
rotate (-radians(angles [i+1])); //bend
}
preview = !preview;
popMatrix ();
}
}
void mousePressed() { //mouse press sets resolution based on mouse location
println ("RESOLUTION SET TO = " + res + " mm");
steps = int(shp.getCurveLength()/res);
println ("NUMBER OF STEPS = " + steps);
chngRes = !chngRes;
}
void keyPressed() { //If keys are pressed run corresbonding subroutine
switch (key) {
case '1': //calculates bend angles and feed lengths
calcs();
break;
case '2': //shows a shape preview based on set resolution
preview = !preview;
break;
case '3': //sends bend angles and feed lengths to the arduino for printing
sendArd ();
break;
}
}
//this subroutine accepts feedback from the arduino. It is not necessary but som
etimes needed to establish initial serial communication
/*void serialEvent (Serial p) {
String inString = arduino.readStringUntil ('\n');
if (inString !=null) {
println(inString);
}
}*/
void calcs () {
float lastAng=0;
float lastlastAng = 0;
println ( key );
println ();
( key );
();
( "Sending to Arduino" );
( "Printing" );
println ();
//send commands
for (int i = 2; i < steps+233; i++) {
//sends arduino feed marker followed by feed length
delay (100);
arduino.write (feedmotor);
delay (100);
arduino.write (byte(round (feeds[i-2])));
delay (100);
println (feeds[i-2]);
//sends arduino bend marker followed by bend angle
arduino.write (bendmotor);
delay (100);
arduino.write (byte(round (-angles[i])));
println (angles[i]);
}
println (end+128); //tells arduino the array of values in complete
arduino.write(end);
}