Buy MPU6050 self-contained attitude solver greatly reduces the work done by the upper processor. By getting acquainted with the processing after doing a small example is to feel the magic of this sensor.
Arduino section
The main is to read MPU6050 data and the acquisition of Euler angle through the serial port printing to the host computer, the acquisition of data is very simple, MPU6050 received the Arduino mega2560 Serial1 can receive data, and then transmitted to the computer via serial. MPU6050 has a Kalman filter, so the upper layer is directly using the data, the observed error is really very small, very practical sensor.
Note: The MPU6050 uses the serial port mode, the configuration when the power is saved, the baud rate is 9600
#include <wire.h>byte a[11];void setup () {serial.begin (115200); Serial1.begin (9600); Serial.println ("initial ..."); Serial1.write (0xFF); Serial1.write (0XAA); Serial1.write (0x52); Serial1.flush (); Serial.println ("End");} static int Counter=0;char buffer[20];void loop () {if (counter==0&&a[0]==0x55) {int index=2;switch (a[1]) {case 0x53:{float xa= (a[index]| ( a[index+1]<<8))/32768.0*180;float ya= (a[index+2]| ( a[index+3]<<8))/32768.0*180;float za= (a[index+4]| ( a[index+5]<<8))/32768.0*180;//if (ABS (temp-97.77) <3) break;//sprintf (buffer, "%lf,", XA); Serial.print ("x="); Serial.println (XA);//sprintf (buffer, "%lf,", ya); Serial.print ("y="); Serial.println (ya);//sprintf (buffer, "%LF", za); Serial.print ("z="); Serial.println (za); A[0]=0;delay (50);} Break;default:break;}}} void SerialEvent1 () {while (serial1.available ()) {a[counter]=serial1.read (); if (counter==0&&a[counter]!= 0x55) return;counter= (counter+1)%11;}} void Serialevent () {char buf[255];int i=0;while (serial.available ()) Buf[i++]=seriAl.read (); Buf[i]=0;if (strcmp ("Stop", buf) ==0) serial1.end (); else if (strcmp ("Run", buf) ==0) Serial1.begin (115200);}
MPU6050 's four lines are attached to the Arduino to receive data, uploaded to the computer in the format:
x=12.44
y=56.01
z=67.34
The following processing program will parse the received data according to this and adjust the posture.
Processing part
Processing This program was first heard yesterday, see the foreign video of the model is really comfortable to look at, so also want to try. Although the official network called processing as a language, but the personal sense of grammar is java+opengl+p3d+c++ mixed. Transmission Door
Through the Arduino serial port sent out the message, in the processing receive processing, there is not much to say, the general feeling under
Processing
Import Processing.serial.*;import processing.opengl.*; Serial myserial;float value[]=new float[4]; String Rf[]=new string[4]; Pgraphics GRAPHICS1; Pgraphics GRAPHICS2; Pgraphics GRAPHICS3; Pgraphics graphics4;void Setup () {size (600,600,p2d); String name= "COM5"; Myserial=new Serial (this,name,115200); Smooth (); for (int i=0;i<4;++i) value[i]=0; Graphics1=creategraphics (298,298,OPENGL); Graphics2=creategraphics (298,298,OPENGL); Graphics3=creategraphics (298,298,OPENGL); Graphics4=creategraphics (298,298,OPENGL); TextMode (MODEL); TextSize (32);} Long Lasttime=0;void Draw () {framerate (60); DrawRect (graphics1,0,0,1,0); FONT DrawRect (graphics2,90,0,1,0);//lleft drawrect (graphics3,-90,1,0,0);//top drawrect (graphics4,90,1,1,0);// RANDOM image (graphics1,0,0); Image (graphics2,300,0); Image (GRAPHICS3,0,300); Image (GRAPHICS4,300,300); Fill (248,147,147); Text ("FONT VIEW", 10,50); Text ("Left VIEW", 310,50); Text ("TOP VIEW", 10,350); Text ("RANDOM VIEW", 310,350); Lasttime=Millis ();} void DrawRect (pgraphics pg,float rot,int x,int y,int z) {Pg.begindraw (); Pg.lights (); Pg.background (126); Pg.textsize (20); Pg.fill (3,60,244); Long Framerate=1000/(Millis ()-lasttime); Pg.text ("fps:" +framerate,200,20); Pg.fill (246,225,65); Pg.translate (100,100,-100); Pg.rotate (ROT*PI/180,X,Y,Z); Pg.rotatey (value[2]*pi/180); Pg.rotatez (-value[1]*pi/180); Pg.rotatex (value[0]*pi/180); Pg.box (50,50,100); Pg.enddraw ();} String st;void serialevent (Serial p) {st=p.readstringuntil (10); if (st==null) return; if (St.indexof ("x=")!=-1) {value[0]=float (st.substring (2)); } else if (St.indexof ("y=")!=-1) {value[1]=float (st.substring (2)); } else if (St.indexof ("z=")!=-1) {value[2]=float (st.substring (2)); }}
Also made a demo video click
Arduino and MPU6050 attitude Solver + processing