ジャイロセンサーを使ってみよう

ジャイロセンサー

ここでは、parallaxで販売しているL3G4200Dというジャイロセンサーを使ってみます。このデバイスはI2Cプロトコルのデバイスですので簡単なEEPROMのコントロールなどを先に理解してください。


ヘッダーファイル

L3G4200D.h
#ifndef L3G4200D_H
#define L3G4200D_H

#include <mbed.h>
#include <I2C.h>

class L3G4200D : public I2C
{
  public:
    unsigned char address;
    L3G4200D();
    void write(unsigned char control, unsigned char  dt);
    void readData(short*);
    unsigned char read(unsigned char control);
  protected:
  private:
    void init();
    void waitForDataReady();
    short mean[3];
};

#endif // L3G4200D_H
 1 
 2 
 3 
 4 
 5 
 6 
 7 I2Cクラスから継承される
 8 
 9 
10 デバイスのアドレス
11 コンストラクタ
12 値の設定用メソッド
13 生データを読むメソッド
14 レジスタ読み込みメソッド
15 
16 
17 初期化メソッド
18 データ待ちメソッド
19 キャリブレーション値
20 
21 
22 


クラスファイル



L3G4200D.cpp
#include "L3G4200D.h"

L3G4200D::L3G4200D():I2C(p28,p27) // sda, scl
{
  init();
}

void L3G4200D::init()
{
  address = 0xD2;
  write(0x22,8);  // CTRL_REG3 Set up data ready signal
  write(0x23,0x80);  // CTRL_REG4 Set up "block data update" mode
  write(0x20,0x1f);     // CTRL_REG1 Send the get continuous output command
  int average[3];
  for(int i=0; i<3; i++)  average[i]=mean[i]=0;
  short data[3];
  for(int i=0; i<30; i++)
  {
    readData(data);
    for(int j=0; j<3; j++) average[j] += data[j];
  }
  for(int i=0; i<3; i++)
  {
     mean[i] = average[i]/30;
  }
}

void L3G4200D::write(unsigned char control, unsigned char  dt)
{
  start();
  ((I2C*)this)->write(address);
  ((I2C*)this)->write(control);
  ((I2C*)this)->write(dt);
  stop();
}

unsigned char L3G4200D::read(unsigned char control)
{
  start();
  ((I2C*)this)->write(address);
  ((I2C*)this)->write(control);
  start();
  ((I2C*)this)->write(address|1);
  unsigned char dt=((I2C*)this)->read(0);
  stop();
  return dt;
}

void L3G4200D::readData(short* data)
{
  unsigned char high, low;
  waitForDataReady();
  start();
  ((I2C*)this)->write(address);
  ((I2C*)this)->write(0xA8); //OUT_X_INC Read the data starting at pointer register
  stop();
  start();
  ((I2C*)this)->write(address|1);
  for(int i=0; i<3; i++) {
    low  = ((I2C*)this)->read(1);
    high = ((I2C*)this)->read((i==2)?0:1);
    data[i] = (short)(((high) << 8) | low)-mean[i];
  }
  stop();
}

void L3G4200D::waitForDataReady()
{
  unsigned char data;
  do {
    data = read(0x27);
  } while(!(data & 0x08));
}
 1 
 2 
 3 コンストラクタ
 4 
 5 
 6 
 7 
 8 初期化メソッド
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 ジャイロ値を読む
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 


メインプログラム


ここでは、I2CのLCDに表示するようにしています。
main.cpp
#include <mbed.h>
#include "L3G4200D.h"
#include "I2CLCD.h"

int main()
{
  I2CLCD lcd(p9,p10);
  L3G4200D motion;
  short data[3];

  while(true)
  {
    motion.readData(data);
    lcd.write(data[0],5,0);
    lcd.write(data[1],5,0);
    lcd.write(data[2],5,0);
    wait_ms(100);
    lcd.home();
  }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 I2CのLCDオブジェクト
 8 ジャイロセンサーのオブジェクト
 9 
10 
11 
12 
13 ジャイロ値を測定する
14 表示
15 
16 
17 
18 
19 
20 


方位とモーションを両方同時に調べてみよう