Difference between revisions of "SparkFun: ITG-3200,Triple-Axis Gyro"

From eLinux.org
Jump to: navigation, search
(Sample C Code)
(Header File)
Line 235: Line 235:
 
  * Defines
 
  * Defines
 
  */
 
  */
#define ITG3200_I2C_ADDRESS 0x69 //7-bit address
+
#define ITG3200_I2C_ADDRESS 0x69 //7-bit address that Gyro is originally configured by the break-out board
  
 
//-----------
 
//-----------
Line 269: Line 269:
 
// Offsets
 
// Offsets
 
//-----------
 
//-----------
int TEMP_OUT_OFFSET;
+
short int TEMP_OUT_OFFSET = 0;
int GYRO_XOUT_OFFSET;
+
short int GYRO_XOUT_OFFSET = 0;
int GYRO_YOUT_OFFSET;
+
short int GYRO_YOUT_OFFSET = 0;
int GYRO_ZOUT_OFFSET;
+
short int GYRO_ZOUT_OFFSET = 0;
  
  
Line 282: Line 282:
  
 
//This function is used to initialize the gyroscope. The function returns the -errno if an error accrues.
 
//This function is used to initialize the gyroscope. The function returns the -errno if an error accrues.
int initialize(int i2cbus, int address){
+
short int initialize(int i2cbus, int address){
  
 
char filename[20];
 
char filename[20];
Line 296: Line 296:
 
}
 
}
 
return file;
 
return file;
 +
 +
}
 +
 +
//This function is used to initialize the gyroscope. The function returns the -errno if an error accrues.
 +
short int zeroGyro(int file){
 +
 +
GYRO_XOUT_OFFSET = readX(file);
 +
GYRO_YOUT_OFFSET = readY(file);
 +
GYRO_ZOUT_OFFSET = readZ(file);
  
 
}
 
}
Line 301: Line 310:
 
//This function is used to read the WHO_AM_I_REG of the gyroscope.
 
//This function is used to read the WHO_AM_I_REG of the gyroscope.
 
//Usage: int gyroID = readWhoAmI();
 
//Usage: int gyroID = readWhoAmI();
int readWhoAmI(int file)
+
short int readWhoAmI(int file)
 
