<table width="100%" cellpadding="0" cellspacing="0" style="font-size: 13px; font-family: arial; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);"><tbody><tr><td align="right" class="eng" style="font-stretch: normal; font-size: 8pt; line-height: normal; padding: 5px;">Posted at 2016-11-06 23:19:18</td></tr><tr><td height="200" valign="top" id="contents" style="padding: 10px;"><table width="100%" style="font-size: 13px; font-family: arial; color: rgb(0, 0, 0); table-layout: fixed;"><tbody><tr><td id="contents_3838" valign="top" style="word-wrap: break-word; word-break: break-all;"><img onLoad='miniSelfResize(contents_3909,this); if(this.parentNode.tagName=="A"){this.onclick = "";}' src="http://mechasolution.com/shop/data/editor/afd5dd869c4c0d9e.png" width="862" title="popup original size Image" style="border: 0px; cursor: pointer;"><div><br></div><div>MPU 9250 connection Failed °¡ ³ª´Â ÀÌÀ¯°¡ ¹«¾ùÀϱî¿ä..?</div><div><br></div><div><br></div><div><br></div><div><b style="font-stretch: normal; font-size: 12pt; line-height: normal;">[WAVESHARE] 10 ÀÚÀ¯µµ IMU ¼¾¼ (10 DOF IMU Sensor) </b></div><div><br></div><div><br></div><div>À§ IMU »ç¿ëÇÏ°í ÀÖÀ¸¸ç »ç¿ëÇÑ ÄÚµå´Â ¾Æ·¡¿Í °°½À´Ï´Ù.</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>============================</div><div><br></div><div><div>#include "Wire.h"</div><div><br></div><div>// I2Cdev and MPU9250 must be installed as libraries, or else the .cpp/.h files</div><div>// for both classes must be in the include path of your project</div><div>#include "I2Cdev.h"</div><div>#include "MPU9250.h"</div><div>#include "BMP180.h"</div><div><br></div><div>// class default I2C address is 0x68</div><div>// specific I2C addresses may be passed as a parameter here</div><div>// AD0 low = 0x68 (default for InvenSense evaluation board)</div><div>// AD0 high = 0x69</div><div>MPU9250 accelgyro;</div><div>I2Cdev I2C_M;</div><div><br></div><div>uint8_t buffer_m[6];</div><div><br></div><div><br></div><div>int16_t ax, ay, az;</div><div>int16_t gx, gy, gz;</div><div>int16_t mx, my, mz;</div><div><br></div><div><br></div><div><br></div><div>float heading;</div><div>float tiltheading;</div><div><br></div><div>float Axyz[3];</div><div>float Gxyz[3];</div><div>float Mxyz[3];</div><div><br></div><div><br></div><div>#define sample_num_mdate 5000</div><div><br></div><div>volatile float mx_sample[3];</div><div>volatile float my_sample[3];</div><div>volatile float mz_sample[3];</div><div><br></div><div>static float mx_centre = 0;</div><div>static float my_centre = 0;</div><div>static float mz_centre = 0;</div><div><br></div><div>volatile int mx_max = 0;</div><div>volatile int my_max = 0;</div><div>volatile int mz_max = 0;</div><div><br></div><div>volatile int mx_min = 0;</div><div>volatile int my_min = 0;</div><div>volatile int mz_min = 0;</div><div><br></div><div>float temperature;</div><div>float pressure;</div><div>float atm;</div><div>float altitude;</div><div>BMP180 Barometer;</div><div><br></div><div>void setup()</div><div>{</div><div> // join I2C bus (I2Cdev library doesn't do this automatically)</div><div> Wire.begin();</div><div><br></div><div> // initialize serial communication</div><div> // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but</div><div> // it's really up to you depending on your project)</div><div> Serial.begin(38400);</div><div><br></div><div> // initialize device</div><div> Serial.println("Initializing I2C devices...");</div><div> accelgyro.initialize();</div><div> Barometer.init();</div><div><br></div><div> // verify connection</div><div> Serial.println("Testing device connections...");</div><div> Serial.println(accelgyro.testConnection() ? "MPU9250 connection successful" : "MPU9250 connection failed");</div><div><br></div><div> delay(1000);</div><div> Serial.println(" ");</div><div><br></div><div> // Mxyz_init_calibrated ();</div><div><br></div><div>}</div><div><br></div><div>void loop()</div><div>{</div><div><br></div><div> getAccel_Data();</div><div> getGyro_Data();</div><div> getCompassDate_calibrated(); // compass data has been calibrated here</div><div> getHeading(); //before we use this function we should run 'getCompassDate_calibrated()' frist, so that we can get calibrated data ,then we can get correct angle .</div><div> getTiltHeading();</div><div><br></div><div> Serial.println("calibration parameter: ");</div><div> Serial.print(mx_centre);</div><div> Serial.print(" ");</div><div> Serial.print(my_centre);</div><div> Serial.print(" ");</div><div> Serial.println(mz_centre);</div><div> Serial.println(" ");</div><div><br></div><div><br></div><div> Serial.println("Acceleration(g) of X,Y,Z:");</div><div> Serial.print(Axyz[0]);</div><div> Serial.print(",");</div><div> Serial.print(Axyz[1]);</div><div> Serial.print(",");</div><div> Serial.println(Axyz[2]);</div><div> Serial.println("Gyro(degress/s) of X,Y,Z:");</div><div> Serial.print(Gxyz[0]);</div><div> Serial.print(",");</div><div> Serial.print(Gxyz[1]);</div><div> Serial.print(",");</div><div> Serial.println(Gxyz[2]);</div><div> Serial.println("Compass Value of X,Y,Z:");</div><div> Serial.print(Mxyz[0]);</div><div> Serial.print(",");</div><div> Serial.print(Mxyz[1]);</div><div> Serial.print(",");</div><div> Serial.println(Mxyz[2]);</div><div> Serial.println("The clockwise angle between the magnetic north and X-Axis:");</div><div> Serial.print(heading);</div><div> Serial.println(" ");</div><div> Serial.println("The clockwise angle between the magnetic north and the projection of the positive X-Axis in the horizontal plane:");</div><div> Serial.println(tiltheading);</div><div> Serial.println(" ");</div><div><br></div><div> temperature = Barometer.bmp180GetTemperature(Barometer.bmp180ReadUT()); //Get the temperature, bmp180ReadUT MUST be called first</div><div> pressure = Barometer.bmp180GetPressure(Barometer.bmp180ReadUP());//Get the temperature</div><div> altitude = Barometer.calcAltitude(pressure); //Uncompensated caculation - in Meters</div><div> atm = pressure / 101325;</div><div><br></div><div> Serial.print("Temperature: ");</div><div> Serial.print(temperature, 2); //display 2 decimal places</div><div> Serial.println("deg C");</div><div><br></div><div> Serial.print("Pressure: ");</div><div> Serial.print(pressure, 0); //whole number only.</div><div> Serial.println(" Pa");</div><div><br></div><div> Serial.print("Ralated Atmosphere: ");</div><div> Serial.println(atm, 4); //display 4 decimal places</div><div><br></div><div> Serial.print("Altitude: ");</div><div> Serial.print(altitude, 2); //display 2 decimal places</div><div> Serial.println(" m");</div><div><br></div><div> Serial.println();</div><div> delay(1000);</div><div><br></div><div>}</div><div><br></div><div><br></div><div>void getHeading(void)</div><div>{</div><div> heading = 180 * atan2(Mxyz[1], Mxyz[0]) / PI;</div><div> if (heading < 0) heading += 360;</div><div>}</div><div><br></div><div>void getTiltHeading(void)</div><div>{</div><div> float pitch = asin(-Axyz[0]);</div><div> float roll = asin(Axyz[1] / cos(pitch));</div><div><br></div><div> float xh = Mxyz[0] * cos(pitch) + Mxyz[2] * sin(pitch);</div><div> float yh = Mxyz[0] * sin(roll) * sin(pitch) + Mxyz[1] * cos(roll) - Mxyz[2] * sin(roll) * cos(pitch);</div><div> float zh = -Mxyz[0] * cos(roll) * sin(pitch) + Mxyz[1] * sin(roll) + Mxyz[2] * cos(roll) * cos(pitch);</div><div> tiltheading = 180 * atan2(yh, xh) / PI;</div><div> if (yh < 0) tiltheading += 360;</div><div>}</div><div><br></div><div><br></div><div><br></div><div>void Mxyz_init_calibrated ()</div><div>{</div><div><br></div><div> Serial.println(F("Before using 9DOF,we need to calibrate the compass frist,It will takes about 2 minutes."));</div><div> Serial.print(" ");</div><div> Serial.println(F("During calibratting ,you should rotate and turn the 9DOF all the time within 2 minutes."));</div><div> Serial.print(" ");</div><div> Serial.println(F("If you are ready ,please sent a command data 'ready' to start sample and calibrate."));</div><div> while (!Serial.find("ready"));</div><div> Serial.println(" ");</div><div> Serial.println("ready");</div><div> Serial.println("Sample starting......");</div><div> Serial.println("waiting ......");</div><div><br></div><div> get_calibration_Data ();</div><div><br></div><div> Serial.println(" ");</div><div> Serial.println("compass calibration parameter ");</div><div> Serial.print(mx_centre);</div><div> Serial.print(" ");</div><div> Serial.print(my_centre);</div><div> Serial.print(" ");</div><div> Serial.println(mz_centre);</div><div> Serial.println(" ");</div><div>}</div><div><br></div><div><br></div><div>void get_calibration_Data ()</div><div>{</div><div> for (int i = 0; i < sample_num_mdate; i++)</div><div> {</div><div> get_one_sample_date_mxyz();</div><div> /*</div><div> Serial.print(mx_sample[2]);</div><div> Serial.print(" ");</div><div> Serial.print(my_sample[2]); //you can see the sample data here .</div><div> Serial.print(" ");</div><div> Serial.println(mz_sample[2]);</div><div> */</div><div><br></div><div><br></div><div><br></div><div> if (mx_sample[2] >= mx_sample[1])mx_sample[1] = mx_sample[2];</div><div> if (my_sample[2] >= my_sample[1])my_sample[1] = my_sample[2]; //find max value</div><div> if (mz_sample[2] >= mz_sample[1])mz_sample[1] = mz_sample[2];</div><div><br></div><div> if (mx_sample[2] <= mx_sample[0])mx_sample[0] = mx_sample[2];</div><div> if (my_sample[2] <= my_sample[0])my_sample[0] = my_sample[2]; //find min value</div><div> if (mz_sample[2] <= mz_sample[0])mz_sample[0] = mz_sample[2];</div><div><br></div><div> }</div><div><br></div><div> mx_max = mx_sample[1];</div><div> my_max = my_sample[1];</div><div> mz_max = mz_sample[1];</div><div><br></div><div> mx_min = mx_sample[0];</div><div> my_min = my_sample[0];</div><div> mz_min = mz_sample[0];</div><div><br></div><div><br></div><div><br></div><div> mx_centre = (mx_max + mx_min) / 2;</div><div> my_centre = (my_max + my_min) / 2;</div><div> mz_centre = (mz_max + mz_min) / 2;</div><div><br></div><div>}</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>void get_one_sample_date_mxyz()</div><div>{</div><div> getCompass_Data();</div><div> mx_sample[2] = Mxyz[0];</div><div> my_sample[2] = Mxyz[1];</div><div> mz_sample[2] = Mxyz[2];</div><div>}</div><div><br></div><div><br></div><div>void getAccel_Data(void)</div><div>{</div><div> accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);</div><div> Axyz[0] = (double) ax / 16384;</div><div> Axyz[1] = (double) ay / 16384;</div><div> Axyz[2] = (double) az / 16384;</div><div>}</div><div><br></div><div>void getGyro_Data(void)</div><div>{</div><div> accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);</div><div> Gxyz[0] = (double) gx * 250 / 32768;</div><div> Gxyz[1] = (double) gy * 250 / 32768;</div><div> Gxyz[2] = (double) gz * 250 / 32768;</div><div>}</div><div><br></div><div>void getCompass_Data(void)</div><div>{</div><div> I2C_M.writeByte(MPU9150_RA_MAG_ADDRESS, 0x0A, 0x01); //enable the magnetometer</div><div> delay(10);</div><div> I2C_M.readBytes(MPU9150_RA_MAG_ADDRESS, MPU9150_RA_MAG_XOUT_L, 6, buffer_m);</div><div><br></div><div> mx = ((int16_t)(buffer_m[1]) << 8) | buffer_m[0] ;</div><div> my = ((int16_t)(buffer_m[3]) << 8) | buffer_m[2] ;</div><div> mz = ((int16_t)(buffer_m[5]) << 8) | buffer_m[4] ;</div><div><br></div><div> Mxyz[0] = (double) mx * 1200 / 4096;</div><div> Mxyz[1] = (double) my * 1200 / 4096;</div><div> Mxyz[2] = (double) mz * 1200 / 4096;</div><div>}</div><div><br></div><div>void getCompassDate_calibrated ()</div><div>{</div><div> getCompass_Data();</div><div> Mxyz[0] = Mxyz[0] - mx_centre;</div><div> Mxyz[1] = Mxyz[1] - my_centre;</div><div> Mxyz[2] = Mxyz[2] - mz_centre;</div><div>}</div></div></td></tr></tbody></table></td></tr></tbody></table>