Orientation in 3D Program
import processing.serial.*;
import processing.opengl.*;
Serial port;
int getByte = -1; // Variable to hold keystoke values
int xRad,yRad = 0;
int xStart,yStart, zStart = 0;
int xDown, yDown, zDown = 0;
int XVal, YVal, ZVal = 0;
int XdataByteLSB = -1; // Variable to hold keystoke values
int XdataByteMSB = -1; // Variable to hold keystoke values
int YdataByteLSB = -1; // Variable to hold keystoke values
int YdataByteMSB = -1; // Variable to hold keystoke values
int ZdataByteLSB = -1; // Variable to hold keystoke values
int ZdataByteMSB = -1; // Variable to hold keystoke values
int XdataByte = -1; // Variable to hold keystoke values
int YdataByte = -1; // Variable to hold keystoke values
int ZdataByte = -1; // Variable to hold keystoke values
int headerByte = 224; //
int loopHold = 0;
PFont fontA; // Font for printing
void setup(){
size(800,800, OPENGL);
background(0);
fontA = loadFont("CourierNewPSMT-24.vlw");
textFont(fontA, 24);
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
//lights();
// smooth();
}
int reconstruct(int MSB,int LSB) {
int val=0;
val = MSB << 7;
val = val + LSB;
return val;
}
void serialChecker(){
while (port.available() > 0) {
getByte = port.read();
if (getByte == headerByte) {
XdataByteLSB = port.read(); //get LSB
XdataByteMSB = port.read(); //get MSB
YdataByteLSB = port.read(); //get LSB
YdataByteMSB = port.read(); //get MSB
ZdataByteLSB = port.read(); //get LSB
ZdataByteMSB = port.read(); //get MSB
}
}
}
void keyPressed(){
if (keyCode == DOWN){
xDown = XVal;
yDown = YVal;
zDown = ZVal;
}
if (keyCode == UP){
xStart = XVal;
yStart = YVal;
zStart = ZVal;
}
}
void orienatate(){
float X = map(XVal, xStart, xDown, 0, 180);
float Y = map(YVal, yStart, yDown, 0, 180);
float Z = map(ZVal, zStart, zDown, 0, 180);
pushMatrix();
translate(width/2,height/2,0);
X = radians(X);
Y = radians(Y);
Z = radians(Z);
rotateZ(X);
rotateY(Y);
rotateX(Z);
box(100,10,100);
popMatrix();
//background(0);
}
void draw(){
if (xStart < 0 || yStart < 0 || zStart < 0) {
serialChecker();
xStart = reconstruct(XdataByteMSB, XdataByteLSB);
yStart = reconstruct(YdataByteMSB, YdataByteLSB);
zStart = reconstruct(ZdataByteMSB, ZdataByteLSB);
}
serialChecker();
XdataByte = reconstruct(XdataByteMSB, XdataByteLSB);
YdataByte = reconstruct(YdataByteMSB, YdataByteLSB);
ZdataByte = reconstruct(ZdataByteMSB, ZdataByteLSB);
if (XdataByte >= 0 && XdataByte <= 1024){
XVal = XdataByte;
}
if (YdataByte >= 0 && YdataByte <= 1024){
YVal = YdataByte;
}
if (ZdataByte >= 0 && ZdataByte <= 1024){
ZVal = ZdataByte;
}
if (loopHold > 25){
background(0);
text("X Value: ",100,100);
text(XVal,250,100);
text(xStart, 350, 100);
text((xStart - XVal), 450, 100);
text("Y Value: ",100,150);
text(YVal,250,150);
text(yStart, 350, 150);
text((yStart - YVal), 450, 150);
text("Z Value: ",100,200);
text(ZVal,250,200);
text(zStart, 350, 200);
text((zStart - ZVal), 450, 200);
orienatate();
loopHold = 0;
}
loopHold++;
}
import processing.opengl.*;
Serial port;
int getByte = -1; // Variable to hold keystoke values
int xRad,yRad = 0;
int xStart,yStart, zStart = 0;
int xDown, yDown, zDown = 0;
int XVal, YVal, ZVal = 0;
int XdataByteLSB = -1; // Variable to hold keystoke values
int XdataByteMSB = -1; // Variable to hold keystoke values
int YdataByteLSB = -1; // Variable to hold keystoke values
int YdataByteMSB = -1; // Variable to hold keystoke values
int ZdataByteLSB = -1; // Variable to hold keystoke values
int ZdataByteMSB = -1; // Variable to hold keystoke values
int XdataByte = -1; // Variable to hold keystoke values
int YdataByte = -1; // Variable to hold keystoke values
int ZdataByte = -1; // Variable to hold keystoke values
int headerByte = 224; //
int loopHold = 0;
PFont fontA; // Font for printing
void setup(){
size(800,800, OPENGL);
background(0);
fontA = loadFont("CourierNewPSMT-24.vlw");
textFont(fontA, 24);
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
//lights();
// smooth();
}
int reconstruct(int MSB,int LSB) {
int val=0;
val = MSB << 7;
val = val + LSB;
return val;
}
void serialChecker(){
while (port.available() > 0) {
getByte = port.read();
if (getByte == headerByte) {
XdataByteLSB = port.read(); //get LSB
XdataByteMSB = port.read(); //get MSB
YdataByteLSB = port.read(); //get LSB
YdataByteMSB = port.read(); //get MSB
ZdataByteLSB = port.read(); //get LSB
ZdataByteMSB = port.read(); //get MSB
}
}
}
void keyPressed(){
if (keyCode == DOWN){
xDown = XVal;
yDown = YVal;
zDown = ZVal;
}
if (keyCode == UP){
xStart = XVal;
yStart = YVal;
zStart = ZVal;
}
}
void orienatate(){
float X = map(XVal, xStart, xDown, 0, 180);
float Y = map(YVal, yStart, yDown, 0, 180);
float Z = map(ZVal, zStart, zDown, 0, 180);
pushMatrix();
translate(width/2,height/2,0);
X = radians(X);
Y = radians(Y);
Z = radians(Z);
rotateZ(X);
rotateY(Y);
rotateX(Z);
box(100,10,100);
popMatrix();
//background(0);
}
void draw(){
if (xStart < 0 || yStart < 0 || zStart < 0) {
serialChecker();
xStart = reconstruct(XdataByteMSB, XdataByteLSB);
yStart = reconstruct(YdataByteMSB, YdataByteLSB);
zStart = reconstruct(ZdataByteMSB, ZdataByteLSB);
}
serialChecker();
XdataByte = reconstruct(XdataByteMSB, XdataByteLSB);
YdataByte = reconstruct(YdataByteMSB, YdataByteLSB);
ZdataByte = reconstruct(ZdataByteMSB, ZdataByteLSB);
if (XdataByte >= 0 && XdataByte <= 1024){
XVal = XdataByte;
}
if (YdataByte >= 0 && YdataByte <= 1024){
YVal = YdataByte;
}
if (ZdataByte >= 0 && ZdataByte <= 1024){
ZVal = ZdataByte;
}
if (loopHold > 25){
background(0);
text("X Value: ",100,100);
text(XVal,250,100);
text(xStart, 350, 100);
text((xStart - XVal), 450, 100);
text("Y Value: ",100,150);
text(YVal,250,150);
text(yStart, 350, 150);
text((yStart - YVal), 450, 150);
text("Z Value: ",100,200);
text(ZVal,250,200);
text(zStart, 350, 200);
text((zStart - ZVal), 450, 200);
orienatate();
loopHold = 0;
}
loopHold++;
}
0 Comments:
Post a Comment
<< Home