Codes Processing
Version des codes Alpha (encore pas mal de bugs)
import processing.serial.*;
Serial myPort;
String numScanner = « »;
/// plateau
final int numberOfSteps = 40;
final int maxStepArduinoPlateau = 2270;
final int stepToSendPlateau = maxStepArduinoPlateau/numberOfSteps;
final int maxValueToTouchZero = 240;
int currentStep = 0;
/// head
final int numberOfHeightMeasures = 40;
final int maxStepArduinoHead = 4460;
final int stepToSendHead = maxStepArduinoHead/numberOfHeightMeasures;
int currentHeightStep = 0;
boolean stateLeftToRght = true;
boolean go = false;
boolean goPlateauTurn = false;
boolean goAndBack = true;
boolean finishHim = false;
/// x,y,z
float measuresXYZ[][][] = new float[numberOfSteps][numberOfHeightMeasures][3];
void setup() {
size(800,800,P3D);
// List all the available serial ports:
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
myPort = new Serial(this, Serial.list()[0], 9600);
// prepareLists();
}
void draw() {
int input = 0;
if (finishHim!=true) {
String myString = « »;
while (myPort.available() > 0) {
myString = myPort.readStringUntil(10);
if (myString != null) {
String[] a = splitTokens(myString);
input = int(a[0]);
if (input>0) go = true;
// println(input);
}
}
}
background(30);
///iterate scanner
noStroke();
if (go) {
if (goAndBack) {
if (currentHeightStep<numberOfHeightMeasures-1) {
currentHeightStep++;
}
else {
goPlateauTurn = true;
goAndBack = false;
}
}
else {
if (currentHeightStep>0) {
currentHeightStep–;
}
else {
goPlateauTurn = true;
goAndBack = !goAndBack;
}
}
if ((currentStep<numberOfSteps-1) && (goPlateauTurn==true)) {
currentStep++;
goPlateauTurn = false;
}
else {
if (currentStep==numberOfSteps-1)
finishHim = true;
}
// give info
String xStr = str(currentHeightStep*stepToSendHead) + « x »;
String yStr = str(currentStep*stepToSendPlateau) + « y »;
// println(xStr + » » + yStr);
myPort.write(xStr);
myPort.write(yStr);
println(currentStep + « plateau » + currentHeightStep + » head »);
//println(input);
//calculate angles
float currentStepAngle = (TWO_PI/numberOfSteps) * float(currentStep); //plateau
float currentHeightAngle = (PI/numberOfHeightMeasures) * float(currentHeightStep); ///head
// get coordinates
float z = calculateZ(input, currentHeightAngle);
float x = calculateDistanceFromCenter(input, currentHeightAngle);
float y = 0.0;
// rotate coordinates in to the good angle, Z is constant
float xp = sin(currentStepAngle)*x;
float yp = cos(currentStepAngle)*x;
// put values into the memory
measuresXYZ[currentStep][currentHeightStep][0] = xp;
measuresXYZ[currentStep][currentHeightStep][1] = yp;
measuresXYZ[currentStep][currentHeightStep][2] = z;
go = false;
}
/// RENDER
/// view transformations
rotateX(0.8);
translate(width/2,0,-height/2);
fill(255);
for (int i = 0; i<numberOfSteps; i++) {
for (int j = 0; j<numberOfHeightMeasures; j++) {
pushMatrix();
translate(measuresXYZ[i][j][0], measuresXYZ[i][j][1], measuresXYZ[i][j][2]);
ellipse(0,0, 5,5);
popMatrix();
}
}
}
float calculateZ(int in, float angle) {
int c = maxValueToTouchZero-in;
return float(c)*sin(angle);
}
float calculateDistanceFromCenter(int in, float angle) {
int c = in-maxValueToTouchZero;
return float(c)*cos(angle);
}
void prepareLists() {
for (currentStep = 0; currentStep<numberOfSteps; currentStep++) {
for (currentHeightStep = 0; currentHeightStep<numberOfHeightMeasures; currentHeightStep++) {
//calculate angles
float currentStepAngle = (TWO_PI/numberOfSteps) * float(currentStep); //plateau
float currentHeightAngle = (PI/numberOfHeightMeasures) * float(currentHeightStep); ///head
// get coordinates
float z = calculateZ(100, currentHeightAngle);
float x = calculateDistanceFromCenter(100, currentHeightAngle);
float y = 0.0;
// rotate coordinates in to the good angle, Z is constant
float xp = sin(currentStepAngle)*x;
float yp = cos(currentStepAngle)*x;
// put values into the memory
measuresXYZ[currentStep][currentHeightStep][0] = xp;
measuresXYZ[currentStep][currentHeightStep][1] = yp;
measuresXYZ[currentStep][currentHeightStep][2] = z;
}
}
}