Skip to content

Instantly share code, notes, and snippets.

@anbara
Last active March 29, 2016 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anbara/9b6cfe07968ce771c971 to your computer and use it in GitHub Desktop.
Save anbara/9b6cfe07968ce771c971 to your computer and use it in GitHub Desktop.
/***Z軸の角度(yaw)を読み取る方法***
[はじめに]
JY901は"-180から180"の間で値を返してきます。ご注意ください。
また、角度は小数点2桁まで出できます。
本プログラムはデータシートpp10 "5.1.4 Angle Output" を主に参考にしました。
[手順]
1.JY901のIICアドレスは"0x50"なので、Wire.beginTransmission(0x50)を行う。
2.yawを読み取る信号は"0x3f"を送ることで認識されるので、Wire.write(0x3f)を行う。
3.データの送信作業は終わりなので、 Wire.endTransmission(false)を行う。
4.2byteのデータが送られてくるので、Wire.requestFrom(0x50, 2)を行う。
5.2byteのデータが送られてくるまで待機したいので、while (Wire.available() < 2)を行う。
6.2byteのデータを読むので、Wire.read()を2回行う。
7.計算を行い、2byteのデータを角度にする。
計算式 : (2byte目<<8 | 1byte目) / 32768 * 180
[あとづけ]
JY901発売元 ElecMaster http://www.aliexpress.com/store/1836321
本プログラム作成 あんばら(twitter : @ISM__M)
***********************************/
#include <Wire.h>
int yawl, yawh;
float angle;
void setup()
{
Wire.begin();
Serial.begin(9600);
Serial.println("JY901 Yaw Output");
}
void loop()
{
//1
Wire.beginTransmission(0x50); //J901との通信開始
//2
Wire.write(0x3f); //z軸の角度を読み取るアドレスを送信
//3
Wire.endTransmission(false); //送信終了
//4
Wire.requestFrom(0x50, 2); //データ要求
//5
while (Wire.available() < 2) //データ待機
{
Wire.beginTransmission(0x50); //J901との通信開始
Wire.write(0x3f); //z軸の角度を読み取るアドレスを送信
Wire.endTransmission(false); //送信終了
Wire.requestFrom(0x50, 2); //データ要求
}
//6
yawl = Wire.read(); //1byte目受信
yawh = Wire.read(); //2byte目受信
//7
angle = (float)((yawh << 8) | yawl) / 32768 * 180;
Serial.println(angle);
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment