Last active
June 2, 2022 06:32
-
-
Save doojinkang/7c2449e50f2954098189a3445017f6d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.serial.*; | |
import processing.opengl.*; | |
import toxi.geom.*; | |
import toxi.processing.*; | |
import java.nio.*; | |
ToxiclibsSupport gfx; | |
Serial port; // The serial port | |
char[] teapotPacket = new char[100]; // roll, pitch, yaw csv string from port | |
int serialCount = 0; // current packet byte position | |
int synced = 0; | |
int interval = 0; | |
float[] yprVal = new float[3]; | |
void setup() { | |
// 300px square viewport using OpenGL rendering | |
size(300, 300, OPENGL); | |
gfx = new ToxiclibsSupport(this); | |
gfx.origin(new Vec3D(), 100); | |
// show (0,0,0) ~ (1,0,0), (0,1,0), (0.0.1) | |
// setup lights and antialiasing | |
lights(); | |
smooth(); | |
// display serial port list for debugging/clarity | |
println(Serial.list()); | |
// get the first available port (use EITHER this OR the specific port code below) | |
String portName = Serial.list()[2]; | |
// get a specific serial port (use EITHER this OR the first-available code above) | |
//portName = "/dev/ttyUSB0"; | |
// open the serial port | |
port = new Serial(this, portName, 115200); | |
// send single character to trigger DMP init/start | |
// (expected by MPU6050_DMP6 example Arduino sketch) | |
port.write('1'); | |
} | |
void draw() { | |
if (millis() - interval > 1000) { | |
// resend single character to trigger DMP init/start | |
// in case the MPU is halted/reset while applet is running | |
port.write('1'); | |
interval = millis(); | |
} | |
// black background | |
background(0); | |
// translate everything to the middle of the viewport | |
pushMatrix(); | |
translate(width / 2, height / 2); | |
rotateX(-yprVal[1]); | |
rotateY(yprVal[0]); | |
rotateZ(-yprVal[2]); | |
// draw main body in red | |
fill(255, 0, 0, 200); | |
box(10, 10, 200); | |
// draw front-facing tip in blue | |
fill(0, 0, 255, 200); | |
pushMatrix(); | |
translate(0, 0, -120); | |
rotateX(PI/2); | |
drawCylinder(0, 20, 20, 8); | |
popMatrix(); | |
// draw wings and tail fin in green | |
fill(0, 255, 0, 200); | |
beginShape(TRIANGLES); | |
vertex(-100, 2, 30); vertex(0, 2, -80); vertex(100, 2, 30); // wing top layer | |
vertex(-100, -2, 30); vertex(0, -2, -80); vertex(100, -2, 30); // wing bottom layer | |
vertex(-2, 0, 98); vertex(-2, -30, 98); vertex(-2, 0, 70); // tail left layer | |
vertex( 2, 0, 98); vertex( 2, -30, 98); vertex( 2, 0, 70); // tail right layer | |
endShape(); | |
beginShape(QUADS); | |
vertex(-100, 2, 30); vertex(-100, -2, 30); vertex( 0, -2, -80); vertex( 0, 2, -80); | |
vertex( 100, 2, 30); vertex( 100, -2, 30); vertex( 0, -2, -80); vertex( 0, 2, -80); | |
vertex(-100, 2, 30); vertex(-100, -2, 30); vertex(100, -2, 30); vertex(100, 2, 30); | |
vertex(-2, 0, 98); vertex(2, 0, 98); vertex(2, -30, 98); vertex(-2, -30, 98); | |
vertex(-2, 0, 98); vertex(2, 0, 98); vertex(2, 0, 70); vertex(-2, 0, 70); | |
vertex(-2, -30, 98); vertex(2, -30, 98); vertex(2, 0, 70); vertex(-2, 0, 70); | |
endShape(); | |
popMatrix(); | |
} | |
void serialEvent(Serial port) { | |
interval = millis(); | |
while (port.available() > 0) { | |
int ch = port.read(); | |
if (ch != 10) { | |
teapotPacket[serialCount++] = (char)ch; | |
} | |
else { | |
String line = new String(teapotPacket, 0, serialCount-1); | |
String[] parsed = split(line, ','); | |
print(line); | |
print(" : " ); | |
if ( parsed.length == 3) { | |
yprVal[2] = float(parsed[0]); | |
yprVal[1] = float(parsed[1]); | |
yprVal[0] = float(parsed[2]); | |
print(yprVal[2]); | |
print(" " ); | |
print(yprVal[1]); | |
print(" " ); | |
println(yprVal[0]); | |
} | |
serialCount = 0; | |
} | |
} | |
} | |
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) { | |
float angle = 0; | |
float angleIncrement = TWO_PI / sides; | |
beginShape(QUAD_STRIP); | |
for (int i = 0; i < sides + 1; ++i) { | |
vertex(topRadius*cos(angle), 0, topRadius*sin(angle)); | |
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle)); | |
angle += angleIncrement; | |
} | |
endShape(); | |
// If it is not a cone, draw the circular top cap | |
if (topRadius != 0) { | |
angle = 0; | |
beginShape(TRIANGLE_FAN); | |
// Center point | |
vertex(0, 0, 0); | |
for (int i = 0; i < sides + 1; i++) { | |
vertex(topRadius * cos(angle), 0, topRadius * sin(angle)); | |
angle += angleIncrement; | |
} | |
endShape(); | |
} | |
// If it is not a cone, draw the circular bottom cap | |
if (bottomRadius != 0) { | |
angle = 0; | |
beginShape(TRIANGLE_FAN); | |
// Center point | |
vertex(0, tall, 0); | |
for (int i = 0; i < sides + 1; i++) { | |
vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle)); | |
angle += angleIncrement; | |
} | |
endShape(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment