2016年8月8日月曜日

Johnny-Fiveでマイコン制御20 - IMU・加速度センサー

○IMUオブジェクトを作成する
 IMU(Inertial Measurement Unit:慣性計測ユニット)を使用するには、IMUオブジェクトを使います。IMUオブジェクトは、物理的なボードに接続された単一のIMUモジュールを表すオブジェクトです。IMUオブジェクトを作成するには、IMUクラスのコンストラクタを使います。
Johnny-FiveのサポートするIMUデバイスは「MPU6050」と「BN055」の2つです。オプションの「controller」プロパティにIMUのデバイス名を指定します。
new five.IMU({
  controller: デバイス名
});

○IMUの測定データを取得する
IMUの測定データを取得するには、イベントハンドラを使います。イベントハンドラは、「on」メソッドを使って書きます。イベントにはイベント名を、ハンドラにはイベント発生時に実行する処理(コールバック関数)を指定します。
on("イベント", ハンドラ)

 IMUオブジェクトで発生するイベントは、次のとおりです。
イベント
説明
change
測定データが変更されたときに発生する
data
25ミリ秒(freq」プロパティで変更可)間隔で発生する
calibrated
IMUの準備ができたときに発生する

取得した値は、次のプロパティで参照することができます。
プロパティ
説明
accelerometer
加速度(Accelerometerオブジェクト)
gyro
ジャイロ(Gyroオブジェクト)
thermometer
温度(Thermometerオブジェクト)
compass
方位(Compassオブジェクト)
orientation
向き(Orientationオブジェクト)

Accelerometerオブジェクト
プロパティ
説明
zeroV
現在のゼロ値
pitch
ピッチ角度
roll
ロール角度
x
x方向加速度
y
y方向加速度
z
z方向加速度
acceleration
加速度の大きさ
inclination
デバイスの傾斜
orientation
デバイスの向き

Gyroオブジェクト
プロパティ
説明
isCalibrated
デバイスのキャリブレーション状態
pitch
ピッチ角を含むオブジェクト
roll
ロール角を含むオブジェクト
yaw
ヨーレートの値を含むオブジェクト
rate
XY、およびZの速度値を含むオブジェクト.
x
x方向加速度
y
y方向加速度
z
z方向加速度

Thermometerオブジェクト
プロパティ
説明
celsiusC
摂氏温度
fahrenheitF
華氏温度
kelvinK
ケルビン温度

Compassオブジェクト
プロパティ
説明
heading
北を基準にした方位(0360)
bearing
関連するベアリング情報のプロパティのオブジェクト

bearingオブジェクト
プロパティ
説明
name
方位名(例:「North」「South」「Wast」「Eest)
abbr
短縮した方位名(例:「N」「S」「E」「W)
low
32方位(方位間隔11.15)での現在の方角の最小角度
mid
32方位(方位間隔11.15)での現在の方角の中間角度
high
32方位(方位間隔11.15)での現在の方角の最大角度

 たとえば、MPU6050の温度、加速度、方位を取得するには、次のように書きます。
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  // IMUオブジェクトを作成
  // コントローラ「MPU6050
  var imu = new five.IMU({
               controller: "MPU6050",
             });

  // データを取得
  imu.on("change", function() {
    console.log("temperature");
    console.log("  celsius      : ", this.temperature.celsius);
    console.log("  fahrenheit   : ", this.temperature.fahrenheit)
    console.log("  kelvin       : ", this.temperature.kelvin);
    console.log("--------------------------------------");

    console.log("accelerometer");
    console.log("  x            : ", this.accelerometer.x);
    console.log("  y            : ", this.accelerometer.y);
    console.log("  z            : ", this.accelerometer.z);
    console.log("  pitch        : ", this.accelerometer.pitch);
    console.log("  roll         : ", this.accelerometer.roll);
    console.log("  acceleration : ", this.accelerometer.acceleration);
    console.log("  inclination  : ", this.accelerometer.inclination);
    console.log("  orientation  : ", this.accelerometer.orientation);
    console.log("--------------------------------------");

    console.log("gyro");
    console.log("  x            : ", this.gyro.x);
    console.log("  y            : ", this.gyro.y);
    console.log("  z            : ", this.gyro.z);
    console.log("  pitch        : ", this.gyro.pitch);
    console.log("  roll         : ", this.gyro.roll);
    console.log("  yaw          : ", this.gyro.yaw);
    console.log("  rate         : ", this.gyro.rate);
    console.log("  isCalibrated : ", this.gyro.isCalibrated);
    console.log("--------------------------------------");
  });
});

・配線図
 

○IMUのデータの読み取り間隔を設定する
 IMUのデータの読み取り間隔(data」イベントの間隔)を設定するには、オプションの「freq」プロパティを使います。値にはミリ秒単位の時間を指定します。たとえば、5000ミリ秒(5)間隔でデータを読み取るには、次のように書きます。
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  // IMUオブジェクトを作成
  // コントローラ「MPU6050」、データ取得間隔5000ms
  var imu = new five.IMU({
              controller: "MPU6050",
              freq:5000
            });

  // データを取得
  imu.on("data", function() {
    console.log("Accelerometer: %d, %d, %d", this.accelerometer.x, this.accelerometer.z, this.accelerometer.z);
    console.log("Gyro: %d, %d, %d", this.gyro.x, this.gyro.z, this.gyro.z);
    console.log("Temperature: %d", this.temperature.celsius);
  });
})

