mardi 22 mars 2011

How to send commands to a plotter?

We use a serial connection, so we need software to talk through this serial port. This can be done in various languages.
We use Processing. ( www.processing.org )

But we also need a driver for the USB serial Converter, we use a generic one Prolific2303:
http://sourceforge.net/projects/osx-pl2303/

In the processing examples we find in File ->Examples ->Library ->Serial the sketch for sending commands using a serial port.

In the coding we connect to a certain port from the list Processing finds.
We get this list like this:

println(  Serial.list() );



Then we have to choose from this list: (this list can vary, then we hav to choose another number from the list...)

 String portName = Serial.list()[0];



and initialize this connection.

  myPort = new Serial(this, portName, 9600);


And then we can simply send commands like this:

myPort.write("PU;");


The next step is to shift these raw commands to a library and just use our very own functions:
for example

myLine ( point1, point2 );



the myline being a function using the raw commands.

Remark: Processing sends the commands way too fast for the plotter, so I had to add a delay(250) in the code, which makes the plotter very happy. Maybe we could speed it up to its limit in a few experiments...

So the plotter function in my lib is this:


void plotterLine(float x1, float y1, float x2, float y2) {
println("plotterline");
myPort.write("PU");
myPort.write("PA" + str(x1) + "," + str(y1) +";");
myPort.write("PD");
myPort.write("PA" + str(x2) + "," + str(y2) +";");
myPort.write("PU");
}


And this is where I call the plotterLine, because I first check the screen result varying with mouseX and mouseY before plotting, I have added a flag ( printOnce) so that when I decide to print (in my code press the key 'p' that the plotter gets the drawing commands only once.

if ( printOnce == 1 ){
float scaleF = 20;
plotterLine ( int(x1*scaleF), int(y1*scaleF), int(x2*scaleF), int(y2*scaleF));
println ( str(x1*scaleF) + " " + str(y1*scaleF) + " " + str(x2*scaleF) + " " + str(y2*scaleF) );
delay(250); //otherwise the commands are coming in to fast
}



<img alt="" class="aligncenter" src="http://www.contrechoc.com/blogPics/plotter/pi10.jpg" title="plotterdesign" width="400" />

Aucun commentaire:

Enregistrer un commentaire