// NavBall Code //https://www.instructables.com/How-to-Measure-Angle-With-MPU-6050GY-521/// library link for MPU6050 or GY-521// https://github.com/jarzebski/Arduino-MPU6050#include<Wire.h> // wire libraryconstintMPU_addr=0x68;// MPU addressint16_tAcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;// 16 bit data arrayintminVal=265;intmaxVal=402;doublex;doubley;doublez;voidsetup(){Serial.begin(9600);Wire.begin();Wire.beginTransmission(MPU_addr);Wire.write(0x6B);Wire.write(0);Wire.endTransmission(true);}voidloop(){Wire.beginTransmission(MPU_addr);Wire.write(0x3B);Wire.endTransmission(false);Wire.requestFrom(MPU_addr,14,true);AcX=Wire.read()<<8|Wire.read();AcY=Wire.read()<<8|Wire.read();AcZ=Wire.read()<<8|Wire.read();intxAng=map(AcX,minVal,maxVal,-90,90);intyAng=map(AcY,minVal,maxVal,-90,90);intzAng=map(AcZ,minVal,maxVal,-90,90);x=RAD_TO_DEG*(atan2(-yAng,-zAng)+PI);y=RAD_TO_DEG*(atan2(-xAng,-zAng)+PI);z=RAD_TO_DEG*(atan2(-yAng,-xAng)+PI);Serial.print(x);Serial.print(" ");Serial.print(y);Serial.print(" ");Serial.print(z);Serial.print(" ");Serial.print("\n");}
Processing - Navi
Processing
Added the other half of the sphere, made it two different colors. and went from there. Feel free to comment your updates to this. A little shaky, but it's fine. Azimuth doesn't work for now.
// it all started here....//https://forum.processing.org/two/discussion/13353/creating-arc-sections-in-3dimportorg.firmata.*;importcc.arduino.*;importprocessing.serial.*;importjava.awt.event.KeyEvent;importjava.io.IOException;importprocessing.opengl.*;SerialmyPort;Stringdata="";floatPitch;// replaces roll and pitch floatBank;floatAzimuth;floatPhi;//Dimensional axisfloatTheta;floatPsi;floatradius=250.0;floatrho=radius;floatfactor=TWO_PI/40.0;// detail is 40floatx,y,z;PVector[]sphereVertexPoints;////////////////////////////////////////////////////////////////////////voidsetup(){size(400,400,P3D);smooth();myPort=newSerial(this,"COM4",9600);// starts the serial communication myPort.bufferUntil('\n');}////////////////////////////////////////////////////////////////////voiddraw(){background(0);stroke(100);strokeWeight(4);fill(200);rect(130,10,150,50,7);// (x,y,w,h, round corner dia)fill(CLOSE);fill(0);rect(150,30,70,25,7);fill(CLOSE);strokeWeight(CLOSE);// very useful fill(CLOSE);stroke(CLOSE);fill(0);textSize(14);text("Altitude: ",150,50,50);fill(CLOSE);floaty1=y++;floatAlt=abs(Pitch/25+y1*10);fill(0,255,255);text(Alt,160,70,50);// alt, x,y,zfill(CLOSE);fill(0,0,255);textSize(12);text(" m/s",220,70,50);fill(CLOSE);translate(width/2,height/2,-250);// camera placementlights();textLayer();MakeAnglesDependentOnMPU6050();fill(100);circle(0,0,700);// dark gray circle fill(CLOSE);fill(150);circle(0,0,670);// light grey circle fill(CLOSE);beginShape();fill(255,255,0);vertex(-14,-10,300);vertex(14,-10,300);// yellow triangle, each vertex is x and y coordsvertex(0,-20,300);vertex(0,-20,300);stroke(0);strokeWeight(2);stroke(CLOSE);endShape(CLOSE);beginShape();fill(255,255,0);stroke(0);strokeWeight(2);stroke(CLOSE);vertex(-50,-5,300);vertex(50,-5,300);// yellow linevertex(50,-10,300);vertex(-50,-10,300);fill(CLOSE);endShape(CLOSE);fill(210);arc(-120,0,380,525,PI/2,3*PI/2);// left gauge fillarc(120,0,-380,-525,PI/2,3*PI/2);// right gauge fillfill(CLOSE);//////////////////////////////////////////////////////////////pushMatrix();rotateX(radians(Bank));rotateZ(radians(Pitch));// comment out to change to mouserotateY(radians(Azimuth));// rotateX(radians(mouseY)); // comment in, to change to mouse. // rotateY(radians(mouseX));for(floatPHI=0.0;PHI<HALF_PI;PHI+=factor){beginShape(QUAD_STRIP);stroke(240);strokeWeight(1);for(floatTHETA=0.0;THETA<TWO_PI+factor;THETA+=factor){x=rho*sin(PHI)*cos(THETA);z=rho*sin(PHI)*sin(THETA);y=-rho*cos(PHI);vertex(x,y,z);x=rho*sin(PHI+factor)*cos(THETA);z=rho*sin(PHI+factor)*sin(THETA);y=-rho*cos(PHI+factor);vertex(x,y,z);fill(100,100,255);}endShape(CLOSE);}for(floatphi=0.0;phi<HALF_PI;phi+=factor){beginShape(QUAD_STRIP);for(floattheta=0.0;theta<TWO_PI+factor;theta+=factor){x=rho*sin(phi)*cos(theta);z=rho*sin(phi)*sin(theta);y=-rho*cos(phi);vertex(-x,-y,-z);x=rho*sin(phi+factor)*cos(theta);z=rho*sin(phi+factor)*sin(theta);y=-rho*cos(phi+factor);vertex(-x,-y,-z);fill(255,128,0);}endShape(CLOSE);}popMatrix();///////////////////////////////////////////////////////////////////// }// void draw//////////////////////////////////////////////////////////////////////////voidtextLayer(){MakeAnglesDependentOnMPU6050();/*pushMatrix(); rotateX(Bank/10); rotateY(-Pitch/10); rotateZ(Azimuth/10); fill(255); textSize(15); text( "50", 0, 110, 230); text( "90", 0, 180, 200); popMatrix(); *////////////////////////////////////////////////////////// pushMatrix();floaty1=y++;floatalt=abs(Pitch/25+y1*10+90);beginShape();rotateZ(alt);stroke(255,255,0);// throttle measurement yellowstrokeWeight(8);line(0,0,-310,0);stroke(CLOSE);endShape();strokeWeight(CLOSE);popMatrix();///////////////////////////////////////////////////////////////////////////// intradius=300;intlines=5*17;for(inta=120;a<240;a=a+360/lines){floatx=radius*cos(radians(a));// gauge lines yeah baby floaty=radius*sin(radians(a));stroke(50);line(0,0,x,y);line(0,0,-x,-y);strokeWeight(3);stroke(CLOSE);}/////////////////////////////////////////////////////////// for(inta=120;a<160;a=a+360/lines){floatx=radius*cos(radians(a));// red gauge lines yeah baby floaty=radius*sin(radians(a));stroke(255,0,0);line(0,0,-x,-y);strokeWeight(3);stroke(CLOSE);}for(inta=160;a<190;a=a+360/lines){floatx=radius*cos(radians(a));// orange gauge lines yeah baby floaty=radius*sin(radians(a));stroke(255,128,0);line(0,0,-x,-y);strokeWeight(3);stroke(CLOSE);}for(inta=220;a<240;a=a+360/lines){floatx=radius*cos(radians(a));// red gauge max throttle floaty=radius*sin(radians(a));stroke(255,0,0);strokeWeight(3);line(0,0,x,y);stroke(CLOSE);}////////////////////////////////////////////////////////////////}///////////////////////////////////////////////////////////////////////voidserialEvent(Serialmyport){//Reading the datas by Processing. Stringinput=myport.readStringUntil('\n');if(input!=null){input=trim(input);String[]values=split(input," ");if(values.length==3){floatphi=float(values[0]);floattheta=float(values[1]);floatpsi=float(values[2]);print(phi);print(theta);println(psi);Phi=phi;Theta=theta;Psi=psi;}}}///////////////////////////////////////////////////////////////////////voidMakeAnglesDependentOnMPU6050(){Bank=round(-Theta);Pitch=round(-Phi-3);Azimuth=round(Psi/700);// Bank = -Phi/10; // Pitch = Theta/10; // use these values for the ADXL345 // Azimuth = Psi/10;}/////////////////////////////////////////////////////////////////////////
Comments
Please log in or sign up to comment.