使用Processing 软件, 通过 arduino 输入 电子罗盘的数据,通过PC端进行校准,程序如下:
import processing.serial.*; Serial myPort; ArrayList<Float> xList = new ArrayList<Float>(); ArrayList<Float> yList = new ArrayList<Float>(); ArrayList<Float> zList = new ArrayList<Float>(); float minX,maxX; float minY,maxY; float minZ,maxZ; float moX,moY; float count; void setup() { size(600, 600, P3D); myPort = new Serial(this,"COM5", 38400); myPort.bufferUntil(10); } void draw() { background(100); float midX = (minX+maxX)/2; float midY = (minY+maxY)/2; float midZ = (minZ+maxZ)/2; if (mousePressed) { moX= mouseX; moY= mouseY; } //camera(moX,moY, (height/2) / tan(PI/5), midX, midY,midZ, 0, 1, 0); camera(moX,moY,(height/2) / tan(PI/5), width/2, height/2,0, 0, 1, 0); translate(width/2, height/2, -100); strokeWeight(2); // Default stroke(255); noFill();
//x
line(midX-100,midY,midZ,midX+100,midY,midZ);
//y
line(midX,midY+100,midZ,midX,midY-100,midZ);
//z
line(midX,midY,midZ-100,midX,midY,midZ+100);
//box(200); for(int i = 0;i<xList.size();i++){ point(xList.get(i),yList.get(i),zList.get(i)); } println(count); println("midX:"+midX+","+"midY"+midY+","+"midZ:"+midZ); } void serialEvent (Serial myPort) { float mX = 0; float mY = 0; float mZ = 0; String inString = myPort.readStringUntil(10); if (inString != null) { inString = trim(inString); String[] list = split(inString, ‘,‘); if(list.length ==4){ count = float(list[0]); mX = float(list[1])/100; mY = float(list[2])/100; mZ = float(list[3])/100; //------------------------- if(mX < minX){ minX = mX; } if(mX > maxX){ maxX = mX; } //---------------------------- if(mY < minY){ minY = mY; } if(mY > maxY){ maxY = mY; } //---------------------------- if(mZ > maxZ){ maxZ = mZ; } if(mZ < minZ){ minZ = mZ; } xList.add(mX); yList.add(mY); zList.add(mZ); } } }
arduino 代码如下:
int mx,my,mz; : : : 略 Serial.print(count); Serial.print(","); Serial.print(mx); //Inclination X axis (as measured by accelerometer) Serial.print(","); Serial.print(my); //Inclination X axis (estimated / filtered) Serial.print(","); Serial.print(mz); //Inclination X axis (estimated / filtered) Serial.println(""); count++;
PC端获取的效果如下图:
最终坐标系完全落入球内表示校准成功.
视频:
时间: 2024-10-24 03:09:38