<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">본 프로젝트에서는 아두이노 우노와 MPU-9250센서를 이용해 센서의 운동상태를 파악하고, 나아가 BLDC모터를 이용해 센서의자세를 제어하는 단계까지 진행하고자 합니다.</p>
<h2 style="margin: 30px 0px 10px; color: #222222; font-family: Helvetica, Arial, sans-serif; background-color: #ffffff;">1. MPU-9250의 raw data 받아오기</h2>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">MPU-9250센서는 가속도 센서, 자이로 센서, 지자기 센서 세 가지가 결합되어 있는 센서입니다. 3차원 공간에서 센서의 가속도, 각속도, 그리고 주위의 지자기를 측정하여 벡터형태로 값을 나타내게 됩니다.&nbsp;<br /><img src="http://kr.bluesink.io/uploads/default/original/1X/801db627589f51b274b18f416a7a4f38a7412277.jpg" width="228" height="228" style="border: 0px; vertical-align: middle; max-width: 690px; max-height: 500px;" /></p>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">가장 먼저, 본 센서의 raw data를 받아오기 위하여 아두이노 우노와 아래 그림과 같이 연결합니다.</p>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;"><img src="http://kr.bluesink.io/uploads/default/original/1X/852aadc0f03338e86bad0b2e4deda739997bfa93.jpg" width="380" height="238" style="border: 0px; vertical-align: middle; max-width: 690px; max-height: 500px;" /></p>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">아두이노와 MPU-9250센서는 각각 마스터와 슬레이브가 되어 I2C통신을 하게 됩니다. 이 때 아두이노 우노의 경우, 아두이노에서 자체적으로 I2C통신을 위해 아날로그 4, 5번 핀을 각각 SDA, SCL pin으로 지정하여 사용가능하게 하였습니다.</p>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">&nbsp;</p>
<pre style="overflow: auto; font-family: Consolas, Menlo, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; font-size: 14px; color: #222222; background-color: #ffffff;">
   <code class="hljs php" style="font-family: Consolas, Menlo, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; overflow: auto; tab-size: 4; color: #333333; background: rgb(249, 249, 249); overflow-wrap: normal; display: block; padding: 0.5em; max-height: 500px;"><span class="hljs-comment" style="color: #999988; font-style: italic;">#include "MPU9250.h"</span><span class="hljs-comment" style="color: #999988; font-style: italic;">#define AHRS true         // Set to false for basic data read</span><span class="hljs-comment" style="color: #999988; font-style: italic;">#define SerialDebug true  // Set to true to get Serial output for debugging</span>
    MPU9250 myIMU;
    void setup() {
	Wire.begin();
	<span class="hljs-comment" style="color: #999988; font-style: italic;">//I2C통신 준비</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// TWBR = 12;  // 400 kbit/sec I2C speed</span>
	Serial.begin(<span class="hljs-number" style="color: #009999;">38400</span>);
	<span class="hljs-comment" style="color: #999988; font-style: italic;">//38400 bits/s</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// Read the WHO_AM_I register, this is a good test of communication</span>
	byte c = myIMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
	Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"MPU9250 "</span>);
	Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"I AM "</span>);
	Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(c, HEX);
	Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">" I should be "</span>);
	Serial.println(<span class="hljs-number" style="color: #009999;">0x71</span>, HEX);
	<span class="hljs-keyword" style="font-weight: bold;">if</span> (c == <span class="hljs-number" style="color: #009999;">0x71</span>) <span class="hljs-comment" style="color: #999988; font-style: italic;">// WHO_AM_I should always be 0x71 {</span>
		Serial.println(<span class="hljs-string" style="color: #dd1144;">"MPU9250 is online..."</span>);
		<span class="hljs-comment" style="color: #999988; font-style: italic;">// Start by performing self test and reporting values</span>
		myIMU.MPU9250SelfTest(myIMU.SelfTest);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"x-axis self test: acceleration trim within : "</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(myIMU.SelfTest[<span class="hljs-number" style="color: #009999;">0</span>],<span class="hljs-number" style="color: #009999;">1</span>);
		Serial.println(<span class="hljs-string" style="color: #dd1144;">"% of factory value"</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"y-axis self test: acceleration trim within : "</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(myIMU.SelfTest[<span class="hljs-number" style="color: #009999;">1</span>],<span class="hljs-number" style="color: #009999;">1</span>);
		Serial.println(<span class="hljs-string" style="color: #dd1144;">"% of factory value"</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"z-axis self test: acceleration trim within : "</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(myIMU.SelfTest[<span class="hljs-number" style="color: #009999;">2</span>],<span class="hljs-number" style="color: #009999;">1</span>);
		Serial.println(<span class="hljs-string" style="color: #dd1144;">"% of factory value"</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"x-axis self test: gyration trim within : "</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(myIMU.SelfTest[<span class="hljs-number" style="color: #009999;">3</span>],<span class="hljs-number" style="color: #009999;">1</span>);
		Serial.println(<span class="hljs-string" style="color: #dd1144;">"% of factory value"</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"y-axis self test: gyration trim within : "</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(myIMU.SelfTest[<span class="hljs-number" style="color: #009999;">4</span>],<span class="hljs-number" style="color: #009999;">1</span>);
		Serial.println(<span class="hljs-string" style="color: #dd1144;">"% of factory value"</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"z-axis self test: gyration trim within : "</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(myIMU.SelfTest[<span class="hljs-number" style="color: #009999;">5</span>],<span class="hljs-number" style="color: #009999;">1</span>);
		Serial.println(<span class="hljs-string" style="color: #dd1144;">"% of factory value"</span>);
		<span class="hljs-comment" style="color: #999988; font-style: italic;">// Calibrate gyro and accelerometers, load biases in bias registers</span>
		myIMU.calibrateMPU9250(myIMU.gyroBias, myIMU.accelBias);
		myIMU.initMPU9250();
		<span class="hljs-comment" style="color: #999988; font-style: italic;">// Initialize device for active mode read of acclerometer, gyroscope, and</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// temperature</span>
		Serial.println(<span class="hljs-string" style="color: #dd1144;">"MPU9250 initialized for active data mode...."</span>);
		<span class="hljs-comment" style="color: #999988; font-style: italic;">// Read the WHO_AM_I register of the magnetometer, this is a good test of</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// communication</span>
		byte d = myIMU.readByte(AK8963_ADDRESS, WHO_AM_I_AK8963);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"AK8963 "</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"I AM "</span>);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(d, HEX);
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">" I should be "</span>);
		Serial.println(<span class="hljs-number" style="color: #009999;">0x48</span>, HEX);
		<span class="hljs-comment" style="color: #999988; font-style: italic;">// Get magnetometer calibration from AK8963 ROM</span>
		myIMU.initAK8963(myIMU.factoryMagCalibration);
		<span class="hljs-comment" style="color: #999988; font-style: italic;">// Initialize device for active mode read of magnetometer</span>
		Serial.println(<span class="hljs-string" style="color: #dd1144;">"AK8963 initialized for active data mode...."</span>);
		<span class="hljs-comment" style="color: #999988; font-style: italic;">// Get sensor resolutions, only need to do this once</span>
		myIMU.getAres();
		myIMU.getGres();
		myIMU.getMres();
		<span class="hljs-comment" style="color: #999988; font-style: italic;">// The next call delays for 4 seconds, and then records about 15 seconds of</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// data to calculate bias and scale.</span>
		myIMU.magCalMPU9250(myIMU.magBias, myIMU.magScale);
		Serial.println(<span class="hljs-string" style="color: #dd1144;">"AK8963 mag biases (mG)"</span>);
		Serial.println(myIMU.magBias[<span class="hljs-number" style="color: #009999;">0</span>]);
		Serial.println(myIMU.magBias[<span class="hljs-number" style="color: #009999;">1</span>]);
		Serial.println(myIMU.magBias[<span class="hljs-number" style="color: #009999;">2</span>]);
		Serial.println(<span class="hljs-string" style="color: #dd1144;">"AK8963 mag scale (mG)"</span>);
		Serial.println(myIMU.magScale[<span class="hljs-number" style="color: #009999;">0</span>]);
		Serial.println(myIMU.magScale[<span class="hljs-number" style="color: #009999;">1</span>]);
		Serial.println(myIMU.magScale[<span class="hljs-number" style="color: #009999;">2</span>]);
		delay(<span class="hljs-number" style="color: #009999;">2000</span>);
		<span class="hljs-comment" style="color: #999988; font-style: italic;">// Add delay to see results before serial spew of data</span>
	}
	<span class="hljs-comment" style="color: #999988; font-style: italic;">// if (c == 0x71) </span><span class="hljs-keyword" style="font-weight: bold;">else</span> {
		Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"Could not connect to MPU9250: 0x"</span>);
		Serial.println(c, HEX);
		<span class="hljs-keyword" style="font-weight: bold;">while</span>(<span class="hljs-number" style="color: #009999;">1</span>) ;
		<span class="hljs-comment" style="color: #999988; font-style: italic;">// Loop forever if communication doesn't happen</span>
	}
}
void loop() {
	myIMU.readAccelData(myIMU.accelCount);
	<span class="hljs-comment" style="color: #999988; font-style: italic;">// Read the x/y/z adc values</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// Now we'll calculate the accleration value into actual g's</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// This depends on scale being set</span>
	myIMU.ax = (float)myIMU.accelCount[<span class="hljs-number" style="color: #009999;">0</span>] * myIMU.aRes;
	<span class="hljs-comment" style="color: #999988; font-style: italic;">// - myIMU.accelBias[0];</span>
	myIMU.ay = (float)myIMU.accelCount[<span class="hljs-number" style="color: #009999;">1</span>] * myIMU.aRes;
	<span class="hljs-comment" style="color: #999988; font-style: italic;">// - myIMU.accelBias[1];</span>
	myIMU.az = (float)myIMU.accelCount[<span class="hljs-number" style="color: #009999;">2</span>] * myIMU.aRes;
	<span class="hljs-comment" style="color: #999988; font-style: italic;">// - myIMU.accelBias[2];</span>
	myIMU.readGyroData(myIMU.gyroCount);
	<span class="hljs-comment" style="color: #999988; font-style: italic;">// Read the x/y/z adc values</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// Calculate the gyro value into actual degrees per second</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// This depends on scale being set</span>
	myIMU.gx = (float)myIMU.gyroCount[<span class="hljs-number" style="color: #009999;">0</span>] * myIMU.gRes;
	myIMU.gy = (float)myIMU.gyroCount[<span class="hljs-number" style="color: #009999;">1</span>] * myIMU.gRes;
	myIMU.gz = (float)myIMU.gyroCount[<span class="hljs-number" style="color: #009999;">2</span>] * myIMU.gRes;
	myIMU.readMagData(myIMU.magCount);
	<span class="hljs-comment" style="color: #999988; font-style: italic;">// Read the x/y/z adc values</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// Calculate the magnetometer values in milliGauss</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// Include factory calibration per data sheet and user environmental</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// corrections</span><span class="hljs-comment" style="color: #999988; font-style: italic;">// Get actual magnetometer value, this depends on scale being set</span>
	myIMU.mx = (float)myIMU.magCount[<span class="hljs-number" style="color: #009999;">0</span>] * myIMU.mRes
	               * myIMU.factoryMagCalibration[<span class="hljs-number" style="color: #009999;">0</span>] - myIMU.magBias[<span class="hljs-number" style="color: #009999;">0</span>];
	myIMU.my = (float)myIMU.magCount[<span class="hljs-number" style="color: #009999;">1</span>] * myIMU.mRes
	               * myIMU.factoryMagCalibration[<span class="hljs-number" style="color: #009999;">1</span>] - myIMU.magBias[<span class="hljs-number" style="color: #009999;">1</span>];
	myIMU.mz = (float)myIMU.magCount[<span class="hljs-number" style="color: #009999;">2</span>] * myIMU.mRes
	               * myIMU.factoryMagCalibration[<span class="hljs-number" style="color: #009999;">2</span>] - myIMU.magBias[<span class="hljs-number" style="color: #009999;">2</span>];
	Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">"ax = "</span>);
	Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>((int)<span class="hljs-number" style="color: #009999;">1000</span> * myIMU.ax);
	Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">" ay = "</span>);
	Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>((int)<span class="hljs-number" style="color: #009999;">1000</span> * myIMU.ay);
	Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>(<span class="hljs-string" style="color: #dd1144;">" az = "</span>);
	Serial.<span class="hljs-keyword" style="font-weight: bold;">print</span>((int)<span class="hljs-number" style="color: #009999;">1000</span> * myIMU.az);
	Serial.println(<span class="hljs-string" style="color: #dd1144;">" mg"</span>);
	<span class="hljs-comment" style="color: #999988; font-style: italic;">// Must be called before updating quaternions!</span>
	myIMU.updateTime();
}</code>
   </pre>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">소스코드는&nbsp;<a href="https://github.com/sparkfun/SparkFun_MPU-9250_Breakout_Arduino_Library" rel="nofollow noopener" style="background: transparent; color: #0088cc; text-decoration-line: none; cursor: pointer; overflow-wrap: break-word;" target="_blank">https://github.com/sparkfun/SparkFun_MPU-9250_Breakout_Arduino_Library<span class="badge badge-notification clicks" title="544번 클릭" style="display: inline-block; white-space: nowrap; border-radius: 10px; padding: 4px 5px 2px; vertical-align: middle; color: #919191; font-size: 11px; line-height: 1; text-align: center; background-color: #e9e9e9; top: -1px; position: relative; margin-left: 2px; border: none;">544</span></a>&nbsp;에서 참고하여 간소화 한 것입니다. 이를 아두이노IDE에서 컴파일하여 실행하면 시리얼 모니터에서 아래와 같은 결과를 확인할 수 있습니다.</p>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">&nbsp;</p>