○Accelerometerオブジェクトを作成する
 加速度センサーを使用するには、Accelerometerオブジェクトを使います。Accelerometerオブジェクトは、物理的なボードに接続された単一の加速度センサーを表すオブジェクトです。Accelerometerオブジェクトを作成するには、Accelerometerクラスのコンストラクタを使います。
 Johnny-Fiveのサポートする加速度センサーは、次のとおりです。

◇アナログインターフェイス
ADXL362
LIS344AL
MMA7361
I2Cインターフェイス

アナログインターフェイスの加速度センサーを使用するためのAccelerometerオブジェクトを作成するには、引数に加速度センサーのXY、およびZ(オプション)出力を接続するアナログピンの名前を配列で指定します。
new five.Accelerometer ( [x, y, z]}

 オプションの「pins」プロパティでピン番号を指定する場合は、次のように書きます。
new five.Accelerometer({
  pins: [x, y, z]
});

 加速度センサーのデバイス名を明示的に指定する場合は、「controller」プロパティに使用する加速度センサーのデバイス名を指定します。「controller」プロパティの既定値は「ANALOG」です。
new five.Accelerometer({
  controller:  デバイス名,
   pins: [x, y, z]
});

 たとえば、ADXL335の加速度センサー・モジュールのX出力をA0Y出力をA1Z出力をA2に接続して使用するには、次のように書きます。
new five.Accelerometer({
  controller: "ADXL335",
   pins: ["A0", "A1", "A2"]
});

・配線図

I2Cインターフェイスの加速度センサー場合は、「controller」プロパティに使用する加速度センサーのデバイス名を指定します。たとえば、MPU6050を使用する場合は、次のように書きます。
new five.Accelerometer({
  controller: "MPU6050"
});

・配線図
 

オプションを指定してAccelerometerオブジェクトを作成する
 Accelerometerオブジェクトは、次のオプションを指定して作成することができます。

・アナログオプション(controller: "ANALOG")
プロパティ
タイプ
/説明
既定値
sensitivity
数値
感度
96()
aref
数値
電圧リファレンス
5
zeroV
数値の配列
各軸のゼロ値(01023)
478
autoCalibrate
真偽
trueでゼロ値を自動校正する
false
※デバイスごとに異なります。

MPU6050オプション(controller: "MPU6050")
プロパティ
タイプ
/説明
既定
sensitivity
数値
感度(±250の範囲)
131

MMA7361オプション(controller: "MMA7361")
プロパティ
タイプ
/説明
既定
sleepPin
数値の配列
スリープを制御するデジタルピン番号()
null
※このピンを設定しない場合は、10kの抵抗でVccにプルアップする必要があります。

加速度センサーの測定データを取得する
加速度センサーのデータを取得するには、イベントハンドラを使います。イベントハンドラは、「on」メソッドを使って書きます。イベントにはイベント名を、ハンドラにはイベント発生時に実行する処理(コールバック関数)を指定します。
on("イベント", ハンドラ)

 Accelerometerオブジェクトで発生するイベントは、次のとおりです。
イベント
説明
change
測定データが変更されたときに発生する
data
25ミリ秒間隔で発生する

取得した値は、次のプロパティで参照することができます。
プロパティ
説明
zeroV
現在のゼロ値
pitch
ピッチ角度
roll
ロール角度
x
x方向加速度
y
y方向加速度
z
z方向加速度
acceleration
加速度の大きさ
inclination
デバイスの傾斜
orientation
デバイスの向き

 たとえば、MPU6050の加速度センサーのデータを取得するには、次のように書きます。
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  // Accelerometerオブジェクトを作成
  // コントローラ「MPU6050
  var accelerometer = new five.Accelerometer({
                        controller: "MPU6050"
                      });
  // データを取得
  accelerometer.on("change", function() {
    console.log("accelerometer");
    console.log("  x            : ", this.x);
    console.log("  y            : ", this.y);
    console.log("  z            : ", this.z);
    console.log("  pitch        : ", this.pitch);
    console.log("  roll         : ", this.roll);
    console.log("  acceleration : ", this.acceleration);
    console.log("  inclination  : ", this.inclination);
    console.log("  orientation  : ", this.orientation);
    console.log("--------------------------------------");
  });
})

加速度センサーのデバイスとイベントを無効/有効にする
 加速度センサーのデバイスとイベントを無効するには、「disable」メソッドを、有効にするには「enable」メソッドを使います。MMA7361のように、スリープ状態に置くことができるデバイスの場合は、「disable」でスリープ状態に、「enable」でそれを起こします。
 たとえば、デバイスとイベントを1000ミリ秒後に無効にし、2000ミリ秒後に有効に戻すには、次のように書きます。
  // 1000ミリ秒後にデバイスとイベントを無効にする
  this.wait(1000, function() {
    accelerometer.disable();
  });
  // 2000ミリ秒後にデバイスとイベントを有効にする
  this.wait(2000, function() {
    accelerometer.enable();
  });


0 件のコメント:

コメントを投稿