본문 바로가기

HMC5883L

3D 컴파스 모듈과의 첫 실험.

3축 전자나침반 모듈과의 첫 실험.

Triple-axis Magnetometer (Compass) Board - HMC5883L


   예전에 시간 나면 한번 갖고 놀아보려고 사 두었던 3D 컴파스 모듈(HMC5883L)을 정말 구입한지 거의 일년이 지나서 처음으로 만들어 보았다.


사실 이런 장비는 기본적으로 조율을 먼저 해야 한다.

참조 : http://www.instructables.com/id/Easy-hard-and-soft-iron-magnetometer-calibration/?ALLSTEPS


아직 조율작업(calibration) 이 안 거쳤기 때문에 약간 오차가 있기는 하지만,  

그런대로 자료입력은 성공했다.   그리고 남아 도는 MPU6050도 같이 넣었다.

보다 세련된 작업은 나중에 하기로 하고 I2C를 사용해서 기본 데이타입력만 해 보기로 했다.    두 모듈에서 3차원 컴파스, 3차원 가속, 3차원 원속, 그리고 온도측정을 입력받을 수 있었다.







동영상은 다음과 같다.






그리고 아듀이노 코드는 아래를 참조.  (라이브러리는 필수)


// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation

// is used in I2Cdev.h

#include "I2Cdev.h"

#include "HMC5883L.h"

#include "MPU6050.h"

#include "Wire.h"


// list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read,

#define OUTPUT_READABLE_ACCELGYRO


// class default I2C address is 0x1E

// specific I2C addresses may be passed as a parameter here

// this device only supports one I2C address (0x1E)

HMC5883L Compass(0x1E);  // 3D Compass  (0x1E)

MPU6050  accelgyro(0x68);  // 6D Accel & Gyro & Temp  (0x69)


int16_t cmx, cmy, cmz;  // xyz for compass


int16_t ax, ay, az;

int16_t gx, gy, gz;


int16_t rawTemp;  // Temperature value from MPU6050 Module

float temp;       // Converted Value in Celcius degree


String  s;  // Temporary string

int i = 100;  // Simple Counter


void setup() {   

    Wire.begin();


    Serial.begin(38400);


    Serial.println("Initializing I2C for COMPASS devices...");

    Compass.initialize();  // initialize device

    

    // verify connection

    Serial.println("Testing device connections...");

    Serial.println(Compass.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed");


    Serial.println("Initializing I2C for 6DOF AccelGyro devices...");

    accelgyro.initialize();

    

    // verify connection

    Serial.println("Testing device connections...");

    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");


//    delay(2000);  // to see what has been done

}


void loop() {

   // Get temporature for every 100 cycle, no need fast change of temp.

   if (i == 100) {  // with delay(100), it will be every 10 sec.

      i = 0;  // reset counter

      rawTemp = accelgyro.getTemperature();  // get value from machine

      temp=(rawTemp/340.)+36.53;  // conver to tempature in C

 

      s = "<R=TEMP:";

      s += temp;

      s += ">";

      Serial.println(s);

   }

   i++;

   // read raw accel/gyro measurements from device

   accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

   // these methods (and a few others) are also available

   //accelgyro.getAcceleration(&ax, &ay, &az);

   //accelgyro.getRotation(&gx, &gy, &gz);

   s = "<R=6DOF-AXYZ-GXYZ:";

   s += ax;

   s += ",";

   s += ay;         

   s += ",";    

   s += az;         

   s += ","; 

   s += gx;

   s += ",";

   s += gy;         

   s += ",";    

   s += gz;         

   s += ">";

   Serial.println(s);

   

   Compass.getHeading(&cmx, &cmy, &cmz);  // read raw heading measurements from device


   // display , separated gyro x/y/z values + Heading value

   s = "<R=COMPASS-XYZH:";  

   s += cmx;

   s += ",";

   s += cmy;         

   s += ",";    

   s += cmz;         

   s += ",";    

    

   // To calculate heading in degrees. 0 degree indicates North

   float heading = atan2(cmy, cmx);

   if(heading < 0)

      heading += 2 * M_PI;

   s += (heading * 180/M_PI);         

   s += ">";       

   Serial.println(s);

   

   delay(100);  // without delay, it can be too annoying

                // for fast result, delay can be removed.

}





'HMC5883L' 카테고리의 다른 글

3축 컴파스 (HMC5883L) 에 주변 쇠붙이들이 주는 영향  (0) 2017.08.18