2016年 01月 19日
3軸加速度センサーから角度を取得する。 |
2015-12-21
3軸加速度センサー試し。ブレッドボードに組み込んだ3軸加速度センサーから値を取得して、度数法でシリアルモニタに出力するのを目標とする。
参考:第17回 Arduinoでパーツやセンサを使ってみよう~加速度センサ編(その1)
http://deviceplus.jp/hobby/entry017/
参考URLの写真を見つつブレッドボードにセンサーを組み込んでいく。
ソフト側のコード。
//********************************
//加速度センサの値を取得するプログラム
//********************************
void setup()
{
// シリアルモニターの初期化をする
Serial.begin(9600) ;
}
void loop()
{
long x , y , z ;
x = y = z = 0 ;
x = analogRead(3) ; // X軸
y = analogRead(4) ; // Y軸
z = analogRead(5) ; // Z軸
Serial.print("X:") ;
Serial.print(x) ;
Serial.print(" Y:") ;
Serial.print(y) ;
Serial.print(" Z:") ;
Serial.println(z) ;
delay(50) ;
}
机に置いた状態で
X:511 Y:526 Z:711
を安定値として示している。
X,Y,Z方向に傾けてみる。XYZの最大最小を取得するためにコードを書き換える。
コード
//********************************
//加速度センサの値を取得するプログラム
//********************************
void setup()
{
// シリアルモニターの初期化をする
Serial.begin(9600) ;
}
// XYZの初期値を置く、事前に測定した値。
int Xmin = 511;
int Xmax = 511;
int Ymin = 526;
int Ymax = 526;
int Zmin = 711;
int Zmax = 711;
void loop()
{
long x , y , z ;
x = y = z = 0 ;
x = analogRead(0) ; // X軸
y = analogRead(1) ; // Y軸
z = analogRead(2) ; // Z軸
Serial.print("X:") ;
Serial.print(x) ;
Serial.print(" Y:") ;
Serial.print(y) ;
Serial.print(" Z:") ;
Serial.println(z) ;
delay(1000) ;
if(Xmin > x) Xmin = x;
if(Xmax < x) Xmax = x;
if(Ymin > y) Ymin = y;
if(Ymax < y) Ymax = y;
if(Zmin > z) Zmin = z;
if(Zmax < z) Zmax = z;
Serial.print("Xmin:") ;
Serial.print(Xmin) ;
Serial.print(" Xmax:") ;
Serial.print(Xmax) ;
Serial.print(" Ymin:") ;
Serial.print(Ymin) ;
Serial.print(" Ymax:") ;
Serial.print(Ymax) ;
Serial.print(" Zmin:") ;
Serial.print(Zmin) ;
Serial.print(" Zmax:") ;
Serial.println(Zmax) ;
}
(随分ぐだぐだしたコードだけどこれ以上短くならんよね・・・ううむ
これでこのセンサーにおける各軸の最大最小がとれる。前後左右に傾けたり上下ひっくり返したりして各軸のMinとMaxを取得していく。
出力結果
X:511 Y:526 Z:711
Xmin:263 Xmax:742 Ymin:282 Ymax:758 Zmin:311 Zmax:735
各軸の最大最小がわかったので計算式に代入すれば度数法によるセンサーの角度がわかる。
参考URL(http://deviceplus.jp/hobby/entry017/)の真ん中下あたりの計算式から、
Xについて
一度あたりのセンサ値X1 = (Xmax - Xmin)/180
= (742 - 263)/180
= 2.661…
角度 angleX = (x - Xmin)/X1 - 90
= (x - 263)/2.66 - 90
Yについて
一度あたりのセンサ値Y1 = (Ymax - Ymin)/180
= (758 - 282)/180
=2.6444…
角度 angleY = (y - Ymin)/Y1 - 90
= (y - 282)/2.64 - 90
Zについて
一度あたりのセンサ値Z1 = (Zmax - Zmin)/180
= (735 - 311)/180
= 2.355…
角度 angleZ = (z - Zmin)/Z1 - 90
= (z - 311)/2.36 - 90
よってセンサーの出力値から度数法による角度の換算式が得られたので、コードを書き換える。
各換算式
angleX = (x - 263)/2.66 - 90
angleY = (y - 282)/2.64 - 90
angleZ = (z - 311)/2.36 - 90
コード。
//********************************
//加速度センサの値を取得するプログラム
//********************************
void setup()
{
// シリアルモニターの初期化をする
Serial.begin(9600) ;
}
//度数法のXYZを定義
float angleX, angleY, angleZ;
void loop()
{
long x , y , z ;
x = y = z = 0 ;
x = analogRead(0) ; // X軸
y = analogRead(1) ; // Y軸
z = analogRead(2) ; // Z軸
//前プログラムで得られた値を換算式に組み込む
angleX = (x - 263)/2.66 - 90;
angleY = (y - 282)/2.64 - 90;
angleZ = (z - 311)/2.36 - 90;
Serial.print("angleX:") ;
Serial.print(angleX) ;
Serial.print(" angleY:") ;
Serial.print(angleY) ;
Serial.print(" angleZ:") ;
Serial.println(angleZ) ;
delay(1000) ;
}
これで、センサーの現在の角度が度数法でそれぞれ表示されるようになった。
参考URL
第17回 Arduinoでパーツやセンサを使ってみよう~加速度センサ編(その1)
http://deviceplus.jp/hobby/entry017/
3軸加速度センサで傾斜角度を測定してみます
http://www.geocities.jp/zattouka/GarageHouse/micon/Arduino/Acceleration/Acceleration.htm
3軸加速度センサー試し。ブレッドボードに組み込んだ3軸加速度センサーから値を取得して、度数法でシリアルモニタに出力するのを目標とする。
参考:第17回 Arduinoでパーツやセンサを使ってみよう~加速度センサ編(その1)
http://deviceplus.jp/hobby/entry017/
参考URLの写真を見つつブレッドボードにセンサーを組み込んでいく。
ソフト側のコード。
//********************************
//加速度センサの値を取得するプログラム
//********************************
void setup()
{
// シリアルモニターの初期化をする
Serial.begin(9600) ;
}
void loop()
{
long x , y , z ;
x = y = z = 0 ;
x = analogRead(3) ; // X軸
y = analogRead(4) ; // Y軸
z = analogRead(5) ; // Z軸
Serial.print("X:") ;
Serial.print(x) ;
Serial.print(" Y:") ;
Serial.print(y) ;
Serial.print(" Z:") ;
Serial.println(z) ;
delay(50) ;
}
机に置いた状態で
X:511 Y:526 Z:711
を安定値として示している。
X,Y,Z方向に傾けてみる。XYZの最大最小を取得するためにコードを書き換える。
コード
//********************************
//加速度センサの値を取得するプログラム
//********************************
void setup()
{
// シリアルモニターの初期化をする
Serial.begin(9600) ;
}
// XYZの初期値を置く、事前に測定した値。
int Xmin = 511;
int Xmax = 511;
int Ymin = 526;
int Ymax = 526;
int Zmin = 711;
int Zmax = 711;
void loop()
{
long x , y , z ;
x = y = z = 0 ;
x = analogRead(0) ; // X軸
y = analogRead(1) ; // Y軸
z = analogRead(2) ; // Z軸
Serial.print("X:") ;
Serial.print(x) ;
Serial.print(" Y:") ;
Serial.print(y) ;
Serial.print(" Z:") ;
Serial.println(z) ;
delay(1000) ;
if(Xmin > x) Xmin = x;
if(Xmax < x) Xmax = x;
if(Ymin > y) Ymin = y;
if(Ymax < y) Ymax = y;
if(Zmin > z) Zmin = z;
if(Zmax < z) Zmax = z;
Serial.print("Xmin:") ;
Serial.print(Xmin) ;
Serial.print(" Xmax:") ;
Serial.print(Xmax) ;
Serial.print(" Ymin:") ;
Serial.print(Ymin) ;
Serial.print(" Ymax:") ;
Serial.print(Ymax) ;
Serial.print(" Zmin:") ;
Serial.print(Zmin) ;
Serial.print(" Zmax:") ;
Serial.println(Zmax) ;
}
(随分ぐだぐだしたコードだけどこれ以上短くならんよね・・・ううむ
これでこのセンサーにおける各軸の最大最小がとれる。前後左右に傾けたり上下ひっくり返したりして各軸のMinとMaxを取得していく。
出力結果
X:511 Y:526 Z:711
Xmin:263 Xmax:742 Ymin:282 Ymax:758 Zmin:311 Zmax:735
各軸の最大最小がわかったので計算式に代入すれば度数法によるセンサーの角度がわかる。
参考URL(http://deviceplus.jp/hobby/entry017/)の真ん中下あたりの計算式から、
Xについて
一度あたりのセンサ値X1 = (Xmax - Xmin)/180
= (742 - 263)/180
= 2.661…
角度 angleX = (x - Xmin)/X1 - 90
= (x - 263)/2.66 - 90
Yについて
一度あたりのセンサ値Y1 = (Ymax - Ymin)/180
= (758 - 282)/180
=2.6444…
角度 angleY = (y - Ymin)/Y1 - 90
= (y - 282)/2.64 - 90
Zについて
一度あたりのセンサ値Z1 = (Zmax - Zmin)/180
= (735 - 311)/180
= 2.355…
角度 angleZ = (z - Zmin)/Z1 - 90
= (z - 311)/2.36 - 90
よってセンサーの出力値から度数法による角度の換算式が得られたので、コードを書き換える。
各換算式
angleX = (x - 263)/2.66 - 90
angleY = (y - 282)/2.64 - 90
angleZ = (z - 311)/2.36 - 90
コード。
//********************************
//加速度センサの値を取得するプログラム
//********************************
void setup()
{
// シリアルモニターの初期化をする
Serial.begin(9600) ;
}
//度数法のXYZを定義
float angleX, angleY, angleZ;
void loop()
{
long x , y , z ;
x = y = z = 0 ;
x = analogRead(0) ; // X軸
y = analogRead(1) ; // Y軸
z = analogRead(2) ; // Z軸
//前プログラムで得られた値を換算式に組み込む
angleX = (x - 263)/2.66 - 90;
angleY = (y - 282)/2.64 - 90;
angleZ = (z - 311)/2.36 - 90;
Serial.print("angleX:") ;
Serial.print(angleX) ;
Serial.print(" angleY:") ;
Serial.print(angleY) ;
Serial.print(" angleZ:") ;
Serial.println(angleZ) ;
delay(1000) ;
}
これで、センサーの現在の角度が度数法でそれぞれ表示されるようになった。
参考URL
第17回 Arduinoでパーツやセンサを使ってみよう~加速度センサ編(その1)
http://deviceplus.jp/hobby/entry017/
3軸加速度センサで傾斜角度を測定してみます
http://www.geocities.jp/zattouka/GarageHouse/micon/Arduino/Acceleration/Acceleration.htm
by madmaker398
| 2016-01-19 20:34
| 電子工作