<div class="lightbox-wrapper" style="display: inline-block; outline: 0px; color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;"><a data-download-href="//kr.bluesink.io/uploads/default/98aef49a581fa4240778c84db259d9d0c085e5d0" href="http://kr.bluesink.io/uploads/default/original/1X/98aef49a581fa4240778c84db259d9d0c085e5d0.png" class="lightbox" title="게시된 이미지" style="background: transparent; color: #0088cc; text-decoration-line: none; cursor: pointer; position: relative; display: inline-block; outline: 0px; overflow-wrap: break-word;" target="_blank"><img src="http://kr.bluesink.io/uploads/default/optimized/1X/98aef49a581fa4240778c84db259d9d0c085e5d0_1_690x418.png" width="690" height="418" style="border: 0px; vertical-align: middle; outline: 0px; max-width: 690px; max-height: 500px;" />
      <div class="meta" style="outline: 0px; position: absolute; bottom: 0px; width: 690px; color: #999999; background: rgb(34, 34, 34); opacity: 0; transition: opacity 0.2s ease 0s; font-weight: bold;"><span class="filename" style="outline: 0px; float: left; margin: 5px; max-width: 100%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;">게시된 이미지</span><span class="informations" style="outline: 0px; float: left; margin: 6px; padding-right: 20px; color: #b6b6b6; font-size: 1em;">903x548 35.4 KB</span><span class="expand" style="outline: 0px; float: left; position: absolute; bottom: 4px; right: 7px;"></span></div>
      </a></div>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">&nbsp;</p>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">회로가 제대로 연결되어 통신을 시작할 준비가 완료되었다는 표시가 뜨고,</p>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">&nbsp;</p>
