Ce site contient essentiellement des notes de travail. Le contenu est en constante évolution, et loin d'être achevé. (+ d'infos)
La plupart des documentations informatiques sont orientées Debian / Ubuntu.

Electronique/Hardware/Sensors/ITG3200/Arduino

De Ordinoscope.net
Sauter à la navigation Sauter à la recherche

Code

//Inspired from the basic example sketch for the ITG-3200 (http://www.sparkfun.com/products/9801), by Ryan Owens, SparkFun Electronics

#include <Wire.h>
#include <Streaming.h>

#define WHO_AM_I    0x00
#define SMPLRT_DIV  0x15
#define DLPF_FS     0x16
#define GYRO_XOUT_H 0x1D
#define GYRO_XOUT_L 0x1E
#define GYRO_YOUT_H 0x1F
#define GYRO_YOUT_L 0x20
#define GYRO_ZOUT_H 0x21
#define GYRO_ZOUT_L 0x22

//This is a list of settings that can be loaded into the registers.
//DLPF, Full Scale Register Bits
//FS_SEL must be set to 3 for proper operation
//Set DLPF_CFG to 3 for 1kHz Fint and 42 Hz Low Pass Filter
char DLPF_CFG_0    = (1<<0);
char DLPF_CFG_1    = (1<<1);
char DLPF_CFG_2    = (1<<2);
char DLPF_FS_SEL_0 = (1<<3);
char DLPF_FS_SEL_1 = (1<<4);

char itgAddress = 0x68;

int offsetX;
int offsetY;
int offsetZ;

void setup () {
  Serial.begin (115200);
  Wire.begin ();
  char id = 0; 
  id = itgRead (itgAddress, 0x00);  
  Serial.print ("ID: ");
  Serial.println (id, HEX);
  itgWrite (itgAddress, DLPF_FS, (DLPF_FS_SEL_0|DLPF_FS_SEL_1|DLPF_CFG_0));  //Set the gyroscope scale for the outputs to +/-2000 degrees per second
  itgWrite (itgAddress, SMPLRT_DIV, 9);                                      //Set the sample rate to 100 hz
  calibrate ();
}

void loop () {
  int xRate, yRate, zRate;
  static int xPos, yPos, zPos;

  xRate = readX();
  yRate = readY();
  zRate = readZ();

  Serial << xRate << '\t' << yRate << '\t' << zRate << endl;

  delay (10);  //Wait 10ms before reading the values again. (Remember, the output rate was set to 100hz and 1reading per 10ms = 100hz.)
  delay (100);
}

void calibrate () {
  int i=0, x, y, z, okX = 0, okY = 0, okZ = 0;
  boolean ok = 0;
  int mingood = 10;
  int margin  = 1;

  while (!ok) {
    x = readX ();
    y = readY ();
    z = readZ ();

    if      (x < -margin) { offsetX -= x / 2; okX--; }
    else if (x >  margin) { offsetX -= x / 2; okX--; }
    else                  { okX++; }
    
    if      (y < -margin) { offsetY -= y / 2; okY--; }
    else if (y >  margin) { offsetY -= y / 2; okY--; }
    else                  { okY++; }

    if      (z < -margin) { offsetZ -= z / 2; okZ--; }
    else if (z >  margin) { offsetZ -= z / 2; okZ--; }
    else                  { okZ++; }

    if (okX < 0)  okX = 0;
    if (okX > mingood) okX = mingood;
    if (okY < 0)  okY = 0;
    if (okY > mingood) okY = mingood;
    if (okZ < 0)  okZ = 0;
    if (okZ > mingood) okZ = mingood;
    
    if (okX >= mingood && okY >= mingood && okZ >= mingood) ok = 1;
    i++;
    delay (10);
  }
  
  Serial << "Calibration done (" << i << " tries)" << endl;
}

void itgWrite (char address, char registerAddress, char data) {
  Wire.beginTransmission (address);
  Wire.send (registerAddress);
  Wire.send (data);
  Wire.endTransmission ();
}

unsigned char itgRead (char address, char registerAddress) {
  unsigned char data = 0;
  Wire.beginTransmission (address);
  Wire.send (registerAddress);
  Wire.endTransmission ();
  Wire.beginTransmission (address);
  Wire.requestFrom (address, 1);
  if (Wire.available ()) data = Wire.receive();
  Wire.endTransmission ();
  return data;
}

int readX (void) {
  int data = 0;
  data  = itgRead (itgAddress, GYRO_XOUT_H) << 8;
  data |= itgRead (itgAddress, GYRO_XOUT_L);  
  return data + offsetX;
}

