Skip to content

Commit

Permalink
support azimuth
Browse files Browse the repository at this point in the history
  • Loading branch information
yevgeny hong committed Sep 21, 2017
1 parent 690c67d commit c25ce67
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
16 changes: 16 additions & 0 deletions MechaQMC5883.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,19 @@ void MechaQMC5883::read(uint16_t* x,uint16_t* y,uint16_t* z){
*z = Wire.read(); //LSB y
*z |= Wire.read() << 8; //MSB y
}

void MechaQMC5883::read(uint16_t* x,uint16_t* y,uint16_t* z,int* a){
read(x,y,z);
*a = azimuth(y,x);
}

void MechaQMC5883::read(uint16_t* x,uint16_t* y,uint16_t* z,float* a){
read(x,y,z);
*a = azimuth(y,x);
}


float MechaQMC5883::azimuth(uint16_t *a, uint16_t *b){
float azimuth = atan2((int)*a,(int)*b) * 180.0/PI;
return azimuth < 0?360 + azimuth:azimuth;
}
6 changes: 6 additions & 0 deletions MechaQMC5883.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ void setMode(uint16_t mode,uint16_t odr,uint16_t rng,uint16_t osr); // setting
void softReset(); //soft RESET

void read(uint16_t* x,uint16_t* y,uint16_t* z); //reading
void read(uint16_t* x,uint16_t* y,uint16_t* z,int* a);
void read(uint16_t* x,uint16_t* y,uint16_t* z,float* a);

float azimuth(uint16_t* a,uint16_t* b);

private:

void WriteReg(uint8_t Reg,uint8_t val);



uint8_t address = QMC5883_ADDR;

};
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ void loop(){
}
```

and we can get azimuth too.

```cpp
void loop(){
int x,y,z;
int a;
//float a; //can get float value

qmc.read(&x,&y,&z,&a);
}
```

also can claculate azimuth you want

```cpp
void loop(){
int x,y,z;
int a;

qmc.read(&x,&y,&z);
a = qmc.azimuth(&y,&x);
}
```

## Basic example

It can be seen as a collection of the contents mentioned above.
Expand Down
23 changes: 23 additions & 0 deletions README_KO.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ void loop(){
}
```

방위각에 대한 값입니다.

```cpp
void loop(){
int x,y,z;
int a;
//float a; //float 형도 지원됩니다.

qmc.read(&x,&y,&z,&a);
}
```

별도로 원하는 방위각도 구할 수 있습니다.

```cpp
void loop(){
int x,y,z;
int a;

qmc.read(&x,&y,&z);
a = qmc.azimuth(&y,&x);
}
```
### 기본 예제

다음은 라이브러리 기본 예제인 raw입니다.
Expand Down
29 changes: 29 additions & 0 deletions example/azimuth/azimuth.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <Wire.h>
#include <MechaQMC5883.h>

MechaQMC5883 qmc;

void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}

void loop() {
int x, y, z;
int azimuth;
//float azimuth; //is supporting float too
qmc.read(&x, &y, &z,&azimuth);
//azimuth = qmc.azimuth(&y,&x);//you can get custom azimuth
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.print(" a: ");
Serial.print(azimuth);
Serial.println();
delay(100);
}

0 comments on commit c25ce67

Please sign in to comment.