[Arduino] MPU6050 값 출력하기 (각도 구하기)

PersesTitan·2022년 6월 28일
0

Arduino

목록 보기
2/2

MPU6050이란?

각속도, 가속도를 (각도, 속도)구하는 센서입니다.
여기서는 저는 VR를 만들기 위해 구매하게 되었는데요... 그래서 가속도보다 각속도를 이용할 생각입니다.

아듀이노 듀를 저는 사용했기에 다음과 같이 연결하였습니다.

5V      -> VCC  
GND     -> GND  
SDA(20) -> SDA  
SCL(21) -> SCL  
#include <MPU6050_tockn.h>
#include <Wire.h>

MPU6050 mpu(Wire);

//바뀌기 전 각도
float bx=0.0;
float by=0.0;
float bz=0.0;

void setup() {
  Serial.begin(19200);
  Wire.begin();
  mpu.begin();
  mpu.setGyroOffsets(true);
  //처음에만 써주고 초기값을 알게되면 초기값을 써주면 됩니다.
  //예시) mpu.setGyroOffsets(-0.57,1.31,1.49);

  bx=mpu.getAngleX();
  by=mpu.getAngleY();
  bz=mpu.getAngleZ();
}

void loop() {
  mpu.update();
 
  //현재 각도 가져오기
  float ax=mpu.getAngleX();
  float ay=mpu.getAngleY();
  float az=mpu.getAngleZ();

  //50ms 전 각도와 차이 구하기
  float x = ax-bx;
  float y = ay-by;
  float z = az-bz;

  //계산 후 바뀌기 전 각도 업데이트
  bx = mpu.getAngleX();
  by = mpu.getAngleY();
  bz = mpu.getAngleZ();

  Serial.print(x);
  Serial.print(" ");
  Serial.print(y);
  Serial.print(" ");
  Serial.print(z);
  Serial.println();

  delay(50);
}

(Unity/Arduino)
깃허브 링크

profile
안녕하세요 페르세스 티탄입니다! 부족한 부분이 많이 있겠지만 잘부탁드립니다.

0개의 댓글