int readY (void) {
  int data=0;
  data  = itgRead (itgAddress, GYRO_YOUT_H) << 8;
  data |= itgRead (itgAddress, GYRO_YOUT_L);  
  return data + offsetY;
}

int readZ (void) {
  int data = 0;
  data  = itgRead (itgAddress, GYRO_ZOUT_H) << 8;
  data |= itgRead (itgAddress, GYRO_ZOUT_L);  
  return data + offsetZ;
}

Code 2

Exemple trouvé sur http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1278367409/all

#include <Wire.h> // I2C library, gyroscope

#define GYRO_ADDR 0x68 // gyro address, binary = 11101001 when AD0 is connected to Vcc (see schematics of your breakout board)
#define SMPLRT_DIV 0x15
#define DLPF_FS 0x16
#define INT_CFG 0x17
#define PWR_MGM 0x3E

#define TO_READ 6 // 2 bytes for each axis x, y, z


//initializes the gyroscope
void initGyro () {
  /*****************************************
  * ITG 3200
  * power management set to:
  * clock select = internal oscillator
  *     no reset, no sleep mode
  *   no standby mode
  * sample rate to = 3Hz
  * parameter to +/- 2000 degrees/sec
  * low pass filter = 5Hz
  * no interrupt
  ******************************************/

  writeTo (GYRO_ADDR, PWR_MGM, 0x00);
  writeTo (GYRO_ADDR, SMPLRT_DIV, 0xFF); // EB, 50, 80, 7F, DE, 23, 20, FF
  writeTo (GYRO_ADDR, DLPF_FS, 0x1E); // +/- 2000 dgrs/sec, 1KHz, 1E, 19
  writeTo (GYRO_ADDR, INT_CFG, 0x00);
}


void getGyroscopeData () {
  /**************************************
  Gyro ITG-3200 I2C
  registers:
  x axis MSB = 1D, x axis LSB = 1E
  y axis MSB = 1F, y axis LSB = 20
  z axis MSB = 21, z axis LSB = 22
  *************************************/

  int regAddress = 0x1D;
  int x, y, z;
  byte buff[TO_READ];
  char str[50]; // 50 should be enough to store all the data from the

  readFrom(GYRO_ADDR, regAddress, TO_READ, buff); //read the gyro data from the ITG3200

  x = (buff[0] << 8) | buff[1];
  y = (buff[2] << 8) | buff[3];
  z = (buff[4] << 8) | buff[5];

  //we send the x y z values as a string to the serial port
  sprintf (str, "%d,%d,%d", x, y, z);
  Serial.println (str);
}


void setup () {
  Serial.begin (115200);
  Wire.begin ();
  initGyro ();
}


void loop () {
  getGyroscopeData ();
}


//---------------- Functions
//Writes val to address register on device
void writeTo (int device, byte address, byte val) {
   Wire.beginTransmission (device); //start transmission to device
   Wire.send (address);	    // send register address
   Wire.send (val);         // send value to write
   Wire.endTransmission (); //end transmission
}


//reads num bytes starting from address register on device in to buff array
void readFrom (int device, byte address, int num, byte buff[]) {
  Wire.beginTransmission (device); //start transmission to device
  Wire.send (address);             //sends address to read from
  Wire.endTransmission ();         //end transmission

  Wire.beginTransmission (device); //start transmission to device
  Wire.requestFrom (device, num);  // request 6 bytes from device

  int i = 0;
  while (Wire.available ()) {      //device may send less than requested (abnormal)
    buff[i] = Wire.receive ();     //receive a byte
    i++;
  }
  Wire.endTransmission ();         //end transmission
}

itg-3200driver

Fait crasher l'arduino. Aucun affichage en sériel.

Testé les versions

  • 0.5 (Arduino 0023)
  • 0.6 (Arduino 1.0)

itglib

Ne se compile pas avec Arduino 0023

itglib.cpp:74:9: error: redefinition of ‘ITG3200 gyro’
itglib.cpp:30:9: error: ‘ITG3200 gyro’ previously declared here
itglib.cpp: In function ‘void setup()’:
itglib.cpp:76:6: error: redefinition of ‘void setup()’
itglib.cpp:32:6: error: ‘void setup()’ previously defined here
itglib.cpp: In function ‘void loop()’:
itglib.cpp:91:6: error: redefinition of ‘void loop()’
itglib.cpp:38:6: error: ‘void loop()’ previously defined here

Références