{
 
{
   int data=0;
+
   short int data=0;
 
   data = i2c_smbus_read_byte_data(file, WHO_AM_I_REG);
 
   data = i2c_smbus_read_byte_data(file, WHO_AM_I_REG);
 +
 +
  return data;
 +
}
 +
 +
//This function is used to write the WHO_AM_I_REG of the gyroscope.
 +
//Usage: data = readWhoAmI(data);
 +
short int writeWhoAmI(int file, short int data)
 +
{
 +
  i2c_smbus_write_byte_data(file,WHO_AM_I_REG,data);
 +
 +
  return data;
 +
}
 +
 +
 +
//This function is used to read the SMPLRT_DIV_REG of the gyroscope.
 +
short int readSmplrtDiv(int file)
 +
{
 +
  short int data=0;
 +
  data = i2c_smbus_read_byte_data(file, SMPLRT_DIV_REG);
 +
 +
  return data;
 +
}
 +
 +
//This function is used to write the SMPLRT_DIV_REG of the gyroscope.
 +
short int writeSmplrtDiv(int file, short int data)
 +
{
 +
  i2c_smbus_write_byte_data(file,SMPLRT_DIV_REG,data);
 +
 +
  return data;
 +
}
 +
 +
//This function is used to read the DLPF_FS_REG of the gyroscope.
 +
short int readDlpfFs(int file)
 +
{
 +
  short int data=0;
 +
  data = i2c_smbus_read_byte_data(file, DLPF_FS_REG);
 +
 +
  return data;
 +
}
 +
 +
//This function is used to write the DLPF_FS_REG of the gyroscope.
 +
short int writeDlpfFs(int file, short int data)
 +
{
 +
  i2c_smbus_write_byte_data(file,DLPF_FS_REG,data);
 +
 +
  return data;
 +
}
 +
 +
//This function is used to read the INT_CFG_REG of the gyroscope.
 +
short int readIntCfg(int file)
 +
{
 +
  short int data=0;
 +
  data = i2c_smbus_read_byte_data(file, INT_CFG_REG);
 +
 +
  return data;
 +
}
 +
 +
//This function is used to write the INT_CFG_REG of the gyroscope.
 +
short int writeIntCfg(int file, short int data)
 +
{
 +
  i2c_smbus_write_byte_data(file,INT_CFG_REG,data);
 +
 +
  return data;
 +
}
 +
 +
//This function is used to read the INT_STATUS of the gyroscope.
 +
short int readIntStatus(int file)
 +
{
 +
  short int data=0;
 +
  data = i2c_smbus_read_byte_data(file, INT_STATUS);
  
 
   return data;
 
   return data;
Line 312: Line 391:
 
//This function is used to read the temperature of the gyroscope.
 
//This function is used to read the temperature of the gyroscope.
 
//Usage: int gyroTemp = readTemp();
 
//Usage: int gyroTemp = readTemp();
int readTemp(int file)
+
short int readTemp(int file)
 
{
 
{
   int data=0;
+
   short int data=0;
 
   data = i2c_smbus_read_byte_data(file, TEMP_OUT_H_REG)<<8;
 
   data = i2c_smbus_read_byte_data(file, TEMP_OUT_H_REG)<<8;
 
   data |= i2c_smbus_read_byte_data(file, TEMP_OUT_L_REG);
 
   data |= i2c_smbus_read_byte_data(file, TEMP_OUT_L_REG);
Line 324: Line 403:
 
//NOTE: This value is NOT in degrees per second.
 
//NOTE: This value is NOT in degrees per second.
 
//Usage: int xRate = readX();
 
//Usage: int xRate = readX();
int readX(int file)
+
short int readX(int file)
 
{
 
{
   int data=0;
+
   short int data=0;
 
   data = i2c_smbus_read_byte_data(file, GYRO_XOUT_H_REG)<<8;
 
   data = i2c_smbus_read_byte_data(file, GYRO_XOUT_H_REG)<<8;
 
   data |= i2c_smbus_read_byte_data(file, GYRO_XOUT_L_REG);
 
   data |= i2c_smbus_read_byte_data(file, GYRO_XOUT_L_REG);
  
   return data;
+
   return data - GYRO_XOUT_OFFSET;
 
}
 
}
  
Line 336: Line 415:
 
//NOTE: This value is NOT in degrees per second.
 
//NOTE: This value is NOT in degrees per second.
 
//Usage: int yRate = readY();
 
//Usage: int yRate = readY();
int readY(int file)
+
short int readY(int file)
 
{
 
{
   int data=0;
+
   short int data=0;
 
   data = i2c_smbus_read_byte_data(file, GYRO_YOUT_H_REG)<<8;
 
   data = i2c_smbus_read_byte_data(file, GYRO_YOUT_H_REG)<<8;
 
   data |= i2c_smbus_read_byte_data(file, GYRO_YOUT_L_REG);
 
   data |= i2c_smbus_read_byte_data(file, GYRO_YOUT_L_REG);
  
   return data;
+
   return data - GYRO_YOUT_OFFSET;
 
}
 
}
  
Line 348: Line 427:
 
//NOTE: This value is NOT in degrees per second.
 
//NOTE: This value is NOT in degrees per second.
 
//Usage: int zRate = readZ();
 
//Usage: int zRate = readZ();
int readZ(int file)
+
short int readZ(int file)
 
{
 
{
   int data=0;
+
   short int data=0;
 
   data = i2c_smbus_read_byte_data(file, GYRO_ZOUT_H_REG)<<8;
 
   data = i2c_smbus_read_byte_data(file, GYRO_ZOUT_H_REG)<<8;
 
   data |= i2c_smbus_read_byte_data(file, GYRO_ZOUT_L_REG);
 
   data |= i2c_smbus_read_byte_data(file, GYRO_ZOUT_L_REG);
 +
 +
  return data - GYRO_ZOUT_OFFSET;
 +
}
 +
 +
//This function is used to read the PWR_MGM_REG of the gyroscope.
 +
short int readPwrMgm(int file)
 +
{
 +
  short int data=0;
 +
  data = i2c_smbus_read_byte_data(file, PWR_MGM_REG);
  
 
   return data;
 
   return data;
 
}
 
}
  
</pre>
+
//This function is used to write the PWR_MGM_REG of the gyroscope.
 +
short int writePwrMgm(int file, short int data)
 +
{
 +
  i2c_smbus_write_byte_data(file,PWR_MGM_REG,data);
  
 +
  return data;
 +
}
 +
 +
</pre>
  
 
== Features ==
 
== Features ==

Revision as of 18:11, 26 September 2012

Breakout board for InvenSense's ITG-3200

Overview

The ITG-3200 is a three-axis gyro that can be purchased from SparkFun. The datasheet describes it:

The ITG-3200 is the world’s first single-chip, digital-output, 3-axis MEMS gyro IC optimized for gaming, 3D mice, and 3D remote control applications. The part features enhanced bias and sensitivity temperature stability, reducing the need for user calibration. Low frequency noise is lower than previous generation devices, simplifying application development and making for more-responsive remote controls.

ITG-3200 breakout board pin-out

Inputs and Outputs

The ITG-3200 takes a supply voltage (Vs) of 1.8-3.6 V. The analog outputs are scaled proportionally to the supply voltage; at Vs = 3.6 V, the output will change by 2x for the same acceleration as compared to Vs = 1.8 V. Although the output sensitivity is scaled proportionally to the input voltage, noise is not, so higher supply voltages are advisable to reduce the impact of noise.

At all supply voltages, 0 g acceleration corresponds to an output voltage of Vs/2. At Vs = 3.6 V, the datasheet specs the typical sensitivity at 360 mV / g, with g as standard gravitational acceleration.

Bone Usage

Because the ITG-3200 has and I2C port, data can be read into the Bone via the I2C buss pins. One sets of I2C buss pins are required, for the X, Y, Z, and temperature outputs on the gyro. Here we will use the ITG300.h (This header also imports the i2c-dev.h and errno.h automatically, these are necessary to poll the device later on). See the picture on the right for an example wiring configuration.

File:ABone.jpg
Wiring example for ITG-3200 on a Bone

Note: There are two unpopulated resistors on the I2C lines, these can be added later by the customer if desired..

Sample C Code

The code shown below is sample code to demonstrate reading the registers via I2C and using the library.


/*
 * MiniProject02.c
 *
 *  Created on: Sep 20, 2012
 *      Author: Ruffin White
 */

#include "Header.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include "ITG-3200.h"

int loop=1;


void signal_handler(int sig)
{
	printf( "Ctrl-C pressed, cleaning up and exiting..\n" );
	loop = 0;
}

int main(int argc, char** argv){

	//variable declarations
	struct pollfd fdset[1];
	int nfds = 1;
	int timeout = 100;
	int rc;
	char* buf[MAX_BUF];
	int gpio1, gpio2;
	int gpio1_fd, gpio2_fd;
	int gpio2_value = 0;
	int pattern =0;
	int value =0;

	int freq = 10;
	int duty = 25;

	short int gyroID, gyroTemp, gyroX, gyroY, gyroZ;

	// I2C Variables
	char *end;
	int res, i2cbus, address, size, file;
	int daddress;

	//check that at least two arguments are passed in
	if(argc < 4){
		printf("Usage: %s <input-gpio> <output-gpio> <i2c-bus>\n", argv[0]);
		printf("polls input-gpio, and writes value to output-gpio\n");
		fflush(stdout);
		return 1;
	}


	//set signal handler for Ctrl + C
	if (signal(SIGINT, signal_handler) == SIG_ERR)
		printf("\ncan't catch SIGINT\n");

	//assign gpio values
	gpio1 = atoi(argv[1]);
	gpio2 = atoi(argv[2]);


	//assign I2C values
	i2cbus   = atoi(argv[3]);
	address  = ITG3200_I2C_ADDRESS;
	file = initialize(i2cbus, address);
	zeroGyro(file);


	//argument 1 will be input
	export_gpio(gpio1);
	set_gpio_direction(gpio1, "in");
	set_gpio_edge(gpio1, "falling");
	gpio1_fd = gpio_fd_open(gpio1);

	//argument 2 will be output
	export_gpio(gpio2);
	set_gpio_direction(gpio2, "out");
	set_gpio_value(gpio2, gpio2_value);
	gpio2_fd = gpio_fd_open(gpio2);

	set_mux_value("gpmc_a2",6);


	while(loop){
		memset((void*)fdset, 0, sizeof(fdset));

		fdset[0].fd = gpio1_fd;
		fdset[0].events = POLLPRI;

		rc = poll(fdset, nfds, timeout);

		if (rc < 0){
			printf("\npoll() failed!\n");
		}

		if (rc == 0){
			printf(".");
		}

		if((fdset[0].revents & POLLPRI) == POLLPRI) {
			read(fdset[0].fd, buf, MAX_BUF);
			printf("interrupt value=%c\n", buf[0]);
			pattern++;
		if(pattern == 4){
		pattern = 0;
		}
		}

		switch(pattern){

			// blink led
			case 0:
				printf("Case 0\n");
				value = read_ain("ain6");
				printf("Voltage: %d\n",value);
				set_pwm("ehrpwm.1:0",10,25);
				if(gpio2_value){
					gpio2_value = 0;
				}
				else{
					gpio2_value = 1;
				}
				set_gpio_value(gpio2, gpio2_value);
				break;

			//PWM output
			case 1:
				printf("Case 1\n");

				gyroID = readWhoAmI(file);

				printf("gyroID: %6d\n", gyroID);
				break;

			//Read Gyro Temperature
			case 2:
				printf("Case 2\n");

				gyroTemp = readTemp(file);

				printf("gyroTemp: %6d\n", gyroTemp);
				break;

			//Read Gyro XYZ
			case 3:
				printf("Case 3\n");

				gyroX = readX(file);
				gyroY = readY(file);
				gyroZ = readZ(file);

				printf("gyroX: %6d\n", gyroX);
				printf("gyroY: %6d\n", gyroY);
				printf("gyroZ: %6d\n", gyroZ);
				break;

			default:
				break;
			}
		}
	close(file);
	gpio_fd_close(gpio1_fd);
	gpio_fd_close(gpio2_fd);
	unexport_gpio(gpio1);
	unexport_gpio(gpio2);
	fflush(stdout);
	return 0;
}

Header File

The code shown below is header file code used for using the gyro.


/*
 * ITG3200.h
 *
 *  Created on: Sep 20, 2012
 *      Author: Ruffin White
 */

/**
 * Includes
 */
#include <errno.h>
#include "i2c-dev.h"


/**
 * Defines
 */
#define ITG3200_I2C_ADDRESS 0x69 //7-bit address that Gyro is originally configured by the break-out board

//-----------
// Registers
//-----------
#define WHO_AM_I_REG    0x00
#define SMPLRT_DIV_REG  0x15
#define DLPF_FS_REG     0x16
#define INT_CFG_REG     0x17
#define INT_STATUS      0x1A
#define TEMP_OUT_H_REG  0x1B
#define TEMP_OUT_L_REG  0x1C
#define GYRO_XOUT_H_REG 0x1D
#define GYRO_XOUT_L_REG 0x1E
#define GYRO_YOUT_H_REG 0x1F
#define GYRO_YOUT_L_REG 0x20
#define GYRO_ZOUT_H_REG 0x21
#define GYRO_ZOUT_L_REG 0x22
#define PWR_MGM_REG     0x3E

//----------------------------
// Low Pass Filter Bandwidths
//----------------------------
#define LPFBW_256HZ 0x00
#define LPFBW_188HZ 0x01
#define LPFBW_98HZ  0x02
#define LPFBW_42HZ  0x03
#define LPFBW_20HZ  0x04
#define LPFBW_10HZ  0x05
#define LPFBW_5HZ   0x06

//-----------
// Offsets
//-----------
short int TEMP_OUT_OFFSET = 0;
short int GYRO_XOUT_OFFSET = 0;
short int GYRO_YOUT_OFFSET = 0;
short int GYRO_ZOUT_OFFSET = 0;




//----------------
// Read Functions
//----------------

//This function is used to initialize the gyroscope. The function returns the -errno if an error accrues.
short int initialize(int i2cbus, int address){

	char filename[20];
	int file;
	sprintf(filename, "/dev/i2c-%d", i2cbus);
	file = open(filename, O_RDWR);
	if (file<0) {
		return -errno;
	}

	if (ioctl(file, I2C_SLAVE, address) < 0) {
			return -errno;
	}
	return file;

}

//This function is used to initialize the gyroscope. The function returns the -errno if an error accrues.
short int zeroGyro(int file){

	GYRO_XOUT_OFFSET = readX(file);
	GYRO_YOUT_OFFSET = readY(file);
	GYRO_ZOUT_OFFSET = readZ(file);

}

//This function is used to read the WHO_AM_I_REG of the gyroscope.
//Usage: int gyroID = readWhoAmI();
short int readWhoAmI(int file)
{
  short int data=0;
  data = i2c_smbus_read_byte_data(file, WHO_AM_I_REG);

  return data;
}

//This function is used to write the WHO_AM_I_REG of the gyroscope.
//Usage: data = readWhoAmI(data);
short int writeWhoAmI(int file, short int data)
{
  i2c_smbus_write_byte_data(file,WHO_AM_I_REG,data);

  return data;
}


//This function is used to read the SMPLRT_DIV_REG of the gyroscope.
short int readSmplrtDiv(int file)
{
  short int data=0;
  data = i2c_smbus_read_byte_data(file, SMPLRT_DIV_REG);

  return data;
}

//This function is used to write the SMPLRT_DIV_REG of the gyroscope.
short int writeSmplrtDiv(int file, short int data)
{
  i2c_smbus_write_byte_data(file,SMPLRT_DIV_REG,data);

  return data;
}

//This function is used to read the DLPF_FS_REG of the gyroscope.
short int readDlpfFs(int file)
{
  short int data=0;
  data = i2c_smbus_read_byte_data(file, DLPF_FS_REG);

  return data;
}

//This function is used to write the DLPF_FS_REG of the gyroscope.
short int writeDlpfFs(int file, short int data)
{
  i2c_smbus_write_byte_data(file,DLPF_FS_REG,data);

  return data;
}

//This function is used to read the INT_CFG_REG of the gyroscope.
short int readIntCfg(int file)
{
  short int data=0;
  data = i2c_smbus_read_byte_data(file, INT_CFG_REG);

  return data;
}

//This function is used to write the INT_CFG_REG of the gyroscope.
short int writeIntCfg(int file, short int data)
{
  i2c_smbus_write_byte_data(file,INT_CFG_REG,data);

  return data;
}

//This function is used to read the INT_STATUS of the gyroscope.
short int readIntStatus(int file)
{
  short int data=0;
  data = i2c_smbus_read_byte_data(file, INT_STATUS);

  return data;
}


//This function is used to read the temperature of the gyroscope.
//Usage: int gyroTemp = readTemp();
short int readTemp(int file)
{
  short int data=0;
  data = i2c_smbus_read_byte_data(file, TEMP_OUT_H_REG)<<8;
  data |= i2c_smbus_read_byte_data(file, TEMP_OUT_L_REG);

  return data;
}

//This function is used to read the X-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second.
//Usage: int xRate = readX();
short int readX(int file)
{
  short int data=0;
  data = i2c_smbus_read_byte_data(file, GYRO_XOUT_H_REG)<<8;
  data |= i2c_smbus_read_byte_data(file, GYRO_XOUT_L_REG);

  return data - GYRO_XOUT_OFFSET;
}

//This function is used to read the Y-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second.
//Usage: int yRate = readY();
short int readY(int file)
{
  short int data=0;
  data = i2c_smbus_read_byte_data(file, GYRO_YOUT_H_REG)<<8;
  data |= i2c_smbus_read_byte_data(file, GYRO_YOUT_L_REG);

  return data - GYRO_YOUT_OFFSET;
}

//This function is used to read the Z-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second.
//Usage: int zRate = readZ();
short int readZ(int file)
{
  short int data=0;
  data = i2c_smbus_read_byte_data(file, GYRO_ZOUT_H_REG)<<8;
  data |= i2c_smbus_read_byte_data(file, GYRO_ZOUT_L_REG);

  return data - GYRO_ZOUT_OFFSET;
}

//This function is used to read the PWR_MGM_REG of the gyroscope.
short int readPwrMgm(int file)
{
  short int data=0;
  data = i2c_smbus_read_byte_data(file, PWR_MGM_REG);

  return data;
}

//This function is used to write the PWR_MGM_REG of the gyroscope.
short int writePwrMgm(int file, short int data)
{
  i2c_smbus_write_byte_data(file,PWR_MGM_REG,data);

  return data;
}

Features

Digital-output X-, Y-, and Z-Axis angular rate sensors (gyros) on one integrated circuit Digitally-programmable low-pass filter Low 6.5mA operating current consumption for long battery life Wide VDD supply voltage range of 2.1V to 3.6V Standby current: 5μA Digital-output temperature sensor Fast Mode I2C (400kHz) serial interface Optional external clock inputs of 32.768kHz or 19.2MHz to synchronize with system clock Pins broken out to a breadboard friendly 7-pin 0.1" pitch header

Dimensions

0.70 x 0.55" (17.78 x 13.97mm)

Documents

Schematic

Eagle Files

Quickstart Guide

ITG-3200 Datasheet

Code (ATmega328)

Example