//TCP_Server_ADXL345_HMC5883_2 27章WiFi無線でprocessingの3Dサンプルを動かす import processing.net.*; int port = 13000; boolean myServerRunning = true; Server myServer; import processing.serial.*; Serial myPort; // The serial port float Mx=0; float My=0; float Mz=0; int gC=0;int gF=0;int gN=20; int[] Ax=new int[gN+1]; int[] Ay=new int[gN+1]; int[] Az=new int[gN+1]; float Dx=0,Dy=0,Dz=0; int gC0=0,gCmax=4; float Dxnew=0,Dynew=0,Dznew=0,Dxold,Dyold,Dzold; float Mxnew=0,Mynew=0,Mznew=0,Mxold,Myold,Mzold; int gM=0; void setup () { size(740, 660, P3D); myServer = new Server(this, port); // Starts a myServer on port 13000 } void draw () { int i; int Sx=0,Sy=0,Sz=0; background(0, 256, 256); Client thisClient = myServer.available(); if (thisClient != null) { if (thisClient.available() > 0) { String myString =thisClient.readString(); myString = trim(myString); int sensors[] = int(split(myString, ',')); int sensorNum; for (sensorNum = 0; sensorNum < sensors.length; sensorNum++) { print(sensors[sensorNum] + "\t"); } println(); int f; if(sensorNum==6){f=1;} else { f=0; thisClient.write("BT"+gM+"\r\n"); } if(f==1) { Mxold=Mxnew;Mxnew=atan2(sensors[0],sensors[2]); Myold=Mynew;Mynew=atan2(sensors[1],sensors[2]); Mzold=Mznew;Mznew=atan2(sensors[4],sensors[3]); if(abs(Mxnew)>abs(Mynew)) { if(abs(Mxnew)<0.2) {gM=0;} else { if(abs(Mxnew)<0.4) {gM=3;} else{gM=1;} } } else { if(abs(Mynew)<0.2) {gM=0;} else { if(abs(Mynew)<0.4) {gM=4;} else{gM=2;} } } Ax[gC]=sensors[0]; Ay[gC]=sensors[1]; Az[gC]=sensors[2]; if(gF==1) { for(i=0;i gN) {gF=1;gC=0;} gCmax=gC0; gC0=0; } } } if(gF==1) { Mx=Mxold+(Mxnew-Mxold)*gC0/gCmax; My=Myold+(Mynew-Myold)*gC0/gCmax; Mz=Mzold+(Mznew-Mzold)*gC0/gCmax; Dx=Dxold+(Dxnew-Dxold)*gC0/gCmax; Dy=Dyold+(Dynew-Dyold)*gC0/gCmax; Dz=Dzold+(Dznew-Dzold)*gC0/gCmax; println(Mx+" " +My +" " +Mz+" "+Dx+" " +Dy +" " +Dz); } gC0++; lights(); translate(width / 2, height / 2,-200); rotateX(Mx); rotateY(Mz); rotateZ(My); noStroke(); fill(240, 240, 230); translate(0,0,100); translate(Dx,Dz,Dy); OB_A2();//Z軸回転体(胴体) OB_A3();//Z軸回転体(胴体先端) OB2();//主翼 OB3();//水平尾翼 OB4();//垂直尾翼 OB_A4();//Z軸回転体(エンジン) } void OB_A4()//Z軸回転体(エンジン) { int sides=16;//分割数 int Sn=6;//面数 float Pz[]={0,-1,-50,-60,-70,-70};//z座標 float R[]={15,20,20,15,15,1};//回転物半径 int i,j; float angleIncrement = TWO_PI/sides; pushMatrix(); translate(60, 36.3,-145); for(j=0;j