//sketch_3D_Engin_MPU6050 import processing.serial.*; Serial myPort; // The serial port Part P;//使用クラスの定義 int gCT=0; float My=0; float Mz=0; float Mx=0; void setup () { size(740, 660, P3D); println(Serial.list()); myPort = new Serial(this, Serial.list()[1], 38400); myPort.bufferUntil('\n'); P=new Part();//My_OBクラスのオブジェクトを生成 } void draw () { colorMode(HSB,100,100,100,100); background(0,0,100); lights(); noStroke(); fill(240, 240, 230); translate(width / 2, height / 2); translate(mouseX-width / 2,0,200+mouseY- height / 2); rotateX(Mx); rotateY(Mz); rotateZ(My); int N=64;//角度分割数 float Ai=4*PI*gCT/N; float R=20; float L=85; float Aj=asin(R*sin(Ai)/L); float Yg=R*sin(Ai)/2; float Xg=R*cos(Ai)+L*cos(Aj)/2; float Xs=R*cos(Ai)+L*cos(Aj); gCT++;if(gCT>N){gCT=0;} scale(1.5,1.5,1.5); rotateX(-90*PI/180); pushMatrix(); translate(0,0,-Xs); fill(60,100, 100,100); P.Part2();//ピストン popMatrix(); pushMatrix(); //translate(0,0,-62.5); fill(40,50, 100,100); translate(-Yg,0,-Xg); rotateY(-Aj); P.Part3();//リンク popMatrix(); pushMatrix(); fill(70,20, 70,100); rotateY(Ai); P.Part4();//クランク popMatrix(); pushMatrix(); fill(gCT/2,100,100,50); translate(0,0,-(145+30+Xs)/2); P.Z_Half_Circle(30,145-30-Xs);//燃焼ガス popMatrix(); fill(50,80,40,70); P.Part1();//シリンダ } void serialEvent(Serial myPort) { String myString = myPort.readStringUntil('\n'); myString = trim(myString); float sensors[] = float(split(myString, ',')); for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) { print(sensors[sensorNum] + "\t"); } println(); Mx=sensors[3]/10000; My=sensors[4]/10000; Mz=sensors[5]/10000; myPort.write("A"); } //部品 class Part { void Part4()//クランク { pushMatrix(); rotateX(-90*PI/180); pushMatrix(); translate(0,0,15); Z_Bar(30,10); translate(0,0,37.5); Z_Bar(5,65); popMatrix(); translate(0,20,0); Z_Bar(5,20); popMatrix(); } void Part3()//リンク { int Pn=9;//ポイント数 float[] X={5,10,10,5,-5,-10,-10,-5,5,5.5}; float[] Y={52.5,47.5,-47.5,-52.5,-52.5,-47.5,47.5,52.5,52.5}; float L=10; pushMatrix(); rotateX(90*PI/180); Z_Side(Pn,X,Y,L);//側面 translate(0,0,-L/2); Z_Polyon(Pn,X,Y);//多角形 translate(0,0,L); Z_Polyon(Pn,X,Y);//多角形 popMatrix(); } void Part2()//ピストン { pushMatrix(); rotateX(90*PI/180); Z_Bar(5,60); popMatrix(); pushMatrix(); translate(0,0,5); Z_Half_Pipe(30,20,50);//Half_Pipe 半パイプ translate(0,0,-30); Z_Half_Circle(30,10);//Half_Circle 半円板 popMatrix(); } void Part1()//シリンダ { pushMatrix(); translate(0,0,-2.5); Z_Half_Pipe(50,40,75);//Half_Pipe 半パイプ translate(0,0,-90); Z_Half_Pipe(40,30,125);//Half_Pipe 半パイプ translate(0,0,-57.5); Z_Half_Circle(40,10);//Half_Circle 半円板 popMatrix(); pushMatrix(); //void Z_Pipe(float Ro,float Ri,float L)//パイプ rotateX(90*PI/180); translate(0,0,-40); Z_Pipe(15,5,40); popMatrix(); } void Z_Half_Pipe(float R1,float R2,float L)//Half_Pipe 半パイプ { int Pn1=8;//R1,R2分割数 int Pn=2*Pn1+3;//全ポイント数 float[] X=new float[Pn]; float[] Y=new float[Pn]; int i; float Ai; for(i=0;i<=Pn1;i++) { Ai=i*PI/Pn1; X[i]=R1*cos(Ai); Y[i]=R1*sin(Ai); } for(i=0;i<=Pn1;i++) { Ai=PI-i*PI/Pn1; X[i+Pn1+1]=R2*cos(Ai); Y[i+Pn1+1]=R2*sin(Ai); } X[2*Pn1+2]=R1;Y[2*Pn1+2]=0; pushMatrix(); Z_Side(Pn,X,Y,L);//側面 translate(0,0,-L/2); Z_Polyon(Pn,X,Y);//多角形 translate(0,0,L); Z_Polyon(Pn,X,Y);//多角形 popMatrix(); } void Z_Half_Circle(float R1,float L)//Half_Circle 半円板 { int Pn1=8;//R1分割数 int Pn=Pn1+2;//全ポイント数 float[] X=new float[Pn]; float[] Y=new float[Pn]; int i; float Ai; for(i=0;i<=Pn1;i++) { Ai=i*PI/Pn1; X[i]=R1*cos(Ai); Y[i]=R1*sin(Ai); } X[Pn1+1]=R1;Y[Pn1+1]=0; pushMatrix(); Z_Side(Pn,X,Y,L);//側面 translate(0,0,-L/2); Z_Polyon(Pn,X,Y);//多角形 translate(0,0,L); Z_Polyon(Pn,X,Y);//多角形 popMatrix(); } void Z_Bar(float R1,float L)//Bar 丸棒 { int Pn1=16;//R1分割数 int Pn=Pn1+2;//全ポイント数 float[] X=new float[Pn]; float[] Y=new float[Pn]; int i; float Ai; for(i=0;i<=Pn1;i++) { Ai=i*2*PI/Pn1; X[i]=R1*cos(Ai); Y[i]=R1*sin(Ai); } X[Pn1+1]=R1;Y[Pn1+1]=0; pushMatrix(); Z_Side(Pn,X,Y,L);//側面 translate(0,0,-L/2); Z_Polyon(Pn,X,Y);//多角形 translate(0,0,L); Z_Polyon(Pn,X,Y);//多角形 popMatrix(); } void Z_Side(int Pn,float X[],float Y[],float L)//側面 { int i; beginShape(QUAD_STRIP); for (i = 0; i < Pn; ++i) { vertex(X[i],Y[i], -L/2); vertex(X[i],Y[i], L/2); } endShape(CLOSE); } void Z_Polyon(int Pn,float X[],float Y[])//多角形 { int i; beginShape(); for (i = 0; i < Pn; ++i) { vertex(X[i],Y[i]); } endShape(CLOSE); } void Z_Pipe(float Ro,float Ri,float L)//パイプ { pushMatrix(); Z_Cylinder(Ro,L); Z_Cylinder(Ri,L); translate(0,0,-L/2); Z_Ring_2D(Ro,Ri); translate(0,0,L); Z_Ring_2D(Ro,Ri); popMatrix(); } void Z_Cylinder(float r,float L)//円筒 { int sides=16;//分割数 int i; beginShape(QUAD_STRIP); for (i = 0; i < sides + 1; ++i) { float angle = i*TWO_PI/sides; vertex(r*cos(angle), r*sin(angle), -L/2); vertex(r*cos(angle), r*sin(angle), L/2); } endShape(CLOSE); } void Z_Ring_2D(float Ro,float Ri) { int sides=16;//分割数 int i; beginShape(); for (i = 0; i < sides + 1; ++i) { float angle = i*TWO_PI/sides; vertex(Ro*cos(angle), Ro*sin(angle),0); } beginContour(); for (i = 0; i < sides + 1; ++i) { float angle = -i*TWO_PI/sides; vertex(Ri*cos(angle), Ri*sin(angle),0); } endContour(); endShape(CLOSE); } }