<div class="lightbox-wrapper" style="display: inline-block; outline: 0px; color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;"><a data-download-href="//kr.bluesink.io/uploads/default/3241f06ddae9c4bd99a1f741556b2f82691cb18b" href="http://kr.bluesink.io/uploads/default/original/1X/3241f06ddae9c4bd99a1f741556b2f82691cb18b.png" class="lightbox" title="게시된 이미지" style="background: transparent; color: #0088cc; text-decoration-line: none; cursor: pointer; position: relative; display: inline-block; outline: 0px; overflow-wrap: break-word;" target="_blank"><img src="http://kr.bluesink.io/uploads/default/optimized/1X/3241f06ddae9c4bd99a1f741556b2f82691cb18b_1_690x416.png" width="690" height="416" style="border: 0px; vertical-align: middle; outline: 0px; max-width: 690px; max-height: 500px;" />
      <div class="meta" style="outline: 0px; position: absolute; bottom: 0px; width: 690px; color: #999999; background: rgb(34, 34, 34); opacity: 0; transition: opacity 0.2s ease 0s; font-weight: bold;"><span class="filename" style="outline: 0px; float: left; margin: 5px; max-width: 100%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;">게시된 이미지</span><span class="informations" style="outline: 0px; float: left; margin: 6px; padding-right: 20px; color: #b6b6b6; font-size: 1em;">906x547 31.6 KB</span><span class="expand" style="outline: 0px; float: left; position: absolute; bottom: 4px; right: 7px;"></span></div>
      </a></div>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">&nbsp;</p>
<p style="color: #222222; font-family: Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;">몇초 후 위와 같이 MPU-9250이 raw data를 받아오는 것을 확인할 수 있습니다.</p>