成人伊人|一艘游轮一百个女的一个男的|绿巨人污|韩国三级电影网址|国产深夜福利

當前位置: 首頁 > 傳感器常見問題 > arduino+L3G4200D的代碼有嗎?

相關商品

瀏覽歷史

arduino+L3G4200D的代碼有嗎?
傳感器常見問題 / 2012-12-27

 最近在研究陀螺儀,參照網上的一些玩家貼子,用的是arduino+L3G4200D,很多貼子里的代碼有:

#include 
這一行,但arduino1.01版的開發環境里并沒帶,樓主也沒說是哪添加進來的。折騰了一晚上,終于在國外壇子里看到一個鏈接:https://github.com/pololu/L3G4200D/tree/66f1448d7f6767e12d0fe0c5c50d4e037aedc27c/L3G4200D

沒錯,這是老外公布出來的代碼,經過試用可以讓L3G4200D工作。但要注意幾步:
1、下載這兩個文件L3G4200D.cpp   L3G4200D.h,但文件好像不能直接下,代碼都貼在網頁上,拷下來,還得在編輯器里把格式重新編輯一下(一行行換行,唉);
2、下好的文件放在你的arduino開發環境的libraries目錄下,建一個L3G4200D的目錄,兩個文件就放這里;
3、可能要重新開啟arduino程序才能用;

這樣就OK了;代碼我貼下面,不知道好不好提?。?/p>

 

#ifndef L3G4200D_h
#define L3G4200D_h
#include // for byte data type// register addresses
#define L3G4200D_WHO_AM_I      0x0F
#define L3G4200D_CTRL_REG1     0x20
#define L3G4200D_CTRL_REG2     0x21
#define L3G4200D_CTRL_REG3     0x22
#define L3G4200D_CTRL_REG4     0x23
#define L3G4200D_CTRL_REG5     0x24
#define L3G4200D_REFERENCE     0x25
#define L3G4200D_OUT_TEMP      0x26
#define L3G4200D_STATUS_REG    0x27
#define L3G4200D_OUT_X_L       0x28
#define L3G4200D_OUT_X_H       0x29
#define L3G4200D_OUT_Y_L       0x2A
#define L3G4200D_OUT_Y_H       0x2B
#define L3G4200D_OUT_Z_L       0x2C
#define L3G4200D_OUT_Z_H       0x2D
#define L3G4200D_FIFO_CTRL_REG 0x2E
#define L3G4200D_FIFO_SRC_REG  0x2F
#define L3G4200D_INT1_CFG      0x30
#define L3G4200D_INT1_SRC      0x31
#define L3G4200D_INT1_THS_XH   0x32
#define L3G4200D_INT1_THS_XL   0x33
#define L3G4200D_INT1_THS_YH   0x34
#define L3G4200D_INT1_THS_YL   0x35
#define L3G4200D_INT1_THS_ZH   0x36
#define L3G4200D_INT1_THS_ZL   0x37
#define L3G4200D_INT1_DURATION 0x38

class L3G4200D
{
 public:
 typedef struct vector
 {
  float x, y, z;
 } vector;
 vector g;

 // gyro angular velocity readings
 void enableDefault(void);
 void writeReg(byte reg, byte value);
 byte readReg(byte reg);
 void read(void);

 // vector functions
 static void vector_cross(const vector *a, const vector *b, vector *out);
 static float vector_dot(const vector *a,const vector *b);
 static void vector_normalize(vector *a);
};
#endif

 

 

#include 
#include 
#include

// Defines ////////////////////////////////////////////////////////////////
// The Arduino two-wire interface uses a 7-bit number for the address, 
// and sets the last bit correctly based on reads and writes
#define GYR_ADDRESS (0xD2 >> 1)
// Public Methods //////////////////////////////////////////////////////////////
// Turns on the L3G4200D's gyro and places it in normal mode.
void L3G4200D::enableDefault(void)
{
 // 0x0F = 0b00001111
 // Normal power mode, all axes enabled
 writeReg(L3G4200D_CTRL_REG1, 0x0F);
}
// Writes a gyro register
void L3G4200D::writeReg(byte reg, byte value)
{
 Wire.beginTransmission(GYR_ADDRESS);
 Wire.write(reg);
 Wire.write(value);
 Wire.endTransmission();
}
// Reads a gyro register
byte L3G4200D::readReg(byte reg)
{
 byte value;
 Wire.beginTransmission(GYR_ADDRESS);
 Wire.write(reg);
 Wire.endTransmission();
 Wire.requestFrom(GYR_ADDRESS, 1);
 value = Wire.read();
 Wire.endTransmission();
 return value;
}
// Reads the 3 gyro channels and stores them in vector g
void L3G4200D::read()
{
 Wire.beginTransmission(GYR_ADDRESS);
 // assert the MSB of the address to get the gyro
  // to do slave-transmit subaddress updating.
 Wire.write(L3G4200D_OUT_X_L | (1 << 7));
  Wire.endTransmission();
 Wire.requestFrom(GYR_ADDRESS, 6);

 while (Wire.available() < 6);

 uint8_t xla = Wire.read();
 uint8_t xha = Wire.read();
 uint8_t yla = Wire.read();
 uint8_t yha = Wire.read();
 uint8_t zla = Wire.read();
 uint8_t zha = Wire.read();

 g.x = xha << 8 | xla;
 g.y = yha << 8 | yla;
 g.z = zha << 8 | zla;
}

void L3G4200D::vector_cross(const vector *a,const vector *b, vector *out)
{
  out->x = a->y*b->z - a->z*b->y;
  out->y = a->z*b->x - a->x*b->z;
  out->z = a->x*b->y - a->y*b->x;
}

float L3G4200D::vector_dot(const vector *a,const vector *b)
{
  return a->x*b->x+a->y*b->y+a->z*b->z;
}

void L3G4200D::vector_normalize(vector *a)
{
  float mag = sqrt(vector_dot(a,a));
  a->x /= mag;
  a->y /= mag;
  a->z /= mag;
}

用戶評論(共0條評論)

  • 暫時還沒有任何用戶評論
總計 0 個記錄,共 1 頁。 第一頁 上一頁 下一頁 最末頁
用戶名: 游客
E-mail:
評價等級:
評論內容: