Processingのスケッチ変更点
Integration_MPU6050.pde以外は、 52章:3Dサンプルの統合のスケッチと同一となります。
Integration_MPU6050.pdeの以下のように変更しました。
//Integration_MPU6050
import processing.serial.*;
Serial myPort; // The serial port
float My=0;
float Mz=0;
float Mx=0;
float Py=0;
float Pz=0;
float Px=0;
import controlP5.*;
ControlP5 cp5;
DropdownList d1;
int mode;
float scale;
MyJet MyJet;//使用クラスの定義
Yacht Yacht;
Train Train;
Engin Engin;
BirdBox BirdBox;
GuardRail GuardRail;
SolarUnits SolarUnits;
TrussStructure TrussStructure;
void setup ()
{
size(1000, 800, P3D);
println(Serial.list());
myPort = new Serial(this, Serial.list()[1], 38400);
myPort.bufferUntil('\n');
myPort.write("B");
MyJet=new MyJet();//MyJetクラスのオブジェクトを生成
Yacht=new Yacht();
Train=new Train();
Engin=new Engin();
BirdBox=new BirdBox();
GuardRail=new GuardRail();
SolarUnits=new SolarUnits();
TrussStructure=new TrussStructure();
noStroke();
cp5 = new ControlP5(this);
cp5.addSlider("scale")
.setPosition(200,0)
.setSize(200,20)
.setRange(0,2)
.setColorLabel(color(0))
.setValue(1.0);
d1 = cp5.addDropdownList("List-d1")
.setPosition(0, 0)
.setSize(100,180)
.setItemHeight(20)
.setBarHeight(20)
.addItem("My-Jet", 0)
.addItem("Yacht", 1)
.addItem("Train", 2)
.addItem("Engin", 3)
.addItem("Bird-Box", 4)
.addItem("Guard-Rail", 5)
.addItem("Solar-Units", 6)
.addItem("Truss-Structure", 7)
;
}
void draw ()
{
pushMatrix();
background(0, 256, 256);
lights();
translate(width / 2, height / 2);
//rotateY(map(mouseX, 0, width, -PI/1, PI/1));
//rotateX(map(mouseY, 0, height, PI/1, -PI/1));
translate(Px,Pz,Py);
rotateX(Mx);
rotateY(Mz);
rotateZ(My);
scale = cp5.getController("scale").getValue();
mode=(int)cp5.getController("List-d1").getValue();
scale(scale,scale,scale);
switch(mode)
{
case(0):MyJet.MyJet();break;
case(1):Yacht.Yacht();break;
case(2):Train.Train();;break;
case(3):Engin.Engin();break;
case(4):BirdBox.BirdBox();break;
case(5):GuardRail.GuardRail();break;
case(6):SolarUnits.SolarUnits();break;
case(7):TrussStructure.TrussStructure();break;
}
popMatrix();
}
void controlEvent(ControlEvent theEvent)
{
if (theEvent.isController())
{
println(theEvent.getController()+"\t"+mode+"\t"+ scale);
}
}
void serialEvent(Serial myPort)
{
float r=0.2,Mxn,Myn;
String myString = myPort.readStringUntil('\n');
myString = trim(myString);
int sensors[] = int(split(myString, ','));
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++)
{
print(sensors[sensorNum] + "\t");
}
println();
Mxn=1.0*atan2(sensors[1],sensors[2]);//重力方向
Myn=1.0*atan2(sensors[0],sensors[2]);//重力方向
if(abs(Mxn) < 3.2 && abs(Myn) < 3.2)
{
if(sensors[6]==1)
{
Mx=Mx*(1-r)+r*Mxn;//重力方向
My=My*(1-r)+r*Myn;//重力方向
Mz=sensors[5]/100000.0;
}
else
{
Px=Px*(1-r)+400*r*Mxn;//重力方向
Pz=Py*(1-r)+1000*r*Myn;//重力方向
Py=sensors[5]/400.0;
}
}
myPort.write("A");
}